Перейти к основному содержимому

makeEnvelope

@mineflow/contracts


@mineflow/contracts / makeEnvelope

Function: makeEnvelope()

function makeEnvelope<T>(input: object): object & object;

Defined in: envelope.ts:127

Собирает конверт события на стороне продюсера: подставляет occurredAt (текущее время), генерирует id и копирует остальные поля из input.

id по умолчанию — crypto.randomUUID(). Если передан deterministicId, id вычисляется детерминированно как UUIDv5(namespace, '${organizationId}:${type}:${deterministicId}') — это даёт стабильный eventId для безопасного retry (см. описание deterministicId).

Возвращаемый тип сужает data до T, поэтому продюсер не теряет типизацию нагрузки (в отличие от EventEnvelope, где data: unknown).

Type Parameters

Type ParameterDescription
T— тип полезной нагрузки data.

Parameters

ParameterTypeDescription
input{ causationId?: string; correlationId?: string; data: T; deterministicId?: string; organizationId: string; producedBy: string; type: string; version: string; }— поля события без id/occurredAt (они вычисляются).
input.causationId?stringUUID события-причины (опционально).
input.correlationId?stringUUID цепочки корреляции (опционально).
input.dataTПолезная нагрузка события.
input.deterministicId?stringR10-PLAT-F5: deterministic envelope.id seed. Если задан — id вычисляется как UUIDv5(namespace, ${organizationId}:${type}:${deterministicId}). Use-case: retry-with-same-Idempotency-Key который пробил cache (5xx не кешируется). Без этого второй attempt сгенерил бы новый crypto.randomUUID() → второй envelope в outbox → дубль в Redis Streams. С deterministicId второй attempt пытается INSERT с тем же eventId и Prisma бросает P2002 (eventOutbox Unique на eventId) → caller знает что событие уже зафиксировано и retry безопасен. Producer формирует deterministicId из стабильного входа: Idempotency- Key HTTP-header'а, или sagaId+stepName для idempotent saga steps, или externalId для inbox-driven cascade'ов.
input.organizationIdstringUUID организации-владельца (ADR-0020).
input.producedBystringИмя продюсера.
input.typestringИмя события <context>.<entity>.<verb-past>.
input.versionstringВерсия data-схемы (major.minor).

Returns

Готовый конверт EventEnvelope с типизированным data: T.

Example

const evt = makeEnvelope({
type: 'eam.asset.transferred',
version: '1.0',
data: { assetId, fromObjectId, toObjectId, performedBy },
producedBy: 'eam-api',
organizationId,
deterministicId: idempotencyKey, // повторный publish → тот же id
});