@cullet/erp-core 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/KIT_CONTEXT.md +11 -3
- package/README.md +160 -0
- package/dist/abac/index.cjs +7 -0
- package/dist/abac/index.d.cts +2 -0
- package/dist/abac/index.d.ts +2 -0
- package/dist/abac/index.js +2 -0
- package/dist/app-error.cjs +201 -0
- package/dist/app-error.cjs.map +1 -0
- package/dist/app-error.js +178 -0
- package/dist/app-error.js.map +1 -0
- package/dist/application/index.cjs +2 -1
- package/dist/application/index.d.cts +2 -1
- package/dist/application/index.d.ts +2 -1
- package/dist/application/index.js +2 -1
- package/dist/authorization-error.cjs +157 -0
- package/dist/authorization-error.cjs.map +1 -0
- package/dist/authorization-error.d.cts +113 -0
- package/dist/authorization-error.d.ts +113 -0
- package/dist/authorization-error.js +152 -0
- package/dist/authorization-error.js.map +1 -0
- package/dist/authorizer.port.d.cts +130 -0
- package/dist/authorizer.port.d.ts +130 -0
- package/dist/composite-authorizer.d.cts +216 -0
- package/dist/composite-authorizer.d.ts +216 -0
- package/dist/condition-evaluator.cjs +579 -0
- package/dist/condition-evaluator.cjs.map +1 -0
- package/dist/condition-evaluator.js +550 -0
- package/dist/condition-evaluator.js.map +1 -0
- package/dist/{gate-types.d.ts → core-config.d.cts} +78 -77
- package/dist/{gate-types.d.cts → core-config.d.ts} +78 -77
- package/dist/domain/index.cjs +2 -1
- package/dist/domain/index.d.cts +2 -1
- package/dist/domain/index.d.ts +2 -1
- package/dist/domain/index.js +2 -1
- package/dist/domain-event-contracts.cjs +3 -2
- package/dist/domain-event-contracts.cjs.map +1 -1
- package/dist/domain-event-contracts.js +2 -1
- package/dist/domain-event-contracts.js.map +1 -1
- package/dist/entity.cjs +122 -0
- package/dist/entity.cjs.map +1 -0
- package/dist/entity.js +111 -0
- package/dist/entity.js.map +1 -0
- package/dist/errors/index.cjs +10 -7
- package/dist/errors/index.d.cts +2 -1
- package/dist/errors/index.d.ts +2 -1
- package/dist/errors/index.js +6 -3
- package/dist/gate-engine-registry.cjs +5 -4
- package/dist/gate-engine-registry.cjs.map +1 -1
- package/dist/gate-engine-registry.d.cts +3 -2
- package/dist/gate-engine-registry.d.ts +3 -2
- package/dist/gate-engine-registry.js +2 -1
- package/dist/gate-engine-registry.js.map +1 -1
- package/dist/gate-v1-payload.schema.cjs +0 -562
- package/dist/gate-v1-payload.schema.cjs.map +1 -1
- package/dist/gate-v1-payload.schema.js +1 -533
- package/dist/gate-v1-payload.schema.js.map +1 -1
- package/dist/hashing.cjs +2 -44
- package/dist/hashing.cjs.map +1 -1
- package/dist/hashing.js +2 -38
- package/dist/hashing.js.map +1 -1
- package/dist/index.cjs +36 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -12
- package/dist/index.d.ts +19 -12
- package/dist/index.js +17 -10
- package/dist/index.js.map +1 -1
- package/dist/not-found-error.cjs +5 -5
- package/dist/not-found-error.cjs.map +1 -1
- package/dist/not-found-error.js +1 -1
- package/dist/outcome.d.cts +1 -83
- package/dist/outcome.d.ts +1 -83
- package/dist/parse-gate-payload.d.cts +2 -2
- package/dist/parse-gate-payload.d.ts +2 -2
- package/dist/path.d.cts +2 -2
- package/dist/path.d.ts +2 -2
- package/dist/policies/engines/index.d.cts +1 -1
- package/dist/policies/engines/index.d.ts +1 -1
- package/dist/policies/engines/v1/gate/index.cjs +5 -4
- package/dist/policies/engines/v1/gate/index.cjs.map +1 -1
- package/dist/policies/engines/v1/gate/index.d.cts +4 -2
- package/dist/policies/engines/v1/gate/index.d.ts +4 -2
- package/dist/policies/engines/v1/gate/index.js +2 -1
- package/dist/policies/engines/v1/gate/index.js.map +1 -1
- package/dist/policies/index.cjs +5 -5
- package/dist/policies/index.d.cts +3 -2
- package/dist/policies/index.d.ts +3 -2
- package/dist/policies/index.js +2 -2
- package/dist/policy-bridge.cjs +437 -0
- package/dist/policy-bridge.cjs.map +1 -0
- package/dist/policy-bridge.d.cts +185 -0
- package/dist/policy-bridge.d.ts +185 -0
- package/dist/policy-bridge.js +396 -0
- package/dist/policy-bridge.js.map +1 -0
- package/dist/policy-service.cjs +241 -240
- package/dist/policy-service.cjs.map +1 -1
- package/dist/policy-service.d.cts +2 -2
- package/dist/policy-service.d.ts +2 -2
- package/dist/policy-service.js +240 -239
- package/dist/policy-service.js.map +1 -1
- package/dist/rbac/index.cjs +9 -0
- package/dist/rbac/index.d.cts +3 -0
- package/dist/rbac/index.d.ts +3 -0
- package/dist/rbac/index.js +2 -0
- package/dist/requested-by.cjs +54 -0
- package/dist/requested-by.cjs.map +1 -0
- package/dist/requested-by.d.cts +25 -0
- package/dist/requested-by.d.ts +25 -0
- package/dist/requested-by.js +49 -0
- package/dist/requested-by.js.map +1 -0
- package/dist/result/index.d.cts +2 -1
- package/dist/result/index.d.ts +2 -1
- package/dist/result.d.cts +84 -0
- package/dist/result.d.ts +84 -0
- package/dist/rule.cjs +327 -0
- package/dist/rule.cjs.map +1 -0
- package/dist/rule.js +298 -0
- package/dist/rule.js.map +1 -0
- package/dist/stable-stringify.cjs +47 -0
- package/dist/stable-stringify.cjs.map +1 -0
- package/dist/stable-stringify.js +42 -0
- package/dist/stable-stringify.js.map +1 -0
- package/dist/temporal-use-case.cjs +6 -53
- package/dist/temporal-use-case.cjs.map +1 -1
- package/dist/temporal-use-case.d.cts +11 -28
- package/dist/temporal-use-case.d.ts +11 -28
- package/dist/temporal-use-case.js +7 -48
- package/dist/temporal-use-case.js.map +1 -1
- package/dist/unexpected-error.cjs +3 -192
- package/dist/unexpected-error.cjs.map +1 -1
- package/dist/unexpected-error.js +2 -167
- package/dist/unexpected-error.js.map +1 -1
- package/dist/use-case.cjs +2 -6
- package/dist/use-case.cjs.map +1 -1
- package/dist/use-case.js +2 -6
- package/dist/use-case.js.map +1 -1
- package/dist/uuid-identifier.cjs +2 -7
- package/dist/uuid-identifier.cjs.map +1 -1
- package/dist/uuid-identifier.d.cts +2 -85
- package/dist/uuid-identifier.d.ts +2 -85
- package/dist/uuid-identifier.js +1 -6
- package/dist/uuid-identifier.js.map +1 -1
- package/dist/uuid.cjs +18 -0
- package/dist/uuid.cjs.map +1 -0
- package/dist/uuid.js +13 -0
- package/dist/uuid.js.map +1 -0
- package/dist/validation-error.cjs +33 -166
- package/dist/validation-error.cjs.map +1 -1
- package/dist/validation-error.d.cts +12 -103
- package/dist/validation-error.d.ts +12 -103
- package/dist/validation-error.js +7 -134
- package/dist/validation-error.js.map +1 -1
- package/dist/value-object.cjs +12 -126
- package/dist/value-object.cjs.map +1 -1
- package/dist/value-object.d.cts +97 -0
- package/dist/value-object.d.ts +97 -0
- package/dist/value-object.js +13 -115
- package/dist/value-object.js.map +1 -1
- package/meta.json +20 -4
- package/package.json +27 -1
- package/src/abac/index.ts +1 -0
- package/src/core/abac/abac-request.ts +29 -0
- package/src/core/abac/attributes.ts +39 -0
- package/src/core/abac/authorizer.ts +108 -0
- package/src/core/abac/combining.ts +63 -0
- package/src/core/abac/composite-authorizer.ts +45 -0
- package/src/core/abac/domain/policy-set.ts +52 -0
- package/src/core/abac/domain/rule.ts +197 -0
- package/src/core/abac/index.ts +21 -0
- package/src/core/application/commands/requested-by.ts +3 -4
- package/src/core/application/ports/abac-authorizer.port.ts +20 -0
- package/src/core/application/ports/authorizer.port.ts +22 -0
- package/src/core/application/queries/query.ts +6 -1
- package/src/core/application/use-case.ts +1 -1
- package/src/core/domain/entity.ts +1 -1
- package/src/core/domain/uuid-identifier.ts +1 -8
- package/src/core/domain/value-object.ts +12 -3
- package/src/core/errors/authorization-error.ts +26 -0
- package/src/core/errors/error-codes.ts +1 -0
- package/src/core/errors/utils/json-safe.ts +20 -11
- package/src/core/index.ts +7 -0
- package/src/core/policies/engines/v1/condition-evaluator.ts +56 -1
- package/src/core/rbac/access-request.ts +32 -0
- package/src/core/rbac/authorizer.ts +91 -0
- package/src/core/rbac/domain/grant.ts +96 -0
- package/src/core/rbac/domain/permission-set.ts +67 -0
- package/src/core/rbac/domain/permission.ts +119 -0
- package/src/core/rbac/domain/role.ts +101 -0
- package/src/core/rbac/domain/scope.ts +84 -0
- package/src/core/rbac/index.ts +15 -0
- package/src/core/rbac/policy-bridge.ts +44 -0
- package/src/core/shared/hashing.ts +1 -55
- package/src/core/shared/stable-stringify.ts +57 -0
- package/src/core/shared/uuid.ts +11 -0
- package/src/examples/application/authorize-cancel-order-abac.example.ts +107 -0
- package/src/examples/application/authorize-cancel-order.example.ts +101 -0
- package/src/rbac/index.ts +1 -0
- package/src/version.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.js","names":["compactMetadata","extractAppErrorOptions"],"sources":["../src/core/errors/authentication-error.ts","../src/core/errors/authorization-error.ts","../src/core/errors/conflict-error.ts","../src/core/errors/idempotency-error.ts","../src/core/errors/integration-error.ts","../src/core/errors/legacy-incompatible-error.ts","../src/core/errors/serialization-error.ts","../src/core/errors/temporal-error.ts","../src/core/errors/validation-error.ts"],"sourcesContent":["import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions, ErrorSeverity } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Types\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype AuthenticationErrorReason =\n | \"missing_token\"\n | \"malformed_token\"\n | \"invalid_token\"\n | \"expired_token\"\n | \"revoked_token\"\n | \"invalid_credentials\"\n | \"session_not_found\"\n | \"session_expired\"\n | \"unknown\";\n\ntype AuthenticationErrorMetadata = {\n reason: AuthenticationErrorReason;\n authScheme?: \"bearer\" | \"cookie\" | \"basic\" | \"unknown\";\n tokenLocation?: \"header\" | \"cookie\" | \"query\" | \"unknown\";\n expiresAtIso?: string;\n correlationId?: string;\n requestId?: string;\n details?: string;\n};\n\ntype AuthenticationErrorFactoryOptions = Omit<\n AuthenticationErrorMetadata,\n \"reason\"\n> &\n Omit<AppErrorOptions, \"metadata\">;\n\n// ─────────────────────────────────────────────────────────────────────────────\n// AuthenticationError\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass AuthenticationError extends AppError {\n public readonly reason: AuthenticationErrorReason;\n\n private constructor(params: {\n message: string;\n code: string;\n reason: AuthenticationErrorReason;\n metadata?: Omit<AuthenticationErrorMetadata, \"reason\">;\n cause?: unknown;\n type?: string;\n severity?: ErrorSeverity;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n createdAtIso?: string;\n publicMessage?: string;\n }) {\n super(params.message, params.code, {\n cause: params.cause,\n metadata: {\n reason: params.reason,\n ...(params.metadata ?? {}),\n },\n type: params.type,\n severity: params.severity,\n correlationId: params.correlationId,\n requestId: params.requestId,\n commandId: params.commandId,\n createdAtIso: params.createdAtIso,\n publicMessage: params.publicMessage,\n });\n\n this.reason = params.reason;\n Object.freeze(this);\n }\n\n // ─────────────────────────────────────────────────────────────────────────\n // Factory methods\n // ─────────────────────────────────────────────────────────────────────────\n\n static missingToken(\n options?: AuthenticationErrorFactoryOptions,\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Missing credentials\",\n code: ErrorCodes.authentication.missingToken,\n reason: \"missing_token\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n\n static invalidToken(\n options?: AuthenticationErrorFactoryOptions & { cause?: unknown },\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Invalid credentials\",\n code: ErrorCodes.authentication.invalidToken,\n reason: \"invalid_token\",\n metadata: compactMetadata({\n authScheme: options?.authScheme,\n tokenLocation: options?.tokenLocation,\n details: options?.details,\n correlationId: options?.correlationId,\n requestId: options?.requestId,\n }),\n ...extractAppErrorOptions(options),\n });\n }\n\n static expiredToken(\n options?: AuthenticationErrorFactoryOptions & { sessionId?: string },\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Expired credentials\",\n code: ErrorCodes.authentication.expiredToken,\n reason: \"expired_token\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n\n static invalidCredentials(\n options?: Pick<\n AuthenticationErrorFactoryOptions,\n | \"correlationId\"\n | \"requestId\"\n | \"type\"\n | \"severity\"\n | \"createdAtIso\"\n | \"publicMessage\"\n >,\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Invalid username or password\",\n code: ErrorCodes.authentication.invalidCredentials,\n reason: \"invalid_credentials\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Helpers\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction extractAppErrorOptions(options?: Partial<AppErrorOptions>) {\n return {\n cause: options?.cause,\n type: options?.type,\n severity: options?.severity,\n correlationId: options?.correlationId,\n requestId: options?.requestId,\n commandId: options?.commandId,\n createdAtIso: options?.createdAtIso,\n publicMessage: options?.publicMessage,\n };\n}\n\nfunction compactMetadata<T extends Record<string, unknown>>(\n input?: T,\n): T | undefined {\n if (!input) return undefined;\n\n return Object.fromEntries(\n Object.entries(input).filter(([, value]) => value !== undefined),\n ) as T;\n}\n\nexport { AuthenticationError };\nexport type { AuthenticationErrorMetadata, AuthenticationErrorReason };\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions, ErrorSeverity } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Types\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype AuthorizationErrorReason =\n | \"forbidden\"\n | \"missing_role\"\n | \"missing_capability\"\n | \"out_of_scope\"\n | \"outside_time_window\"\n | \"policy_denied\"\n | \"unknown\";\n\ntype AuthorizationRequirement = {\n role?: string;\n capability?: string;\n scope?: string; // e.g.: \"school:{id}\" or \"class:{id}\"\n};\n\ntype AuthorizationErrorMetadata = {\n reason: AuthorizationErrorReason;\n\n /** Stable action you evaluated (not an HTTP route — a \"business action\") */\n action: string;\n\n /** Resource identification (without leaking the full payload) */\n resource?: { type: string; id?: string };\n\n /** Optional: actor (do not include sensitive data) */\n actor?: { userId: string; role?: string };\n\n /** Optional: expected requirement */\n required?: AuthorizationRequirement;\n\n /** Optional: policy (when you already have PolicyEvaluation or similar) */\n policyId?: string;\n policyVersion?: number;\n evaluatedAtIso?: string;\n decision?: \"allow\" | \"deny\";\n reasonCode?: string;\n\n /** Optional: additional non-sensitive info */\n details?: string;\n};\n\n/**\n * Options for AuthorizationError factory methods.\n * Combines metadata fields (except 'reason') with common AppError options.\n */\ntype AuthorizationErrorFactoryOptions = Omit<\n AuthorizationErrorMetadata,\n \"reason\"\n> &\n Omit<AppErrorOptions, \"metadata\">;\n\n// ─────────────────────────────────────────────────────────────────────────────\n// AuthorizationError\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Raised when an authenticated actor is not allowed to perform a business\n * action — the \"you may not\" error, distinct from authentication (\"who are\n * you\"). Maps to an HTTP 403 at the edge.\n *\n * The discriminating {@link reason} records *why* access was denied (a flat\n * forbid, a missing role/capability, an out-of-scope target, or a policy\n * decision) so the boundary can shape the response without re-deriving it. The\n * metadata is deliberately built around a stable business `action` and a\n * type/id resource reference rather than an HTTP route or full payload, keeping\n * sensitive data out of logs. Instances are frozen. Construct through the\n * static factories, never directly.\n */\nclass AuthorizationError extends AppError {\n public readonly reason: AuthorizationErrorReason;\n\n private constructor(params: {\n message: string;\n code: string;\n reason: AuthorizationErrorReason;\n metadata: AuthorizationErrorMetadata;\n cause?: unknown;\n type?: string;\n severity?: ErrorSeverity;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n createdAtIso?: string;\n publicMessage?: string;\n }) {\n super(params.message, params.code, {\n cause: params.cause,\n metadata: params.metadata,\n type: params.type,\n severity: params.severity,\n correlationId: params.correlationId,\n requestId: params.requestId,\n commandId: params.commandId,\n createdAtIso: params.createdAtIso,\n publicMessage: params.publicMessage,\n });\n\n this.reason = params.reason;\n Object.freeze(this);\n }\n\n // ─────────────────────────────────────────────────────────────────────────\n // Factories\n // ─────────────────────────────────────────────────────────────────────────\n\n /**\n * A flat denial with no more specific reason — the actor simply may not\n * perform this action. Reach for a more precise factory\n * ({@link AuthorizationError.missingCapability}, {@link AuthorizationError.outOfScope},\n * {@link AuthorizationError.policyDenied}) when the cause is known.\n */\n static forbidden(\n input: AuthorizationErrorFactoryOptions,\n ): AuthorizationError {\n return new AuthorizationError({\n message: \"Action not allowed\",\n code: ErrorCodes.authorization.forbidden,\n reason: \"forbidden\",\n metadata: { reason: \"forbidden\", ...extractMetadataOnly(input) },\n ...extractAppErrorOptions(input),\n });\n }\n\n /**\n * The action was denied by an evaluated policy. Captures the deciding\n * policy's id, version, and evaluation instant into the metadata (with\n * `decision: \"deny\"`) so the denial is auditable back to the exact policy\n * that produced it.\n *\n * @param input - Factory options plus the required `policyId`,\n * `policyVersion`, and `evaluatedAtIso` of the deciding policy.\n */\n static policyDenied(\n input: AuthorizationErrorFactoryOptions & {\n policyId: string;\n policyVersion: number;\n evaluatedAtIso: string;\n },\n ): AuthorizationError {\n return new AuthorizationError({\n message: \"Action denied by policy\",\n code: ErrorCodes.authorization.policyDenied,\n reason: \"policy_denied\",\n metadata: {\n reason: \"policy_denied\",\n ...extractMetadataOnly(input),\n decision: \"deny\",\n },\n ...extractAppErrorOptions(input),\n });\n }\n\n /**\n * The actor lacks a required role or capability. The expected\n * {@link AuthorizationRequirement} is recorded so the boundary can tell the\n * caller precisely what grant is missing.\n *\n * @param input - Factory options plus the `required` role/capability/scope.\n */\n static missingCapability(\n input: AuthorizationErrorFactoryOptions & {\n required: AuthorizationRequirement;\n },\n ): AuthorizationError {\n return new AuthorizationError({\n message: \"Insufficient capability to perform the action\",\n code: ErrorCodes.authorization.missingCapability,\n reason: \"missing_capability\",\n metadata: {\n reason: \"missing_capability\",\n ...extractMetadataOnly(input),\n },\n ...extractAppErrorOptions(input),\n });\n }\n\n /**\n * The actor may perform the action in general, but not on *this* target —\n * the resource falls outside the actor's permitted scope (e.g. a different\n * school or tenant than the one they are bound to).\n *\n * @param input - Factory options plus the optional `required` scope that the\n * target failed to satisfy.\n */\n static outOfScope(\n input: AuthorizationErrorFactoryOptions & {\n required?: AuthorizationRequirement;\n },\n ): AuthorizationError {\n return new AuthorizationError({\n message: \"Action outside the allowed scope\",\n code: ErrorCodes.authorization.outOfScope,\n reason: \"out_of_scope\",\n metadata: { reason: \"out_of_scope\", ...extractMetadataOnly(input) },\n ...extractAppErrorOptions(input),\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Helpers\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction extractAppErrorOptions(options?: Partial<AppErrorOptions>) {\n return {\n cause: options?.cause,\n type: options?.type,\n severity: options?.severity,\n correlationId: options?.correlationId,\n requestId: options?.requestId,\n commandId: options?.commandId,\n createdAtIso: options?.createdAtIso,\n publicMessage: options?.publicMessage,\n };\n}\n\nfunction extractMetadataOnly(\n input: AuthorizationErrorFactoryOptions,\n): Omit<AuthorizationErrorMetadata, \"reason\"> {\n const {\n cause,\n type,\n severity,\n correlationId,\n requestId,\n commandId,\n createdAtIso,\n publicMessage,\n ...metadata\n } = input;\n return metadata;\n}\n\nexport { AuthorizationError };\nexport type {\n AuthorizationErrorMetadata,\n AuthorizationErrorReason,\n AuthorizationRequirement,\n};\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n/**\n * Conflict kinds we want to distinguish semantically.\n */\ntype ConflictKind = \"already_exists\" | \"duplicate\" | \"unique_violation\";\n\n/**\n * Metadata attached to conflicts without exposing sensitive values.\n */\ntype ConflictErrorMetadata = {\n kind: ConflictKind;\n entity: string;\n operation?: string;\n field?: string;\n constraintName?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n};\n\n/**\n * Storage-level description of a unique-constraint breach, as reported by a\n * database driver. Used to translate a raw DB error into a domain\n * {@link DuplicateError} via {@link translateUniqueViolationToDuplicate}.\n */\ntype UniqueConstraintViolation = {\n kind: \"unique_violation\";\n constraintName?: string;\n table?: string;\n columns?: string[];\n};\n\n/**\n * Base for errors that signal a state conflict — the request collides with data\n * that already exists. Common at an HTTP 409 boundary.\n *\n * The discriminating {@link kind} lets a handler branch on *why* it conflicted\n * (an already-existing record, a domain duplicate, or a raw storage uniqueness\n * breach) without `instanceof` chains. Abstract: construct one of the concrete\n * subclasses through its `detected(...)` factory, which fills in the right code,\n * message, and metadata. Instances are frozen, so a conflict error can be shared\n * and rethrown without risk of tampering.\n */\nabstract class ConflictError extends AppError {\n public readonly kind: ConflictKind;\n\n protected constructor(\n params: {\n message: string;\n code: string;\n kind: ConflictKind;\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super(params.message, params.code, {\n ...params,\n metadata: {\n kind: params.kind,\n ...params.metadata,\n },\n });\n\n this.kind = params.kind;\n Object.freeze(this);\n }\n}\n\n/**\n * The operation cannot proceed because a matching record already exists —\n * e.g. creating something whose natural key is already taken. Construct via\n * {@link AlreadyExistsError.detected}.\n */\nclass AlreadyExistsError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message:\n \"A matching record already exists for the requested operation\",\n code: ErrorCodes.conflict.alreadyExists,\n kind: \"already_exists\",\n ...input,\n });\n }\n\n /**\n * Builds an {@link AlreadyExistsError} for a detected collision. `operation`\n * defaults to `\"create\"` and a generic retry `hint` is supplied when none is\n * given, so the error is actionable even from a minimal call site.\n *\n * @param input - The conflicting entity plus optional non-sensitive context\n * (operation, field, existing id, value hash/preview, correlation ids).\n */\n static detected(input: {\n entity: string;\n operation?: string;\n field?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n cause?: unknown;\n }): AlreadyExistsError {\n return new AlreadyExistsError({\n metadata: {\n entity: input.entity,\n operation: input.operation ?? \"create\",\n field: input.field,\n existingId: input.existingId,\n valueHash: input.valueHash,\n valuePreview: input.valuePreview,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n hint: input.hint ?? \"Verify existing records before retrying.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * A domain-level duplicate: the data being written would duplicate an existing\n * record under the model's own uniqueness rules. Use this when the duplication\n * is recognized in the domain, as opposed to a raw DB constraint\n * ({@link UniqueConstraintViolationError}). Construct via\n * {@link DuplicateError.detected}.\n */\nclass DuplicateError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message: \"Conflict: an existing record violates uniqueness\",\n code: ErrorCodes.conflict.duplicate,\n kind: \"duplicate\",\n ...input,\n });\n }\n\n /**\n * Builds a {@link DuplicateError} for a detected duplicate, defaulting to a\n * \"adjust the data so it does not duplicate\" hint when none is provided.\n *\n * @param input - The conflicting entity plus optional non-sensitive context\n * (field, constraint name, existing id, value hash/preview, correlation ids).\n */\n static detected(input: {\n entity: string;\n operation?: string;\n field?: string;\n constraintName?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n cause?: unknown;\n }): DuplicateError {\n return new DuplicateError({\n metadata: {\n entity: input.entity,\n operation: input.operation,\n field: input.field,\n constraintName: input.constraintName,\n existingId: input.existingId,\n valueHash: input.valueHash,\n valuePreview: input.valuePreview,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n hint:\n input.hint ??\n \"Adjust the data so it does not duplicate existing records.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * A uniqueness breach surfaced by the storage layer itself (a database unique\n * index), carrying the raw {@link UniqueConstraintViolation} (constraint, table,\n * columns). Keep this distinct from {@link DuplicateError}: this one is the\n * infrastructure signal; translate it into a domain duplicate at the boundary\n * with {@link translateUniqueViolationToDuplicate} when the model should own the\n * message. Construct via {@link UniqueConstraintViolationError.detected}.\n */\nclass UniqueConstraintViolationError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\"> & {\n violation: UniqueConstraintViolation;\n };\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message: \"Uniqueness violation in storage\",\n code: ErrorCodes.conflict.uniqueViolation,\n kind: \"unique_violation\",\n ...input,\n });\n }\n\n /**\n * Builds a {@link UniqueConstraintViolationError} from the constraint details\n * a driver reports, packaging them into a nested `violation` payload.\n *\n * @param input - The entity plus the offending constraint name, table, and\n * columns, and optional correlation ids.\n */\n static detected(input: {\n entity: string;\n operation?: string;\n constraintName?: string;\n table?: string;\n columns?: string[];\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n cause?: unknown;\n }): UniqueConstraintViolationError {\n return new UniqueConstraintViolationError({\n metadata: {\n entity: input.entity,\n operation: input.operation,\n constraintName: input.constraintName,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n violation: {\n kind: \"unique_violation\",\n constraintName: input.constraintName,\n table: input.table,\n columns: input.columns,\n },\n hint: \"Uniqueness conflict detected in the database.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * Translates a raw storage {@link UniqueConstraintViolation} into a domain-level\n * {@link DuplicateError}. This is the seam where an infrastructure concern (a DB\n * unique index firing) is re-expressed in the language of the model, so callers\n * upstream catch a `DuplicateError` and never have to know a database was\n * involved.\n *\n * @param input - The entity, the driver-reported `violation`, and optional\n * request context (`ctx`) carrying correlation ids and a clock instant.\n * @returns A {@link DuplicateError} carrying the constraint name as its field hint.\n */\nfunction translateUniqueViolationToDuplicate(input: {\n entity: string;\n operation?: string;\n violation: UniqueConstraintViolation;\n field?: string;\n ctx?: {\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n nowIso?: string;\n };\n cause?: unknown;\n}): DuplicateError {\n return DuplicateError.detected({\n entity: input.entity,\n operation: input.operation,\n field: input.field,\n constraintName: input.violation.constraintName,\n detectedAtIso: input.ctx?.nowIso,\n correlationId: input.ctx?.correlationId,\n requestId: input.ctx?.requestId,\n commandId: input.ctx?.commandId,\n cause: input.cause,\n });\n}\n\nexport {\n AlreadyExistsError,\n ConflictError,\n DuplicateError,\n translateUniqueViolationToDuplicate,\n UniqueConstraintViolationError,\n};\n\nexport type { ConflictErrorMetadata, ConflictKind, UniqueConstraintViolation };\n","import { payloadHash, sha256Hex, stableStringify } from \"../shared/hashing.js\";\n\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\n\nexport type IdempotencyFailureKind =\n | \"key_missing\"\n | \"in_progress\"\n | \"payload_mismatch\"\n | \"replay_not_supported\"\n | \"unknown\";\n\nexport type IdempotencyErrorMetadata = {\n kind: IdempotencyFailureKind;\n operation: string;\n scope?: string;\n entity?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n};\n\ntype IdempotencyErrorConstructorInput = {\n message: string;\n code: string;\n kind: IdempotencyFailureKind;\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n};\n\nabstract class IdempotencyError extends AppError {\n public readonly kind: IdempotencyFailureKind;\n\n protected constructor(input: IdempotencyErrorConstructorInput) {\n super(input.message, input.code, {\n cause: input.cause,\n metadata: { kind: input.kind, ...input.metadata },\n });\n\n this.kind = input.kind;\n Object.freeze(this);\n }\n\n static keyMissing(input: {\n operation: string;\n scope?: string;\n entity?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyKeyMissingError {\n return IdempotencyKeyMissingError.detected(input);\n }\n\n static inProgress(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyInProgressError {\n return IdempotencyInProgressError.detected(input);\n }\n\n static payloadMismatch(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyPayloadMismatchError {\n return IdempotencyPayloadMismatchError.detected(input);\n }\n\n static replayNotSupported(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyReplayNotSupportedError {\n return IdempotencyReplayNotSupportedError.detected(input);\n }\n}\n\nclass IdempotencyKeyMissingError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message:\n \"Idempotency key/commandId missing for the requested operation\",\n code: ErrorCodes.idempotency.keyMissing,\n kind: \"key_missing\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyKeyMissingError {\n return new IdempotencyKeyMissingError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Send a commandId/idempotency key to prevent duplicate processing.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyInProgressError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message: \"The same business intent is already being processed\",\n code: ErrorCodes.idempotency.inProgress,\n kind: \"in_progress\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyInProgressError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyInProgressError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Wait for completion and retry using the same key.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyPayloadMismatchError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message: \"The idempotency key was reused with a different payload\",\n code: ErrorCodes.idempotency.payloadMismatch,\n kind: \"payload_mismatch\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyPayloadMismatchError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyPayloadMismatchError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n incomingPayloadHash: input.incomingPayloadHash,\n existingPayloadHash: input.existingPayloadHash,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Use a new idempotency key for a new intent. The same key should only be reused for retries of the same payload.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyReplayNotSupportedError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message:\n \"Re-execution detected, but replay of the previous result is not implemented\",\n code: ErrorCodes.idempotency.replayNotSupported,\n kind: \"replay_not_supported\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyReplayNotSupportedError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyReplayNotSupportedError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Try again later, or enable result replay for full idempotency.\",\n details: input.details,\n },\n });\n }\n}\n\nfunction resolveKeyIdentity(\n key?: string,\n keyHash?: string,\n keyPreview?: string,\n): { keyHash?: string; keyPreview?: string } {\n const preview =\n keyPreview ??\n (key ? (key.length <= 8 ? key : key.slice(0, 8)) : undefined);\n\n return {\n keyHash: keyHash,\n keyPreview: preview,\n };\n}\n\nexport {\n IdempotencyError,\n IdempotencyKeyMissingError,\n IdempotencyInProgressError,\n IdempotencyPayloadMismatchError,\n IdempotencyReplayNotSupportedError,\n sha256Hex,\n stableStringify,\n payloadHash,\n};\n","import type { AppErrorOptions, ErrorSeverity } from \"./types.js\";\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\n\ntype IntegrationErrorReason =\n | \"timeout\"\n | \"unreachable\"\n | \"bad_response\"\n | \"unknown\";\n\ntype IntegrationErrorMetadata = {\n reason: IntegrationErrorReason;\n provider: string;\n operation: string;\n startedAtIso: string;\n durationMs: number;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n details?: string;\n statusCode?: number;\n responseCode?: string;\n};\n\ntype IntegrationErrorAppOptions = Pick<\n AppErrorOptions,\n | \"cause\"\n | \"type\"\n | \"severity\"\n | \"correlationId\"\n | \"requestId\"\n | \"commandId\"\n | \"createdAtIso\"\n | \"publicMessage\"\n>;\n\ntype IntegrationErrorConstructorParams = {\n message: string;\n code: string;\n reason: IntegrationErrorReason;\n metadata: IntegrationErrorMetadata;\n} & IntegrationErrorAppOptions;\n\n/**\n * Raised when a call to an external provider fails — a timeout, an unreachable\n * endpoint, or a malformed response. This is the boundary error that separates\n * \"our code is fine, the outside world misbehaved\" from internal faults, which\n * matters for retry and alerting decisions.\n *\n * Every instance records the `provider`, the `operation`, and timing\n * (`startedAtIso` / `durationMs`) so failures are correlatable across services\n * and a slow dependency is visible in the metadata. The discriminating\n * {@link reason} lets callers decide whether a failure is worth retrying.\n * Instances are frozen; construct through the static factories.\n */\nclass IntegrationError extends AppError {\n public readonly reason: IntegrationErrorReason;\n\n private constructor(params: IntegrationErrorConstructorParams) {\n const { message, code, metadata, ...rest } = params;\n super(message, code, {\n ...rest,\n metadata,\n });\n\n this.reason = params.reason;\n Object.freeze(this);\n }\n\n /**\n * The provider did not respond within the allotted time. Usually retryable,\n * since a timeout leaves the outcome unknown rather than known-failed.\n */\n static timeout(options: IntegrationErrorTimeoutOptions): IntegrationError {\n return new IntegrationError({\n message: \"Timeout while integrating with external provider\",\n code: ErrorCodes.integration.timeout,\n reason: \"timeout\",\n metadata: buildMetadata({ reason: \"timeout\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n\n /**\n * The provider could not be reached at all (connection refused, DNS\n * failure, network partition) — the request never landed.\n */\n static unreachable(\n options: IntegrationErrorEndpointOptions,\n ): IntegrationError {\n return new IntegrationError({\n message: \"Provider unavailable\",\n code: ErrorCodes.integration.unreachable,\n reason: \"unreachable\",\n metadata: buildMetadata({ reason: \"unreachable\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n\n /**\n * The provider answered, but with something the system cannot use — an\n * unexpected status, an unparsable body, or a contract mismatch. Unlike a\n * timeout this is a definite failure, so blind retries rarely help.\n */\n static badResponse(\n options: IntegrationErrorEndpointOptions,\n ): IntegrationError {\n return new IntegrationError({\n message: \"Unexpected response from provider\",\n code: ErrorCodes.integration.badResponse,\n reason: \"bad_response\",\n metadata: buildMetadata({ reason: \"bad_response\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n}\n\ntype IntegrationErrorBaseOptions = {\n provider: string;\n operation: string;\n startedAtIso: string;\n durationMs: number;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n details?: string;\n statusCode?: number;\n responseCode?: string;\n cause?: unknown;\n type?: string;\n severity?: ErrorSeverity;\n createdAtIso?: string;\n publicMessage?: string;\n};\n\ntype IntegrationErrorTimeoutOptions = IntegrationErrorBaseOptions;\ntype IntegrationErrorEndpointOptions = IntegrationErrorBaseOptions;\n\nfunction buildMetadata(\n input: IntegrationErrorBaseOptions & { reason: IntegrationErrorReason },\n): IntegrationErrorMetadata {\n return compactMetadata({\n reason: input.reason,\n provider: input.provider,\n operation: input.operation,\n startedAtIso: input.startedAtIso,\n durationMs: input.durationMs,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n details: input.details,\n statusCode: input.statusCode,\n responseCode: input.responseCode,\n }) as IntegrationErrorMetadata;\n}\n\nfunction pickAppErrorFields(\n options: IntegrationErrorBaseOptions,\n): IntegrationErrorAppOptions {\n return {\n cause: options.cause,\n type: options.type,\n severity: options.severity,\n correlationId: options.correlationId,\n requestId: options.requestId,\n commandId: options.commandId,\n createdAtIso: options.createdAtIso,\n publicMessage: options.publicMessage,\n };\n}\n\nfunction compactMetadata<T extends Record<string, unknown>>(input: T): T {\n return Object.fromEntries(\n Object.entries(input).filter(([, value]) => value !== undefined),\n ) as T;\n}\n\nexport { IntegrationError };\nexport type { IntegrationErrorReason, IntegrationErrorMetadata };\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// LegacyIncompatibleError\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass LegacyIncompatibleError extends AppError {\n constructor(\n message: string,\n context?: Record<string, unknown>,\n options?: AppErrorOptions,\n ) {\n const mergedMetadata = options?.metadata\n ? { ...(context ?? {}), ...options.metadata }\n : context;\n\n super(message, ErrorCodes.legacyIncompatible, {\n ...options,\n metadata: mergedMetadata,\n });\n }\n}\n\nexport { LegacyIncompatibleError };\n","import { AppError } from \"./app-error.js\";\nimport { serializationErrorCode } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Types\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype SerializationDirection = \"serialize\" | \"deserialize\";\n\ntype SerializationFailureCategory =\n | \"invalid_json\"\n | \"invalid_shape\"\n | \"missing_field\"\n | \"unexpected_field\"\n | \"invalid_type\"\n | \"schema_version_missing\"\n | \"schema_version_unsupported\"\n | \"mapping_not_implemented\"\n | \"contract_mismatch\"\n | \"parse_failed\"\n | \"stringify_failed\"\n | \"unknown\";\n\ntype SerializationBoundary =\n | \"http_in\"\n | \"http_out\"\n | \"graphql_in\"\n | \"graphql_out\"\n | \"dto_to_domain\"\n | \"domain_to_dto\"\n | \"persistence_to_domain\"\n | \"domain_to_persistence\"\n | \"event_in\"\n | \"event_out\"\n | \"projection\"\n | \"unknown\";\n\n/**\n * Metadata shared by serialization/deserialization failures. Payload previews must be sanitized.\n */\ntype SerializationErrorMetadata = {\n direction: SerializationDirection;\n category: SerializationFailureCategory;\n boundary: SerializationBoundary;\n contract: string;\n schemaVersion?: number | string;\n path?: string;\n payloadPreview?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n details?: string;\n};\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Base error\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype SerializationErrorOptions = Omit<AppErrorOptions, \"metadata\"> & {\n message: string;\n code: string;\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\">;\n direction: SerializationDirection;\n category: SerializationFailureCategory;\n};\n\nabstract class SerializationError extends AppError {\n public readonly direction: SerializationDirection;\n public readonly category: SerializationFailureCategory;\n public readonly boundary: SerializationBoundary;\n public readonly contract: string;\n\n protected constructor(input: SerializationErrorOptions) {\n super(input.message ?? \"Serialization failure\", input.code, {\n cause: input.cause,\n metadata: {\n direction: input.direction,\n category: input.category,\n ...input.metadata,\n },\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n\n this.direction = input.direction;\n this.category = input.category;\n this.boundary = input.metadata.boundary;\n this.contract = input.metadata.contract;\n\n Object.freeze(this);\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Serialization In\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationInError extends SerializationError {\n private constructor(input: SerializationErrorOptions) {\n super(input);\n }\n\n static failure(\n category: SerializationFailureCategory,\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\"> & {\n cause?: unknown;\n },\n ): SerializationInError {\n return new SerializationInError({\n message: SerializationMessages.message(\"deserialize\", category),\n code: SerializationCodes.code(\"deserialize\", category),\n category,\n direction: \"deserialize\",\n metadata,\n cause: metadata.cause,\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Serialization\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationOutError extends SerializationError {\n private constructor(input: SerializationErrorOptions) {\n super(input);\n }\n\n static failure(\n category: SerializationFailureCategory,\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\"> & {\n cause?: unknown;\n },\n ): SerializationOutError {\n return new SerializationOutError({\n message: SerializationMessages.message(\"serialize\", category),\n code: SerializationCodes.code(\"serialize\", category),\n category,\n direction: \"serialize\",\n metadata,\n cause: metadata.cause,\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Codes + Messages helpers\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationCodes {\n static code(\n direction: SerializationDirection,\n category: SerializationFailureCategory,\n ): string {\n return serializationErrorCode(direction, category);\n }\n}\n\nclass SerializationMessages {\n static message(\n direction: SerializationDirection,\n category: SerializationFailureCategory,\n ): string {\n if (direction === \"deserialize\") {\n if (category === \"invalid_json\") {\n return \"Invalid payload: malformed JSON\";\n }\n if (category === \"schema_version_missing\") {\n return \"Invalid payload: schemaVersion is missing\";\n }\n if (category === \"schema_version_unsupported\") {\n return \"Invalid payload: schemaVersion is not supported\";\n }\n if (category === \"invalid_shape\") {\n return \"Invalid payload: incompatible shape\";\n }\n if (category === \"mapping_not_implemented\") {\n return \"Unsupported payload: mapping not implemented\";\n }\n if (category === \"contract_mismatch\") {\n return \"Payload does not match the expected contract\";\n }\n return \"Failed to parse incoming payload\";\n }\n\n if (category === \"stringify_failed\") {\n return \"Failed to serialize the response\";\n }\n if (category === \"contract_mismatch\") {\n return \"Response does not match the expected contract\";\n }\n return \"Failed to build the response\";\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Utilities\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction safePreview(value: unknown, limit = 240): string | undefined {\n try {\n const serialized =\n typeof value === \"string\" ? value : JSON.stringify(value);\n return serialized.length > limit\n ? `${serialized.slice(0, limit)}…`\n : serialized;\n } catch {\n return undefined;\n }\n}\n\nexport {\n safePreview,\n SerializationCodes,\n SerializationError,\n SerializationInError as DeserializationError,\n SerializationInError,\n SerializationMessages,\n SerializationOutError,\n};\nexport type {\n SerializationBoundary,\n SerializationDirection,\n SerializationErrorMetadata,\n SerializationFailureCategory,\n};\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\ntype TemporalKind = \"expired\" | \"not_yet_valid\";\n\ntype TemporalPrecision = \"EXACT\" | \"ESTIMATED\" | \"UNKNOWN\" | \"APPROXIMATE\";\n\ntype TemporalErrorMetadata = {\n resourceType: string;\n resourceId?: string;\n operation?: string;\n validFromIso?: string | null;\n validUntilIso?: string | null;\n evaluatedAtIso: string;\n policyId?: string;\n precision?: TemporalPrecision;\n hint?: string;\n details?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n};\n\ntype TemporalErrorOptions = {\n message: string;\n code: string;\n kind: TemporalKind;\n metadata: TemporalErrorMetadata;\n} & AppErrorOptions;\n\nabstract class TemporalError extends AppError {\n public readonly kind: TemporalKind;\n public readonly resourceType: string;\n public readonly evaluatedAtIso: string;\n\n protected constructor(input: TemporalErrorOptions) {\n super(input.message, input.code, {\n ...input,\n metadata: {\n kind: input.kind,\n ...input.metadata,\n },\n });\n\n this.kind = input.kind;\n this.resourceType = input.metadata.resourceType;\n this.evaluatedAtIso = input.metadata.evaluatedAtIso;\n Object.freeze(this);\n }\n}\n\ntype TemporalCreationInput = TemporalErrorMetadata &\n Partial<Omit<AppErrorOptions, \"metadata\">> & {\n cause?: unknown;\n };\n\nclass ExpiredError extends TemporalError {\n private constructor(input: TemporalErrorOptions) {\n super(input);\n }\n\n static detected(input: TemporalCreationInput): ExpiredError {\n return new ExpiredError({\n message: \"The validity of the resource/action has expired\",\n code: ErrorCodes.temporal.expired,\n kind: \"expired\",\n metadata: {\n ...input,\n hint:\n input.hint ??\n \"The window has expired. Request a new link or try again within the valid period.\",\n },\n cause: input.cause,\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n }\n}\n\nclass NotYetValidError extends TemporalError {\n private constructor(input: TemporalErrorOptions) {\n super(input);\n }\n\n static detected(input: TemporalCreationInput): NotYetValidError {\n return new NotYetValidError({\n message: \"The resource/action is not yet valid\",\n code: ErrorCodes.temporal.notYetValid,\n kind: \"not_yet_valid\",\n metadata: {\n ...input,\n hint:\n input.hint ??\n \"Not yet within the valid period. Try again later.\",\n },\n cause: input.cause,\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n }\n}\n\nfunction evaluateTemporalWindow(input: {\n validFromIso?: string | null;\n validUntilIso?: string | null;\n evaluatedAtIso: string;\n}): { expired: boolean; notYetValid: boolean } | null {\n const evaluatedMs = Date.parse(input.evaluatedAtIso);\n if (Number.isNaN(evaluatedMs)) return null;\n\n let notYetValid = false;\n\n if (input.validFromIso) {\n const fromMs = Date.parse(input.validFromIso);\n if (!Number.isNaN(fromMs) && evaluatedMs < fromMs) {\n notYetValid = true;\n }\n }\n\n if (input.validUntilIso) {\n const untilMs = Date.parse(input.validUntilIso);\n if (!Number.isNaN(untilMs) && evaluatedMs > untilMs) {\n return { expired: true, notYetValid: false };\n }\n }\n\n return { expired: false, notYetValid };\n}\n\nexport {\n evaluateTemporalWindow,\n ExpiredError,\n NotYetValidError,\n TemporalError,\n};\nexport type { TemporalErrorMetadata, TemporalKind, TemporalPrecision };\n","import { ValidationCode } from \"../exceptions/validation-code.js\";\nimport { ValidationField } from \"../exceptions/validation-field.js\";\n\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// ValidationError\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Raised when a single input fails a validation rule — the canonical\n * \"this field is wrong, and here is why\" error.\n *\n * It pins the offending `field` and a machine-readable `validationCode` into\n * the metadata (merged ahead of any caller-supplied metadata) so an API layer\n * can map the failure straight onto a form field without parsing the message.\n * The fixed {@link ErrorCodes.validation} code lets callers catch all\n * validation failures uniformly while still distinguishing the specific rule\n * through `validationCode`.\n */\nclass ValidationError extends AppError {\n /**\n * @param field - The input that failed validation; surfaced as `metadata.field`.\n * @param code - The specific rule that was violated; surfaced as `metadata.validationCode`.\n * @param message - The developer-facing description of the failure.\n * @param options - Optional cause, correlation ids, and extra metadata\n * (merged over the field/code metadata, never overwriting it by accident).\n */\n constructor(\n field: ValidationField,\n code: ValidationCode,\n message: string,\n options?: AppErrorOptions,\n ) {\n const baseMetadata = {\n field: field.value,\n validationCode: code.value,\n };\n const mergedMetadata = options?.metadata\n ? { ...baseMetadata, ...options.metadata }\n : baseMetadata;\n\n super(message, ErrorCodes.validation, {\n ...options,\n metadata: mergedMetadata,\n });\n }\n}\n\nexport { ValidationError };\n"],"mappings":";;AAuCA,IAAM,sBAAN,MAAM,4BAA4B,SAAS;CAGvC,YAAoB,QAajB;EACC,MAAM,OAAO,SAAS,OAAO,MAAM;GAC/B,OAAO,OAAO;GACd,UAAU;IACN,QAAQ,OAAO;IACf,GAAI,OAAO,Y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MAAM,KAAK,cAAc,QACvC,cAAc;CAEtB;CAEA,IAAI,MAAM,eAAe;EACrB,MAAM,UAAU,KAAK,MAAM,MAAM,aAAa;EAC9C,IAAI,CAAC,OAAO,MAAM,OAAO,KAAK,cAAc,SACxC,OAAO;GAAE,SAAS;GAAM,aAAa;EAAM;CAEnD;CAEA,OAAO;EAAE,SAAS;EAAO;CAAY;AACzC;;;;;;;;;;;;;;ACpHA,IAAM,kBAAN,cAA8B,SAAS;;;;;;;;CAQnC,YACI,OACA,MACA,SACA,SACF;EACE,MAAM,eAAe;GACjB,OAAO,MAAM;GACb,gBAAgB,KAAK;EACzB;EACA,MAAM,iBAAiB,SAAS,WAC1B;GAAE,GAAG;GAAc,GAAG,QAAQ;EAAS,IACvC;EAEN,MAAM,SAAS,WAAW,YAAY;GAClC,GAAG;GACH,UAAU;EACd,CAAC;CACL;AACJ"}
|
|
1
|
+
{"version":3,"file":"validation-error.js","names":["compactMetadata"],"sources":["../src/core/errors/authentication-error.ts","../src/core/errors/conflict-error.ts","../src/core/errors/idempotency-error.ts","../src/core/errors/integration-error.ts","../src/core/errors/legacy-incompatible-error.ts","../src/core/errors/serialization-error.ts","../src/core/errors/temporal-error.ts","../src/core/errors/validation-error.ts"],"sourcesContent":["import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions, ErrorSeverity } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Types\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype AuthenticationErrorReason =\n | \"missing_token\"\n | \"malformed_token\"\n | \"invalid_token\"\n | \"expired_token\"\n | \"revoked_token\"\n | \"invalid_credentials\"\n | \"session_not_found\"\n | \"session_expired\"\n | \"unknown\";\n\ntype AuthenticationErrorMetadata = {\n reason: AuthenticationErrorReason;\n authScheme?: \"bearer\" | \"cookie\" | \"basic\" | \"unknown\";\n tokenLocation?: \"header\" | \"cookie\" | \"query\" | \"unknown\";\n expiresAtIso?: string;\n correlationId?: string;\n requestId?: string;\n details?: string;\n};\n\ntype AuthenticationErrorFactoryOptions = Omit<\n AuthenticationErrorMetadata,\n \"reason\"\n> &\n Omit<AppErrorOptions, \"metadata\">;\n\n// ─────────────────────────────────────────────────────────────────────────────\n// AuthenticationError\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass AuthenticationError extends AppError {\n public readonly reason: AuthenticationErrorReason;\n\n private constructor(params: {\n message: string;\n code: string;\n reason: AuthenticationErrorReason;\n metadata?: Omit<AuthenticationErrorMetadata, \"reason\">;\n cause?: unknown;\n type?: string;\n severity?: ErrorSeverity;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n createdAtIso?: string;\n publicMessage?: string;\n }) {\n super(params.message, params.code, {\n cause: params.cause,\n metadata: {\n reason: params.reason,\n ...(params.metadata ?? {}),\n },\n type: params.type,\n severity: params.severity,\n correlationId: params.correlationId,\n requestId: params.requestId,\n commandId: params.commandId,\n createdAtIso: params.createdAtIso,\n publicMessage: params.publicMessage,\n });\n\n this.reason = params.reason;\n Object.freeze(this);\n }\n\n // ─────────────────────────────────────────────────────────────────────────\n // Factory methods\n // ─────────────────────────────────────────────────────────────────────────\n\n static missingToken(\n options?: AuthenticationErrorFactoryOptions,\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Missing credentials\",\n code: ErrorCodes.authentication.missingToken,\n reason: \"missing_token\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n\n static invalidToken(\n options?: AuthenticationErrorFactoryOptions & { cause?: unknown },\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Invalid credentials\",\n code: ErrorCodes.authentication.invalidToken,\n reason: \"invalid_token\",\n metadata: compactMetadata({\n authScheme: options?.authScheme,\n tokenLocation: options?.tokenLocation,\n details: options?.details,\n correlationId: options?.correlationId,\n requestId: options?.requestId,\n }),\n ...extractAppErrorOptions(options),\n });\n }\n\n static expiredToken(\n options?: AuthenticationErrorFactoryOptions & { sessionId?: string },\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Expired credentials\",\n code: ErrorCodes.authentication.expiredToken,\n reason: \"expired_token\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n\n static invalidCredentials(\n options?: Pick<\n AuthenticationErrorFactoryOptions,\n | \"correlationId\"\n | \"requestId\"\n | \"type\"\n | \"severity\"\n | \"createdAtIso\"\n | \"publicMessage\"\n >,\n ): AuthenticationError {\n return new AuthenticationError({\n message: \"Invalid username or password\",\n code: ErrorCodes.authentication.invalidCredentials,\n reason: \"invalid_credentials\",\n metadata: compactMetadata(options),\n ...extractAppErrorOptions(options),\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Helpers\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction extractAppErrorOptions(options?: Partial<AppErrorOptions>) {\n return {\n cause: options?.cause,\n type: options?.type,\n severity: options?.severity,\n correlationId: options?.correlationId,\n requestId: options?.requestId,\n commandId: options?.commandId,\n createdAtIso: options?.createdAtIso,\n publicMessage: options?.publicMessage,\n };\n}\n\nfunction compactMetadata<T extends Record<string, unknown>>(\n input?: T,\n): T | undefined {\n if (!input) return undefined;\n\n return Object.fromEntries(\n Object.entries(input).filter(([, value]) => value !== undefined),\n ) as T;\n}\n\nexport { AuthenticationError };\nexport type { AuthenticationErrorMetadata, AuthenticationErrorReason };\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n/**\n * Conflict kinds we want to distinguish semantically.\n */\ntype ConflictKind = \"already_exists\" | \"duplicate\" | \"unique_violation\";\n\n/**\n * Metadata attached to conflicts without exposing sensitive values.\n */\ntype ConflictErrorMetadata = {\n kind: ConflictKind;\n entity: string;\n operation?: string;\n field?: string;\n constraintName?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n};\n\n/**\n * Storage-level description of a unique-constraint breach, as reported by a\n * database driver. Used to translate a raw DB error into a domain\n * {@link DuplicateError} via {@link translateUniqueViolationToDuplicate}.\n */\ntype UniqueConstraintViolation = {\n kind: \"unique_violation\";\n constraintName?: string;\n table?: string;\n columns?: string[];\n};\n\n/**\n * Base for errors that signal a state conflict — the request collides with data\n * that already exists. Common at an HTTP 409 boundary.\n *\n * The discriminating {@link kind} lets a handler branch on *why* it conflicted\n * (an already-existing record, a domain duplicate, or a raw storage uniqueness\n * breach) without `instanceof` chains. Abstract: construct one of the concrete\n * subclasses through its `detected(...)` factory, which fills in the right code,\n * message, and metadata. Instances are frozen, so a conflict error can be shared\n * and rethrown without risk of tampering.\n */\nabstract class ConflictError extends AppError {\n public readonly kind: ConflictKind;\n\n protected constructor(\n params: {\n message: string;\n code: string;\n kind: ConflictKind;\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super(params.message, params.code, {\n ...params,\n metadata: {\n kind: params.kind,\n ...params.metadata,\n },\n });\n\n this.kind = params.kind;\n Object.freeze(this);\n }\n}\n\n/**\n * The operation cannot proceed because a matching record already exists —\n * e.g. creating something whose natural key is already taken. Construct via\n * {@link AlreadyExistsError.detected}.\n */\nclass AlreadyExistsError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message:\n \"A matching record already exists for the requested operation\",\n code: ErrorCodes.conflict.alreadyExists,\n kind: \"already_exists\",\n ...input,\n });\n }\n\n /**\n * Builds an {@link AlreadyExistsError} for a detected collision. `operation`\n * defaults to `\"create\"` and a generic retry `hint` is supplied when none is\n * given, so the error is actionable even from a minimal call site.\n *\n * @param input - The conflicting entity plus optional non-sensitive context\n * (operation, field, existing id, value hash/preview, correlation ids).\n */\n static detected(input: {\n entity: string;\n operation?: string;\n field?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n cause?: unknown;\n }): AlreadyExistsError {\n return new AlreadyExistsError({\n metadata: {\n entity: input.entity,\n operation: input.operation ?? \"create\",\n field: input.field,\n existingId: input.existingId,\n valueHash: input.valueHash,\n valuePreview: input.valuePreview,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n hint: input.hint ?? \"Verify existing records before retrying.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * A domain-level duplicate: the data being written would duplicate an existing\n * record under the model's own uniqueness rules. Use this when the duplication\n * is recognized in the domain, as opposed to a raw DB constraint\n * ({@link UniqueConstraintViolationError}). Construct via\n * {@link DuplicateError.detected}.\n */\nclass DuplicateError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\">;\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message: \"Conflict: an existing record violates uniqueness\",\n code: ErrorCodes.conflict.duplicate,\n kind: \"duplicate\",\n ...input,\n });\n }\n\n /**\n * Builds a {@link DuplicateError} for a detected duplicate, defaulting to a\n * \"adjust the data so it does not duplicate\" hint when none is provided.\n *\n * @param input - The conflicting entity plus optional non-sensitive context\n * (field, constraint name, existing id, value hash/preview, correlation ids).\n */\n static detected(input: {\n entity: string;\n operation?: string;\n field?: string;\n constraintName?: string;\n existingId?: string;\n valueHash?: string;\n valuePreview?: string;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n cause?: unknown;\n }): DuplicateError {\n return new DuplicateError({\n metadata: {\n entity: input.entity,\n operation: input.operation,\n field: input.field,\n constraintName: input.constraintName,\n existingId: input.existingId,\n valueHash: input.valueHash,\n valuePreview: input.valuePreview,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n hint:\n input.hint ??\n \"Adjust the data so it does not duplicate existing records.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * A uniqueness breach surfaced by the storage layer itself (a database unique\n * index), carrying the raw {@link UniqueConstraintViolation} (constraint, table,\n * columns). Keep this distinct from {@link DuplicateError}: this one is the\n * infrastructure signal; translate it into a domain duplicate at the boundary\n * with {@link translateUniqueViolationToDuplicate} when the model should own the\n * message. Construct via {@link UniqueConstraintViolationError.detected}.\n */\nclass UniqueConstraintViolationError extends ConflictError {\n private constructor(\n input: {\n metadata: Omit<ConflictErrorMetadata, \"kind\"> & {\n violation: UniqueConstraintViolation;\n };\n cause?: unknown;\n } & AppErrorOptions,\n ) {\n super({\n message: \"Uniqueness violation in storage\",\n code: ErrorCodes.conflict.uniqueViolation,\n kind: \"unique_violation\",\n ...input,\n });\n }\n\n /**\n * Builds a {@link UniqueConstraintViolationError} from the constraint details\n * a driver reports, packaging them into a nested `violation` payload.\n *\n * @param input - The entity plus the offending constraint name, table, and\n * columns, and optional correlation ids.\n */\n static detected(input: {\n entity: string;\n operation?: string;\n constraintName?: string;\n table?: string;\n columns?: string[];\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n cause?: unknown;\n }): UniqueConstraintViolationError {\n return new UniqueConstraintViolationError({\n metadata: {\n entity: input.entity,\n operation: input.operation,\n constraintName: input.constraintName,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n violation: {\n kind: \"unique_violation\",\n constraintName: input.constraintName,\n table: input.table,\n columns: input.columns,\n },\n hint: \"Uniqueness conflict detected in the database.\",\n },\n cause: input.cause,\n });\n }\n}\n\n/**\n * Translates a raw storage {@link UniqueConstraintViolation} into a domain-level\n * {@link DuplicateError}. This is the seam where an infrastructure concern (a DB\n * unique index firing) is re-expressed in the language of the model, so callers\n * upstream catch a `DuplicateError` and never have to know a database was\n * involved.\n *\n * @param input - The entity, the driver-reported `violation`, and optional\n * request context (`ctx`) carrying correlation ids and a clock instant.\n * @returns A {@link DuplicateError} carrying the constraint name as its field hint.\n */\nfunction translateUniqueViolationToDuplicate(input: {\n entity: string;\n operation?: string;\n violation: UniqueConstraintViolation;\n field?: string;\n ctx?: {\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n nowIso?: string;\n };\n cause?: unknown;\n}): DuplicateError {\n return DuplicateError.detected({\n entity: input.entity,\n operation: input.operation,\n field: input.field,\n constraintName: input.violation.constraintName,\n detectedAtIso: input.ctx?.nowIso,\n correlationId: input.ctx?.correlationId,\n requestId: input.ctx?.requestId,\n commandId: input.ctx?.commandId,\n cause: input.cause,\n });\n}\n\nexport {\n AlreadyExistsError,\n ConflictError,\n DuplicateError,\n translateUniqueViolationToDuplicate,\n UniqueConstraintViolationError,\n};\n\nexport type { ConflictErrorMetadata, ConflictKind, UniqueConstraintViolation };\n","import { payloadHash, sha256Hex, stableStringify } from \"../shared/hashing.js\";\n\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\n\nexport type IdempotencyFailureKind =\n | \"key_missing\"\n | \"in_progress\"\n | \"payload_mismatch\"\n | \"replay_not_supported\"\n | \"unknown\";\n\nexport type IdempotencyErrorMetadata = {\n kind: IdempotencyFailureKind;\n operation: string;\n scope?: string;\n entity?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n};\n\ntype IdempotencyErrorConstructorInput = {\n message: string;\n code: string;\n kind: IdempotencyFailureKind;\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n};\n\nabstract class IdempotencyError extends AppError {\n public readonly kind: IdempotencyFailureKind;\n\n protected constructor(input: IdempotencyErrorConstructorInput) {\n super(input.message, input.code, {\n cause: input.cause,\n metadata: { kind: input.kind, ...input.metadata },\n });\n\n this.kind = input.kind;\n Object.freeze(this);\n }\n\n static keyMissing(input: {\n operation: string;\n scope?: string;\n entity?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyKeyMissingError {\n return IdempotencyKeyMissingError.detected(input);\n }\n\n static inProgress(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyInProgressError {\n return IdempotencyInProgressError.detected(input);\n }\n\n static payloadMismatch(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyPayloadMismatchError {\n return IdempotencyPayloadMismatchError.detected(input);\n }\n\n static replayNotSupported(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyReplayNotSupportedError {\n return IdempotencyReplayNotSupportedError.detected(input);\n }\n}\n\nclass IdempotencyKeyMissingError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message:\n \"Idempotency key/commandId missing for the requested operation\",\n code: ErrorCodes.idempotency.keyMissing,\n kind: \"key_missing\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyKeyMissingError {\n return new IdempotencyKeyMissingError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Send a commandId/idempotency key to prevent duplicate processing.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyInProgressError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message: \"The same business intent is already being processed\",\n code: ErrorCodes.idempotency.inProgress,\n kind: \"in_progress\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyInProgressError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyInProgressError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Wait for completion and retry using the same key.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyPayloadMismatchError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message: \"The idempotency key was reused with a different payload\",\n code: ErrorCodes.idempotency.payloadMismatch,\n kind: \"payload_mismatch\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n incomingPayloadHash?: string;\n existingPayloadHash?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyPayloadMismatchError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyPayloadMismatchError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n incomingPayloadHash: input.incomingPayloadHash,\n existingPayloadHash: input.existingPayloadHash,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Use a new idempotency key for a new intent. The same key should only be reused for retries of the same payload.\",\n details: input.details,\n },\n });\n }\n}\n\nclass IdempotencyReplayNotSupportedError extends IdempotencyError {\n private constructor(input: {\n metadata: Omit<IdempotencyErrorMetadata, \"kind\">;\n cause?: unknown;\n }) {\n super({\n message:\n \"Re-execution detected, but replay of the previous result is not implemented\",\n code: ErrorCodes.idempotency.replayNotSupported,\n kind: \"replay_not_supported\",\n metadata: input.metadata,\n cause: input.cause,\n });\n }\n\n static detected(input: {\n operation: string;\n scope?: string;\n entity?: string;\n key?: string;\n keyHash?: string;\n keyPreview?: string;\n idempotencyRecordId?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n detectedAtIso?: string;\n hint?: string;\n details?: string;\n cause?: unknown;\n }): IdempotencyReplayNotSupportedError {\n const resolved = resolveKeyIdentity(\n input.key,\n input.keyHash,\n input.keyPreview,\n );\n\n return new IdempotencyReplayNotSupportedError({\n cause: input.cause,\n metadata: {\n operation: input.operation,\n scope: input.scope,\n entity: input.entity,\n keyHash: resolved.keyHash,\n keyPreview: resolved.keyPreview,\n idempotencyRecordId: input.idempotencyRecordId,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n detectedAtIso: input.detectedAtIso,\n hint:\n input.hint ??\n \"Try again later, or enable result replay for full idempotency.\",\n details: input.details,\n },\n });\n }\n}\n\nfunction resolveKeyIdentity(\n key?: string,\n keyHash?: string,\n keyPreview?: string,\n): { keyHash?: string; keyPreview?: string } {\n const preview =\n keyPreview ??\n (key ? (key.length <= 8 ? key : key.slice(0, 8)) : undefined);\n\n return {\n keyHash: keyHash,\n keyPreview: preview,\n };\n}\n\nexport {\n IdempotencyError,\n IdempotencyKeyMissingError,\n IdempotencyInProgressError,\n IdempotencyPayloadMismatchError,\n IdempotencyReplayNotSupportedError,\n sha256Hex,\n stableStringify,\n payloadHash,\n};\n","import type { AppErrorOptions, ErrorSeverity } from \"./types.js\";\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\n\ntype IntegrationErrorReason =\n | \"timeout\"\n | \"unreachable\"\n | \"bad_response\"\n | \"unknown\";\n\ntype IntegrationErrorMetadata = {\n reason: IntegrationErrorReason;\n provider: string;\n operation: string;\n startedAtIso: string;\n durationMs: number;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n details?: string;\n statusCode?: number;\n responseCode?: string;\n};\n\ntype IntegrationErrorAppOptions = Pick<\n AppErrorOptions,\n | \"cause\"\n | \"type\"\n | \"severity\"\n | \"correlationId\"\n | \"requestId\"\n | \"commandId\"\n | \"createdAtIso\"\n | \"publicMessage\"\n>;\n\ntype IntegrationErrorConstructorParams = {\n message: string;\n code: string;\n reason: IntegrationErrorReason;\n metadata: IntegrationErrorMetadata;\n} & IntegrationErrorAppOptions;\n\n/**\n * Raised when a call to an external provider fails — a timeout, an unreachable\n * endpoint, or a malformed response. This is the boundary error that separates\n * \"our code is fine, the outside world misbehaved\" from internal faults, which\n * matters for retry and alerting decisions.\n *\n * Every instance records the `provider`, the `operation`, and timing\n * (`startedAtIso` / `durationMs`) so failures are correlatable across services\n * and a slow dependency is visible in the metadata. The discriminating\n * {@link reason} lets callers decide whether a failure is worth retrying.\n * Instances are frozen; construct through the static factories.\n */\nclass IntegrationError extends AppError {\n public readonly reason: IntegrationErrorReason;\n\n private constructor(params: IntegrationErrorConstructorParams) {\n const { message, code, metadata, ...rest } = params;\n super(message, code, {\n ...rest,\n metadata,\n });\n\n this.reason = params.reason;\n Object.freeze(this);\n }\n\n /**\n * The provider did not respond within the allotted time. Usually retryable,\n * since a timeout leaves the outcome unknown rather than known-failed.\n */\n static timeout(options: IntegrationErrorTimeoutOptions): IntegrationError {\n return new IntegrationError({\n message: \"Timeout while integrating with external provider\",\n code: ErrorCodes.integration.timeout,\n reason: \"timeout\",\n metadata: buildMetadata({ reason: \"timeout\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n\n /**\n * The provider could not be reached at all (connection refused, DNS\n * failure, network partition) — the request never landed.\n */\n static unreachable(\n options: IntegrationErrorEndpointOptions,\n ): IntegrationError {\n return new IntegrationError({\n message: \"Provider unavailable\",\n code: ErrorCodes.integration.unreachable,\n reason: \"unreachable\",\n metadata: buildMetadata({ reason: \"unreachable\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n\n /**\n * The provider answered, but with something the system cannot use — an\n * unexpected status, an unparsable body, or a contract mismatch. Unlike a\n * timeout this is a definite failure, so blind retries rarely help.\n */\n static badResponse(\n options: IntegrationErrorEndpointOptions,\n ): IntegrationError {\n return new IntegrationError({\n message: \"Unexpected response from provider\",\n code: ErrorCodes.integration.badResponse,\n reason: \"bad_response\",\n metadata: buildMetadata({ reason: \"bad_response\", ...options }),\n ...pickAppErrorFields(options),\n });\n }\n}\n\ntype IntegrationErrorBaseOptions = {\n provider: string;\n operation: string;\n startedAtIso: string;\n durationMs: number;\n detectedAtIso?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n details?: string;\n statusCode?: number;\n responseCode?: string;\n cause?: unknown;\n type?: string;\n severity?: ErrorSeverity;\n createdAtIso?: string;\n publicMessage?: string;\n};\n\ntype IntegrationErrorTimeoutOptions = IntegrationErrorBaseOptions;\ntype IntegrationErrorEndpointOptions = IntegrationErrorBaseOptions;\n\nfunction buildMetadata(\n input: IntegrationErrorBaseOptions & { reason: IntegrationErrorReason },\n): IntegrationErrorMetadata {\n return compactMetadata({\n reason: input.reason,\n provider: input.provider,\n operation: input.operation,\n startedAtIso: input.startedAtIso,\n durationMs: input.durationMs,\n detectedAtIso: input.detectedAtIso,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n details: input.details,\n statusCode: input.statusCode,\n responseCode: input.responseCode,\n }) as IntegrationErrorMetadata;\n}\n\nfunction pickAppErrorFields(\n options: IntegrationErrorBaseOptions,\n): IntegrationErrorAppOptions {\n return {\n cause: options.cause,\n type: options.type,\n severity: options.severity,\n correlationId: options.correlationId,\n requestId: options.requestId,\n commandId: options.commandId,\n createdAtIso: options.createdAtIso,\n publicMessage: options.publicMessage,\n };\n}\n\nfunction compactMetadata<T extends Record<string, unknown>>(input: T): T {\n return Object.fromEntries(\n Object.entries(input).filter(([, value]) => value !== undefined),\n ) as T;\n}\n\nexport { IntegrationError };\nexport type { IntegrationErrorReason, IntegrationErrorMetadata };\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// LegacyIncompatibleError\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass LegacyIncompatibleError extends AppError {\n constructor(\n message: string,\n context?: Record<string, unknown>,\n options?: AppErrorOptions,\n ) {\n const mergedMetadata = options?.metadata\n ? { ...(context ?? {}), ...options.metadata }\n : context;\n\n super(message, ErrorCodes.legacyIncompatible, {\n ...options,\n metadata: mergedMetadata,\n });\n }\n}\n\nexport { LegacyIncompatibleError };\n","import { AppError } from \"./app-error.js\";\nimport { serializationErrorCode } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Types\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype SerializationDirection = \"serialize\" | \"deserialize\";\n\ntype SerializationFailureCategory =\n | \"invalid_json\"\n | \"invalid_shape\"\n | \"missing_field\"\n | \"unexpected_field\"\n | \"invalid_type\"\n | \"schema_version_missing\"\n | \"schema_version_unsupported\"\n | \"mapping_not_implemented\"\n | \"contract_mismatch\"\n | \"parse_failed\"\n | \"stringify_failed\"\n | \"unknown\";\n\ntype SerializationBoundary =\n | \"http_in\"\n | \"http_out\"\n | \"graphql_in\"\n | \"graphql_out\"\n | \"dto_to_domain\"\n | \"domain_to_dto\"\n | \"persistence_to_domain\"\n | \"domain_to_persistence\"\n | \"event_in\"\n | \"event_out\"\n | \"projection\"\n | \"unknown\";\n\n/**\n * Metadata shared by serialization/deserialization failures. Payload previews must be sanitized.\n */\ntype SerializationErrorMetadata = {\n direction: SerializationDirection;\n category: SerializationFailureCategory;\n boundary: SerializationBoundary;\n contract: string;\n schemaVersion?: number | string;\n path?: string;\n payloadPreview?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n hint?: string;\n details?: string;\n};\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Base error\n// ─────────────────────────────────────────────────────────────────────────────\n\ntype SerializationErrorOptions = Omit<AppErrorOptions, \"metadata\"> & {\n message: string;\n code: string;\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\">;\n direction: SerializationDirection;\n category: SerializationFailureCategory;\n};\n\nabstract class SerializationError extends AppError {\n public readonly direction: SerializationDirection;\n public readonly category: SerializationFailureCategory;\n public readonly boundary: SerializationBoundary;\n public readonly contract: string;\n\n protected constructor(input: SerializationErrorOptions) {\n super(input.message ?? \"Serialization failure\", input.code, {\n cause: input.cause,\n metadata: {\n direction: input.direction,\n category: input.category,\n ...input.metadata,\n },\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n\n this.direction = input.direction;\n this.category = input.category;\n this.boundary = input.metadata.boundary;\n this.contract = input.metadata.contract;\n\n Object.freeze(this);\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Serialization In\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationInError extends SerializationError {\n private constructor(input: SerializationErrorOptions) {\n super(input);\n }\n\n static failure(\n category: SerializationFailureCategory,\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\"> & {\n cause?: unknown;\n },\n ): SerializationInError {\n return new SerializationInError({\n message: SerializationMessages.message(\"deserialize\", category),\n code: SerializationCodes.code(\"deserialize\", category),\n category,\n direction: \"deserialize\",\n metadata,\n cause: metadata.cause,\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Serialization\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationOutError extends SerializationError {\n private constructor(input: SerializationErrorOptions) {\n super(input);\n }\n\n static failure(\n category: SerializationFailureCategory,\n metadata: Omit<SerializationErrorMetadata, \"direction\" | \"category\"> & {\n cause?: unknown;\n },\n ): SerializationOutError {\n return new SerializationOutError({\n message: SerializationMessages.message(\"serialize\", category),\n code: SerializationCodes.code(\"serialize\", category),\n category,\n direction: \"serialize\",\n metadata,\n cause: metadata.cause,\n });\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Codes + Messages helpers\n// ─────────────────────────────────────────────────────────────────────────────\n\nclass SerializationCodes {\n static code(\n direction: SerializationDirection,\n category: SerializationFailureCategory,\n ): string {\n return serializationErrorCode(direction, category);\n }\n}\n\nclass SerializationMessages {\n static message(\n direction: SerializationDirection,\n category: SerializationFailureCategory,\n ): string {\n if (direction === \"deserialize\") {\n if (category === \"invalid_json\") {\n return \"Invalid payload: malformed JSON\";\n }\n if (category === \"schema_version_missing\") {\n return \"Invalid payload: schemaVersion is missing\";\n }\n if (category === \"schema_version_unsupported\") {\n return \"Invalid payload: schemaVersion is not supported\";\n }\n if (category === \"invalid_shape\") {\n return \"Invalid payload: incompatible shape\";\n }\n if (category === \"mapping_not_implemented\") {\n return \"Unsupported payload: mapping not implemented\";\n }\n if (category === \"contract_mismatch\") {\n return \"Payload does not match the expected contract\";\n }\n return \"Failed to parse incoming payload\";\n }\n\n if (category === \"stringify_failed\") {\n return \"Failed to serialize the response\";\n }\n if (category === \"contract_mismatch\") {\n return \"Response does not match the expected contract\";\n }\n return \"Failed to build the response\";\n }\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Utilities\n// ─────────────────────────────────────────────────────────────────────────────\n\nfunction safePreview(value: unknown, limit = 240): string | undefined {\n try {\n const serialized =\n typeof value === \"string\" ? value : JSON.stringify(value);\n return serialized.length > limit\n ? `${serialized.slice(0, limit)}…`\n : serialized;\n } catch {\n return undefined;\n }\n}\n\nexport {\n safePreview,\n SerializationCodes,\n SerializationError,\n SerializationInError as DeserializationError,\n SerializationInError,\n SerializationMessages,\n SerializationOutError,\n};\nexport type {\n SerializationBoundary,\n SerializationDirection,\n SerializationErrorMetadata,\n SerializationFailureCategory,\n};\n","import { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\ntype TemporalKind = \"expired\" | \"not_yet_valid\";\n\ntype TemporalPrecision = \"EXACT\" | \"ESTIMATED\" | \"UNKNOWN\" | \"APPROXIMATE\";\n\ntype TemporalErrorMetadata = {\n resourceType: string;\n resourceId?: string;\n operation?: string;\n validFromIso?: string | null;\n validUntilIso?: string | null;\n evaluatedAtIso: string;\n policyId?: string;\n precision?: TemporalPrecision;\n hint?: string;\n details?: string;\n correlationId?: string;\n requestId?: string;\n commandId?: string;\n};\n\ntype TemporalErrorOptions = {\n message: string;\n code: string;\n kind: TemporalKind;\n metadata: TemporalErrorMetadata;\n} & AppErrorOptions;\n\nabstract class TemporalError extends AppError {\n public readonly kind: TemporalKind;\n public readonly resourceType: string;\n public readonly evaluatedAtIso: string;\n\n protected constructor(input: TemporalErrorOptions) {\n super(input.message, input.code, {\n ...input,\n metadata: {\n kind: input.kind,\n ...input.metadata,\n },\n });\n\n this.kind = input.kind;\n this.resourceType = input.metadata.resourceType;\n this.evaluatedAtIso = input.metadata.evaluatedAtIso;\n Object.freeze(this);\n }\n}\n\ntype TemporalCreationInput = TemporalErrorMetadata &\n Partial<Omit<AppErrorOptions, \"metadata\">> & {\n cause?: unknown;\n };\n\nclass ExpiredError extends TemporalError {\n private constructor(input: TemporalErrorOptions) {\n super(input);\n }\n\n static detected(input: TemporalCreationInput): ExpiredError {\n return new ExpiredError({\n message: \"The validity of the resource/action has expired\",\n code: ErrorCodes.temporal.expired,\n kind: \"expired\",\n metadata: {\n ...input,\n hint:\n input.hint ??\n \"The window has expired. Request a new link or try again within the valid period.\",\n },\n cause: input.cause,\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n }\n}\n\nclass NotYetValidError extends TemporalError {\n private constructor(input: TemporalErrorOptions) {\n super(input);\n }\n\n static detected(input: TemporalCreationInput): NotYetValidError {\n return new NotYetValidError({\n message: \"The resource/action is not yet valid\",\n code: ErrorCodes.temporal.notYetValid,\n kind: \"not_yet_valid\",\n metadata: {\n ...input,\n hint:\n input.hint ??\n \"Not yet within the valid period. Try again later.\",\n },\n cause: input.cause,\n type: input.type,\n severity: input.severity,\n correlationId: input.correlationId,\n requestId: input.requestId,\n commandId: input.commandId,\n createdAtIso: input.createdAtIso,\n publicMessage: input.publicMessage,\n });\n }\n}\n\nfunction evaluateTemporalWindow(input: {\n validFromIso?: string | null;\n validUntilIso?: string | null;\n evaluatedAtIso: string;\n}): { expired: boolean; notYetValid: boolean } | null {\n const evaluatedMs = Date.parse(input.evaluatedAtIso);\n if (Number.isNaN(evaluatedMs)) return null;\n\n let notYetValid = false;\n\n if (input.validFromIso) {\n const fromMs = Date.parse(input.validFromIso);\n if (!Number.isNaN(fromMs) && evaluatedMs < fromMs) {\n notYetValid = true;\n }\n }\n\n if (input.validUntilIso) {\n const untilMs = Date.parse(input.validUntilIso);\n if (!Number.isNaN(untilMs) && evaluatedMs > untilMs) {\n return { expired: true, notYetValid: false };\n }\n }\n\n return { expired: false, notYetValid };\n}\n\nexport {\n evaluateTemporalWindow,\n ExpiredError,\n NotYetValidError,\n TemporalError,\n};\nexport type { TemporalErrorMetadata, TemporalKind, TemporalPrecision };\n","import { ValidationCode } from \"../exceptions/validation-code.js\";\nimport { ValidationField } from \"../exceptions/validation-field.js\";\n\nimport { AppError } from \"./app-error.js\";\nimport { ErrorCodes } from \"./error-codes.js\";\nimport type { AppErrorOptions } from \"./types.js\";\n\n// ─────────────────────────────────────────────────────────────────────────────\n// ValidationError\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Raised when a single input fails a validation rule — the canonical\n * \"this field is wrong, and here is why\" error.\n *\n * It pins the offending `field` and a machine-readable `validationCode` into\n * the metadata (merged ahead of any caller-supplied metadata) so an API layer\n * can map the failure straight onto a form field without parsing the message.\n * The fixed {@link ErrorCodes.validation} code lets callers catch all\n * validation failures uniformly while still distinguishing the specific rule\n * through `validationCode`.\n */\nclass ValidationError extends AppError {\n /**\n * @param field - The input that failed validation; surfaced as `metadata.field`.\n * @param code - The specific rule that was violated; surfaced as `metadata.validationCode`.\n * @param message - The developer-facing description of the failure.\n * @param options - Optional cause, correlation ids, and extra metadata\n * (merged over the field/code metadata, never overwriting it by accident).\n */\n constructor(\n field: ValidationField,\n code: ValidationCode,\n message: string,\n options?: AppErrorOptions,\n ) {\n const baseMetadata = {\n field: field.value,\n validationCode: code.value,\n };\n const mergedMetadata = options?.metadata\n ? { ...baseMetadata, ...options.metadata }\n : baseMetadata;\n\n super(message, ErrorCodes.validation, {\n ...options,\n metadata: mergedMetadata,\n });\n }\n}\n\nexport { ValidationError };\n"],"mappings":";;AAuCA,IAAM,sBAAN,MAAM,4BAA4B,SAAS;CAGvC,Y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,OAAO;GAAE,SAAS;GAAM,aAAa;EAAM;CAEnD;CAEA,OAAO;EAAE,SAAS;EAAO;CAAY;AACzC;;;;;;;;;;;;;;ACpHA,IAAM,kBAAN,cAA8B,SAAS;;;;;;;;CAQnC,YACI,OACA,MACA,SACA,SACF;EACE,MAAM,eAAe;GACjB,OAAO,MAAM;GACb,gBAAgB,KAAK;EACzB;EACA,MAAM,iBAAiB,SAAS,WAC1B;GAAE,GAAG;GAAc,GAAG,QAAQ;EAAS,IACvC;EAEN,MAAM,SAAS,WAAW,YAAY;GAClC,GAAG;GACH,UAAU;EACd,CAAC;CACL;AACJ"}
|
package/dist/value-object.cjs
CHANGED
|
@@ -1,116 +1,6 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_stable_stringify = require("./stable-stringify.cjs");
|
|
2
2
|
const require_immutable = require("./immutable.cjs");
|
|
3
|
-
const require_temporal_guards = require("./temporal-guards.cjs");
|
|
4
3
|
const require_plugin = require("./plugin.cjs");
|
|
5
|
-
//#region src/core/shared/aggregate-version.ts
|
|
6
|
-
function assertValidAggregateVersion(aggregateVersion) {
|
|
7
|
-
if (!Number.isInteger(aggregateVersion) || aggregateVersion < 0) throw new require_invariant_violation_exception.InvariantViolationException(`aggregateVersion must be a non-negative integer. Received: ${aggregateVersion}`);
|
|
8
|
-
}
|
|
9
|
-
//#endregion
|
|
10
|
-
//#region src/core/domain/entity.ts
|
|
11
|
-
var _Entity;
|
|
12
|
-
/**
|
|
13
|
-
* Base class for domain entities — objects defined by a stable identity rather
|
|
14
|
-
* than by their attributes. Two entities are "the same" when their `id`
|
|
15
|
-
* matches, even if every other field differs; this is the opposite of a
|
|
16
|
-
* {@link ValueObject}, which is defined entirely by its contents.
|
|
17
|
-
*
|
|
18
|
-
* The class owns the bookkeeping common to every aggregate root: a creation
|
|
19
|
-
* timestamp that never changes, a last-modified timestamp, and an
|
|
20
|
-
* `aggregateVersion` counter used for optimistic concurrency control. All three
|
|
21
|
-
* are validated on construction and the dates are defensively cloned, so an
|
|
22
|
-
* entity can never be built into — or leak — an inconsistent temporal state.
|
|
23
|
-
*
|
|
24
|
-
* @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).
|
|
25
|
-
*/
|
|
26
|
-
let Entity = class Entity {
|
|
27
|
-
static {
|
|
28
|
-
_Entity = this;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Reconstitutes an entity from its persisted {@link EntityState}.
|
|
32
|
-
*
|
|
33
|
-
* Validates the temporal invariants up front so an invalid entity is
|
|
34
|
-
* impossible to construct: both dates must be valid, the aggregate version
|
|
35
|
-
* must be a non-negative integer, and `updatedAt` may not predate
|
|
36
|
-
* `createdAt`. Both dates are cloned on the way in so a later mutation of
|
|
37
|
-
* the caller's `Date` objects cannot reach into the entity's internal state.
|
|
38
|
-
*
|
|
39
|
-
* Declared `protected` because entities are reconstituted through a
|
|
40
|
-
* subclass factory, never instantiated directly by application code.
|
|
41
|
-
*
|
|
42
|
-
* @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
|
|
43
|
-
*/
|
|
44
|
-
constructor(state) {
|
|
45
|
-
require_temporal_guards.assertValidDate("createdAt", state.createdAt);
|
|
46
|
-
require_temporal_guards.assertValidDate("updatedAt", state.updatedAt);
|
|
47
|
-
assertValidAggregateVersion(state.aggregateVersion);
|
|
48
|
-
if (state.updatedAt.getTime() < state.createdAt.getTime()) throw new require_invariant_violation_exception.InvariantViolationException("updatedAt cannot be earlier than createdAt");
|
|
49
|
-
this._id = state.id;
|
|
50
|
-
this._createdAt = require_temporal_guards.cloneDate(state.createdAt);
|
|
51
|
-
this._updatedAt = require_temporal_guards.cloneDate(state.updatedAt);
|
|
52
|
-
this._aggregateVersion = state.aggregateVersion;
|
|
53
|
-
}
|
|
54
|
-
/** The entity's stable identity — the basis for equality between entities. */
|
|
55
|
-
get id() {
|
|
56
|
-
return this._id;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* When the entity was first created.
|
|
60
|
-
*
|
|
61
|
-
* Returns a clone so callers cannot mutate the entity's internal `Date`;
|
|
62
|
-
* the value is fixed at construction and never changes thereafter.
|
|
63
|
-
*/
|
|
64
|
-
get createdAt() {
|
|
65
|
-
return require_temporal_guards.cloneDate(this._createdAt);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* When the entity was last modified.
|
|
69
|
-
*
|
|
70
|
-
* Returns a clone for the same encapsulation reason as {@link createdAt};
|
|
71
|
-
* the underlying value advances only through {@link markAsModified}.
|
|
72
|
-
*/
|
|
73
|
-
get updatedAt() {
|
|
74
|
-
return require_temporal_guards.cloneDate(this._updatedAt);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Monotonic counter incremented on every mutation, used for optimistic
|
|
78
|
-
* concurrency control: a writer reads this value, and the persistence layer
|
|
79
|
-
* rejects the write if the stored version has moved on in the meantime.
|
|
80
|
-
*/
|
|
81
|
-
get aggregateVersion() {
|
|
82
|
-
return this._aggregateVersion;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* The schema/contract version stamped on this entity type by the
|
|
86
|
-
* `@version` decorator — used to detect and migrate state persisted under
|
|
87
|
-
* an older shape.
|
|
88
|
-
*/
|
|
89
|
-
get contractVersion() {
|
|
90
|
-
return _Entity.CONTRACT_VERSION;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* The single seam through which an entity records a mutation: it advances
|
|
94
|
-
* `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must
|
|
95
|
-
* call this from every state-changing method so the version counter stays
|
|
96
|
-
* an accurate optimistic-lock token.
|
|
97
|
-
*
|
|
98
|
-
* @param updatedAt - The modification instant; defaults to now. Validated
|
|
99
|
-
* and required not to predate `createdAt`, preserving the same invariant
|
|
100
|
-
* the constructor enforces.
|
|
101
|
-
* @returns The new aggregate version after the increment.
|
|
102
|
-
* @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
|
|
103
|
-
*/
|
|
104
|
-
markAsModified(updatedAt = /* @__PURE__ */ new Date()) {
|
|
105
|
-
require_temporal_guards.assertValidDate("updatedAt", updatedAt);
|
|
106
|
-
if (updatedAt.getTime() < this._createdAt.getTime()) throw new require_invariant_violation_exception.InvariantViolationException("updatedAt cannot be earlier than createdAt");
|
|
107
|
-
this._updatedAt = require_temporal_guards.cloneDate(updatedAt);
|
|
108
|
-
this._aggregateVersion += 1;
|
|
109
|
-
return this._aggregateVersion;
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
Entity = _Entity = require_immutable.__decorate([require_immutable.version("1.0")], Entity);
|
|
113
|
-
//#endregion
|
|
114
4
|
//#region src/core/domain/value-object.ts
|
|
115
5
|
var _ValueObject;
|
|
116
6
|
/**
|
|
@@ -125,6 +15,12 @@ var _ValueObject;
|
|
|
125
15
|
* any risk of a consumer mutating shared state. Subclasses seal the instance
|
|
126
16
|
* itself with {@link finalize} once their own fields are set.
|
|
127
17
|
*
|
|
18
|
+
* One exception: nested `Date` instances are cloned but NOT frozen —
|
|
19
|
+
* `Object.freeze` does not block `setTime`/`setFullYear`, so a consumer of
|
|
20
|
+
* `value` can still mutate an inner `Date` in place. Store instants as ISO
|
|
21
|
+
* strings or epoch timestamps in the wrapped state (converting to `Date` only
|
|
22
|
+
* in an accessor) when that guarantee matters.
|
|
23
|
+
*
|
|
128
24
|
* @typeParam T - The shape of the wrapped data.
|
|
129
25
|
* @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.
|
|
130
26
|
*/
|
|
@@ -148,7 +44,7 @@ let ValueObject = class ValueObject {
|
|
|
148
44
|
* `@version` decorator — used to detect state persisted under an older shape.
|
|
149
45
|
*/
|
|
150
46
|
get contractVersion() {
|
|
151
|
-
return
|
|
47
|
+
return this.constructor.CONTRACT_VERSION;
|
|
152
48
|
}
|
|
153
49
|
/**
|
|
154
50
|
* Freezes the instance shell, blocking reassignment of any own field.
|
|
@@ -177,32 +73,22 @@ let ValueObject = class ValueObject {
|
|
|
177
73
|
* same data are considered equal.
|
|
178
74
|
*
|
|
179
75
|
* Delegates to the registered equality {@link plugins}; with no plugin
|
|
180
|
-
* registered it falls back to comparing the serialized wrapped `value
|
|
76
|
+
* registered it falls back to comparing the serialized wrapped `value`,
|
|
77
|
+
* with object keys sorted so insertion order (e.g. code-built vs
|
|
78
|
+
* JSON-rehydrated) never affects the result.
|
|
181
79
|
* Subclasses may still override for a faster or domain-specific comparison.
|
|
182
80
|
*/
|
|
183
81
|
equals(other) {
|
|
184
|
-
return _ValueObject.plugins.invoke("equals", [this, other], { fallback: (a, b) =>
|
|
82
|
+
return _ValueObject.plugins.invoke("equals", [this, other], { fallback: (a, b) => require_stable_stringify.stableStringify(a.value) === require_stable_stringify.stableStringify(b.value) });
|
|
185
83
|
}
|
|
186
84
|
};
|
|
187
85
|
ValueObject = _ValueObject = require_immutable.__decorate([require_immutable.version("1.0")], ValueObject);
|
|
188
86
|
//#endregion
|
|
189
|
-
Object.defineProperty(exports, "Entity", {
|
|
190
|
-
enumerable: true,
|
|
191
|
-
get: function() {
|
|
192
|
-
return Entity;
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
87
|
Object.defineProperty(exports, "ValueObject", {
|
|
196
88
|
enumerable: true,
|
|
197
89
|
get: function() {
|
|
198
90
|
return ValueObject;
|
|
199
91
|
}
|
|
200
92
|
});
|
|
201
|
-
Object.defineProperty(exports, "assertValidAggregateVersion", {
|
|
202
|
-
enumerable: true,
|
|
203
|
-
get: function() {
|
|
204
|
-
return assertValidAggregateVersion;
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
93
|
|
|
208
94
|
//# sourceMappingURL=value-object.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"value-object.cjs","names":["InvariantViolationException","InvariantViolationException","cloneDate","version","PluginManager","makeImmutable","version"],"sources":["../src/core/shared/aggregate-version.ts","../src/core/domain/entity.ts","../src/core/domain/value-object.ts"],"sourcesContent":["import { InvariantViolationException } from \"../exceptions/invariant-violation-exception.js\";\n\nfunction assertValidAggregateVersion(aggregateVersion: number): void {\n if (!Number.isInteger(aggregateVersion) || aggregateVersion < 0) {\n throw new InvariantViolationException(\n `aggregateVersion must be a non-negative integer. Received: ${aggregateVersion}`,\n );\n }\n}\n\nexport { assertValidAggregateVersion };\n","import { InvariantViolationException } from \"../exceptions/invariant-violation-exception.js\";\nimport { assertValidAggregateVersion } from \"../shared/aggregate-version.js\";\nimport { assertValidDate, cloneDate } from \"../shared/temporal-guards.js\";\nimport { type ContractVersion, version } from \"../versioning/version.js\";\n\n/**\n * The minimal persisted shape needed to reconstitute an {@link Entity}.\n *\n * This is the contract between storage and the domain: a repository maps a row\n * (or document) onto these four fields and hands them to a subclass constructor.\n * `aggregateVersion` travels with the state so optimistic-concurrency checks\n * survive a round-trip through the database.\n */\ninterface EntityState<TIdentifier> {\n readonly id: TIdentifier;\n readonly createdAt: Date;\n readonly updatedAt: Date;\n readonly aggregateVersion: number;\n}\n\n/**\n * Base class for domain entities — objects defined by a stable identity rather\n * than by their attributes. Two entities are \"the same\" when their `id`\n * matches, even if every other field differs; this is the opposite of a\n * {@link ValueObject}, which is defined entirely by its contents.\n *\n * The class owns the bookkeeping common to every aggregate root: a creation\n * timestamp that never changes, a last-modified timestamp, and an\n * `aggregateVersion` counter used for optimistic concurrency control. All three\n * are validated on construction and the dates are defensively cloned, so an\n * entity can never be built into — or leak — an inconsistent temporal state.\n *\n * @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).\n */\n@version(\"1.0\")\nabstract class Entity<TIdentifier> {\n declare public static readonly CONTRACT_VERSION: ContractVersion;\n\n private readonly _id: TIdentifier;\n private readonly _createdAt: Date;\n private _updatedAt: Date;\n private _aggregateVersion: number;\n\n /**\n * Reconstitutes an entity from its persisted {@link EntityState}.\n *\n * Validates the temporal invariants up front so an invalid entity is\n * impossible to construct: both dates must be valid, the aggregate version\n * must be a non-negative integer, and `updatedAt` may not predate\n * `createdAt`. Both dates are cloned on the way in so a later mutation of\n * the caller's `Date` objects cannot reach into the entity's internal state.\n *\n * Declared `protected` because entities are reconstituted through a\n * subclass factory, never instantiated directly by application code.\n *\n * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.\n */\n protected constructor(state: EntityState<TIdentifier>) {\n assertValidDate(\"createdAt\", state.createdAt);\n assertValidDate(\"updatedAt\", state.updatedAt);\n assertValidAggregateVersion(state.aggregateVersion);\n\n if (state.updatedAt.getTime() < state.createdAt.getTime()) {\n throw new InvariantViolationException(\n \"updatedAt cannot be earlier than createdAt\",\n );\n }\n\n this._id = state.id;\n this._createdAt = cloneDate(state.createdAt);\n this._updatedAt = cloneDate(state.updatedAt);\n this._aggregateVersion = state.aggregateVersion;\n }\n\n /** The entity's stable identity — the basis for equality between entities. */\n public get id(): TIdentifier {\n return this._id;\n }\n\n /**\n * When the entity was first created.\n *\n * Returns a clone so callers cannot mutate the entity's internal `Date`;\n * the value is fixed at construction and never changes thereafter.\n */\n public get createdAt(): Date {\n return cloneDate(this._createdAt);\n }\n\n /**\n * When the entity was last modified.\n *\n * Returns a clone for the same encapsulation reason as {@link createdAt};\n * the underlying value advances only through {@link markAsModified}.\n */\n public get updatedAt(): Date {\n return cloneDate(this._updatedAt);\n }\n\n /**\n * Monotonic counter incremented on every mutation, used for optimistic\n * concurrency control: a writer reads this value, and the persistence layer\n * rejects the write if the stored version has moved on in the meantime.\n */\n public get aggregateVersion(): number {\n return this._aggregateVersion;\n }\n\n /**\n * The schema/contract version stamped on this entity type by the\n * `@version` decorator — used to detect and migrate state persisted under\n * an older shape.\n */\n public get contractVersion(): ContractVersion {\n return Entity.CONTRACT_VERSION;\n }\n\n /**\n * The single seam through which an entity records a mutation: it advances\n * `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must\n * call this from every state-changing method so the version counter stays\n * an accurate optimistic-lock token.\n *\n * @param updatedAt - The modification instant; defaults to now. Validated\n * and required not to predate `createdAt`, preserving the same invariant\n * the constructor enforces.\n * @returns The new aggregate version after the increment.\n * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.\n */\n protected markAsModified(updatedAt: Date = new Date()): number {\n assertValidDate(\"updatedAt\", updatedAt);\n\n if (updatedAt.getTime() < this._createdAt.getTime()) {\n throw new InvariantViolationException(\n \"updatedAt cannot be earlier than createdAt\",\n );\n }\n\n this._updatedAt = cloneDate(updatedAt);\n this._aggregateVersion += 1;\n\n return this._aggregateVersion;\n }\n}\n\nexport { Entity, type EntityState };\n","import { PluginManager } from \"../plugins/index.js\";\nimport { type DeepReadonly, makeImmutable } from \"../shared/immutable.js\";\nimport { type ContractVersion, version } from \"../versioning/version.js\";\n\n/**\n * Extension point for value-object equality. A plugin implementing this\n * contract overrides how any two value objects are compared, letting the host\n * application swap in a structural comparator (e.g. `lodash.isEqual` over the\n * wrapped `value`) without every value object having to implement `equals` by\n * hand.\n */\ntype ValueObjectPluginContract = {\n equals: (\n a: ValueObject<unknown, unknown>,\n b: ValueObject<unknown, unknown>,\n ) => boolean;\n};\n\n/**\n * Base class for value objects — domain concepts defined entirely by their\n * contents, with no identity of their own. Two value objects are\n * interchangeable when they hold equal data, which is the opposite of an\n * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one\n * instance with an equal one changes nothing about the model.\n *\n * Immutability is the defining guarantee here. The wrapped `value` is\n * deep-frozen on construction, so a value object can be shared freely without\n * any risk of a consumer mutating shared state. Subclasses seal the instance\n * itself with {@link finalize} once their own fields are set.\n *\n * @typeParam T - The shape of the wrapped data.\n * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.\n */\n@version(\"1.0\")\nabstract class ValueObject<T, P> {\n declare public static readonly CONTRACT_VERSION: ContractVersion;\n\n /**\n * Registry of equality plugins shared by every value object. Empty by\n * default — when nothing is registered, {@link equals} falls back to a\n * structural comparison of the wrapped `value`. Hosts register a plugin\n * (e.g. `lodash.isEqual`) once at startup to customise equality globally.\n */\n public static readonly plugins =\n new PluginManager<ValueObjectPluginContract>();\n\n /** The wrapped data, deep-frozen so it can never be mutated after construction. */\n public readonly value: DeepReadonly<T>;\n\n /**\n * Wraps `value`, deep-freezing it so the value object is immutable from the\n * moment it exists. Declared `protected` because value objects are built\n * through a validating subclass factory, never instantiated directly.\n */\n protected constructor(value: T) {\n this.value = makeImmutable(value);\n }\n\n /**\n * The schema/contract version stamped on this value-object type by the\n * `@version` decorator — used to detect state persisted under an older shape.\n */\n public get contractVersion(): ContractVersion {\n return ValueObject.CONTRACT_VERSION;\n }\n\n /**\n * Freezes the instance shell, blocking reassignment of any own field.\n *\n * `value` is already deep-frozen by the constructor, so the wrapped data is\n * immutable regardless. The instance itself is NOT frozen automatically\n * because a subclass may still need to assign its own fields after\n * `super(value)` runs. Call `finalize()` at the very end of the subclass\n * constructor (after all fields are set) to make the whole value object\n * immutable.\n */\n protected finalize(): void {\n Object.freeze(this);\n }\n\n /**\n * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value\n * object serializes to its primitive form rather than exposing the internal\n * `value` wrapper.\n */\n public toJSON(): P {\n return this.toPrimitive();\n }\n\n /**\n * Compares this value object with another by content. Because value objects\n * carry no identity, two independently constructed instances holding the\n * same data are considered equal.\n *\n * Delegates to the registered equality {@link plugins}; with no plugin\n * registered it falls back to comparing the serialized wrapped `value`.\n * Subclasses may still override for a faster or domain-specific comparison.\n */\n public equals(other: this): boolean {\n return ValueObject.plugins.invoke(\"equals\", [this, other], {\n fallback: (a, b) =>\n JSON.stringify(a.value) === JSON.stringify(b.value),\n });\n }\n\n /**\n * Projects the value object down to a plain, serializable primitive form —\n * the representation suitable for persistence, transport, or comparison.\n */\n public abstract toPrimitive(): P;\n}\n\nexport { type DeepReadonly, ValueObject, type ValueObjectPluginContract };\n"],"mappings":";;;;;AAEA,SAAS,4BAA4B,kBAAgC;CACjE,IAAI,CAAC,OAAO,UAAU,gBAAgB,KAAK,mBAAmB,GAC1D,MAAM,IAAIA,sCAAAA,4BACN,8DAA8D,kBAClE;AAER;;;;;;;;;;;;;;;;;;AC0BA,IAAA,SAAA,MAC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B;CACvB;;;;;;;;;;;CAYA,WAA2B;EACvB,OAAO,OAAO,IAAI;CACtB;;;;;;CAOA,SAAmB;EACf,OAAO,KAAK,YAAY;CAC5B;;;;;;;;;;CAWA,OAAc,OAAsB;EAChC,OAAA,aAAmB,QAAQ,OAAO,UAAU,CAAC,MAAM,KAAK,GAAG,EACvD,WAAW,GAAG,MACV,KAAK,UAAU,EAAE,KAAK,MAAM,KAAK,UAAU,EAAE,KAAK,EAC1D,CAAC;CACL;AAOJ;2DA7ECC,kBAAAA,QAAQ,KAAK,CAAA,GAAA,WAAA"}
|
|
1
|
+
{"version":3,"file":"value-object.cjs","names":["PluginManager","makeImmutable","stableStringify","version"],"sources":["../src/core/domain/value-object.ts"],"sourcesContent":["import { PluginManager } from \"../plugins/index.js\";\nimport { type DeepReadonly, makeImmutable } from \"../shared/immutable.js\";\nimport { stableStringify } from \"../shared/stable-stringify.js\";\nimport { type ContractVersion, version } from \"../versioning/version.js\";\n\n/**\n * Extension point for value-object equality. A plugin implementing this\n * contract overrides how any two value objects are compared, letting the host\n * application swap in a structural comparator (e.g. `lodash.isEqual` over the\n * wrapped `value`) without every value object having to implement `equals` by\n * hand.\n */\ntype ValueObjectPluginContract = {\n equals: (\n a: ValueObject<unknown, unknown>,\n b: ValueObject<unknown, unknown>,\n ) => boolean;\n};\n\n/**\n * Base class for value objects — domain concepts defined entirely by their\n * contents, with no identity of their own. Two value objects are\n * interchangeable when they hold equal data, which is the opposite of an\n * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one\n * instance with an equal one changes nothing about the model.\n *\n * Immutability is the defining guarantee here. The wrapped `value` is\n * deep-frozen on construction, so a value object can be shared freely without\n * any risk of a consumer mutating shared state. Subclasses seal the instance\n * itself with {@link finalize} once their own fields are set.\n *\n * One exception: nested `Date` instances are cloned but NOT frozen —\n * `Object.freeze` does not block `setTime`/`setFullYear`, so a consumer of\n * `value` can still mutate an inner `Date` in place. Store instants as ISO\n * strings or epoch timestamps in the wrapped state (converting to `Date` only\n * in an accessor) when that guarantee matters.\n *\n * @typeParam T - The shape of the wrapped data.\n * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.\n */\n@version(\"1.0\")\nabstract class ValueObject<T, P> {\n declare public static readonly CONTRACT_VERSION: ContractVersion;\n\n /**\n * Registry of equality plugins shared by every value object. Empty by\n * default — when nothing is registered, {@link equals} falls back to a\n * structural comparison of the wrapped `value`. Hosts register a plugin\n * (e.g. `lodash.isEqual`) once at startup to customise equality globally.\n */\n public static readonly plugins =\n new PluginManager<ValueObjectPluginContract>();\n\n /** The wrapped data, deep-frozen so it can never be mutated after construction. */\n public readonly value: DeepReadonly<T>;\n\n /**\n * Wraps `value`, deep-freezing it so the value object is immutable from the\n * moment it exists. Declared `protected` because value objects are built\n * through a validating subclass factory, never instantiated directly.\n */\n protected constructor(value: T) {\n this.value = makeImmutable(value);\n }\n\n /**\n * The schema/contract version stamped on this value-object type by the\n * `@version` decorator — used to detect state persisted under an older shape.\n */\n public get contractVersion(): ContractVersion {\n return (this.constructor as typeof ValueObject).CONTRACT_VERSION;\n }\n\n /**\n * Freezes the instance shell, blocking reassignment of any own field.\n *\n * `value` is already deep-frozen by the constructor, so the wrapped data is\n * immutable regardless. The instance itself is NOT frozen automatically\n * because a subclass may still need to assign its own fields after\n * `super(value)` runs. Call `finalize()` at the very end of the subclass\n * constructor (after all fields are set) to make the whole value object\n * immutable.\n */\n protected finalize(): void {\n Object.freeze(this);\n }\n\n /**\n * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value\n * object serializes to its primitive form rather than exposing the internal\n * `value` wrapper.\n */\n public toJSON(): P {\n return this.toPrimitive();\n }\n\n /**\n * Compares this value object with another by content. Because value objects\n * carry no identity, two independently constructed instances holding the\n * same data are considered equal.\n *\n * Delegates to the registered equality {@link plugins}; with no plugin\n * registered it falls back to comparing the serialized wrapped `value`,\n * with object keys sorted so insertion order (e.g. code-built vs\n * JSON-rehydrated) never affects the result.\n * Subclasses may still override for a faster or domain-specific comparison.\n */\n public equals(other: this): boolean {\n return ValueObject.plugins.invoke(\"equals\", [this, other], {\n fallback: (a, b) =>\n stableStringify(a.value) === stableStringify(b.value),\n });\n }\n\n /**\n * Projects the value object down to a plain, serializable primitive form —\n * the representation suitable for persistence, transport, or comparison.\n */\n public abstract toPrimitive(): P;\n}\n\nexport { type DeepReadonly, ValueObject, type ValueObjectPluginContract };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,IAAA,cAAA,MACe,YAAkB;;;;;iBAUzB,IAAIA,eAAAA,cAAyC;;;;;;;CAUjD,YAAsB,OAAU;EAC5B,KAAK,QAAQC,kBAAAA,cAAc,KAAK;CACpC;;;;;CAMA,IAAW,kBAAmC;EAC1C,OAAQ,KAAK,YAAmC;CACpD;;;;;;;;;;;CAYA,WAA2B;EACvB,OAAO,OAAO,IAAI;CACtB;;;;;;CAOA,SAAmB;EACf,OAAO,KAAK,YAAY;CAC5B;;;;;;;;;;;;CAaA,OAAc,OAAsB;EAChC,OAAA,aAAmB,QAAQ,OAAO,UAAU,CAAC,MAAM,KAAK,GAAG,EACvD,WAAW,GAAG,MACVC,yBAAAA,gBAAgB,EAAE,KAAK,MAAMA,yBAAAA,gBAAgB,EAAE,KAAK,EAC5D,CAAC;CACL;AAOJ;2DA/ECC,kBAAAA,QAAQ,KAAK,CAAA,GAAA,WAAA"}
|