UNPKG

arvo-core

Version:

The core Arvo package which provides application tier core primitives and contract system for building production-grade event-driven application. Provides ArvoEvent (CloudEvents-compliant), ArvoContract for type-safe service interfaces, event factories, O

103 lines (102 loc) 4.15 kB
import type { z } from 'zod'; import type { VersionedArvoContract } from '../ArvoContract/VersionedArvoContract'; import type { CreateArvoEvent } from '../ArvoEvent/types'; /** * Factory class for creating and validating events based on a versioned Arvo contract. * Handles event creation, validation, and OpenTelemetry integration for a specific * contract version. * * @example * ```typescript * const contract = createArvoContract({ * uri: 'example/api', * type: 'user.create', * versions: { '1.0.0': { ... } } * }); * * const factory = createArvoEventFactory(contract.version('1.0.0')); * ``` */ export default class ArvoEventFactory<TContract extends VersionedArvoContract<any, any>> { protected readonly _name: string; protected readonly contract: TContract; /** * Creates an ArvoEventFactory instance for a specific version of a contract. * * @param contract - The versioned contract to use for event creation and validation */ constructor(contract: TContract); /** * Creates and validates an event matching the contract's accept specification. * * @param event - The event configuration object * @param [extensions] - Optional additional properties for the event * * @returns A validated ArvoEvent matching the contract's accept specification * * @throws {Error} If validation fails or OpenTelemetry operations fail * * @example * ```typescript * const event = factory.accepts({ * source: 'api/users', * data: { name: 'John', email: 'john@example.com' } * }); * ``` */ accepts<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.input<TContract['accepts']['schema']>, TContract['accepts']['type']>, 'type' | 'datacontenttype' | 'dataschema' | 'subject' | 'domain'> & { subject?: string; domain?: string | null; }, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TContract["accepts"]["schema"]>, TExtension, TContract["accepts"]["type"]>; /** * Creates and validates an event matching one of the contract's emit specifications. * * @param event - The event configuration object * @param [extensions] - Optional additional properties for the event * * @returns A validated ArvoEvent matching the specified emit type * * @throws {Error} If validation fails, emit type doesn't exist, or OpenTelemetry operations fail * * @example * ```typescript * const event = factory.emits({ * type: 'user.created', * source: 'api/users', * data: { id: '123', timestamp: new Date() } * }); * ``` */ emits<U extends string & keyof TContract['emits'], TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.input<TContract['emits'][U]>, U>, 'datacontenttype' | 'dataschema' | 'subject' | 'domain'> & { subject?: string; domain?: string | null; }, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TContract["emits"][U]>, TExtension, U>; /** * Creates a system error event for error reporting and handling. * * @param event - The error event configuration * @param event.error - The Error instance to convert to an event * @param [extensions] - Optional additional properties for the event * * @returns A system error ArvoEvent * * @throws {Error} If event creation or OpenTelemetry operations fail * * @example * ```typescript * const errorEvent = factory.systemError({ * error: new Error('Validation failed'), * source: 'api/validation' * }); * ``` */ systemError<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<any, any>, 'data' | 'type' | 'datacontenttype' | 'dataschema' | 'subject' | 'domain'> & { error: Error; subject?: string; domain?: string | null; }, extensions?: TExtension): import("..").ArvoEvent<{ errorName: string; errorMessage: string; errorStack: string | null; }, TExtension, TContract["systemError"]["type"]>; }