arvo-core 1.0.7 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -28,3 +28,7 @@
28
28
 
29
29
  - Updates to OpenTelemetry functions
30
30
 
31
+ ## [1.0.8] - 2024-09-01
32
+
33
+ - Added support for system error in ArvoContract
34
+
package/README.md CHANGED
@@ -51,6 +51,7 @@ At its core, Arvo has only three main data structures:
51
51
  - [ArvoEvent](src/ArvoEvent/README.md) aims to provide a extendible variant of the open-source CloudEvent spec-ed object to define all the event in the system.
52
52
  - [ArvoContract](src/ArvoContract/README.md) is a basic class to define and impose contracts between services, ensuring trust in decoupled systems during build and development.
53
53
  - [ArvoContractLibrary](src/ArvoContractLibrary/README.md) is a utility class designed to manage and access multiple ArvoContract instances efficiently.
54
+ - `ArvoErrorSchema` is the recommeded zod schema for all the errors in the ArvoEvents
54
55
 
55
56
  ## Utilities
56
57
 
@@ -1,13 +1,9 @@
1
1
  import { IArvoContract } from './types';
2
2
  import ArvoContract from '.';
3
- import { CreateArvoEvent } from '../ArvoEvent/types';
4
- import { TelemetryContext } from '../OpenTelemetry/types';
5
- import { ArvoContractRecord } from './types';
6
- import { z } from 'zod';
7
3
  /**
8
4
  * Infers the ArvoContract type from a given IArvoContract.
9
5
  */
10
- export type InferArvoContract<T> = T extends IArvoContract<infer U, infer V, infer W> ? ArvoContract<U, V, W> : never;
6
+ export type InferArvoContract<T> = T extends IArvoContract<infer S, infer U, infer V, infer W> ? ArvoContract<S, U, V, W> : never;
11
7
  /**
12
8
  * Creates an ArvoContract instance from the given contract specification.
13
9
  *
@@ -15,10 +11,11 @@ export type InferArvoContract<T> = T extends IArvoContract<infer U, infer V, inf
15
11
  * with proper type inference.
16
12
  *
17
13
  * @template TContract - The type of the contract specification.
18
- * @param {TContract} contractSpec - The contract specification object.
14
+ *
15
+ * @param contractSpec - The contract specification object.
19
16
  * This should include the URI, accepts, and emits properties as defined in IArvoContract.
20
17
  *
21
- * @returns {InferArvoContract<TContract>} The created ArvoContract instance.
18
+ * @returns The created ArvoContract instance.
22
19
  * The returned type is inferred from the input contract specification.
23
20
  *
24
21
  * @example
@@ -28,48 +25,9 @@ export type InferArvoContract<T> = T extends IArvoContract<infer U, infer V, inf
28
25
  * type: 'com.example.input',
29
26
  * schema: z.object({ name: z.string() }),
30
27
  * },
31
- * emits: [
32
- * {
33
- * type: 'com.example.output',
34
- * schema: z.object({ result: z.number() }),
35
- * },
36
- * ],
28
+ * emits: {
29
+ * 'com.example.output': z.object({ result: z.number() }),
30
+ * },
37
31
  * });
38
32
  */
39
33
  export declare const createArvoContract: <const TContract extends IArvoContract>(contract: TContract) => InferArvoContract<TContract>;
40
- /**
41
- * Creates a contractual ArvoEvent factory based on the provided contract.
42
- *
43
- * @template T - The type of the contract.
44
- * @template TAccepts - The type of record the contract bound handler accepts, defaults to ArvoContractRecord.
45
- * @template TEmits - The type of records the contract bound handler emits.
46
- * @param {ArvoContract<T, TAccepts, TEmits>} contract - The ArvoContract to base the events on.
47
- * @returns {Object} An object with 'accepts' and 'emits' methods for creating events.
48
- */
49
- export declare const contractualArvoEventFactory: <T extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>>(contract: ArvoContract<T, TAccepts, TEmits>) => {
50
- /**
51
- * Creates an ArvoEvent as per the accept record of the contract.
52
- *
53
- * @template TExtension - The type of extensions to add to the event.
54
- * @param {CreateArvoEvent<z.infer<TAccepts['schema']>, TAccepts['type']>} event - The event to create.
55
- * @param {TExtension} [extensions] - Optional extensions to add to the event.
56
- * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
57
- * @returns The created ArvoEvent as per the accept record of the contract.
58
- * @throws {Error} If the event data fails validation against the contract.
59
- */
60
- accepts: <TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<TAccepts["schema"]>, TAccepts["type"]>, extensions?: TExtension, telemetry?: TelemetryContext) => import("..").ArvoEvent<z.TypeOf<z.TypeOf<TAccepts["schema"]>>, TExtension, string>;
61
- /**
62
- * Creates an ArvoEvent as per one of the emits record in the contract.
63
- *
64
- * @template U - The specific event type from TEmits.
65
- * @template TExtension - The type of extensions to add to the event.
66
- * @param {CreateArvoEvent<z.infer<Extract<TEmits, { type: U }>['schema']>, U>} event - The event to create.
67
- * @param {TExtension} [extensions] - Optional extensions to add to the event.
68
- * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
69
- * @returns The created ArvoEvent as per one of the emits records of the contract.
70
- * @throws {Error} If the event data fails validation against the contract.
71
- */
72
- emits: <U extends keyof TEmits & string, TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<TEmits[U]>, U>, extensions?: TExtension, telemetry?: TelemetryContext) => import("..").ArvoEvent<z.TypeOf<Extract<TEmits, {
73
- type: U;
74
- }>["schema"]>, TExtension, string>;
75
- };
@@ -1,23 +1,10 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
16
5
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.contractualArvoEventFactory = exports.createArvoContract = void 0;
6
+ exports.createArvoContract = void 0;
18
7
  var _1 = __importDefault(require("."));
