arvo-core 2.0.3 → 2.0.5

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.
@@ -0,0 +1,39 @@
1
+ import ArvoContract from '.';
2
+ import { ArvoErrorSchema } from '../schema';
3
+ import { ArvoSemanticVersion } from '../types';
4
+ /**
5
+ * Represents a version-specific view of an ArvoContract, providing a structured way to access
6
+ * all contract specifications for a particular semantic version. This type ensures type-safe
7
+ * access to version-specific schemas and event types.
8
+ *
9
+ * @template TContract - The base ArvoContract type to extract version-specific information from
10
+ * @template TVersion - The specific semantic version of the contract to extract
11
+ *
12
+ * @property uri - The URI identifying the contract
13
+ * @property version - The specific semantic version being represented
14
+ * @property description - Optional description of the contract version
15
+ * @property accepts - Specification for accepted events in this version
16
+ * @property accepts.type - The event type this version accepts
17
+ * @property accepts.schema - Zod schema for validating accepted events
18
+ * @property systemError - System error event specification
19
+ * @property systemError.type - System error event type (sys.[type].error)
20
+ * @property systemError.schema - Zod schema for system error events
21
+ * @property emits - Record of emittable event types and their Zod schemas
22
+ *
23
+ * @see {@link ArvoContract} for the base contract class
24
+ * @see {@link ArvoContract.version} for the method to create a versioned view
25
+ */
26
+ export type VersionedArvoContract<TContract extends ArvoContract, TVersion extends ArvoSemanticVersion & keyof TContract['versions']> = {
27
+ uri: TContract['uri'];
28
+ version: TVersion;
29
+ description: string | null;
30
+ accepts: {
31
+ type: TContract['type'];
32
+ schema: TContract['versions'][TVersion]['accepts'];
33
+ };
34
+ systemError: {
35
+ type: `sys.${TContract['type']}.error`;
36
+ schema: typeof ArvoErrorSchema;
37
+ };
38
+ emits: TContract['versions'][TVersion]['emits'];
39
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -2,6 +2,7 @@ import { z } from 'zod';
2
2
  import { ArvoContractJSONSchema, ArvoContractRecord, IArvoContract } from './types';
3
3
  import { ArvoErrorSchema } from '../schema';
4
4
  import { ArvoSemanticVersion } from '../types';
5
+ import { VersionedArvoContract } from './VersionedArvoContract';
5
6
  /**
6
7
  * ArvoContract class represents a contract with defined input and output schemas.
7
8
  * It provides methods for validating inputs and outputs based on the contract's specifications.
@@ -32,6 +33,21 @@ export default class ArvoContract<TUri extends string = string, TType extends st
32
33
  * Gets the URI of the contract.
33
34
  */
34
35
  get uri(): TUri;
36
+ /**
37
+ * Creates a version-specific view of the contract, providing easy access to all
38
+ * schemas and specifications for a particular semantic version.
39
+ *
40
+ * @template V - The semantic version to create a view for, must be a valid version in the contract
41
+ *
42
+ * @param ver - The semantic version string (e.g., "1.0.0")
43
+ * @returns A VersionedArvoContract instance containing all specifications for the requested version
44
+ *
45
+ * The returned object provides a simpler, flatter structure compared to
46
+ * accessing version-specific information through the main contract methods.
47
+ *
48
+ * @see {@link VersionedArvoContract} for the structure of the returned object
49
+ */
50
+ version<V extends ArvoSemanticVersion & keyof TVersions>(ver: V): VersionedArvoContract<typeof this, V>;
35
51
  /**
36
52
  * Get the type of the event the handler
37
53
  * bound to the contract accepts
@@ -55,6 +71,25 @@ export default class ArvoContract<TUri extends string = string, TType extends st
55
71
  * contract bound handler.
56
72
  */
57
73
  emits<V extends ArvoSemanticVersion & keyof TVersions>(version: V): TVersions[V]['emits'];
74
+ /**
75
+ * Gets the system error event specification for this contract.
76
+ * System errors follow a standardized format to handle exceptional conditions
77
+ * and failures in a consistent way across all contracts.
78
+ *
79
+ * The error schema includes:
80
+ * - errorName: The name/type of the error
81
+ * - errorMessage: A descriptive message about what went wrong
82
+ * - errorStack: Optional stack trace information (null if not available)
83
+ *
84
+ * System errors are special events that:
85
+ * - Are automatically prefixed with 'sys.' and suffixed with '.error'
86
+ * - Use a standardized schema across all contracts
87
+ * - Can capture error details, messages, and stack traces
88
+ * - Are version-independent (work the same across all contract versions)
89
+ *
90
+ * @see {@link ArvoErrorSchema} for the detailed error schema definition
91
+ * @see {@link ArvoContractRecord} for the structure of the returned record
92
+ */
58
93
  get systemError(): ArvoContractRecord<`sys.${TType}.error`, typeof ArvoErrorSchema>;
59
94
  /**
60
95
  * Validates the contract bound handler's input/ accept event against the
@@ -90,11 +125,7 @@ export default class ArvoContract<TUri extends string = string, TType extends st
90
125
  * This method provides a way to represent the contract's structure and validation rules
91
126
  * in a format that conforms to the JSON Schema specification.
92
127
  *
93
- * @returns An object representing the contract in JSON Schema format, including:
94
- * - uri: The contract's URI
95
- * - description: The contract's description (if available)
96
- * - accepts: An object containing the accepted input type and its JSON Schema representation
97
- * - emits: An array of objects, each containing an emitted event type and its JSON Schema representation
128
+ * @returns An object representing the contract in JSON Schema format:
98
129
  */
99
130
  toJsonSchema(): ArvoContractJSONSchema;
100
131
  }
