arvo-event-handler 0.0.1 → 0.0.4

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.
package/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
3
3
  ## [0.0.1] - 2024-09-07
4
4
 
5
5
  - Finalised version 0.0.1 of the event handlers for Arvo
6
+ ## [0.0.4] - 2024-09-09
7
+
8
+ - Finalised Event Handler implementation
9
+
@@ -44,7 +44,7 @@ export default class ArvoEventHandler<TContract extends ArvoContract> {
44
44
  * Executes the event handler for a given event.
45
45
  *
46
46
  * @param event - The event to handle.
47
- * @returns A promise that resolves to the resulting ArvoEvent.
47
+ * @returns A promise that resolves to the resulting ArvoEvents.
48
48
  *
49
49
  * @remarks
50
50
  * This method performs the following steps:
@@ -56,7 +56,7 @@ export default class ArvoEventHandler<TContract extends ArvoContract> {
56
56
  *
57
57
  * All telemetry data is properly set and propagated throughout the execution.
58
58
  */
59
- execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>): Promise<ArvoEvent | null>;
59
+ execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>): Promise<ArvoEvent[]>;
60
60
  /**
61
61
  * Provides the schema for system error events.
62
62
  *
@@ -79,12 +79,12 @@ export default class ArvoEventHandler<TContract extends ArvoContract> {
79
79
  errorMessage: import("zod").ZodString;
80
80
  errorStack: import("zod").ZodNullable<import("zod").ZodString>;
81
81
  }, "strip", import("zod").ZodTypeAny, {
82
- errorName: string;
83
82
  errorMessage: string;
83
+ errorName: string;
84
84
  errorStack: string | null;
85
85
  }, {
86
- errorName: string;
87
86
  errorMessage: string;
87
+ errorName: string;
88
88
  errorStack: string | null;
89
89
  }>;
90
90
  };
@@ -109,7 +109,7 @@ var ArvoEventHandler = /** @class */ (function () {
109
109
  * Executes the event handler for a given event.
110
110
  *
111
111
  * @param event - The event to handle.
112
- * @returns A promise that resolves to the resulting ArvoEvent.
112
+ * @returns A promise that resolves to the resulting ArvoEvents.
113
113
  *
114
114
  * @remarks
115
115
  * This method performs the following steps:
@@ -146,7 +146,8 @@ var ArvoEventHandler = /** @class */ (function () {
146
146
  }
147
147
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
148
148
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
149
- var otelSpanHeaders, inputEventValidation, output, __extensions, handlerResult, result, error_1, result;
149
+ var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
150
+ var _this = this;
150
151
  return __generator(this, function (_a) {
151
152
  switch (_a.label) {
152
153
  case 0:
@@ -157,25 +158,33 @@ var ArvoEventHandler = /** @class */ (function () {
157
158
  span.setStatus({ code: api_1.SpanStatusCode.OK });
158
159
  Object.entries(event.otelAttributes).forEach(function (_a) {
159
160
  var key = _a[0], value = _a[1];
160
- return span.setAttribute("to_process.".concat(key), value);
161
+ return span.setAttribute("to_process.0.".concat(key), value);
161
162
  });
162
- inputEventValidation = this.contract.validateInput(event.type, event.data);
163
+ inputEventValidation = this.contract.validateAccepts(event.type, event.data);
163
164
  if (inputEventValidation.error) {
164
165
  throw new Error("Invalid event payload: ".concat(inputEventValidation.error));
165
166
  }
166
- return [4 /*yield*/, this._handler({ event: event })];
167
+ return [4 /*yield*/, this._handler({ event: event, source: this.source })];
167
168
  case 2:
168
- output = _a.sent();
169
- if (!output)
170
- return [2 /*return*/, null];
171
- __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
172
- result = eventFactory.emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: this.source, subject: event.subject, to: handlerResult.to || event.source, executionunits: handlerResult.executionunits || this.executionunits }), __extensions);
173
- span.setAttributes(result.otelAttributes);
174
- Object.entries(result.otelAttributes).forEach(function (_a) {
175
- var key = _a[0], value = _a[1];
176
- return span.setAttribute("to_emit.".concat(key), value);
177
- });
178
- return [2 /*return*/, result];
169
+ _handleOutput = _a.sent();
170
+ if (!_handleOutput)
171
+ return [2 /*return*/, []];
172
+ outputs = [];
173
+ if (Array.isArray(_handleOutput)) {
174
+ outputs = _handleOutput;
175
+ }
176
+ else {
177
+ outputs = [_handleOutput];
178
+ }
179
+ return [2 /*return*/, outputs.map(function (output, index) {
180
+ var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
181
+ var result = eventFactory.emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject, to: handlerResult.to || event.source, executionunits: handlerResult.executionunits || _this.executionunits }), __extensions);
182
+ Object.entries(result.otelAttributes).forEach(function (_a) {
183
+ var key = _a[0], value = _a[1];
184
+ return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
185
+ });
186
+ return result;
187
+ })];
179
188
  case 3:
180
189
  error_1 = _a.sent();
181
190
  (0, arvo_core_1.exceptionToSpan)(error_1);
@@ -194,9 +203,9 @@ var ArvoEventHandler = /** @class */ (function () {
194
203
  }, {});
195
204
  Object.entries(result.otelAttributes).forEach(function (_a) {
196
205
  var key = _a[0], value = _a[1];
197
- return span.setAttribute("to_emit.".concat(key), value);
206
+ return span.setAttribute("to_emit.0.".concat(key), value);
198
207
  });
199
- return [2 /*return*/, result];
208
+ return [2 /*return*/, [result]];
200
209
  case 4:
201
210
  span.end();
202
211
  return [7 /*endfinally*/];
@@ -8,6 +8,8 @@ import { z } from 'zod';
8
8
  export type ArvoEventHandlerFunctionInput<TContract extends ArvoContract> = {
9
9
  /** The ArvoEvent object. */
10
10
  event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>;
11
+ /** The source field data of the handler */
12
+ source: string;
11
13
  };
12
14
  /**
13
15
  * Represents the output of an ArvoEvent handler function.
@@ -31,7 +33,7 @@ export type ArvoEventHandlerFunctionOutput<TContract extends ArvoContract> = {
31
33
  * Defines the structure of an ArvoEvent handler function.
32
34
  * @template TContract - The type of ArvoContract that the handler is associated with.
33
35
  */
34
- export type ArvoEventHandlerFunction<TContract extends ArvoContract> = (params: ArvoEventHandlerFunctionInput<TContract>) => Promise<ArvoEventHandlerFunctionOutput<TContract> | void>;
36
+ export type ArvoEventHandlerFunction<TContract extends ArvoContract> = (params: ArvoEventHandlerFunctionInput<TContract>) => Promise<Array<ArvoEventHandlerFunctionOutput<TContract>> | ArvoEventHandlerFunctionOutput<TContract> | void>;
35
37
  /**
36
38
  * Interface for an ArvoEvent handler.
37
39
  * @template T - The type of the contract (defaults to string).
@@ -36,7 +36,7 @@ export default class MultiArvoEventHandler {
36
36
  * Executes the event handler for a given event.
37
37
  *
38
38
  * @param event - The event to handle.
39
- * @returns A promise that resolves to the resulting ArvoEvent.
39
+ * @returns A promise that resolves to the resulting ArvoEvents.
40
40
  *
41
41
  * @remarks
42
42
  * This method performs the following steps:
@@ -49,7 +49,7 @@ export default class MultiArvoEventHandler {
49
49
  * The method ensures that the resulting event has the correct source, subject,
50
50
  * and execution units, and includes any necessary tracing information.
51
51
  */
52
- execute(event: ArvoEvent): Promise<ArvoEvent | null>;
52
+ execute(event: ArvoEvent): Promise<ArvoEvent[]>;
53
53
  /**
54
54
  * Provides the schema for system error events.
55
55
  *
@@ -72,12 +72,12 @@ export default class MultiArvoEventHandler {
72
72
  errorMessage: import("zod").ZodString;
73
73
  errorStack: import("zod").ZodNullable<import("zod").ZodString>;
74
74
  }, "strip", import("zod").ZodTypeAny, {
75
- errorName: string;
76
75
  errorMessage: string;
76
+ errorName: string;
77
77
  errorStack: string | null;
78
78
  }, {
79
- errorName: string;
80
79
  errorMessage: string;
80
+ errorName: string;
81
81
  errorStack: string | null;
82
82
  }>;
83
83
  };
@@ -100,7 +100,7 @@ var MultiArvoEventHandler = /** @class */ (function () {
100
100
  * Executes the event handler for a given event.
101
101
  *
102
102
  * @param event - The event to handle.
103
- * @returns A promise that resolves to the resulting ArvoEvent.
103
+ * @returns A promise that resolves to the resulting ArvoEvents.
104
104
  *
105
105
  * @remarks
106
106
  * This method performs the following steps:
@@ -137,7 +137,8 @@ var MultiArvoEventHandler = /** @class */ (function () {
137
137
  span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
138
138
  }
139
139
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
140
- var otelSpanHeaders, output, __extensions, handlerResult, result, error_1, result;
140
+ var otelSpanHeaders, _handlerOutput, outputs, error_1, result;
141
+ var _this = this;
141
142
  return __generator(this, function (_a) {
142
143
  switch (_a.label) {
143
144
  case 0:
@@ -148,21 +149,29 @@ var MultiArvoEventHandler = /** @class */ (function () {
148
149
  span.setStatus({ code: api_1.SpanStatusCode.OK });
149
150
  Object.entries(event.otelAttributes).forEach(function (_a) {
150
151
  var key = _a[0], value = _a[1];
151
- return span.setAttribute("to_process.".concat(key), value);
152
+ return span.setAttribute("to_process.0.".concat(key), value);
152
153
  });
153
- return [4 /*yield*/, this._handler({ event: event })];
154
+ return [4 /*yield*/, this._handler({ event: event, source: this.source })];
154
155
  case 2:
155
- output = _a.sent();
156
- if (!output)
157
- return [2 /*return*/, null];
158
- __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
159
- result = (0, arvo_core_1.createArvoEvent)(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: this.source, subject: event.subject, to: handlerResult.to || event.source, executionunits: handlerResult.executionunits || this.executionunits }), __extensions);
160
- span.setAttributes(result.otelAttributes);
161
- Object.entries(result.otelAttributes).forEach(function (_a) {
162
- var key = _a[0], value = _a[1];
163
- return span.setAttribute("to_emit.".concat(key), value);
164
- });
165
- return [2 /*return*/, result];
156
+ _handlerOutput = _a.sent();
157
+ if (!_handlerOutput)
158
+ return [2 /*return*/, []];
159
+ outputs = [];
160
+ if (Array.isArray(_handlerOutput)) {
161
+ outputs = _handlerOutput;
162
+ }
163
+ else {
164
+ outputs = [_handlerOutput];
165
+ }
166
+ return [2 /*return*/, outputs.map(function (output, index) {
167
+ var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
168
+ var result = (0, arvo_core_1.createArvoEvent)(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject, to: handlerResult.to || event.source, executionunits: handlerResult.executionunits || _this.executionunits }), __extensions);
169
+ Object.entries(result.otelAttributes).forEach(function (_a) {
170
+ var key = _a[0], value = _a[1];
171
+ return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
172
+ });
173
+ return result;
174
+ })];
166
175
  case 3:
167
176
  error_1 = _a.sent();
168
177
  (0, arvo_core_1.exceptionToSpan)(error_1);
@@ -186,9 +195,9 @@ var MultiArvoEventHandler = /** @class */ (function () {
186
195
  });
187
196
  Object.entries(result.otelAttributes).forEach(function (_a) {
188
197
  var key = _a[0], value = _a[1];
189
- return span.setAttribute("to_emit.".concat(key), value);
198
+ return span.setAttribute("to_emit.0.".concat(key), value);
190
199
  });
191
- return [2 /*return*/, result];
200
+ return [2 /*return*/, [result]];
192
201
  case 4:
193
202
  span.end();
194
203
  return [7 /*endfinally*/];
@@ -4,7 +4,10 @@ import { ArvoEvent, ArvoExecutionSpanKind, CreateArvoEvent, OpenInferenceSpanKin
4
4
  * Represents the input for a Multi ArvoEvent handler function.
5
5
  */
6
6
  export type MultiArvoEventHandlerFunctionInput = {
7
+ /** The ArvoEvent object. */
7
8
  event: ArvoEvent;
9
+ /** The source field data of the handler */
10
+ source: string;
8
11
  };
9
12
  /**
10
13
  * Represents the output of a Multi ArvoEvent handler function.
@@ -26,7 +29,7 @@ export type MultiArvoEventHandlerFunctionOutput = Omit<CreateArvoEvent<Record<st
26
29
  * Defines the structure of a Multi ArvoEvent handler function.
27
30
  * @template TContract - The type of ArvoContract that the handler is associated with.
28
31
  */
29
- export type MultiArvoEventHandlerFunction = (param: MultiArvoEventHandlerFunctionInput) => Promise<MultiArvoEventHandlerFunctionOutput | void>;
32
+ export type MultiArvoEventHandlerFunction = (param: MultiArvoEventHandlerFunctionInput) => Promise<Array<MultiArvoEventHandlerFunctionOutput> | MultiArvoEventHandlerFunctionOutput | void>;
30
33
  /**
31
34
  * Interface for an Multi ArvoEvent handler.
32
35
  */
@@ -0,0 +1 @@
1
+ export declare function isArray(input: unknown): boolean;
package/dist/utils.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isArray = isArray;
4
+ function isArray(input) {
5
+ return Array.isArray(input);
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "This package contains class and function for event handlers in an Arvo Event Driven system",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@opentelemetry/api": "^1.9.0",
50
- "arvo-core": "^1.0.26",
50
+ "arvo-core": "^1.0.27",
51
51
  "uuid": "^10.0.0",
52
52
  "zod": "^3.23.8"
53
53
  }