19
- var helpers_1 = require("../ArvoEvent/helpers");
20
- var OpenTelemetry_1 = require("../OpenTelemetry");
21
8
  /**
22
9
  * Creates an ArvoContract instance from the given contract specification.
23
10
  *
@@ -25,10 +12,11 @@ var OpenTelemetry_1 = require("../OpenTelemetry");
25
12
  * with proper type inference.
26
13
  *
27
14
  * @template TContract - The type of the contract specification.
28
- * @param {TContract} contractSpec - The contract specification object.
15
+ *
16
+ * @param contractSpec - The contract specification object.
29
17
  * This should include the URI, accepts, and emits properties as defined in IArvoContract.
30
18
  *
31
- * @returns {InferArvoContract<TContract>} The created ArvoContract instance.
19
+ * @returns The created ArvoContract instance.
32
20
  * The returned type is inferred from the input contract specification.
33
21
  *
34
22
  * @example
@@ -38,66 +26,12 @@ var OpenTelemetry_1 = require("../OpenTelemetry");
38
26
  * type: 'com.example.input',
39
27
  * schema: z.object({ name: z.string() }),
40
28
  * },
41
- * emits: [
42
- * {
43
- * type: 'com.example.output',
44
- * schema: z.object({ result: z.number() }),
45
- * },
46
- * ],
29
+ * emits: {
30
+ * 'com.example.output': z.object({ result: z.number() }),
31
+ * },
47
32
  * });
48
33
  */
49
34
  var createArvoContract = function (contract) {
50
35
  return new _1.default(contract);
51
36
  };
52
37
  exports.createArvoContract = createArvoContract;
53
- /**
54
- * Creates a contractual ArvoEvent factory based on the provided contract.
55
- *
56
- * @template T - The type of the contract.
57
- * @template TAccepts - The type of record the contract bound handler accepts, defaults to ArvoContractRecord.
58
- * @template TEmits - The type of records the contract bound handler emits.
59
- * @param {ArvoContract<T, TAccepts, TEmits>} contract - The ArvoContract to base the events on.
60
- * @returns {Object} An object with 'accepts' and 'emits' methods for creating events.
61
- */
62
- var contractualArvoEventFactory = function (contract) { return ({
63
- /**
64
- * Creates an ArvoEvent as per the accept record of the contract.
65
- *
66
- * @template TExtension - The type of extensions to add to the event.
67
- * @param {CreateArvoEvent<z.infer<TAccepts['schema']>, TAccepts['type']>} event - The event to create.
68
- * @param {TExtension} [extensions] - Optional extensions to add to the event.
69
- * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
70
- * @returns The created ArvoEvent as per the accept record of the contract.
71
- * @throws {Error} If the event data fails validation against the contract.
72
- */
73
- accepts: function (event, extensions, telemetry) {
74
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'contractualArvoEventFactory.accepts', {}, function () {
75
- var validationResult = contract.validateInput(event.type, event.data);
76
- if (!validationResult.success) {
77
- throw new Error("Accept Event data validation failed: ".concat(validationResult.error.message));
78
- }
79
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { data: validationResult.data }), extensions, telemetry);
80
- });
81
- },
82
- /**
83
- * Creates an ArvoEvent as per one of the emits record in the contract.
84
- *
85
- * @template U - The specific event type from TEmits.
86
- * @template TExtension - The type of extensions to add to the event.
87
- * @param {CreateArvoEvent<z.infer<Extract<TEmits, { type: U }>['schema']>, U>} event - The event to create.
88
- * @param {TExtension} [extensions] - Optional extensions to add to the event.
89
- * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
90
- * @returns The created ArvoEvent as per one of the emits records of the contract.
91
- * @throws {Error} If the event data fails validation against the contract.
92
- */
93
- emits: function (event, extensions, telemetry) {
94
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'contractualArvoEventFactory.emits', {}, function () {
95
- var validationResult = contract.validateOutput(event.type, event.data);
96
- if (!validationResult.success) {
97
- throw new Error("Emit Event data validation failed: ".concat(validationResult.error.message));
98
- }
99
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { data: validationResult.data }), extensions, telemetry);
100
- });
101
- },
102
- }); };
103
- exports.contractualArvoEventFactory = contractualArvoEventFactory;
@@ -1,16 +1,18 @@
1
1
  import { z } from 'zod';
2
2
  import { ArvoContractRecord, IArvoContract } from './types';
3
+ import { ArvoErrorSchema } from '../schema';
3
4
  /**
4
5
  * ArvoContract class represents a contract with defined input and output schemas.
5
6
  * It provides methods for validating inputs and outputs based on the contract's specifications.
6
7
  * An event handler can be bound to it so the this contract may impose the types
7
8
  * on inputs and outputs of it
8
9
  *
9
- * @template TUri - The URI type, defaults to string.
10
- * @template TAccepts - The type of record the contract bound handler accepts, defaults to ArvoContractRecord.
10
+ * @template TUri - The URI of the contract
11
+ * @template TType - The accept type, defaults to string.
12
+ * @template TAcceptSchema - The of the data which the contract bound can accept
11
13
  * @template TEmits - The type of records the contract bound handler emits.
12
14
  */
