arvo-core 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -12,3 +12,7 @@
12
12
 
13
13
  - Making type in ArvoEvent a generic string for better type control
14
14
 
15
+ ## [0.0.4] - 2024-08-31
16
+
17
+ - Created an event factory generator for creating contractually compliant events
18
+
package/README.md CHANGED
@@ -1,5 +1,3 @@
1
-
2
-
3
1
  # Arvo
4
2
 
5
3
  ## What is Arvo
@@ -28,13 +26,11 @@ This core package defines primitive types and utility functions to help you quic
28
26
 
29
27
  ## Documentation & Resources
30
28
 
31
-
32
- | Source | Link |
33
- | --------------- | ------------------------
34
- | Package | https://www.npmjs.com/package/arvo-core?activeTab=readme |
35
- | Github | https://github.com/SaadAhmad123/arvo-core |
36
- | Documenation | https://saadahmad123.github.io/arvo-core/index.html |
37
-
29
+ | Source | Link |
30
+ | ------------ | -------------------------------------------------------- |
31
+ | Package | https://www.npmjs.com/package/arvo-core?activeTab=readme |
32
+ | Github | https://github.com/SaadAhmad123/arvo-core |
33
+ | Documenation | https://saadahmad123.github.io/arvo-core/index.html |
38
34
 
39
35
  ## Installation
40
36
 
@@ -43,11 +39,11 @@ You can install the core package via `npm` or `yarn`
43
39
  ```bash
44
40
  npm install arvo-core
45
41
  ```
42
+
46
43
  ```bash
47
44
  yarn add arvo-core
48
45
  ```
49
46
 
50
-
51
47
  ## Components
52
48
 
53
49
  At its core, Arvo has only three main data structures:
@@ -70,9 +66,15 @@ To start using Arvo in your project:
70
66
 
71
67
  - Install the package as shown in the Installation section.
72
68
  - Import the necessary components:
69
+
73
70
  ```javascript
74
- import { createArvoEvent, createArvoContract, createArvoContractLibrary } from 'arvo-core';
71
+ import {
72
+ createArvoEvent,
73
+ createArvoContract,
74
+ createArvoContractLibrary,
75
+ } from 'arvo-core';
75
76
  ```
77
+
76
78
  - Begin defining your events and contracts using the provided classes.
77
79
 
78
80
  ## License
@@ -80,4 +82,5 @@ import { createArvoEvent, createArvoContract, createArvoContractLibrary } from '
80
82
  This package is available under the MIT License. For more details, refer to the [LICENSE.md](LICENSE.md) file in the project repository.
81
83
 
82
84
  ## Change Logs
83
- For a detailed list of changes and updates, please refer to the [document](CHANGELOG.md) file.
85
+
86
+ For a detailed list of changes and updates, please refer to the [document](CHANGELOG.md) file.
@@ -1,5 +1,9 @@
1
1
  import { IArvoContract } from './types';
2
- import ArvoContract from '.';
2
+ import ArvoContract, { ExtractEventType } from '.';
3
+ import { CreateArvoEvent } from '../ArvoEvent/types';
4
+ import { TelemetryContext } from '../OpenTelemetry/types';
5
+ import { ArvoContractRecord } from './types';
6
+ import { z } from 'zod';
3
7
  /**
4
8
  * Infers the ArvoContract type from a given IArvoContract.
5
9
  */
@@ -33,3 +37,41 @@ export type InferArvoContract<T> = T extends IArvoContract<infer U, infer V, inf
33
37
  * });
34
38
  */
35
39
  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 events the contract accepts.
45
+ * @template TEmits - The type of events the contract 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 createContractualArvoEvent: <T extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends ArvoContractRecord = ArvoContractRecord>(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 ExtractEventType<TEmits>, TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<Extract<TEmits, {
73
+ type: U;
74
+ }>["schema"]>, U>, extensions?: TExtension, telemetry?: TelemetryContext) => import("..").ArvoEvent<z.TypeOf<Extract<TEmits, {
75
+ type: U;
76
+ }>["schema"]>, TExtension, string>;
77
+ };
@@ -1,10 +1,23 @@
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
+ };
2
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
15
  };
5
16
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createArvoContract = void 0;
17
+ exports.createContractualArvoEvent = exports.createArvoContract = void 0;
7
18
  var _1 = __importDefault(require("."));
19
+ var helpers_1 = require("../ArvoEvent/helpers");
20
+ var OpenTelemetry_1 = require("../OpenTelemetry");
8
21
  /**
9
22
  * Creates an ArvoContract instance from the given contract specification.
10
23
  *
@@ -37,3 +50,54 @@ var createArvoContract = function (contract) {
37
50
  return new _1.default(contract);
38
51
  };
39
52
  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 events the contract accepts.
58
+ * @template TEmits - The type of events the contract 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 createContractualArvoEvent = 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', 'createArvoContractEvent.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', 'createArvoContractEvent.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.createContractualArvoEvent = createContractualArvoEvent;
@@ -4,7 +4,7 @@ import { ArvoContractRecord, IArvoContract } from './types';
4
4
  * Extracts the event type from a given type T.
5
5
  * @template T - The type to extract the event type from.
6
6
  */
7
- type ExtractEventType<T> = T extends {
7
+ export type ExtractEventType<T> = T extends {
8
8
  type: infer U;
9
9
  } ? U : never;
10
10
  /**
@@ -128,4 +128,3 @@ export default class ArvoContract<T extends string = string, TAccepts extends Ar
128
128
  }[];
129
129
  };
130
130
  }
131
- export {};
@@ -47,7 +47,7 @@ var uuid_1 = require("uuid");
47
47
  * );
48
48
  */
49
49
  var createArvoEvent = function (event, extensions, telemetry) {
50
- return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'Create ArvoEvent', {}, function (span) {
50
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'createArvoEvent', {}, function (span) {
51
51
  if (event.datacontenttype &&
52
52
  event.datacontenttype !== schema_1.ArvoDataContentType) {
53
53
  var warning = (0, utils_1.cleanString)("\n Warning! The provided datacontenttype(=".concat(event.datacontenttype, ")\n is not ArvoEvent compatible (=").concat(schema_1.ArvoDataContentType, "). There may \n be some limited functionality.\n "));
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ 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 } from './ArvoContract/helpers';
9
+ import { createArvoContract, InferArvoContract, createContractualArvoEvent } 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';
@@ -77,4 +77,4 @@ declare const ArvoEventSchemas: {
77
77
  tracestate: string | null;
78
78
  }>;
79
79
  };
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, };
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, createContractualArvoEvent, };
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.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.createContractualArvoEvent = 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,6 +24,7 @@ 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, "createContractualArvoEvent", { enumerable: true, get: function () { return helpers_2.createContractualArvoEvent; } });
27
28
  var validators_1 = require("./ArvoContract/validators");
28
29
  Object.defineProperty(exports, "ArvoContractValidators", { enumerable: true, get: function () { return validators_1.ArvoContractValidators; } });
29
30
  var ArvoContractLibrary_1 = __importDefault(require("./ArvoContractLibrary"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
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": {