arvo-core 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## [1.0.0] - 2024-08-31
4
+
5
+ - Finalised the first release version of the core. This contains core class such as ArvoEvent, ArvoContract, and ArvoContractLibrary. Moreover, this release contains utility functions for string manipulation, validation and OpenTelemetry
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2024 Saad Ahmad
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ ---
2
+ title: ArvoEvent
3
+ group: Guides
4
+ ---
5
+
6
+ # Arvo
7
+
8
+ ## What is Arvo
9
+
10
+ Arvo is an opinionated approach to building event-driven systems. It's designed as a pattern and methodology rather than a rigid framework.
11
+
12
+ ## Principal
13
+
14
+ The core principle of Arvo is to provide a solid foundation with enough flexibility for customization, allowing you to impose your own technical posture, including security measures, event brokerage, and telemetry. While Arvo offers a structured approach, it encourages developers to implement their own solutions if they believe they can improve upon or diverge from Arvo's principles.
15
+
16
+ If you're looking to focus on results without getting bogged down in the nitty-gritty of event creation, handling, system state management, and telemetry, while also avoiding vendor lock-in, Arvo provides an excellent starting point. I believe, it strikes a balance between opinionated design and customization, making it an ideal choice for developers who want a head start in building event-driven systems without sacrificing flexibility.
17
+
18
+ Key features of Arvo include:
19
+
20
+ - Lightweight and unopinionated core
21
+ - Extensible architecture
22
+ - Cloud-agnostic design
23
+ - Built-in primitives for event-driven patterns
24
+ - Easy integration with existing systems and tools
25
+
26
+ Whether you're building a small microservice or a large-scale distributed system, my hope with Arvo is to offers you some of the tools and patterns to help you succeed in the world of event-driven architecture.
27
+
28
+ # Arvo - Core
29
+
30
+ This core package defines primitive types and utility functions to help you quickly start building interesting and robust event-driven applications.
31
+
32
+ At its core, Arvo has only two main data structures:
33
+
34
+ ## Components
35
+
36
+ - [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.
37
+ - [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.
38
+ - [ArvoContractLibrary](src/ArvoContractLibrary/README.md) is a utility class designed to manage and access multiple ArvoContract instances efficiently.
39
+
40
+ ## Utilities
41
+
42
+ The package also includes utility functions for:
43
+
44
+ - Creating ArvoEvents
45
+ - Integrating with OpenTelemetry
46
+ - TypeScript types for core components
@@ -0,0 +1,15 @@
1
+ import { IArvoContract } from './types';
2
+ import ArvoContract from '.';
3
+ import { TelemetryContext } from '../OpenTelemetry/types';
4
+ /**
5
+ * Infers the ArvoContract type from a given IArvoContract.
6
+ */
7
+ export type InferArvoContract<T> = T extends IArvoContract<infer U, infer V, infer W> ? ArvoContract<U, V, W> : never;
8
+ /**
9
+ * Creates an ArvoContract instance from the given contract specification.
10
+ * @template TContract - The type of the contract specification.
11
+ * @param {TContract} contractSpec - The contract specification.
12
+ * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
13
+ * @returns {InferArvoContract<TContract>} The created ArvoContract instance.
14
+ */
15
+ export declare const createArvoContract: <const TContract extends IArvoContract>(contract: TContract, telemetry?: TelemetryContext) => InferArvoContract<TContract>;
@@ -0,0 +1,19 @@
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.createArvoContract = void 0;
7
+ var _1 = __importDefault(require("."));
8
+ var OpenTelemetry_1 = require("../OpenTelemetry");
9
+ /**
10
+ * Creates an ArvoContract instance from the given contract specification.
11
+ * @template TContract - The type of the contract specification.
12
+ * @param {TContract} contractSpec - The contract specification.
13
+ * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
14
+ * @returns {InferArvoContract<TContract>} The created ArvoContract instance.
15
+ */
16
+ var createArvoContract = function (contract, telemetry) {
17
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'Arvo Contract Creator', 'Create ArvoContract', {}, function () { return new _1.default(contract); });
18
+ };
19
+ exports.createArvoContract = createArvoContract;
@@ -0,0 +1,80 @@
1
+ import { z } from 'zod';
2
+ import { ArvoContractRecord, IArvoContract } from './types';
3
+ /**
4
+ * Extracts the event type from a given type T.
5
+ * @template T - The type to extract the event type from.
6
+ */
7
+ type ExtractEventType<T> = T extends {
8
+ type: infer U;
9
+ } ? U : never;
10
+ /**
11
+ * ArvoContract class represents a contract with defined input and output schemas.
12
+ * It provides methods for validating inputs and outputs based on the contract's specifications.
13
+ *
14
+ * @template T - The URI type.
15
+ * @template TAccepts - The accepted record type.
16
+ * @template TEmits - The emitted record type.
17
+ */
18
+ export default class ArvoContract<T extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends ArvoContractRecord = ArvoContractRecord> {
19
+ private readonly _uri;
20
+ private readonly _accepts;
21
+ private readonly _emits;
22
+ /**
23
+ * Creates an instance of ArvoContract.
24
+ * @param {IArvoContract<T, TAccepts, TEmits>} params - The contract parameters.
25
+ */
26
+ constructor(params: IArvoContract<T, TAccepts, TEmits>);
27
+ get uri(): T;
28
+ get accepts(): Readonly<TAccepts>;
29
+ /**
30
+ * Gets all emitted event types and schemas as a readonly record.
31
+ * Use this when you need to access all emitted events at once.
32
+ */
33
+ get emits(): Readonly<Record<ExtractEventType<TEmits>, TEmits>>;
34
+ /**
35
+ * Gets a specific emitted event type and schema.
36
+ * Use this when you need to access a single emitted event by its type.
37
+ * @template U - The type of the emit record to retrieve.
38
+ * @param {U} type - The type of the emit record.
39
+ * @returns {Readonly<Extract<TEmits, { type: U }>>} The emit record.
40
+ * @throws {Error} If the emit type is not found in the contract.
41
+ */
42
+ getEmit<U extends ExtractEventType<TEmits>>(type: U): Readonly<Extract<TEmits, {
43
+ type: U;
44
+ }>>;
45
+ /**
46
+ * Validates the input against the contract's accept schema.
47
+ * @template U - The type of the input to validate.
48
+ * @param {TAccepts['type']} type - The type of the input.
49
+ * @param {U} input - The input to validate.
50
+ * @returns {z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>} The validation result.
51
+ * @throws {Error} If the accept type is not found in the contract.
52
+ */
53
+ validateInput<U>(type: TAccepts['type'], input: U): z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>;
54
+ /**
55
+ * Validates the output against the contract's emit schema.
56
+ * @template U - The type of the output to validate.
57
+ * @param {U} type - The type of the output.
58
+ * @param {unknown} output - The output to validate.
59
+ * @returns {z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, { type: U }>['schema']>>} The validation result.
60
+ * @throws {Error} If the emit type is not found in the contract.
61
+ */
62
+ validateOutput<U extends ExtractEventType<TEmits>>(type: U, output: unknown): z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, {
63
+ type: U;
64
+ }>['schema']>>;
65
+ /**
66
+ * Validates the accepts record.
67
+ * @param {TAccepts} accepts - The accepts record to validate.
68
+ * @returns {TAccepts} The validated accepts record.
69
+ * @private
70
+ */
71
+ private validateAccepts;
72
+ /**
73
+ * Validates the emits records.
74
+ * @param {TEmits[]} emits - The emits records to validate.
75
+ * @returns {ReadonlyArray<TEmits>} The validated emits records.
76
+ * @private
77
+ */
78
+ private validateEmits;
79
+ }
80
+ export {};
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var validators_1 = require("./validators");
4
+ /**
5
+ * ArvoContract class represents a contract with defined input and output schemas.
6
+ * It provides methods for validating inputs and outputs based on the contract's specifications.
7
+ *
8
+ * @template T - The URI type.
9
+ * @template TAccepts - The accepted record type.
10
+ * @template TEmits - The emitted record type.
11
+ */
12
+ var ArvoContract = /** @class */ (function () {
13
+ /**
14
+ * Creates an instance of ArvoContract.
15
+ * @param {IArvoContract<T, TAccepts, TEmits>} params - The contract parameters.
16
+ */
17
+ function ArvoContract(params) {
18
+ this._uri = validators_1.ArvoContractValidators.contract.uri.parse(params.uri);
19
+ this._accepts = this.validateAccepts(params.accepts);
20
+ this._emits = this.validateEmits(params.emits);
21
+ Object.freeze(this);
22
+ }
23
+ Object.defineProperty(ArvoContract.prototype, "uri", {
24
+ get: function () {
25
+ return this._uri;
26
+ },
27
+ enumerable: false,
28
+ configurable: true
29
+ });
30
+ Object.defineProperty(ArvoContract.prototype, "accepts", {
31
+ get: function () {
32
+ return this._accepts;
33
+ },
34
+ enumerable: false,
35
+ configurable: true
36
+ });
37
+ Object.defineProperty(ArvoContract.prototype, "emits", {
38
+ /**
39
+ * Gets all emitted event types and schemas as a readonly record.
40
+ * Use this when you need to access all emitted events at once.
41
+ */
42
+ get: function () {
43
+ return Object.freeze(this._emits.reduce(function (acc, emit) {
44
+ // @ts-ignore
45
+ acc[emit.type] = emit;
46
+ return acc;
47
+ }, {}));
48
+ },
49
+ enumerable: false,
50
+ configurable: true
51
+ });
52
+ /**
53
+ * Gets a specific emitted event type and schema.
54
+ * Use this when you need to access a single emitted event by its type.
55
+ * @template U - The type of the emit record to retrieve.
56
+ * @param {U} type - The type of the emit record.
57
+ * @returns {Readonly<Extract<TEmits, { type: U }>>} The emit record.
58
+ * @throws {Error} If the emit type is not found in the contract.
59
+ */
60
+ ArvoContract.prototype.getEmit = function (type) {
61
+ var emit = this._emits.find(function (item) { return item.type === type; });
62
+ if (!emit) {
63
+ throw new Error("Emit type \"".concat(type, "\" not found in contract"));
64
+ }
65
+ return emit;
66
+ };
67
+ /**
68
+ * Validates the input against the contract's accept schema.
69
+ * @template U - The type of the input to validate.
70
+ * @param {TAccepts['type']} type - The type of the input.
71
+ * @param {U} input - The input to validate.
72
+ * @returns {z.SafeParseReturnType<U, z.infer<TAccepts['schema']>>} The validation result.
73
+ * @throws {Error} If the accept type is not found in the contract.
74
+ */
75
+ ArvoContract.prototype.validateInput = function (type, input) {
76
+ if (type !== this._accepts.type) {
77
+ throw new Error("Accept type \"".concat(type, "\" not found in contract"));
78
+ }
79
+ return this._accepts.schema.safeParse(input);
80
+ };
81
+ /**
82
+ * Validates the output against the contract's emit schema.
83
+ * @template U - The type of the output to validate.
84
+ * @param {U} type - The type of the output.
85
+ * @param {unknown} output - The output to validate.
86
+ * @returns {z.SafeParseReturnType<unknown, z.infer<Extract<TEmits, { type: U }>['schema']>>} The validation result.
87
+ * @throws {Error} If the emit type is not found in the contract.
88
+ */
89
+ ArvoContract.prototype.validateOutput = function (type, output) {
90
+ var emit = this.emits[type];
91
+ if (!emit) {
92
+ throw new Error("Emit type \"".concat(type, "\" not found in contract"));
93
+ }
94
+ return emit.schema.safeParse(output);
95
+ };
96
+ /**
97
+ * Validates the accepts record.
98
+ * @param {TAccepts} accepts - The accepts record to validate.
99
+ * @returns {TAccepts} The validated accepts record.
100
+ * @private
101
+ */
102
+ ArvoContract.prototype.validateAccepts = function (accepts) {
103
+ return {
104
+ type: validators_1.ArvoContractValidators.record.type.parse(accepts.type),
105
+ schema: accepts.schema,
106
+ };
107
+ };
108
+ /**
109
+ * Validates the emits records.
110
+ * @param {TEmits[]} emits - The emits records to validate.
111
+ * @returns {ReadonlyArray<TEmits>} The validated emits records.
112
+ * @private
113
+ */
114
+ ArvoContract.prototype.validateEmits = function (emits) {
115
+ return Object.freeze(emits.map(function (item) { return ({
116
+ type: validators_1.ArvoContractValidators.record.type.parse(item.type),
117
+ schema: item.schema,
118
+ }); }));
119
+ };
120
+ return ArvoContract;
121
+ }());
122
+ exports.default = ArvoContract;
@@ -0,0 +1,31 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Represents a record in an Arvo contract.
4
+ * @template TType - The type of the record, defaults to string.
5
+ * @template TSchema - The Zod schema for the record, defaults to any Zod type.
6
+ */
7
+ export type ArvoContractRecord<TType extends string = string, TSchema extends z.ZodTypeAny = z.ZodTypeAny> = {
8
+ /** The type identifier for the record */
9
+ type: TType;
10
+ /** The Zod schema used for validating the record */
11
+ schema: TSchema;
12
+ };
13
+ /**
14
+ * Interface for an Arvo contract.
15
+ * @template TUri - The URI type, defaults to string.
16
+ * @template TAccepts - The type of record the contract accepts, defaults to ArvoContractRecord.
17
+ * @template TEmits - The type of record the contract emits, defaults to ArvoContractRecord.
18
+ */
19
+ export interface IArvoContract<TUri extends string = string, TAccepts extends ArvoContractRecord = ArvoContractRecord, TEmits extends ArvoContractRecord = ArvoContractRecord> {
20
+ /** The unique identifier for the contract */
21
+ uri: TUri;
22
+ /** The record type that the contract accepts */
23
+ accepts: TAccepts;
24
+ /** An array of record types that the contract can emit */
25
+ emits: TEmits[];
26
+ }
27
+ /**
28
+ * Resolves the inferred type of an ArvoContractRecord's schema.
29
+ * @template T - The ArvoContractRecord to resolve.
30
+ */
31
+ export type ResolveArvoContractRecord<T extends ArvoContractRecord> = z.infer<T['schema']>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ import { z } from 'zod';
2
+ export declare const ArvoContractValidators: {
3
+ contract: {
4
+ uri: z.ZodEffects<z.ZodString, string, string>;
5
+ };
6
+ record: {
7
+ type: z.ZodString;
8
+ };
9
+ };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArvoContractValidators = void 0;
4
+ var zod_1 = require("zod");
5
+ var utils_1 = require("../utils");
6
+ exports.ArvoContractValidators = {
7
+ contract: {
8
+ uri: zod_1.z
9
+ .string()
10
+ .min(1, 'URI must be a non-empty string')
11
+ .refine(utils_1.validateURI, 'The URI must a valid URI string')
12
+ .describe((0, utils_1.cleanString)("\n The addressable unique URI of the contract so that\n it can be located and address for reference during\n operations\n ")),
13
+ },
14
+ record: {
15
+ type: zod_1.z
16
+ .string()
17
+ .min(1, 'Type must be a non-empty string')
18
+ .regex(/^[a-z0-9]+(\.[a-z0-9]+)+\.[a-z0-9]+$/, 'Type should be prefixed with a reverse-DNS name')
19
+ .describe((0, utils_1.cleanString)("\n The type of event related to the originating occurrence.\n Should be prefixed with a reverse-DNS name.\n ")),
20
+ },
21
+ };
@@ -0,0 +1,40 @@
1
+ import ArvoEvent from '.';
2
+ import { TelemetryContext } from '../OpenTelemetry/types';
3
+ import { ArvoEventData, CloudEventExtension, CreateArvoEvent } from './types';
4
+ /**
5
+ * Creates an ArvoEvent with the provided data and extensions.
6
+ *
7
+ * This function creates a new ArvoEvent instance using the provided event data and optional extensions.
8
+ * It also supports OpenTelemetry tracing if a telemetry context is provided.
9
+ *
10
+ * @template TData - The type of the event data, extending ArvoEventData.
11
+ * @template TExtension - The type of the cloud event extension, extending CloudEventExtension.
12
+ *
13
+ * @param {CreateArvoEvent<TData>} event - The event data and metadata to create the ArvoEvent.
14
+ * @param {TExtension} [extensions] - Optional cloud event extensions.
15
+ * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
16
+ *
17
+ * @returns {ArvoEvent<TData, TExtension>} The created ArvoEvent instance.
18
+ *
19
+ * @throws {Error} If there's an error during the creation process.
20
+ *
21
+ * @remarks
22
+ * - If no `id` is provided in the event object, a UUID v4 will be generated.
23
+ * - If no `time` is provided, the current timestamp will be used.
24
+ * - If no `datacontenttype` is provided, it defaults to `application/cloudevents+json;charset=UTF-8;profile=arvo`.
25
+ * - If a non-compatible `datacontenttype` is provided, a warning will be logged to the span.
26
+ * - The `source`, `subject`, `to`, `redirectto`, and `dataschema` fields are URI-encoded.
27
+ *
28
+ * @example
29
+ * const event = createArvoEvent(
30
+ * {
31
+ * type: 'com.example.event',
32
+ * source: '/example/source',
33
+ * subject: 'example-subject',
34
+ * data: { key: 'value' }
35
+ * },
36
+ * { customExtension: 'value' },
37
+ * telemetryContext
38
+ * );
39
+ */
40
+ export declare const createArvoEvent: <TData extends ArvoEventData, TExtension extends CloudEventExtension>(event: CreateArvoEvent<TData>, extensions?: TExtension, telemetry?: TelemetryContext) => ArvoEvent<TData, TExtension>;
@@ -0,0 +1,76 @@
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.createArvoEvent = void 0;
7
+ var _1 = __importDefault(require("."));
8
+ var OpenTelemetry_1 = require("../OpenTelemetry");
9
+ var utils_1 = require("../utils");
10
+ var schema_1 = require("./schema");
11
+ var uuid_1 = require("uuid");
12
+ /**
13
+ * Creates an ArvoEvent with the provided data and extensions.
14
+ *
15
+ * This function creates a new ArvoEvent instance using the provided event data and optional extensions.
16
+ * It also supports OpenTelemetry tracing if a telemetry context is provided.
17
+ *
18
+ * @template TData - The type of the event data, extending ArvoEventData.
19
+ * @template TExtension - The type of the cloud event extension, extending CloudEventExtension.
20
+ *
21
+ * @param {CreateArvoEvent<TData>} event - The event data and metadata to create the ArvoEvent.
22
+ * @param {TExtension} [extensions] - Optional cloud event extensions.
23
+ * @param {TelemetryContext} [telemetry] - Optional telemetry context for tracing.
24
+ *
25
+ * @returns {ArvoEvent<TData, TExtension>} The created ArvoEvent instance.
26
+ *
27
+ * @throws {Error} If there's an error during the creation process.
28
+ *
29
+ * @remarks
30
+ * - If no `id` is provided in the event object, a UUID v4 will be generated.
31
+ * - If no `time` is provided, the current timestamp will be used.
32
+ * - If no `datacontenttype` is provided, it defaults to `application/cloudevents+json;charset=UTF-8;profile=arvo`.
33
+ * - If a non-compatible `datacontenttype` is provided, a warning will be logged to the span.
34
+ * - The `source`, `subject`, `to`, `redirectto`, and `dataschema` fields are URI-encoded.
35
+ *
36
+ * @example
37
+ * const event = createArvoEvent(
38
+ * {
39
+ * type: 'com.example.event',
40
+ * source: '/example/source',
41
+ * subject: 'example-subject',
42
+ * data: { key: 'value' }
43
+ * },
44
+ * { customExtension: 'value' },
45
+ * telemetryContext
46
+ * );
47
+ */
48
+ var createArvoEvent = function (event, extensions, telemetry) {
49
+ return (0, OpenTelemetry_1.createOtelSpan)(telemetry || 'ArvoEvent Creation Tracer', 'Create ArvoEvent', {}, function (span) {
50
+ if (event.datacontenttype &&
51
+ event.datacontenttype !== schema_1.ArvoDataContentType) {
52
+ 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 "));
53
+ (0, OpenTelemetry_1.logToSpan)(span, {
54
+ level: 'WARNING',
55
+ message: warning,
56
+ });
57
+ }
58
+ return new _1.default({
59
+ id: event.id || (0, uuid_1.v4)(),
60
+ type: event.type,
61
+ accesscontrol: event.accesscontrol || null,
62
+ executionunits: event.executionunits || null,
63
+ traceparent: event.traceparent || null,
64
+ tracestate: event.tracestate || null,
65
+ datacontenttype: event.datacontenttype || schema_1.ArvoDataContentType,
66
+ specversion: event.specversion || '1.0',
67
+ time: event.time || (0, utils_1.createTimestamp)(),
68
+ source: encodeURI(event.source),
69
+ subject: encodeURI(event.subject),
70
+ to: event.to ? encodeURI(event.to) : null,
71
+ redirectto: event.redirectto ? encodeURI(event.redirectto) : null,
72
+ dataschema: event.dataschema ? encodeURI(event.dataschema) : null,
73
+ }, event.data, extensions);
74
+ });
75
+ };
76
+ exports.createArvoEvent = createArvoEvent;
@@ -0,0 +1,88 @@
1
+ import { ArvoEventData, ArvoExtension, CloudEventContext, CloudEventExtension, OpenTelemetryExtension } from './types';
2
+ /**
3
+ * Represents an ArvoEvent, which extends the CloudEvent specification.
4
+ * @template TData - The type of the event data, extending ArvoEventData.
5
+ * @template TExtension - The type of additional extensions, extending CloudEventExtension.
6
+ */
7
+ export default class ArvoEvent<TData extends ArvoEventData = ArvoEventData, TExtension extends CloudEventExtension = CloudEventExtension> {
8
+ readonly id: string;
9
+ readonly source: string;
10
+ readonly specversion: string;
11
+ readonly type: string;
12
+ readonly subject: string;
13
+ readonly datacontenttype: string;
14
+ readonly dataschema: string | null;
15
+ readonly data: TData;
16
+ readonly time: string;
17
+ extensions: TExtension & ArvoExtension & OpenTelemetryExtension;
18
+ /**
19
+ * Creates an instance of ArvoEvent.
20
+ * @param context - The CloudEvent context along with Arvo and OpenTelemetry extensions.
21
+ * @param data - The event data.
22
+ * @param extensions - Optional additional extensions.
23
+ */
24
+ constructor(context: CloudEventContext & ArvoExtension & OpenTelemetryExtension, data: TData, extensions?: TExtension);
25
+ /**
26
+ * Gets the CloudEvent-specified default fields and extensions.
27
+ * @returns An object containing the base CloudEvent default fields and extensions.
28
+ * `default` fields are the standard CloudEvents attributes, which include:
29
+ * `extension` fields are additional attributes that provide extra context or functionality:
30
+ * - ArvoExtension: Arvo-specific extensions (to, accesscontrol, redirectto, executionunits)
31
+ * - OpenTelemetryExtension: OpenTelemetry-specific extensions (traceparent, tracestate)
32
+ * - TExtension: Any additional custom extensions
33
+ */
34
+ get cloudevent(): {
35
+ default: {
36
+ id: string;
37
+ source: string;
38
+ specversion: string;
39
+ type: string;
40
+ subject: string;
41
+ datacontenttype: string;
42
+ dataschema: string | null;
43
+ data: TData;
44
+ time: string;
45
+ };
46
+ extensions: TExtension & {
47
+ to: string | null;
48
+ accesscontrol: string | null;
49
+ redirectto: string | null;
50
+ executionunits: number | null;
51
+ } & {
52
+ traceparent: string | null;
53
+ tracestate: string | null;
54
+ };
55
+ };
56
+ /**
57
+ * Converts the ArvoEvent to a JSON-serializable object.
58
+ * @returns A plain object representation of the ArvoEvent.
59
+ */
60
+ toJSON(): Record<string, any>;
61
+ /**
62
+ * Converts the ArvoEvent to a JSON string.
63
+ * @param [spacing=0] - The number of spaces to use for indentation in the resulting JSON string.
64
+ * @returns A JSON string representation of the ArvoEvent.
65
+ */
66
+ toString(spacing?: number): string;
67
+ /**
68
+ * Gets OpenTelemetry attributes derived from the ArvoEvent.
69
+ * @returns An object containing OpenTelemetry attributes.
70
+ * The OpenTelemetry attributes for CloudEvents is as per
71
+ * the spec provided [here](https://opentelemetry.io/docs/specs/semconv/attributes-registry/cloudevents/)
72
+ * Additionally, the Arvo extension attributed are also returned
73
+ * as `cloudevents.arvo.event_*` fields
74
+ */
75
+ get otelAttributes(): {
76
+ 'cloudevents.event_id': string;
77
+ 'cloudevents.event_source': string;
78
+ 'cloudevents.event_spec_version': string;
79
+ 'cloudevents.event_subject': string;
80
+ 'cloudevents.event_type': string;
81
+ 'cloudevents.event_time': string;
82
+ 'cloudevents.event_datacontenttype': string;
83
+ 'cloudevents.event_dataschema': string;
84
+ 'cloudevents.arvo.event_redirectto': string;
85
+ 'cloudevents.arvo.event_to': string;
86
+ 'cloudevents.arvo.event_executionunits': string | number;
87
+ };
88
+ }