13
- export default class ArvoContract<TUri extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>> {
15
+ export default class ArvoContract<TUri extends string = string, TType extends string = string, TAcceptSchema extends z.ZodTypeAny = z.ZodTypeAny, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>> {
14
16
  private readonly _uri;
15
17
  private readonly _accepts;
16
18
  private readonly _emits;
@@ -18,60 +20,57 @@ export default class ArvoContract<TUri extends string = string, TAccepts extends
18
20
  readonly description: string | null;
19
21
  /**
20
22
  * Creates an instance of ArvoContract.
21
- * @param {IArvoContract<TUri, TAccepts, TEmits>} params - The contract parameters.
23
+ * @param params - The contract parameters.
22
24
  */
23
- constructor(params: IArvoContract<TUri, TAccepts, TEmits>);
25
+ constructor(params: IArvoContract<TUri, TType, TAcceptSchema, TEmits>);
24
26
  /**
25
27
  * Gets the URI of the contract.
26
- * @returns {T} The contract's URI.
27
28
  */
28
29
  get uri(): TUri;
29
30
  /**
30
31
  * Gets the type and schema of the event that the contact
31
32
  * bound handler listens to.
32
- * @returns {TAccepts} The frozen accepts object.
33
33
  */
34
- get accepts(): TAccepts;
34
+ get accepts(): ArvoContractRecord<TType, TAcceptSchema>;
35
35
  /**
36
36
  * Gets all event types and schemas that can be emitted by the
37
37
  * contract bound handler.
38
- *
39
- * @returns {TEmits} A record of all emitted events.
40
38
  */
41
39
  get emits(): TEmits;
40
+ get systemError(): ArvoContractRecord<`sys.${TType}.error`, typeof ArvoErrorSchema>;
42
41
  /**
43
42
  * Validates the contract bound handler's input against the
44
43
  * contract's accept schema.
45
44
  * @template U - The type of the input to validate.
46
- * @param {TAccepts['type']} type - The type of the input event.
47
- * @param {U} input - The input data to validate.
48
- * @returns {z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>} The validation result.
49
- * @throws {Error} If the accept type is not found in the contract.
45
+ * @param type - The type of the input event.
46
+ * @param input - The input data to validate.
47
+ * @returns The validation result.
48
+ * @throws If the accept type is not found in the contract.
50
49
  */
51
- validateInput<U>(type: TAccepts['type'], input: U): z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>;
50
+ validateInput<U>(type: TType, input: U): z.SafeParseReturnType<U, z.infer<TAcceptSchema>>;
52
51
  /**
53
52
  * Validates the contract bound handler's output against the
54
53
  * contract's emit schema.
55
54
  * @template U - The type of the output to validate.
56
- * @param {U} type - The type of the output event.
57
- * @param {unknown} output - The output data to validate.
58
- * @returns {z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, { type: U }>['schema']>>} The validation result.
59
- * @throws {Error} If the emit type is not found in the contract.
55
+ * @param type - The type of the output event.
56
+ * @param output - The output data to validate.
57
+ * @returns The validation result.
58
+ * @throws If the emit type is not found in the contract.
60
59
  */
61
60
  validateOutput<U extends keyof TEmits>(type: U, output: unknown): z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, {
62
61
  type: U;
63
62
  }>['schema']>>;
64
63
  /**
65
64
  * Validates the accepts record.
66
- * @param {TAccepts} accepts - The accepts record to validate.
67
- * @returns {TAccepts} The validated accepts record.
65
+ * @param accepts - The accepts record to validate.
66
+ * @returns The validated accepts record.
68
67
  * @private
69
68
  */
70
69
  private validateAccepts;
71
70
  /**
72
71
  * Validates the emits records.
73
- * @param {TEmits} emits - The emits records to validate.
74
- * @returns {TEmits} The validated emits records.
72
+ * @param emits - The emits records to validate.
73
+ * @returns The validated emits records.
75
74
  * @private
76
75
  */
77
76
  private validateEmits;
@@ -79,15 +78,15 @@ export default class ArvoContract<TUri extends string = string, TAccepts extends
79
78
  * Exports the ArvoContract instance as a plain object conforming to the IArvoContract interface.
80
79
  * This method can be used to serialize the contract or to create a new instance with the same parameters.
81
80
  *
82
- * @returns {IArvoContract<TUri, TAccepts, TEmits>} An object representing the contract, including its URI, accepts, and emits properties.
81
+ * @returns An object representing the contract, including its URI, accepts, and emits properties.
83
82
  */
84
- export(): IArvoContract<TUri, TAccepts, TEmits>;
83
+ export(): IArvoContract<TUri, TType, TAcceptSchema, TEmits>;
85
84
  /**
86
85
  * Converts the ArvoContract instance to a JSON Schema representation.
87
86
  * This method provides a way to represent the contract's structure and validation rules
88
87
  * in a format that conforms to the JSON Schema specification.
89
88
  *
90
- * @returns {Object} An object representing the contract in JSON Schema format, including:
89
+ * @returns An object representing the contract in JSON Schema format, including:
91
90
  * - uri: The contract's URI
92
91
  * - description: The contract's description (if available)
93
92
  * - accepts: An object containing the accepted input type and its JSON Schema representation
@@ -97,7 +96,7 @@ export default class ArvoContract<TUri extends string = string, TAccepts extends
97
96
  uri: TUri;
98
97
  description: string | null;
99
98
  accepts: {
100
- type: string;
99
+ type: TType;
101
100
  schema: import("zod-to-json-schema").JsonSchema7Type & {
102
101
  $schema?: string | undefined;
103
102
  definitions?: {
@@ -14,32 +14,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
14
14
  var zod_1 = require("zod");
15
15
  var validators_1 = require("./validators");
16
16
  var zod_to_json_schema_1 = require("zod-to-json-schema");
17
+ var schema_1 = require("../schema");
17
18
  /**
18
19
  * ArvoContract class represents a contract with defined input and output schemas.
19
20
  * It provides methods for validating inputs and outputs based on the contract's specifications.
20
21
  * An event handler can be bound to it so the this contract may impose the types
21
22
  * on inputs and outputs of it
22
23
  *
23
- * @template TUri - The URI type, defaults to string.
24
- * @template TAccepts - The type of record the contract bound handler accepts, defaults to ArvoContractRecord.
24
+ * @template TUri - The URI of the contract
25
+ * @template TType - The accept type, defaults to string.
26
+ * @template TAcceptSchema - The of the data which the contract bound can accept
25
27
  * @template TEmits - The type of records the contract bound handler emits.
26
28
  */
27
29
  var ArvoContract = /** @class */ (function () {
28
30
  /**
29
31
  * Creates an instance of ArvoContract.
30
- * @param {IArvoContract<TUri, TAccepts, TEmits>} params - The contract parameters.
32
+ * @param params - The contract parameters.
31
33
  */
32
34
  function ArvoContract(params) {
33
35
  this._uri = validators_1.ArvoContractValidators.contract.uri.parse(params.uri);
34
36
  this._accepts = this.validateAccepts(params.accepts);
35
37
  this._emits = this.validateEmits(params.emits);
36
38
  this.description = params.description || null;
37
- Object.freeze(this);
38
39
  }
39
40
  Object.defineProperty(ArvoContract.prototype, "uri", {
40
41
  /**
41
42
  * Gets the URI of the contract.
42
- * @returns {T} The contract's URI.
43
43
  */
44
44
  get: function () {
45
45
  return this._uri;
@@ -51,10 +51,9 @@ var ArvoContract = /** @class */ (function () {
51
51
  /**
52
52
  * Gets the type and schema of the event that the contact
53
53
  * bound handler listens to.
54
- * @returns {TAccepts} The frozen accepts object.
55
54
  */
56
55
  get: function () {
57
- return Object.freeze(this._accepts);
56
+ return this._accepts;
58
57
  },
59
58
  enumerable: false,
60
59
  configurable: true
@@ -63,8 +62,6 @@ var ArvoContract = /** @class */ (function () {
63
62
  /**
64
63
  * Gets all event types and schemas that can be emitted by the
65
64
  * contract bound handler.
66
- *
67
- * @returns {TEmits} A record of all emitted events.
68
65
  */
69
66
  get: function () {
70
67
  return __assign({}, this._emits);
@@ -72,14 +69,24 @@ var ArvoContract = /** @class */ (function () {
72
69
  enumerable: false,
73
70
  configurable: true
74
71
  });
72
+ Object.defineProperty(ArvoContract.prototype, "systemError", {
73
+ get: function () {
74
+ return {
75
+ type: "sys.".concat(this._accepts.type, ".error"),
76
+ schema: schema_1.ArvoErrorSchema,
77
+ };
78
+ },
79
+ enumerable: false,
80
+ configurable: true
81
+ });
75
82
  /**
76
83
  * Validates the contract bound handler's input against the
77
84
  * contract's accept schema.
78
85
  * @template U - The type of the input to validate.
79
- * @param {TAccepts['type']} type - The type of the input event.
80
- * @param {U} input - The input data to validate.
81
- * @returns {z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>} The validation result.
82
- * @throws {Error} If the accept type is not found in the contract.
86
+ * @param type - The type of the input event.
87
+ * @param input - The input data to validate.
88
+ * @returns The validation result.
89
+ * @throws If the accept type is not found in the contract.
83
90
  */
84
91
  ArvoContract.prototype.validateInput = function (type, input) {
85
92
  if (type !== this._accepts.type) {
@@ -91,10 +98,10 @@ var ArvoContract = /** @class */ (function () {
91
98
  * Validates the contract bound handler's output against the
92
99
  * contract's emit schema.
93
100
  * @template U - The type of the output to validate.
94
- * @param {U} type - The type of the output event.
95
- * @param {unknown} output - The output data to validate.
96
- * @returns {z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, { type: U }>['schema']>>} The validation result.
97
- * @throws {Error} If the emit type is not found in the contract.
101
+ * @param type - The type of the output event.
102
+ * @param output - The output data to validate.
103
+ * @returns The validation result.
104
+ * @throws If the emit type is not found in the contract.
98
105
  */
99
106
  ArvoContract.prototype.validateOutput = function (type, output) {
100
107
  var emit = this.emits[type];
@@ -105,8 +112,8 @@ var ArvoContract = /** @class */ (function () {
105
112
  };
106
113
  /**
107
114
  * Validates the accepts record.
108
- * @param {TAccepts} accepts - The accepts record to validate.
109
- * @returns {TAccepts} The validated accepts record.
115
+ * @param accepts - The accepts record to validate.
116
+ * @returns The validated accepts record.
110
117
  * @private
111
118
  */
112
119
  ArvoContract.prototype.validateAccepts = function (accepts) {
@@ -117,8 +124,8 @@ var ArvoContract = /** @class */ (function () {
117
124
  };
118
125
  /**
119
126
  * Validates the emits records.
120
- * @param {TEmits} emits - The emits records to validate.
121
- * @returns {TEmits} The validated emits records.
127
+ * @param emits - The emits records to validate.
128
+ * @returns The validated emits records.
122
129
  * @private
123
130
  */
124
131
  ArvoContract.prototype.validateEmits = function (emits) {
@@ -126,13 +133,13 @@ var ArvoContract = /** @class */ (function () {
126
133
  var key = _a[0];
127
134
  return validators_1.ArvoContractValidators.record.type.parse(key);
128
135
  });
129
- return Object.freeze(emits);
136
+ return emits;
130
137
  };
131
138
  /**
132
139
  * Exports the ArvoContract instance as a plain object conforming to the IArvoContract interface.
133
140
  * This method can be used to serialize the contract or to create a new instance with the same parameters.
134
141
  *
135
- * @returns {IArvoContract<TUri, TAccepts, TEmits>} An object representing the contract, including its URI, accepts, and emits properties.
142
+ * @returns An object representing the contract, including its URI, accepts, and emits properties.
136
143
  */
137
144
  ArvoContract.prototype.export = function () {
138
145
  return {
@@ -150,7 +157,7 @@ var ArvoContract = /** @class */ (function () {
150
157
  * This method provides a way to represent the contract's structure and validation rules
151
158
  * in a format that conforms to the JSON Schema specification.
152
159
  *
153
- * @returns {Object} An object representing the contract in JSON Schema format, including:
160
+ * @returns An object representing the contract in JSON Schema format, including:
154
161
  * - uri: The contract's URI
155
162
  * - description: The contract's description (if available)
156
163
  * - accepts: An object containing the accepted input type and its JSON Schema representation
@@ -12,15 +12,17 @@ export type ArvoContractRecord<TType extends string = string, TSchema extends z.
12
12
  };
13
13
  /**
14
14
  * Interface for an Arvo contract.
15
- * @template TUri - The URI type, defaults to string.
16
- * @template TAccepts - The type of record the contract bound handler accepts, defaults to ArvoContractRecord.
15
+ *
16
+ * @template TUri - The URI of the contract
17
+ * @template TType - The accept type, defaults to string.
18
+ * @template TAcceptSchema - The of the data which the contract bound can accept
17
19
  * @template TEmits - The type of records the contract bound handler emits.
18
20
  */
19
- export interface IArvoContract<TUri extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>> {
21
+ export interface IArvoContract<TUri extends string = string, TType extends string = string, TAcceptSchema extends z.ZodTypeAny = z.ZodTypeAny, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>> {
20
22
  /** The unique identifier for the contract */
21
23
  uri: TUri;
22
24
  /** The record type that the contract accepts */
23
- accepts: TAccepts;
25
+ accepts: ArvoContractRecord<TType, TAcceptSchema>;
24
26
  /** An array of record types that the contract can emit */
25
27
  emits: TEmits;
26
28
  /** (Optional) The description of the contract or its handler */
@@ -0,0 +1,15 @@
1
+ import ArvoEventFactory from ".";
2
+ import ArvoContract from "../ArvoContract";
3
+ import { z } from 'zod';
4
+ /**
5
+ * Creates a ArvoEventFactory instance based on the provided contract.
6
+ *
7
+ * @template TUri - The URI of the contract
8
+ * @template TType - The accept type, defaults to string.
9
+ * @template TAcceptSchema - The type of the data which the contract bound can accept
10
+ * @template TEmits - The type of records the contract bound handler emits.
11
+ *
12
+ * @param contract - The ArvoContract to base the events on.
13
+ * @returns An instance of ContractualArvoEventFactory.
14
+ */
15
+ export declare const createArvoEventFactory: <TUri extends string = string, TType extends string = string, TAcceptSchema extends z.ZodTypeAny = z.ZodTypeAny, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>>(contract: ArvoContract<TUri, TType, TAcceptSchema, TEmits>) => ArvoEventFactory<TUri, TType, TAcceptSchema, TEmits>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createArvoEventFactory = void 0;
7
+ var _1 = __importDefault(require("."));
8
+ /**
9
+ * Creates a ArvoEventFactory instance based on the provided contract.
10
+ *
11
+ * @template TUri - The URI of the contract
12
+ * @template TType - The accept type, defaults to string.
13
+ * @template TAcceptSchema - The type of the data which the contract bound can accept
14
+ * @template TEmits - The type of records the contract bound handler emits.
15
+ *
16
+ * @param contract - The ArvoContract to base the events on.
17
+ * @returns An instance of ContractualArvoEventFactory.
18
+ */
19
+ var createArvoEventFactory = function (contract) { return new _1.default(contract); };
20
+ exports.createArvoEventFactory = createArvoEventFactory;
@@ -0,0 +1,60 @@
1
+ import ArvoContract from "../ArvoContract";
2
+ import { z } from 'zod';
3
+ import { CreateArvoEvent } from "../ArvoEvent/types";
4
+ import { TelemetryContext } from "../OpenTelemetry/types";
5
+ /**
6
+ * A factory class for creating contractual ArvoEvents based on a given ArvoContract.
7
+ *
8
+ * @template TUri - The URI of the contract
9
+ * @template TType - The accept type, defaults to string.
10
+ * @template TAcceptSchema - The type of the data which the contract bound can accept
11
+ * @template TEmits - The type of records the contract bound handler emits.
12
+ */
13
+ export default class ArvoEventFactory<TUri extends string = string, TType extends string = string, TAcceptSchema extends z.ZodTypeAny = z.ZodTypeAny, TEmits extends Record<string, z.ZodTypeAny> = Record<string, z.ZodTypeAny>> {
14
+ private contract;
15
+ /**
16
+ * Creates an instance of ContractualArvoEventFactory.
17
+ *
18
+ * @param contract - The ArvoContract to base the events on.
19
+ */
20
+ constructor(contract: ArvoContract<TUri, TType, TAcceptSchema, TEmits>);
21
+ /**
22
+ * Creates an ArvoEvent as per the accept record of the contract.
23
+ *
24
+ * @template TExtension - The type of extensions to add to the event.
25
+ * @param event - The event to create.
26
+ * @param [extensions] - Optional extensions to add to the event.
27
+ * @param [telemetry] - Optional telemetry context for tracing.
28
+ * @returns The created ArvoEvent as per the accept record of the contract.
29
+ * @throws If the event data fails validation against the contract.
30
+ */
31
+ accepts<TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<TAcceptSchema>, TType>, extensions?: TExtension, telemetry?: TelemetryContext): import("..").ArvoEvent<z.TypeOf<TAcceptSchema>, TExtension, string>;
32
+ /**
33
+ * Creates an ArvoEvent as per one of the emits record in the contract.
34
+ *
35
+ * @template U - The specific event type from TEmits.
36
+ * @template TExtension - The type of extensions to add to the event.
37
+ * @param event - The event to create.
38
+ * @param [extensions] - Optional extensions to add to the event.
39
+ * @param [telemetry] - Optional telemetry context for tracing.
40
+ * @returns The created ArvoEvent as per one of the emits records of the contract.
41
+ * @throws If the event data fails validation against the contract.
42
+ */
43
+ emits<U extends keyof TEmits & string, TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<TEmits[U]>, U>, extensions?: TExtension, telemetry?: TelemetryContext): import("..").ArvoEvent<z.TypeOf<z.TypeOf<TEmits[U]>>, TExtension, string>;
44
+ /**
45
+ * Creates a system error ArvoEvent.
46
+ *
47
+ * @template TExtension - The type of extensions to add to the event.
48
+ * @param event - The event to create, including the error.
49
+ * @param [extensions] - Optional extensions to add to the event.
50
+ * @param [telemetry] - Optional telemetry context for tracing.
51
+ * @returns The created system error ArvoEvent.
52
+ */
53
+ systemError<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<any, any>, 'data' | 'type'> & {
54
+ error: Error;
55
+ }, extensions?: TExtension, telemetry?: TelemetryContext): import("..").ArvoEvent<{
56
+ errorName: string;
57
+ errorMessage: string;
58
+ errorStack: string | null;
59
+ }, TExtension, string>;
60
+ }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ var helpers_1 = require("../ArvoEvent/helpers");
26
+ var schema_1 = require("../ArvoEvent/schema");
27
+ var OpenTelemetry_1 = require("../OpenTelemetry");
28
+ /**
29
+ * A factory class for creating contractual ArvoEvents based on a given ArvoContract.
30
+ *
31
+ * @template TUri - The URI of the contract
32
+ * @template TType - The accept type, defaults to string.
33
+ * @template TAcceptSchema - The type of the data which the contract bound can accept
34
+ * @template TEmits - The type of records the contract bound handler emits.
35
+ */
36
+ var ArvoEventFactory = /** @class */ (function () {
37
+ /**
38
+ * Creates an instance of ContractualArvoEventFactory.
39
+ *
40
+ * @param contract - The ArvoContract to base the events on.
41
+ */
42
+ function ArvoEventFactory(contract) {
43
+ this.contract = contract;
44
+ }
45
+ /**
46
+ * Creates an ArvoEvent as per the accept record of the contract.
47
+ *
48
+ * @template TExtension - The type of extensions to add to the event.
49
+ * @param event - The event to create.
50
+ * @param [extensions] - Optional extensions to add to the event.
51
+ * @param [telemetry] - Optional telemetry context for tracing.
52
+ * @returns The created ArvoEvent as per the accept record of the contract.
53
+ * @throws If the event data fails validation against the contract.
54
+ */
55
+ ArvoEventFactory.prototype.accepts = function (event, extensions, telemetry) {
56
+ var _this = this;
57
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.accepts', {}, function () {
58
+ var validationResult = _this.contract.validateInput(event.type, event.data);
59
+ if (!validationResult.success) {
60
+ throw new Error("Accept Event data validation failed: ".concat(validationResult.error.message));
61
+ }
62
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetry);
63
+ });
64
+ };
65
+ /**
66
+ * Creates an ArvoEvent as per one of the emits record in the contract.
67
+ *
68
+ * @template U - The specific event type from TEmits.
69
+ * @template TExtension - The type of extensions to add to the event.
70
+ * @param event - The event to create.
71
+ * @param [extensions] - Optional extensions to add to the event.
72
+ * @param [telemetry] - Optional telemetry context for tracing.
73
+ * @returns The created ArvoEvent as per one of the emits records of the contract.
74
+ * @throws If the event data fails validation against the contract.
75
+ */
76
+ ArvoEventFactory.prototype.emits = function (event, extensions, telemetry) {
77
+ var _this = this;
78
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.emits', {}, function () {
79
+ var validationResult = _this.contract.validateOutput(event.type, event.data);
80
+ if (!validationResult.success) {
81
+ throw new Error("Emit Event data validation failed: ".concat(validationResult.error.message));
82
+ }
83
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri, data: validationResult.data }), extensions, telemetry);
84
+ });
85
+ };
86
+ /**
87
+ * Creates a system error ArvoEvent.
88
+ *
89
+ * @template TExtension - The type of extensions to add to the event.
90
+ * @param event - The event to create, including the error.
91
+ * @param [extensions] - Optional extensions to add to the event.
92
+ * @param [telemetry] - Optional telemetry context for tracing.
93
+ * @returns The created system error ArvoEvent.
94
+ */
95
+ ArvoEventFactory.prototype.systemError = function (event, extensions, telemetry) {
96
+ var _this = this;
97
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'ContractualArvoEventFactory.systemError', {}, function () {
98
+ var error = event.error, _events = __rest(event, ["error"]);
99
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { type: "sys.".concat(_this.contract.accepts.type, ".error"), data: {
100
+ errorName: error.name,
101
+ errorMessage: error.message,
102
+ errorStack: error.stack || null,
103
+ }, datacontenttype: schema_1.ArvoDataContentType, dataschema: _this.contract.uri }), extensions, telemetry);
104
+ });
105
+ };
106
+ return ArvoEventFactory;
107
+ }());
108
+ exports.default = ArvoEventFactory;
@@ -154,10 +154,11 @@ var createOtelSpan = function (telemetryContext, spanName, spanOptions, wrappedF
154
154
  var newSpan = activeTracer.startSpan(spanName, spanOptions, activeContext);
155
155
  try {
156
156
  var result = api_1.context.with(api_1.trace.setSpan(activeContext, newSpan), function () {
157
- return wrappedFunction.call.apply(wrappedFunction, __spreadArray([thisArg, {
157
+ return wrappedFunction.call.apply(wrappedFunction, __spreadArray([thisArg,
158
+ {
158
159
  span: newSpan,
159
160
  tracer: activeTracer,
160
- carrier: (0, exports.getTelemetryCarrier)(newSpan, activeContext)
161
+ carrier: (0, exports.getTelemetryCarrier)(newSpan, activeContext),
161
162
  }], args, false));
162
163
  });
163
164
  newSpan.setStatus({
package/dist/index.d.ts CHANGED
@@ -6,11 +6,14 @@ import { exceptionToSpan, logToSpan, getTelemetryContext, getTelemetryCarrier, c
6
6
  import { TelemetryCarrier, TelemetryContext, TelemetryLogLevel } from './OpenTelemetry/types';
7
7
  import { validateURI, cleanString } from './utils';
8
8
  import ArvoContract from './ArvoContract';
9
- import { createArvoContract, InferArvoContract, contractualArvoEventFactory } from './ArvoContract/helpers';
9
+ import { createArvoContract, InferArvoContract } from './ArvoContract/helpers';
10
10
  import { ArvoContractValidators } from './ArvoContract/validators';
11
11
  import { ArvoContractRecord, IArvoContract, ResolveArvoContractRecord } from './ArvoContract/types';
12
12
  import ArvoContractLibrary from './ArvoContractLibrary';
13
13
  import { createArvoContractLibrary } from './ArvoContractLibrary/helpers';
14
+ import ArvoEventFactory from './ArvoEventFactory';
15
+ import { createArvoEventFactory } from './ArvoEventFactory/helpers';
16
+ import { ArvoErrorSchema } from './schema';
14
17
  /**
15
18
  * Collection of Zod schemas for validating various aspects of Arvo events.
16
19
  * @property {z.ZodObject} CloudEventContextSchema - Schema for core CloudEvent properties.
@@ -77,4 +80,4 @@ declare const ArvoEventSchemas: {
77
80
  tracestate: string | null;
78
81
  }>;
79
82
  };
80
- export { ArvoEvent, createArvoEvent, ArvoDataContentType, ArvoEventData, CloudEventExtension, ArvoEventSchemas, CloudEventContext, ArvoExtension, OpenTelemetryExtension, CreateArvoEvent, exceptionToSpan, logToSpan, getTelemetryCarrier, getTelemetryContext, createOtelSpan, TelemetryCarrier, TelemetryContext, TelemetryLogLevel, OTelNull, validateURI, cleanString, ArvoContract, createArvoContract, ArvoContractValidators, ArvoContractRecord, InferArvoContract, IArvoContract, ResolveArvoContractRecord, ArvoContractLibrary, createArvoContractLibrary, contractualArvoEventFactory, };
83
+ export { ArvoEvent, createArvoEvent, ArvoDataContentType, ArvoEventData, CloudEventExtension, ArvoEventSchemas, CloudEventContext, ArvoExtension, OpenTelemetryExtension, CreateArvoEvent, exceptionToSpan, logToSpan, getTelemetryCarrier, getTelemetryContext, createOtelSpan, TelemetryCarrier, TelemetryContext, TelemetryLogLevel, OTelNull, validateURI, cleanString, ArvoContract, createArvoContract, ArvoContractValidators, ArvoContractRecord, InferArvoContract, IArvoContract, ResolveArvoContractRecord, ArvoContractLibrary, createArvoContractLibrary, ArvoEventFactory, createArvoEventFactory, ArvoErrorSchema };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.contractualArvoEventFactory = exports.createArvoContractLibrary = exports.ArvoContractLibrary = exports.ArvoContractValidators = exports.createArvoContract = exports.ArvoContract = exports.cleanString = exports.validateURI = exports.OTelNull = exports.createOtelSpan = exports.getTelemetryContext = exports.getTelemetryCarrier = exports.logToSpan = exports.exceptionToSpan = exports.ArvoEventSchemas = exports.ArvoDataContentType = exports.createArvoEvent = exports.ArvoEvent = void 0;
6
+ exports.ArvoErrorSchema = exports.createArvoEventFactory = exports.ArvoEventFactory = exports.createArvoContractLibrary = exports.ArvoContractLibrary = exports.ArvoContractValidators = exports.createArvoContract = exports.ArvoContract = exports.cleanString = exports.validateURI = exports.OTelNull = exports.createOtelSpan = exports.getTelemetryContext = exports.getTelemetryCarrier = exports.logToSpan = exports.exceptionToSpan = exports.ArvoEventSchemas = exports.ArvoDataContentType = exports.createArvoEvent = exports.ArvoEvent = void 0;
7
7
  var ArvoEvent_1 = __importDefault(require("./ArvoEvent"));
8
8
  exports.ArvoEvent = ArvoEvent_1.default;
9
9
  var schema_1 = require("./ArvoEvent/schema");
@@ -24,13 +24,18 @@ var ArvoContract_1 = __importDefault(require("./ArvoContract"));
24
24
  exports.ArvoContract = ArvoContract_1.default;
25
25
  var helpers_2 = require("./ArvoContract/helpers");
26
26
  Object.defineProperty(exports, "createArvoContract", { enumerable: true, get: function () { return helpers_2.createArvoContract; } });
27
- Object.defineProperty(exports, "contractualArvoEventFactory", { enumerable: true, get: function () { return helpers_2.contractualArvoEventFactory; } });
28
27
  var validators_1 = require("./ArvoContract/validators");
29
28
  Object.defineProperty(exports, "ArvoContractValidators", { enumerable: true, get: function () { return validators_1.ArvoContractValidators; } });
30
29
  var ArvoContractLibrary_1 = __importDefault(require("./ArvoContractLibrary"));
31
30
  exports.ArvoContractLibrary = ArvoContractLibrary_1.default;
32
31
  var helpers_3 = require("./ArvoContractLibrary/helpers");
33
32
  Object.defineProperty(exports, "createArvoContractLibrary", { enumerable: true, get: function () { return helpers_3.createArvoContractLibrary; } });
33
+ var ArvoEventFactory_1 = __importDefault(require("./ArvoEventFactory"));
34
+ exports.ArvoEventFactory = ArvoEventFactory_1.default;
35
+ var helpers_4 = require("./ArvoEventFactory/helpers");
36
+ Object.defineProperty(exports, "createArvoEventFactory", { enumerable: true, get: function () { return helpers_4.createArvoEventFactory; } });
37
+ var schema_2 = require("./schema");
38
+ Object.defineProperty(exports, "ArvoErrorSchema", { enumerable: true, get: function () { return schema_2.ArvoErrorSchema; } });
34
39
  /**
35
40
  * Collection of Zod schemas for validating various aspects of Arvo events.
36
41
  * @property {z.ZodObject} CloudEventContextSchema - Schema for core CloudEvent properties.
@@ -0,0 +1,20 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Schema for Arvo error objects.
4
+ *
5
+ * This schema defines the structure of error objects used in the Arvo system.
6
+ * It uses Zod for runtime type checking and validation.
7
+ */
8
+ export declare const ArvoErrorSchema: z.ZodObject<{
9
+ errorName: z.ZodString;
10
+ errorMessage: z.ZodString;
11
+ errorStack: z.ZodNullable<z.ZodString>;
12
+ }, "strip", z.ZodTypeAny, {
13
+ errorName: string;
14
+ errorMessage: string;
15
+ errorStack: string | null;
16
+ }, {
17
+ errorName: string;
18
+ errorMessage: string;
19
+ errorStack: string | null;
20
+ }>;
package/dist/schema.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArvoErrorSchema = void 0;
4
+ var zod_1 = require("zod");
5
+ /**
6
+ * Schema for Arvo error objects.
7
+ *
8
+ * This schema defines the structure of error objects used in the Arvo system.
9
+ * It uses Zod for runtime type checking and validation.
10
+ */
11
+ exports.ArvoErrorSchema = zod_1.z.object({
12
+ errorName: zod_1.z.string().describe('The name of the error.'),
13
+ errorMessage: zod_1.z.string().describe('A descriptive message for the error.'),
14
+ errorStack: zod_1.z.string().nullable().describe('The stack trace of the error.'),
15
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
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": {