@@ -46,6 +46,33 @@ var ArvoContract = /** @class */ (function () {
46
46
  enumerable: false,
47
47
  configurable: true
48
48
  });
49
+ /**
50
+ * Creates a version-specific view of the contract, providing easy access to all
51
+ * schemas and specifications for a particular semantic version.
52
+ *
53
+ * @template V - The semantic version to create a view for, must be a valid version in the contract
54
+ *
55
+ * @param ver - The semantic version string (e.g., "1.0.0")
56
+ * @returns A VersionedArvoContract instance containing all specifications for the requested version
57
+ *
58
+ * The returned object provides a simpler, flatter structure compared to
59
+ * accessing version-specific information through the main contract methods.
60
+ *
61
+ * @see {@link VersionedArvoContract} for the structure of the returned object
62
+ */
63
+ ArvoContract.prototype.version = function (ver) {
64
+ return {
65
+ uri: this._uri,
66
+ description: this.description,
67
+ version: ver,
68
+ accepts: {
69
+ type: this._type,
70
+ schema: this._versions[ver].accepts,
71
+ },
72
+ systemError: this.systemError,
73
+ emits: this._versions[ver].emits,
74
+ };
75
+ };
49
76
  Object.defineProperty(ArvoContract.prototype, "type", {
50
77
  /**
51
78
  * Get the type of the event the handler
@@ -97,6 +124,25 @@ var ArvoContract = /** @class */ (function () {
97
124
  return __assign({}, this._versions[version].emits);
98
125
  };
99
126
  Object.defineProperty(ArvoContract.prototype, "systemError", {
127
+ /**
128
+ * Gets the system error event specification for this contract.
129
+ * System errors follow a standardized format to handle exceptional conditions
130
+ * and failures in a consistent way across all contracts.
131
+ *
132
+ * The error schema includes:
133
+ * - errorName: The name/type of the error
134
+ * - errorMessage: A descriptive message about what went wrong
135
+ * - errorStack: Optional stack trace information (null if not available)
136
+ *
137
+ * System errors are special events that:
138
+ * - Are automatically prefixed with 'sys.' and suffixed with '.error'
139
+ * - Use a standardized schema across all contracts
140
+ * - Can capture error details, messages, and stack traces
141
+ * - Are version-independent (work the same across all contract versions)
142
+ *
143
+ * @see {@link ArvoErrorSchema} for the detailed error schema definition
144
+ * @see {@link ArvoContractRecord} for the structure of the returned record
145
+ */
100
146
  get: function () {
101
147
  return {
102
148
  type: "sys.".concat(this._type, ".error"),
@@ -159,11 +205,7 @@ var ArvoContract = /** @class */ (function () {
159
205
  * This method provides a way to represent the contract's structure and validation rules
160
206
  * in a format that conforms to the JSON Schema specification.
161
207
  *
162
- * @returns An object representing the contract in JSON Schema format, including:
163
- * - uri: The contract's URI
164
- * - description: The contract's description (if available)
165
- * - accepts: An object containing the accepted input type and its JSON Schema representation
166
- * - emits: An array of objects, each containing an emitted event type and its JSON Schema representation
208
+ * @returns An object representing the contract in JSON Schema format:
167
209
  */
168
210
  ArvoContract.prototype.toJsonSchema = function () {
169
211
  var _this = this;
@@ -178,6 +220,10 @@ var ArvoContract = /** @class */ (function () {
178
220
  type: _this._type,
179
221
  schema: (0, zod_to_json_schema_1.zodToJsonSchema)(contract.accepts),
180
222
  },
223
+ systemError: {
224
+ type: _this.systemError.type,
225
+ schema: (0, zod_to_json_schema_1.zodToJsonSchema)(_this.systemError.schema),
226
+ },
181
227
  emits: Object.entries(contract.emits).map(function (_a) {
182
228
  var key = _a[0], value = _a[1];
183
229
  return ({
@@ -62,6 +62,13 @@ export type ArvoContractJSONSchema = {
62
62
  /** JSON Schema representation of the input validation schema */
63
63
  schema: ReturnType<typeof zodToJsonSchema>;
64
64
  };
65
+ /** The system error event */
66
+ systemError: {
67
+ /** The type of the event */
68
+ type: string;
69
+ /** The schema of the error */
70
+ schema: ReturnType<typeof zodToJsonSchema>;
71
+ };
65
72
  /** Array of schemas for events that can be emitted in this version */
66
73
  emits: {
67
74
  /** The type identifier for the emitted event */
@@ -2,22 +2,23 @@ import ArvoEventFactory from '.';
2
2
  import ArvoContract from '../ArvoContract';
3
3
  import { ArvoSemanticVersion } from '../types';
4
4
  import ArvoEvent from '../ArvoEvent';
5
+ import { VersionedArvoContract } from '../ArvoContract/VersionedArvoContract';
5
6
  /**
6
- * Creates an ArvoEventFactory instance for a given contract.
7
- * This is the recommended way to instantiate an ArvoEventFactory.
7
+ * Creates an ArvoEventFactory for a specific version of a contract.
8
8
  *
9
- * @template TContract - The type of ArvoContract to create a factory for
9
+ * @template TContract - The versioned contract type
10
10
  *
11
- * @param contract - The ArvoContract instance to base the factory on
12
- * @returns A new ArvoEventFactory instance bound to the provided contract
11
+ * @param contract - The versioned contract to create a factory for
12
+ * @returns An ArvoEventFactory instance for the specified contract version
13
13
  *
14
14
  * @example
15
15
  * ```typescript
16
16
  * const contract = createArvoContract({...});
17
- * const factory = createArvoEventFactory(contract);
17
+ * const v1Contract = contract.version('1.0.0');
18
+ * const factory = createArvoEventFactory(v1Contract);
18
19
  * ```
19
20
  */
20
- export declare const createArvoEventFactory: <TContract extends ArvoContract>(contract: TContract) => ArvoEventFactory<TContract>;
21
+ export declare const createArvoEventFactory: <TContract extends VersionedArvoContract<ArvoContract, ArvoSemanticVersion>>(contract: TContract) => ArvoEventFactory<TContract>;
21
22
  /**
22
23
  * Parses an event data schema string or ArvoEvent object to extract the URI and version.
23
24
  * The schema is expected to be in the format "uri/version" where version follows semantic versioning.
@@ -8,18 +8,18 @@ var _1 = __importDefault(require("."));
8
8
  var OpenTelemetry_1 = require("../OpenTelemetry");
9
9
  var schema_1 = require("../schema");
10
10
  /**
11
- * Creates an ArvoEventFactory instance for a given contract.
12
- * This is the recommended way to instantiate an ArvoEventFactory.
11
+ * Creates an ArvoEventFactory for a specific version of a contract.
13
12
  *
14
- * @template TContract - The type of ArvoContract to create a factory for
13
+ * @template TContract - The versioned contract type
15
14
  *
16
- * @param contract - The ArvoContract instance to base the factory on
17
- * @returns A new ArvoEventFactory instance bound to the provided contract
15
+ * @param contract - The versioned contract to create a factory for
16
+ * @returns An ArvoEventFactory instance for the specified contract version
18
17
  *
19
18
  * @example
20
19
  * ```typescript
21
20
  * const contract = createArvoContract({...});
22
- * const factory = createArvoEventFactory(contract);
21
+ * const v1Contract = contract.version('1.0.0');
22
+ * const factory = createArvoEventFactory(v1Contract);
23
23
  * ```
24
24
  */
25
25
  var createArvoEventFactory = function (contract) { return new _1.default(contract); };
@@ -3,79 +3,100 @@ import { z } from 'zod';
3
3
  import { CreateArvoEvent } from '../ArvoEvent/types';
4
4
  import { ExecutionOpenTelemetryConfiguration } from '../OpenTelemetry/types';
5
5
  import { ArvoSemanticVersion } from '../types';
6
+ import { VersionedArvoContract } from '../ArvoContract/VersionedArvoContract';
6
7
  /**
7
- * Factory class for creating contractual ArvoEvents based on a given ArvoContract.
8
- * This class handles the creation and validation of events according to contract specifications.
8
+ * Factory class for creating and validating events based on a versioned Arvo contract.
9
+ * Handles event creation, validation, and OpenTelemetry integration for a specific
10
+ * contract version.
9
11
  *
10
- * @template TContract - The type of ArvoContract this factory is bound to
12
+ * @template TContract - The versioned contract type this factory is bound to
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const contract = createArvoContract({
17
+ * uri: 'example/api',
18
+ * type: 'user.create',
19
+ * versions: { '1.0.0': { ... } }
20
+ * });
21
+ *
22
+ * const factory = createArvoEventFactory(contract.version('1.0.0'));
23
+ * ```
11
24
  */
12
- export default class ArvoEventFactory<TContract extends ArvoContract> {
25
+ export default class ArvoEventFactory<TContract extends VersionedArvoContract<ArvoContract, ArvoSemanticVersion>> {
13
26
  private readonly contract;
14
27
  /**
15
- * Creates an instance of ArvoEventFactory.
28
+ * Creates an ArvoEventFactory instance for a specific version of a contract.
16
29
  *
17
- * @param contract - The ArvoContract to base the events on.
30
+ * @param contract - The versioned contract to use for event creation and validation
18
31
  */
19
32
  constructor(contract: TContract);
20
33
  /**
21
- * Creates a validated ArvoEvent that matches the contract's accept specifications.
22
- * This method ensures the created event conforms to the contract's input schema.
34
+ * Creates and validates an event matching the contract's accept specification.
23
35
  *
24
- * @template V - The semantic version of the contract to use
25
- * @template TExtension - Additional custom properties to include in the event
36
+ * @template TExtension - Additional properties to include in the event
26
37
  *
27
38
  * @param event - The event configuration object
28
- * @param event.version - The semantic version of the contract to use
29
- * @param event.data - The event payload that must conform to the contract's accept schema
30
- * @param [extensions] - Optional additional properties to include in the event
31
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
39
+ * @param [extensions] - Optional additional properties for the event
40
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
41
+ *
42
+ * @returns A validated ArvoEvent matching the contract's accept specification
32
43
  *
33
- * @returns A validated ArvoEvent instance conforming to the contract's accept specifications
44
+ * @throws {Error} If validation fails or OpenTelemetry operations fail
34
45
  *
35
- * @throws {Error} If event data validation fails against the contract schema
36
- * @throws {Error} If OpenTelemetry operations fail
46
+ * @example
47
+ * ```typescript
48
+ * const event = factory.accepts({
49
+ * source: 'api/users',
50
+ * data: { name: 'John', email: 'john@example.com' }
51
+ * });
52
+ * ```
37
53
  */
38
- accepts<V extends ArvoSemanticVersion & keyof TContract['versions'], TExtension extends Record<string, any>>(event: {
39
- version: V;
40
- } & Omit<CreateArvoEvent<z.input<TContract['versions'][V]['accepts']>, TContract['type']>, 'type' | 'datacontenttype' | 'dataschema'>, extensions?: TExtension, opentelemetry?: ExecutionOpenTelemetryConfiguration): import("..").ArvoEvent<z.TypeOf<TContract["versions"][V]["accepts"]>, TExtension, TContract["type"]>;
54
+ accepts<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.input<TContract['accepts']['schema']>, TContract['accepts']['type']>, 'type' | 'datacontenttype' | 'dataschema'>, extensions?: TExtension, opentelemetry?: ExecutionOpenTelemetryConfiguration): import("..").ArvoEvent<z.TypeOf<TContract["accepts"]["schema"]>, TExtension, TContract["accepts"]["type"]>;
41
55
  /**
42
- * Creates a validated ArvoEvent that matches one of the contract's emit specifications.
43
- * This method ensures the created event conforms to the contract's output schema.
56
+ * Creates and validates an event matching one of the contract's emit specifications.
44
57
  *
45
- * @template V - The semantic version of the contract to use
46
58
  * @template U - The specific emit event type from the contract
47
- * @template TExtension - Additional custom properties to include in the event
59
+ * @template TExtension - Additional properties to include in the event
48
60
  *
49
61
  * @param event - The event configuration object
50
- * @param event.version - The semantic version of the contract to use
51
- * @param event.type - The type of emit event to create
52
- * @param event.data - The event payload that must conform to the contract's emit schema
53
- * @param [extensions] - Optional additional properties to include in the event
54
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
62
+ * @param [extensions] - Optional additional properties for the event
63
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
55
64
  *
56
- * @returns A validated ArvoEvent instance conforming to the contract's emit specifications
65
+ * @returns A validated ArvoEvent matching the specified emit type
57
66
  *
58
- * @throws {Error} If event data validation fails against the contract schema
59
- * @throws {Error} If the specified emit type doesn't exist in the contract
60
- * @throws {Error} If OpenTelemetry operations fail
67
+ * @throws {Error} If validation fails, emit type doesn't exist, or OpenTelemetry operations fail
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const event = factory.emits({
72
+ * type: 'user.created',
73
+ * source: 'api/users',
74
+ * data: { id: '123', timestamp: new Date() }
75
+ * });
76
+ * ```
61
77
  */
62
- emits<V extends ArvoSemanticVersion & keyof TContract['versions'], U extends string & keyof TContract['versions'][V]['emits'], TExtension extends Record<string, any>>(event: {
63
- version: V;
64
- } & Omit<CreateArvoEvent<z.input<TContract['versions'][V]['emits'][U]>, U>, 'datacontenttype' | 'dataschema'>, extensions?: TExtension, opentelemetry?: ExecutionOpenTelemetryConfiguration): import("..").ArvoEvent<z.TypeOf<TContract["versions"][V]["emits"][U]>, TExtension, U>;
78
+ emits<U extends string & keyof TContract['emits'], TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.input<TContract['emits'][U]>, U>, 'datacontenttype' | 'dataschema'>, extensions?: TExtension, opentelemetry?: ExecutionOpenTelemetryConfiguration): import("..").ArvoEvent<z.TypeOf<TContract["emits"][U]>, TExtension, U>;
65
79
  /**
66
- * Creates a system error ArvoEvent for handling and reporting errors within the system.
80
+ * Creates a system error event for error reporting and handling.
81
+ *
82
+ * @template TExtension - Additional properties to include in the error event
67
83
  *
68
- * @template TExtension - Additional custom properties to include in the error event
84
+ * @param event - The error event configuration
85
+ * @param event.error - The Error instance to convert to an event
86
+ * @param [extensions] - Optional additional properties for the event
87
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
69
88
  *
70
- * @param event - The error event configuration object
71
- * @param event.error - The Error instance to be converted into an event
72
- * @param [extensions] - Optional additional properties to include in the event
73
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
89
+ * @returns A system error ArvoEvent
74
90
  *
75
- * @returns A system error ArvoEvent containing the error details
91
+ * @throws {Error} If event creation or OpenTelemetry operations fail
76
92
  *
77
- * @throws {Error} If event creation fails
78
- * @throws {Error} If OpenTelemetry operations fail
93
+ * @example
94
+ * ```typescript
95
+ * const errorEvent = factory.systemError({
96
+ * error: new Error('Validation failed'),
97
+ * source: 'api/validation'
98
+ * });
99
+ * ```
79
100
  */
80
101
  systemError<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<any, any>, 'data' | 'type' | 'datacontenttype' | 'dataschema'> & {
81
102
  error: Error;
@@ -83,5 +104,5 @@ export default class ArvoEventFactory<TContract extends ArvoContract> {
83
104
  errorMessage: string;
84
105
  errorName: string;
85
106
  errorStack: string | null;
86
- }, TExtension, `sys.${TContract["type"]}.error`>;
107
+ }, TExtension, TContract["systemError"]["type"]>;
87
108
  }
@@ -31,54 +31,69 @@ var OpenTelemetry_1 = require("../OpenTelemetry");
31
31
  var api_1 = require("@opentelemetry/api");
32
32
  var ArvoOrchestrationSubject_1 = __importDefault(require("../ArvoOrchestrationSubject"));
33
33
  /**
34
- * Factory class for creating contractual ArvoEvents based on a given ArvoContract.
35
- * This class handles the creation and validation of events according to contract specifications.
34
+ * Factory class for creating and validating events based on a versioned Arvo contract.
35
+ * Handles event creation, validation, and OpenTelemetry integration for a specific
36
+ * contract version.
36
37
  *
37
- * @template TContract - The type of ArvoContract this factory is bound to
38
+ * @template TContract - The versioned contract type this factory is bound to
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const contract = createArvoContract({
43
+ * uri: 'example/api',
44
+ * type: 'user.create',
45
+ * versions: { '1.0.0': { ... } }
46
+ * });
47
+ *
48
+ * const factory = createArvoEventFactory(contract.version('1.0.0'));
49
+ * ```
38
50
  */
39
51
  var ArvoEventFactory = /** @class */ (function () {
40
52
  /**
41
- * Creates an instance of ArvoEventFactory.
53
+ * Creates an ArvoEventFactory instance for a specific version of a contract.
42
54
  *
43
- * @param contract - The ArvoContract to base the events on.
55
+ * @param contract - The versioned contract to use for event creation and validation
44
56
  */
45
57
  function ArvoEventFactory(contract) {
46
58
  this.contract = contract;
47
59
  }
48
60
  /**
49
- * Creates a validated ArvoEvent that matches the contract's accept specifications.
50
- * This method ensures the created event conforms to the contract's input schema.
61
+ * Creates and validates an event matching the contract's accept specification.
51
62
  *
52
- * @template V - The semantic version of the contract to use
53
- * @template TExtension - Additional custom properties to include in the event
63
+ * @template TExtension - Additional properties to include in the event
54
64
  *
55
65
  * @param event - The event configuration object
56
- * @param event.version - The semantic version of the contract to use
57
- * @param event.data - The event payload that must conform to the contract's accept schema
58
- * @param [extensions] - Optional additional properties to include in the event
59
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
66
+ * @param [extensions] - Optional additional properties for the event
67
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
60
68
  *
61
- * @returns A validated ArvoEvent instance conforming to the contract's accept specifications
69
+ * @returns A validated ArvoEvent matching the contract's accept specification
62
70
  *
63
- * @throws {Error} If event data validation fails against the contract schema
64
- * @throws {Error} If OpenTelemetry operations fail
71
+ * @throws {Error} If validation fails or OpenTelemetry operations fail
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const event = factory.accepts({
76
+ * source: 'api/users',
77
+ * data: { name: 'John', email: 'john@example.com' }
78
+ * });
79
+ * ```
65
80
  */
66
81
  ArvoEventFactory.prototype.accepts = function (event, extensions, opentelemetry) {
67
82
  var _this = this;
68
83
  opentelemetry = opentelemetry !== null && opentelemetry !== void 0 ? opentelemetry : {
69
84
  tracer: (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
70
85
  };
71
- var span = opentelemetry.tracer.startSpan("ArvoEventFactory<".concat(this.contract.uri, "/").concat(event.version, ">.accepts"));
86
+ var span = opentelemetry.tracer.startSpan("ArvoEventFactory<".concat(this.contract.uri, "/").concat(this.contract.version, ">.accepts"));
72
87
  return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () {
73
88
  var _a, _b, _c, _d;
74
89
  span.setStatus({ code: api_1.SpanStatusCode.OK });
75
90
  var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)();
76
91
  try {
77
- var validationResult = _this.contract.validateAccepts(event.version, _this.contract.type, event.data);
92
+ var validationResult = _this.contract.accepts.schema.safeParse(event.data);
78
93
  if (!validationResult.success) {
79
94
  throw new Error("Accept Event data validation failed: ".concat(validationResult.error.message));
80
95
  }
81
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: _this.contract.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(event.version), data: validationResult.data }), extensions, opentelemetry);
96
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, opentelemetry);
82
97
  }
83
98
  catch (error) {
84
99
  (0, OpenTelemetry_1.exceptionToSpan)(error);
@@ -94,42 +109,44 @@ var ArvoEventFactory = /** @class */ (function () {
94
109
  });
95
110
  };
96
111
  /**
97
- * Creates a validated ArvoEvent that matches one of the contract's emit specifications.
98
- * This method ensures the created event conforms to the contract's output schema.
112
+ * Creates and validates an event matching one of the contract's emit specifications.
99
113
  *
100
- * @template V - The semantic version of the contract to use
101
114
  * @template U - The specific emit event type from the contract
102
- * @template TExtension - Additional custom properties to include in the event
115
+ * @template TExtension - Additional properties to include in the event
103
116
  *
104
117
  * @param event - The event configuration object
105
- * @param event.version - The semantic version of the contract to use
106
- * @param event.type - The type of emit event to create
107
- * @param event.data - The event payload that must conform to the contract's emit schema
108
- * @param [extensions] - Optional additional properties to include in the event
109
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
118
+ * @param [extensions] - Optional additional properties for the event
119
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
120
+ *
121
+ * @returns A validated ArvoEvent matching the specified emit type
110
122
  *
111
- * @returns A validated ArvoEvent instance conforming to the contract's emit specifications
123
+ * @throws {Error} If validation fails, emit type doesn't exist, or OpenTelemetry operations fail
112
124
  *
113
- * @throws {Error} If event data validation fails against the contract schema
114
- * @throws {Error} If the specified emit type doesn't exist in the contract
115
- * @throws {Error} If OpenTelemetry operations fail
125
+ * @example
126
+ * ```typescript
127
+ * const event = factory.emits({
128
+ * type: 'user.created',
129
+ * source: 'api/users',
130
+ * data: { id: '123', timestamp: new Date() }
131
+ * });
132
+ * ```
116
133
  */
117
134
  ArvoEventFactory.prototype.emits = function (event, extensions, opentelemetry) {
118
135
  var _this = this;
119
136
  opentelemetry = opentelemetry !== null && opentelemetry !== void 0 ? opentelemetry : {
120
137
  tracer: (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
121
138
  };
122
- var span = opentelemetry.tracer.startSpan("ArvoEventFactory<".concat(this.contract.uri, "/").concat(event.version, ">.emits<").concat(event.type, ">"));
139
+ var span = opentelemetry.tracer.startSpan("ArvoEventFactory<".concat(this.contract.uri, "/").concat(this.contract.version, ">.emits<").concat(event.type, ">"));
123
140
  return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () {
124
- var _a, _b, _c, _d;
141
+ var _a, _b, _c, _d, _e, _f, _g, _h;
125
142
  span.setStatus({ code: api_1.SpanStatusCode.OK });
126
143
  var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)();
127
144
  try {
128
- var validationResult = _this.contract.validateEmits(event.version, event.type, event.data);
129
- if (!validationResult.success) {
130
- throw new Error("Emit Event data validation failed: ".concat(validationResult.error.message));
145
+ var validationResult = (_b = (_a = _this.contract.emits) === null || _a === void 0 ? void 0 : _a[event.type]) === null || _b === void 0 ? void 0 : _b.safeParse(event.data);
146
+ if (!(validationResult === null || validationResult === void 0 ? void 0 : validationResult.success)) {
147
+ throw new Error("Emit Event data validation failed: ".concat((_d = (_c = validationResult === null || validationResult === void 0 ? void 0 : validationResult.error) === null || _c === void 0 ? void 0 : _c.message) !== null && _d !== void 0 ? _d : "No contract available for ".concat(event.type)));
131
148
  }
132
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(event.version), data: validationResult.data }), extensions, opentelemetry);
149
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_f = (_e = event.traceparent) !== null && _e !== void 0 ? _e : otelHeaders.traceparent) !== null && _f !== void 0 ? _f : undefined, tracestate: (_h = (_g = event.tracestate) !== null && _g !== void 0 ? _g : otelHeaders.tracestate) !== null && _h !== void 0 ? _h : undefined, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, opentelemetry);
133
150
  }
134
151
  catch (error) {
135
152
  (0, OpenTelemetry_1.exceptionToSpan)(error);
@@ -145,19 +162,26 @@ var ArvoEventFactory = /** @class */ (function () {
145
162
  });
146
163
  };
147
164
  /**
148
- * Creates a system error ArvoEvent for handling and reporting errors within the system.
165
+ * Creates a system error event for error reporting and handling.
166
+ *
167
+ * @template TExtension - Additional properties to include in the error event
149
168
  *
150
- * @template TExtension - Additional custom properties to include in the error event
169
+ * @param event - The error event configuration
170
+ * @param event.error - The Error instance to convert to an event
171
+ * @param [extensions] - Optional additional properties for the event
172
+ * @param [opentelemetry] - Optional OpenTelemetry configuration
151
173
  *
152
- * @param event - The error event configuration object
153
- * @param event.error - The Error instance to be converted into an event
154
- * @param [extensions] - Optional additional properties to include in the event
155
- * @param [opentelemetry] - Optional OpenTelemetry configuration for tracing
174
+ * @returns A system error ArvoEvent
156
175
  *
157
- * @returns A system error ArvoEvent containing the error details
176
+ * @throws {Error} If event creation or OpenTelemetry operations fail
158
177
  *
159
- * @throws {Error} If event creation fails
160
- * @throws {Error} If OpenTelemetry operations fail
178
+ * @example
179
+ * ```typescript
180
+ * const errorEvent = factory.systemError({
181
+ * error: new Error('Validation failed'),
182
+ * source: 'api/validation'
183
+ * });
184
+ * ```
161
185
  */
162
186
  ArvoEventFactory.prototype.systemError = function (event, extensions, opentelemetry) {
163
187
  var _this = this;
@@ -171,7 +195,7 @@ var ArvoEventFactory = /** @class */ (function () {
171
195
  var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)();
172
196
  try {
173
197
  var error = event.error, _event = __rest(event, ["error"]);
174
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, _event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: "sys.".concat(_this.contract.type, ".error"), data: {
198
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, _event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: _this.contract.systemError.type, data: {
175
199
  errorName: error.name,
176
200
  errorMessage: error.message,
177
201
  errorStack: (_e = error.stack) !== null && _e !== void 0 ? _e : null,
package/dist/index.d.ts CHANGED
@@ -21,10 +21,11 @@ import { ArvoOrchestrationSubjectContentSchema } from './ArvoOrchestrationSubjec
21
21
  import { ArvoOrchestrationSubjectContent } from './ArvoOrchestrationSubject/type';
22
22
  import ArvoEventHttp from './ArvoEventHttp';
23
23
  import { ArvoEventHttpConfig } from './ArvoEventHttp/types';
24
- import { InferArvoContract, InferArvoEvent, ArvoSemanticVersion, ArvoErrorType } from './types';
24
+ import { InferArvoContract, InferArvoEvent, ArvoSemanticVersion, ArvoErrorType, InferVersionedArvoContract } from './types';
25
25
  import { createArvoOrchestratorContract } from './ArvoOrchestratorContract';
26
26
  import { ICreateArvoOrchestratorContract, ArvoOrchestratorContract } from './ArvoOrchestratorContract/types';
27
27
  import { ArvoOrchestratorEventTypeGen } from './ArvoOrchestratorContract/typegen';
28
+ import { VersionedArvoContract } from './ArvoContract/VersionedArvoContract';
28
29
  /**
29
30
  * Collection of Zod schemas for validating various aspects of Arvo events.
30
31
  * @property {z.ZodObject} CloudEventContextSchema - Schema for core CloudEvent properties.
@@ -99,4 +100,4 @@ declare const ArvoEventSchema: {
99
100
  parentSubject$$: string | null;
100
101
  }>;
101
102
  };
102
- export { ArvoEventHttpConfig, ArvoEventHttp, ArvoEvent, createArvoEvent, ArvoDataContentType, ArvoEventData, CloudEventExtension, ArvoEventSchema, CloudEventContext, ArvoExtension, OpenTelemetryExtension, CreateArvoEvent, exceptionToSpan, logToSpan, OpenTelemetryHeaders, TelemetryLogLevel, OTelNull, validateURI, cleanString, ArvoContract, createArvoContract, ArvoContractValidators, ArvoContractRecord, IArvoContract, ResolveArvoContractRecord, ArvoEventFactory, createArvoEventFactory, currentOpenTelemetryHeaders, OpenInference, OpenInferenceSpanKind, ArvoExecution, ArvoExecutionSpanKind, ArvoContractJSONSchema, ArvoOrchestrationSubject, ArvoOrchestrationSubjectContent, ArvoSemanticVersion, InferArvoEvent, InferArvoContract, createArvoOrchestratorContract, ICreateArvoOrchestratorContract, ArvoOrchestratorEventTypeGen, ExecutionOpenTelemetryConfiguration, parseEventDataSchema, ArvoOrchestrationSubjectContentSchema, ArvoSemanticVersionSchema, ArvoErrorSchema, ArvoErrorType, compareSemanticVersions, parseSemanticVersion, createSimpleArvoContract, ArvoOrchestratorContract, };
103
+ export { ArvoEventHttpConfig, ArvoEventHttp, ArvoEvent, createArvoEvent, ArvoDataContentType, ArvoEventData, CloudEventExtension, ArvoEventSchema, CloudEventContext, ArvoExtension, OpenTelemetryExtension, CreateArvoEvent, exceptionToSpan, logToSpan, OpenTelemetryHeaders, TelemetryLogLevel, OTelNull, validateURI, cleanString, ArvoContract, createArvoContract, ArvoContractValidators, ArvoContractRecord, IArvoContract, ResolveArvoContractRecord, ArvoEventFactory, createArvoEventFactory, currentOpenTelemetryHeaders, OpenInference, OpenInferenceSpanKind, ArvoExecution, ArvoExecutionSpanKind, ArvoContractJSONSchema, ArvoOrchestrationSubject, ArvoOrchestrationSubjectContent, ArvoSemanticVersion, InferArvoEvent, InferArvoContract, createArvoOrchestratorContract, ICreateArvoOrchestratorContract, ArvoOrchestratorEventTypeGen, ExecutionOpenTelemetryConfiguration, parseEventDataSchema, ArvoOrchestrationSubjectContentSchema, ArvoSemanticVersionSchema, ArvoErrorSchema, ArvoErrorType, compareSemanticVersions, parseSemanticVersion, createSimpleArvoContract, ArvoOrchestratorContract, VersionedArvoContract, InferVersionedArvoContract, };
package/dist/types.d.ts CHANGED
@@ -3,6 +3,7 @@ import ArvoContract from './ArvoContract';
3
3
  import ArvoEvent from './ArvoEvent';
4
4
  import { ArvoExtension, OpenTelemetryExtension } from './ArvoEvent/types';
5
5
  import { ArvoErrorSchema } from './schema';
6
+ import { VersionedArvoContract } from './ArvoContract/VersionedArvoContract';
6
7
  /**
7
8
  * Represents the version of Arvo components following Semantic Versioning (SemVer).
8
9
  *
@@ -151,5 +152,60 @@ export type InferArvoContract<T extends ArvoContract<string, string, Record<Arvo
151
152
  };
152
153
  systemError: InferArvoEvent<ArvoEvent<InferZodSchema<T['systemError']['schema']>, {}, T['systemError']['type']>>;
153
154
  } : never;
155
+ /**
156
+ * Represents the structure of an Arvo system error.
157
+ * This type is inferred from the ArvoErrorSchema and provides
158
+ * the standard error format used across all Arvo contracts.
159
+ *
160
+ * @property errorName - The classification or type of the error
161
+ * @property errorMessage - A human-readable description of what went wrong
162
+ * @property errorStack - Optional stack trace for debugging (null if not available)
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const error: ArvoErrorType = {
167
+ * errorName: 'ValidationError',
168
+ * errorMessage: 'Invalid input format',
169
+ * errorStack: 'Error: Invalid input format\n at validate (/app.js:10)'
170
+ * };
171
+ * ```
172
+ *
173
+ * @see {@link ArvoErrorSchema} for the underlying Zod schema definition
174
+ */
154
175
  export type ArvoErrorType = z.infer<typeof ArvoErrorSchema>;
176
+ /**
177
+ * A type utility that infers the complete event structure from a versioned Arvo contract.
178
+ * This type extracts and transforms all event types, schemas, and error definitions
179
+ * from a specific version of a contract into their fully resolved event forms.
180
+ *
181
+ * @template TVersion - The versioned contract to infer from, must extend VersionedArvoContract
182
+ *
183
+ * @property accepts - The fully resolved accept event type for this version
184
+ * @property systemError - The fully resolved system error event type
185
+ * @property emits - Record of all fully resolved emit event types
186
+ *
187
+ * This type utility:
188
+ * - Transforms Zod schemas into their inferred TypeScript types
189
+ * - Wraps all event types in the full ArvoEvent structure
190
+ * - Preserves all event type relationships and constraints
191
+ * - Includes OpenTelemetry and Arvo extensions
192
+ * - Maintains type safety across all transformations
193
+ *
194
+ * Common use cases:
195
+ * - Type-safe event handling in version-specific contexts
196
+ * - Generating TypeScript interfaces for API documentation
197
+ * - Validating event payload types at compile time
198
+ * - Creating type-safe event factories
199
+ *
200
+ * @see {@link VersionedArvoContract} for the input contract structure
201
+ * @see {@link InferArvoEvent} for the event inference utility
202
+ * @see {@link ArvoEvent} for the base event structure
203
+ */
204
+ export type InferVersionedArvoContract<TVersion extends VersionedArvoContract<ArvoContract, ArvoSemanticVersion>> = {
205
+ accepts: InferArvoEvent<ArvoEvent<InferZodSchema<TVersion['accepts']['schema']>, {}, TVersion['accepts']['type']>>;
206
+ systemError: InferArvoEvent<ArvoEvent<InferZodSchema<TVersion['systemError']['schema']>, {}, TVersion['systemError']['type']>>;
207
+ emits: {
208
+ [K in string & keyof TVersion['emits']]: InferArvoEvent<ArvoEvent<InferZodSchema<TVersion['emits'][K]>, {}, K>>;
209
+ };
210
+ };
155
211
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "This core package contains all the core classes and components of the Arvo Event Driven System",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {