arvo-event-handler 0.0.1 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/ArvoEventHandler/index.d.ts +34 -5
- package/dist/ArvoEventHandler/index.js +65 -19
- package/dist/ArvoEventHandler/types.d.ts +3 -1
- package/dist/MultiArvoEventHandler/index.d.ts +32 -7
- package/dist/MultiArvoEventHandler/index.js +62 -20
- package/dist/MultiArvoEventHandler/types.d.ts +4 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -1
- package/dist/utils.d.ts +43 -0
- package/dist/utils.js +67 -0
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -44,19 +44,48 @@ 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
|
|
47
|
+
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
48
48
|
*
|
|
49
49
|
* @remarks
|
|
50
50
|
* This method performs the following steps:
|
|
51
51
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
52
52
|
* 2. Validates the input event against the contract.
|
|
53
53
|
* 3. Executes the handler function.
|
|
54
|
-
* 4. Creates and returns the result event.
|
|
54
|
+
* 4. Creates and returns the result event(s).
|
|
55
55
|
* 5. Handles any errors and creates an error event if necessary.
|
|
56
56
|
*
|
|
57
57
|
* All telemetry data is properly set and propagated throughout the execution.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const contract = createArvoContract({ ... })
|
|
62
|
+
* const handler = createArvoEventHandler({
|
|
63
|
+
* contract: contract,
|
|
64
|
+
* ...
|
|
65
|
+
* });
|
|
66
|
+
* const inputEvent: ArvoEvent<...> = createArvoEvent({ ... });
|
|
67
|
+
* const resultEvents = await handler.execute(inputEvent);
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @throws All error throw during the execution are returned as a system error event
|
|
71
|
+
*
|
|
72
|
+
* **Routing**
|
|
73
|
+
* The routing of the resulting events is determined as follows:
|
|
74
|
+
* - The `to` field of the output event is set in this priority:
|
|
75
|
+
* 1. The `to` field provided by the handler result
|
|
76
|
+
* 2. The `redirectto` field from the input event
|
|
77
|
+
* 3. The `source` field from the input event (as a form of reply)
|
|
78
|
+
* - For system error events, the `to` field is always set to the `source` of the input event.
|
|
79
|
+
*
|
|
80
|
+
* **Telemetry**
|
|
81
|
+
* - Creates a new span for each execution as per the traceparent and tracestate field
|
|
82
|
+
* of the event. If those are not present, then a brand new span is created and distributed
|
|
83
|
+
* tracing is disabled
|
|
84
|
+
* - Sets span attributes for input and output events
|
|
85
|
+
* - Propagates trace context to output events
|
|
86
|
+
* - Handles error cases and sets appropriate span status
|
|
58
87
|
*/
|
|
59
|
-
execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>): Promise<ArvoEvent
|
|
88
|
+
execute(event: ArvoEvent<ResolveArvoContractRecord<TContract['accepts']>, Record<string, any>, TContract['accepts']['type']>): Promise<ArvoEvent[]>;
|
|
60
89
|
/**
|
|
61
90
|
* Provides the schema for system error events.
|
|
62
91
|
*
|
|
@@ -79,12 +108,12 @@ export default class ArvoEventHandler<TContract extends ArvoContract> {
|
|
|
79
108
|
errorMessage: import("zod").ZodString;
|
|
80
109
|
errorStack: import("zod").ZodNullable<import("zod").ZodString>;
|
|
81
110
|
}, "strip", import("zod").ZodTypeAny, {
|
|
82
|
-
errorName: string;
|
|
83
111
|
errorMessage: string;
|
|
112
|
+
errorName: string;
|
|
84
113
|
errorStack: string | null;
|
|
85
114
|
}, {
|
|
86
|
-
errorName: string;
|
|
87
115
|
errorMessage: string;
|
|
116
|
+
errorName: string;
|
|
88
117
|
errorStack: string | null;
|
|
89
118
|
}>;
|
|
90
119
|
};
|
|
@@ -62,6 +62,7 @@ var arvo_core_1 = require("arvo-core");
|
|
|
62
62
|
var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
|
|
63
63
|
var OpenTelemetry_1 = require("../OpenTelemetry");
|
|
64
64
|
var api_1 = require("@opentelemetry/api");
|
|
65
|
+
var utils_1 = require("../utils");
|
|
65
66
|
/**
|
|
66
67
|
* Represents an event handler for Arvo contracts.
|
|
67
68
|
*
|
|
@@ -109,17 +110,46 @@ var ArvoEventHandler = /** @class */ (function () {
|
|
|
109
110
|
* Executes the event handler for a given event.
|
|
110
111
|
*
|
|
111
112
|
* @param event - The event to handle.
|
|
112
|
-
* @returns A promise that resolves to
|
|
113
|
+
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
113
114
|
*
|
|
114
115
|
* @remarks
|
|
115
116
|
* This method performs the following steps:
|
|
116
117
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
117
118
|
* 2. Validates the input event against the contract.
|
|
118
119
|
* 3. Executes the handler function.
|
|
119
|
-
* 4. Creates and returns the result event.
|
|
120
|
+
* 4. Creates and returns the result event(s).
|
|
120
121
|
* 5. Handles any errors and creates an error event if necessary.
|
|
121
122
|
*
|
|
122
123
|
* All telemetry data is properly set and propagated throughout the execution.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const contract = createArvoContract({ ... })
|
|
128
|
+
* const handler = createArvoEventHandler({
|
|
129
|
+
* contract: contract,
|
|
130
|
+
* ...
|
|
131
|
+
* });
|
|
132
|
+
* const inputEvent: ArvoEvent<...> = createArvoEvent({ ... });
|
|
133
|
+
* const resultEvents = await handler.execute(inputEvent);
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @throws All error throw during the execution are returned as a system error event
|
|
137
|
+
*
|
|
138
|
+
* **Routing**
|
|
139
|
+
* The routing of the resulting events is determined as follows:
|
|
140
|
+
* - The `to` field of the output event is set in this priority:
|
|
141
|
+
* 1. The `to` field provided by the handler result
|
|
142
|
+
* 2. The `redirectto` field from the input event
|
|
143
|
+
* 3. The `source` field from the input event (as a form of reply)
|
|
144
|
+
* - For system error events, the `to` field is always set to the `source` of the input event.
|
|
145
|
+
*
|
|
146
|
+
* **Telemetry**
|
|
147
|
+
* - Creates a new span for each execution as per the traceparent and tracestate field
|
|
148
|
+
* of the event. If those are not present, then a brand new span is created and distributed
|
|
149
|
+
* tracing is disabled
|
|
150
|
+
* - Sets span attributes for input and output events
|
|
151
|
+
* - Propagates trace context to output events
|
|
152
|
+
* - Handles error cases and sets appropriate span status
|
|
123
153
|
*/
|
|
124
154
|
ArvoEventHandler.prototype.execute = function (event) {
|
|
125
155
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -146,7 +176,8 @@ var ArvoEventHandler = /** @class */ (function () {
|
|
|
146
176
|
}
|
|
147
177
|
eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
|
|
148
178
|
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,
|
|
179
|
+
var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
|
|
180
|
+
var _this = this;
|
|
150
181
|
return __generator(this, function (_a) {
|
|
151
182
|
switch (_a.label) {
|
|
152
183
|
case 0:
|
|
@@ -157,25 +188,38 @@ var ArvoEventHandler = /** @class */ (function () {
|
|
|
157
188
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
158
189
|
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
159
190
|
var key = _a[0], value = _a[1];
|
|
160
|
-
return span.setAttribute("to_process.".concat(key), value);
|
|
191
|
+
return span.setAttribute("to_process.0.".concat(key), value);
|
|
161
192
|
});
|
|
162
|
-
inputEventValidation = this.contract.
|
|
193
|
+
inputEventValidation = this.contract.validateAccepts(event.type, event.data);
|
|
163
194
|
if (inputEventValidation.error) {
|
|
164
195
|
throw new Error("Invalid event payload: ".concat(inputEventValidation.error));
|
|
165
196
|
}
|
|
166
|
-
return [4 /*yield*/, this._handler({ event: event })];
|
|
197
|
+
return [4 /*yield*/, this._handler({ event: event, source: this.source })];
|
|
167
198
|
case 2:
|
|
168
|
-
|
|
169
|
-
if (!
|
|
170
|
-
return [2 /*return*/,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
return [2 /*return*/,
|
|
199
|
+
_handleOutput = _a.sent();
|
|
200
|
+
if (!_handleOutput)
|
|
201
|
+
return [2 /*return*/, []];
|
|
202
|
+
outputs = [];
|
|
203
|
+
if (Array.isArray(_handleOutput)) {
|
|
204
|
+
outputs = _handleOutput;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
outputs = [_handleOutput];
|
|
208
|
+
}
|
|
209
|
+
return [2 /*return*/, outputs.map(function (output, index) {
|
|
210
|
+
var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
|
|
211
|
+
var result = eventFactory.emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
|
|
212
|
+
// The user should be able to override the `to` field
|
|
213
|
+
// If that is not present then the 'redirectto' field
|
|
214
|
+
// is referred to. Then, after all else, 'source' field
|
|
215
|
+
// is used as a form of reply.
|
|
216
|
+
to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
|
|
217
|
+
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
218
|
+
var key = _a[0], value = _a[1];
|
|
219
|
+
return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
|
|
220
|
+
});
|
|
221
|
+
return result;
|
|
222
|
+
})];
|
|
179
223
|
case 3:
|
|
180
224
|
error_1 = _a.sent();
|
|
181
225
|
(0, arvo_core_1.exceptionToSpan)(error_1);
|
|
@@ -186,6 +230,8 @@ var ArvoEventHandler = /** @class */ (function () {
|
|
|
186
230
|
result = eventFactory.systemError({
|
|
187
231
|
source: this.source,
|
|
188
232
|
subject: event.subject,
|
|
233
|
+
// The system error must always got back to
|
|
234
|
+
// the source
|
|
189
235
|
to: event.source,
|
|
190
236
|
error: error_1,
|
|
191
237
|
executionunits: this.executionunits,
|
|
@@ -194,9 +240,9 @@ var ArvoEventHandler = /** @class */ (function () {
|
|
|
194
240
|
}, {});
|
|
195
241
|
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
196
242
|
var key = _a[0], value = _a[1];
|
|
197
|
-
return span.setAttribute("to_emit.".concat(key), value);
|
|
243
|
+
return span.setAttribute("to_emit.0.".concat(key), value);
|
|
198
244
|
});
|
|
199
|
-
return [2 /*return*/, result];
|
|
245
|
+
return [2 /*return*/, [result]];
|
|
200
246
|
case 4:
|
|
201
247
|
span.end();
|
|
202
248
|
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,20 +36,45 @@ 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
|
|
39
|
+
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
40
40
|
*
|
|
41
41
|
* @remarks
|
|
42
42
|
* This method performs the following steps:
|
|
43
43
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
44
44
|
* 2. Executes the handler function.
|
|
45
|
-
* 3. Creates and returns the result event.
|
|
45
|
+
* 3. Creates and returns the result event(s).
|
|
46
46
|
* 4. Handles any errors and creates an error event if necessary.
|
|
47
47
|
*
|
|
48
48
|
* All telemetry data is properly set and propagated throughout the execution.
|
|
49
|
-
*
|
|
50
|
-
*
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const handler = new MultiArvoEventHandler({
|
|
53
|
+
* source: 'com.multi.handler',
|
|
54
|
+
* ...
|
|
55
|
+
* });
|
|
56
|
+
* const inputEvent: ArvoEvent = createArvoEvent({ ... });
|
|
57
|
+
* const resultEvents = await handler.execute(inputEvent);
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @throws All errors thrown during the execution are returned as a system error event
|
|
61
|
+
*
|
|
62
|
+
* **Routing**
|
|
63
|
+
* The routing of the resulting events is determined as follows:
|
|
64
|
+
* - The `to` field of the output event is set in this priority:
|
|
65
|
+
* 1. The `to` field provided by the handler result
|
|
66
|
+
* 2. The `source` field from the input event (as a form of reply)
|
|
67
|
+
* - For system error events, the `to` field is always set to the `source` of the input event.
|
|
68
|
+
*
|
|
69
|
+
* **Telemetry**
|
|
70
|
+
* - Creates a new span for each execution as per the traceparent and tracestate field
|
|
71
|
+
* of the event. If those are not present, then a brand new span is created and distributed
|
|
72
|
+
* tracing is disabled
|
|
73
|
+
* - Sets span attributes for input and output events
|
|
74
|
+
* - Propagates trace context to output events
|
|
75
|
+
* - Handles error cases and sets appropriate span status
|
|
51
76
|
*/
|
|
52
|
-
execute(event: ArvoEvent): Promise<ArvoEvent
|
|
77
|
+
execute(event: ArvoEvent): Promise<ArvoEvent[]>;
|
|
53
78
|
/**
|
|
54
79
|
* Provides the schema for system error events.
|
|
55
80
|
*
|
|
@@ -72,12 +97,12 @@ export default class MultiArvoEventHandler {
|
|
|
72
97
|
errorMessage: import("zod").ZodString;
|
|
73
98
|
errorStack: import("zod").ZodNullable<import("zod").ZodString>;
|
|
74
99
|
}, "strip", import("zod").ZodTypeAny, {
|
|
75
|
-
errorName: string;
|
|
76
100
|
errorMessage: string;
|
|
101
|
+
errorName: string;
|
|
77
102
|
errorStack: string | null;
|
|
78
103
|
}, {
|
|
79
|
-
errorName: string;
|
|
80
104
|
errorMessage: string;
|
|
105
|
+
errorName: string;
|
|
81
106
|
errorStack: string | null;
|
|
82
107
|
}>;
|
|
83
108
|
};
|
|
@@ -62,6 +62,7 @@ var api_1 = require("@opentelemetry/api");
|
|
|
62
62
|
var arvo_core_1 = require("arvo-core");
|
|
63
63
|
var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
|
|
64
64
|
var OpenTelemetry_1 = require("../OpenTelemetry");
|
|
65
|
+
var utils_1 = require("../utils");
|
|
65
66
|
/**
|
|
66
67
|
* Represents a Multi ArvoEvent handler that can process multiple event types.
|
|
67
68
|
*
|
|
@@ -100,18 +101,43 @@ var MultiArvoEventHandler = /** @class */ (function () {
|
|
|
100
101
|
* Executes the event handler for a given event.
|
|
101
102
|
*
|
|
102
103
|
* @param event - The event to handle.
|
|
103
|
-
* @returns A promise that resolves to
|
|
104
|
+
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
104
105
|
*
|
|
105
106
|
* @remarks
|
|
106
107
|
* This method performs the following steps:
|
|
107
108
|
* 1. Creates an OpenTelemetry span for the execution.
|
|
108
109
|
* 2. Executes the handler function.
|
|
109
|
-
* 3. Creates and returns the result event.
|
|
110
|
+
* 3. Creates and returns the result event(s).
|
|
110
111
|
* 4. Handles any errors and creates an error event if necessary.
|
|
111
112
|
*
|
|
112
113
|
* All telemetry data is properly set and propagated throughout the execution.
|
|
113
|
-
*
|
|
114
|
-
*
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const handler = new MultiArvoEventHandler({
|
|
118
|
+
* source: 'com.multi.handler',
|
|
119
|
+
* ...
|
|
120
|
+
* });
|
|
121
|
+
* const inputEvent: ArvoEvent = createArvoEvent({ ... });
|
|
122
|
+
* const resultEvents = await handler.execute(inputEvent);
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @throws All errors thrown during the execution are returned as a system error event
|
|
126
|
+
*
|
|
127
|
+
* **Routing**
|
|
128
|
+
* The routing of the resulting events is determined as follows:
|
|
129
|
+
* - The `to` field of the output event is set in this priority:
|
|
130
|
+
* 1. The `to` field provided by the handler result
|
|
131
|
+
* 2. The `source` field from the input event (as a form of reply)
|
|
132
|
+
* - For system error events, the `to` field is always set to the `source` of the input event.
|
|
133
|
+
*
|
|
134
|
+
* **Telemetry**
|
|
135
|
+
* - Creates a new span for each execution as per the traceparent and tracestate field
|
|
136
|
+
* of the event. If those are not present, then a brand new span is created and distributed
|
|
137
|
+
* tracing is disabled
|
|
138
|
+
* - Sets span attributes for input and output events
|
|
139
|
+
* - Propagates trace context to output events
|
|
140
|
+
* - Handles error cases and sets appropriate span status
|
|
115
141
|
*/
|
|
116
142
|
MultiArvoEventHandler.prototype.execute = function (event) {
|
|
117
143
|
return __awaiter(this, void 0, void 0, function () {
|
|
@@ -137,7 +163,8 @@ var MultiArvoEventHandler = /** @class */ (function () {
|
|
|
137
163
|
span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
|
|
138
164
|
}
|
|
139
165
|
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,
|
|
166
|
+
var otelSpanHeaders, _handlerOutput, outputs, error_1, result;
|
|
167
|
+
var _this = this;
|
|
141
168
|
return __generator(this, function (_a) {
|
|
142
169
|
switch (_a.label) {
|
|
143
170
|
case 0:
|
|
@@ -148,21 +175,34 @@ var MultiArvoEventHandler = /** @class */ (function () {
|
|
|
148
175
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
149
176
|
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
150
177
|
var key = _a[0], value = _a[1];
|
|
151
|
-
return span.setAttribute("to_process.".concat(key), value);
|
|
178
|
+
return span.setAttribute("to_process.0.".concat(key), value);
|
|
152
179
|
});
|
|
153
|
-
return [4 /*yield*/, this._handler({ event: event })];
|
|
180
|
+
return [4 /*yield*/, this._handler({ event: event, source: this.source })];
|
|
154
181
|
case 2:
|
|
155
|
-
|
|
156
|
-
if (!
|
|
157
|
-
return [2 /*return*/,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
return [2 /*return*/,
|
|
182
|
+
_handlerOutput = _a.sent();
|
|
183
|
+
if (!_handlerOutput)
|
|
184
|
+
return [2 /*return*/, []];
|
|
185
|
+
outputs = [];
|
|
186
|
+
if (Array.isArray(_handlerOutput)) {
|
|
187
|
+
outputs = _handlerOutput;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
outputs = [_handlerOutput];
|
|
191
|
+
}
|
|
192
|
+
return [2 /*return*/, outputs.map(function (output, index) {
|
|
193
|
+
var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
|
|
194
|
+
var result = (0, arvo_core_1.createArvoEvent)(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
|
|
195
|
+
// The user should be able to override the `to` field
|
|
196
|
+
// If that is not present then the 'redirectto' field
|
|
197
|
+
// is referred to. Then, after all else, 'source' field
|
|
198
|
+
// is used as a form of reply.
|
|
199
|
+
to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
|
|
200
|
+
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
201
|
+
var key = _a[0], value = _a[1];
|
|
202
|
+
return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
|
|
203
|
+
});
|
|
204
|
+
return result;
|
|
205
|
+
})];
|
|
166
206
|
case 3:
|
|
167
207
|
error_1 = _a.sent();
|
|
168
208
|
(0, arvo_core_1.exceptionToSpan)(error_1);
|
|
@@ -174,6 +214,8 @@ var MultiArvoEventHandler = /** @class */ (function () {
|
|
|
174
214
|
type: "sys.".concat(this.source, ".error"),
|
|
175
215
|
source: this.source,
|
|
176
216
|
subject: event.subject,
|
|
217
|
+
// The system error must always got back to
|
|
218
|
+
// the source
|
|
177
219
|
to: event.source,
|
|
178
220
|
executionunits: this.executionunits,
|
|
179
221
|
traceparent: otelSpanHeaders.traceparent || undefined,
|
|
@@ -186,9 +228,9 @@ var MultiArvoEventHandler = /** @class */ (function () {
|
|
|
186
228
|
});
|
|
187
229
|
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
188
230
|
var key = _a[0], value = _a[1];
|
|
189
|
-
return span.setAttribute("to_emit.".concat(key), value);
|
|
231
|
+
return span.setAttribute("to_emit.0.".concat(key), value);
|
|
190
232
|
});
|
|
191
|
-
return [2 /*return*/, result];
|
|
233
|
+
return [2 /*return*/, [result]];
|
|
192
234
|
case 4:
|
|
193
235
|
span.end();
|
|
194
236
|
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
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ import { PartialExcept } from './types';
|
|
|
5
5
|
import MultiArvoEventHandler from './MultiArvoEventHandler';
|
|
6
6
|
import { MultiArvoEventHandlerFunctionInput, MultiArvoEventHandlerFunctionOutput, MultiArvoEventHandlerFunction, IMultiArvoEventHandler } from './MultiArvoEventHandler/types';
|
|
7
7
|
import { createMultiArvoEventHandler } from './MultiArvoEventHandler/helpers';
|
|
8
|
-
|
|
8
|
+
import { isNullOrUndefined, getValueOrDefault, coalesce, coalesceOrDefault } from './utils';
|
|
9
|
+
export { ArvoEventHandler, createArvoEventHandler, IArvoEventHandler, ArvoEventHandlerFunctionOutput, ArvoEventHandlerFunctionInput, ArvoEventHandlerFunction, PartialExcept, MultiArvoEventHandler, MultiArvoEventHandlerFunctionInput, MultiArvoEventHandlerFunctionOutput, MultiArvoEventHandlerFunction, IMultiArvoEventHandler, createMultiArvoEventHandler, isNullOrUndefined, getValueOrDefault, coalesce, coalesceOrDefault, };
|
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.createMultiArvoEventHandler = exports.MultiArvoEventHandler = exports.createArvoEventHandler = exports.ArvoEventHandler = void 0;
|
|
6
|
+
exports.coalesceOrDefault = exports.coalesce = exports.getValueOrDefault = exports.isNullOrUndefined = exports.createMultiArvoEventHandler = exports.MultiArvoEventHandler = exports.createArvoEventHandler = exports.ArvoEventHandler = void 0;
|
|
7
7
|
var ArvoEventHandler_1 = __importDefault(require("./ArvoEventHandler"));
|
|
8
8
|
exports.ArvoEventHandler = ArvoEventHandler_1.default;
|
|
9
9
|
var helpers_1 = require("./ArvoEventHandler/helpers");
|
|
@@ -12,3 +12,8 @@ var MultiArvoEventHandler_1 = __importDefault(require("./MultiArvoEventHandler")
|
|
|
12
12
|
exports.MultiArvoEventHandler = MultiArvoEventHandler_1.default;
|
|
13
13
|
var helpers_2 = require("./MultiArvoEventHandler/helpers");
|
|
14
14
|
Object.defineProperty(exports, "createMultiArvoEventHandler", { enumerable: true, get: function () { return helpers_2.createMultiArvoEventHandler; } });
|
|
15
|
+
var utils_1 = require("./utils");
|
|
16
|
+
Object.defineProperty(exports, "isNullOrUndefined", { enumerable: true, get: function () { return utils_1.isNullOrUndefined; } });
|
|
17
|
+
Object.defineProperty(exports, "getValueOrDefault", { enumerable: true, get: function () { return utils_1.getValueOrDefault; } });
|
|
18
|
+
Object.defineProperty(exports, "coalesce", { enumerable: true, get: function () { return utils_1.coalesce; } });
|
|
19
|
+
Object.defineProperty(exports, "coalesceOrDefault", { enumerable: true, get: function () { return utils_1.coalesceOrDefault; } });
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the item is null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @param item - The value to check.
|
|
5
|
+
* @returns True if the item is null or undefined, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isNullOrUndefined(item: unknown): item is null | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the provided value if it's not null or undefined; otherwise, returns the default value.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of the value and default value.
|
|
12
|
+
* @param value - The value to check.
|
|
13
|
+
* @param defaultValue - The default value to return if the provided value is null or undefined.
|
|
14
|
+
* @returns The provided value if it's not null or undefined; otherwise, the default value.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getValueOrDefault<T>(value: T | null | undefined, defaultValue: NonNullable<T>): NonNullable<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the first non-null and non-undefined value from the provided arguments.
|
|
19
|
+
* If all arguments are null or undefined, returns undefined.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The type of the values.
|
|
22
|
+
* @param values - The values to coalesce.
|
|
23
|
+
* @returns The first non-null and non-undefined value, or undefined if all are null or undefined.
|
|
24
|
+
*/
|
|
25
|
+
export declare function coalesce<T>(...values: (T | null | undefined)[]): T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the first non-null and non-undefined value from the provided array of values.
|
|
28
|
+
* If all values in the array are null or undefined, returns the specified default value.
|
|
29
|
+
*
|
|
30
|
+
* @template T - The type of the values and the default value.
|
|
31
|
+
* @param values - An array of values to coalesce.
|
|
32
|
+
* @param _default - The default value to return if all values in the array are null or undefined.
|
|
33
|
+
* @returns The first non-null and non-undefined value from the array, or the default value if all are null or undefined.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const result = coalesceOrDefault([null, undefined, 'hello', 'world'], 'default');
|
|
37
|
+
* console.log(result); // Output: 'hello'
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const result = coalesceOrDefault([null, undefined], 'default');
|
|
41
|
+
* console.log(result); // Output: 'default'
|
|
42
|
+
*/
|
|
43
|
+
export declare function coalesceOrDefault<T>(values: (T | null | undefined)[], _default: NonNullable<T>): NonNullable<T>;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNullOrUndefined = isNullOrUndefined;
|
|
4
|
+
exports.getValueOrDefault = getValueOrDefault;
|
|
5
|
+
exports.coalesce = coalesce;
|
|
6
|
+
exports.coalesceOrDefault = coalesceOrDefault;
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the item is null or undefined.
|
|
9
|
+
*
|
|
10
|
+
* @param item - The value to check.
|
|
11
|
+
* @returns True if the item is null or undefined, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
function isNullOrUndefined(item) {
|
|
14
|
+
return item === null || item === undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns the provided value if it's not null or undefined; otherwise, returns the default value.
|
|
18
|
+
*
|
|
19
|
+
* @template T - The type of the value and default value.
|
|
20
|
+
* @param value - The value to check.
|
|
21
|
+
* @param defaultValue - The default value to return if the provided value is null or undefined.
|
|
22
|
+
* @returns The provided value if it's not null or undefined; otherwise, the default value.
|
|
23
|
+
*/
|
|
24
|
+
function getValueOrDefault(value, defaultValue) {
|
|
25
|
+
return isNullOrUndefined(value) ? defaultValue : value;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the first non-null and non-undefined value from the provided arguments.
|
|
29
|
+
* If all arguments are null or undefined, returns undefined.
|
|
30
|
+
*
|
|
31
|
+
* @template T - The type of the values.
|
|
32
|
+
* @param values - The values to coalesce.
|
|
33
|
+
* @returns The first non-null and non-undefined value, or undefined if all are null or undefined.
|
|
34
|
+
*/
|
|
35
|
+
function coalesce() {
|
|
36
|
+
var values = [];
|
|
37
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
38
|
+
values[_i] = arguments[_i];
|
|
39
|
+
}
|
|
40
|
+
for (var _a = 0, values_1 = values; _a < values_1.length; _a++) {
|
|
41
|
+
var value = values_1[_a];
|
|
42
|
+
if (!isNullOrUndefined(value)) {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the first non-null and non-undefined value from the provided array of values.
|
|
50
|
+
* If all values in the array are null or undefined, returns the specified default value.
|
|
51
|
+
*
|
|
52
|
+
* @template T - The type of the values and the default value.
|
|
53
|
+
* @param values - An array of values to coalesce.
|
|
54
|
+
* @param _default - The default value to return if all values in the array are null or undefined.
|
|
55
|
+
* @returns The first non-null and non-undefined value from the array, or the default value if all are null or undefined.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* const result = coalesceOrDefault([null, undefined, 'hello', 'world'], 'default');
|
|
59
|
+
* console.log(result); // Output: 'hello'
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const result = coalesceOrDefault([null, undefined], 'default');
|
|
63
|
+
* console.log(result); // Output: 'default'
|
|
64
|
+
*/
|
|
65
|
+
function coalesceOrDefault(values, _default) {
|
|
66
|
+
return getValueOrDefault(coalesce.apply(void 0, values), _default);
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arvo-event-handler",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
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": {
|
|
@@ -43,11 +43,12 @@
|
|
|
43
43
|
"ts-node": "^10.9.2",
|
|
44
44
|
"typedoc": "^0.26.6",
|
|
45
45
|
"typedoc-plugin-zod": "^1.2.1",
|
|
46
|
-
"typescript": "^5.5.4"
|
|
46
|
+
"typescript": "^5.5.4",
|
|
47
|
+
"typedoc-plugin-mermaid": "^1.12.0"
|
|
47
48
|
},
|
|
48
49
|
"dependencies": {
|
|
49
50
|
"@opentelemetry/api": "^1.9.0",
|
|
50
|
-
"arvo-core": "^1.0.
|
|
51
|
+
"arvo-core": "^1.0.27",
|
|
51
52
|
"uuid": "^10.0.0",
|
|
52
53
|
"zod": "^3.23.8"
|
|
53
54
|
}
|