arvo-core 1.1.5 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,9 +26,7 @@ export default class ArvoEventFactory<TUri extends string = string, TType extend
26
26
  * @returns The created ArvoEvent as per the accept record of the contract.
27
27
  * @throws If the event data fails validation against the contract.
28
28
  */
29
- accepts<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.infer<TAcceptSchema>, TType>, 'type'> & {
30
- to: string;
31
- }, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TAcceptSchema>, TExtension, TType>;
29
+ accepts<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.infer<TAcceptSchema>, TType>, 'type' | 'datacontenttype' | 'dataschema'>, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TAcceptSchema>, TExtension, TType>;
32
30
  /**
33
31
  * Creates an ArvoEvent as per one of the emits record in the contract.
34
32
  *
@@ -39,9 +37,7 @@ export default class ArvoEventFactory<TUri extends string = string, TType extend
39
37
  * @returns The created ArvoEvent as per one of the emits records of the contract.
40
38
  * @throws If the event data fails validation against the contract.
41
39
  */
42
- emits<U extends keyof TEmits & string, TExtension extends Record<string, any>>(event: CreateArvoEvent<z.infer<TEmits[U]>, U> & {
43
- to: string;
44
- }, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TEmits[U]>, TExtension, U>;
40
+ emits<U extends keyof TEmits & string, TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<z.infer<TEmits[U]>, U>, 'datacontenttype' | 'dataschema'>, extensions?: TExtension): import("..").ArvoEvent<z.TypeOf<TEmits[U]>, TExtension, U>;
45
41
  /**
46
42
  * Creates a system error ArvoEvent.
47
43
  *
@@ -50,9 +46,8 @@ export default class ArvoEventFactory<TUri extends string = string, TType extend
50
46
  * @param [extensions] - Optional extensions to add to the event.
51
47
  * @returns The created system error ArvoEvent.
52
48
  */
53
- systemError<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<any, any>, 'data' | 'type'> & {
49
+ systemError<TExtension extends Record<string, any>>(event: Omit<CreateArvoEvent<any, any>, 'data' | 'type' | 'datacontenttype' | 'dataschema'> & {
54
50
  error: Error;
55
- to: string;
56
51
  }, extensions?: TExtension): import("..").ArvoEvent<{
57
52
  errorMessage: string;
58
53
  errorName: string;
@@ -127,8 +127,8 @@ var ArvoEventFactory = /** @class */ (function () {
127
127
  var _a;
128
128
  span.setStatus({ code: api_1.SpanStatusCode.OK });
129
129
  try {
130
- var error = event.error, _events = __rest(event, ["error"]);
131
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { type: "sys.".concat(_this.contract.accepts.type, ".error"), data: {
130
+ var error = event.error, _event = __rest(event, ["error"]);
131
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, _event), { type: "sys.".concat(_this.contract.accepts.type, ".error"), data: {
132
132
  errorName: error.name,
133
133
  errorMessage: error.message,
134
134
  errorStack: (_a = error.stack) !== null && _a !== void 0 ? _a : null,
@@ -30,4 +30,51 @@ export default class ArvoEventHttp {
30
30
  * @throws {Error} If required fields are missing or if there's an error parsing the input.
31
31
  */
32
32
  static importFromStructured(config: ArvoEventHttpConfig): ArvoEvent;
33
+ /**
34
+ * Validates the content type of the HTTP request.
35
+ * @param actual - The actual content type from the request.
36
+ * @param expected - The expected content type.
37
+ * @throws {Error} If the actual content type doesn't match the expected one.
38
+ */
39
+ private static validateContentType;
40
+ /**
41
+ * Extracts event fields from the HTTP headers.
42
+ * @param headers - The HTTP headers containing event information.
43
+ * @returns An object with extracted event fields.
44
+ */
45
+ private static extractEventFieldsFromHeaders;
46
+ /**
47
+ * Creates an error message for a missing required field.
48
+ * @param type - The type of the missing field.
49
+ * @param isHeader - A flag to distinguish between headers and structured payload
50
+ * @returns A formatted error message string.
51
+ */
52
+ private static createErrorMessageForMissingField;
53
+ /**
54
+ * Validates that all required fields are present in the event data.
55
+ * @param event - The event data to validate.
56
+ * @param isHeader - A flag to distinguish between headers and structured payload
57
+ * @throws {Error} If any required field is missing.
58
+ */
59
+ private static validateRequiredFields;
60
+ /**
61
+ * Extracts extension fields from the HTTP headers.
62
+ * @param headers - The HTTP headers containing event information.
63
+ * @returns An object with extracted extension fields.
64
+ */
65
+ private static extractExtensions;
66
+ /**
67
+ * Creates an ArvoEvent instance from extracted event data, payload, and extensions.
68
+ * @param event - The extracted event data.
69
+ * @param data - The event payload.
70
+ * @param extensions - The extracted extension fields.
71
+ * @returns A new ArvoEvent instance.
72
+ */
73
+ private static createArvoEvent;
74
+ /**
75
+ * Creates an ArvoEvent instance from structured event data.
76
+ * @param eventData - The structured event data.
77
+ * @returns A new ArvoEvent instance.
78
+ */
79
+ private static createArvoEventFromStructured;
33
80
  }
@@ -87,75 +87,12 @@ var ArvoEventHttp = /** @class */ (function () {
87
87
  * @throws {Error} If required fields are missing or if there's an error parsing the input.
88
88
  */
89
89
  ArvoEventHttp.importFromBinary = function (config) {
90
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
91
- try {
92
- if (config.headers['content-type'] !== 'application/json') {
93
- throw new Error("Invalid content-type: ".concat(config.headers['content-type'], ". Expected: application/json"));
94
- }
95
- var eventFields = [
96
- 'id',
97
- 'type',
98
- 'accesscontrol',
99
- 'executionunits',
100
- 'traceparent',
101
- 'tracestate',
102
- 'datacontenttype',
103
- 'specversion',
104
- 'time',
105
- 'source',
106
- 'subject',
107
- 'to',
108
- 'redirectto',
109
- 'dataschema',
110
- ];
111
- var event_1 = {};
112
- // Extract event fields from headers
113
- for (var _i = 0, eventFields_1 = eventFields; _i < eventFields_1.length; _i++) {
114
- var field = eventFields_1[_i];
115
- var headerKey = "ce-".concat(field);
116
- if (headerKey in config.headers) {
117
- event_1[field] = config.headers[headerKey];
118
- }
119
- }
120
- // Validate required fields
121
- if (!event_1.type || !event_1.source || !event_1.subject) {
122
- throw new Error('Missing required header fields: ce-type, ce-source, or ce-subject');
123
- }
124
- // Extract extensions
125
- var prefixedEventFields_1 = eventFields.map(function (item) { return "ce-".concat(item); });
126
- var extensions = Object.entries(config.headers)
127
- .filter(function (_a) {
128
- var key = _a[0];
129
- return key.startsWith('ce-') && !prefixedEventFields_1.includes(key);
130
- })
131
- .reduce(function (acc, _a) {
132
- var key = _a[0], value = _a[1];
133
- acc[key.slice(3)] = value;
134
- return acc;
135
- }, {});
136
- // Create and return ArvoEvent
137
- return new ArvoEvent_1.default({
138
- id: (_a = event_1.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
139
- type: event_1.type,
140
- accesscontrol: (_b = event_1.accesscontrol) !== null && _b !== void 0 ? _b : null,
141
- executionunits: event_1.executionunits
142
- ? Number(event_1.executionunits)
143
- : null,
144
- traceparent: (_c = event_1.traceparent) !== null && _c !== void 0 ? _c : null,
145
- tracestate: (_d = event_1.tracestate) !== null && _d !== void 0 ? _d : null,
146
- datacontenttype: (_e = event_1.datacontenttype) !== null && _e !== void 0 ? _e : schema_1.ArvoDataContentType,
147
- specversion: (_f = event_1.specversion) !== null && _f !== void 0 ? _f : '1.0',
148
- time: (_g = event_1.time) !== null && _g !== void 0 ? _g : (0, utils_1.createTimestamp)(),
149
- source: event_1.source,
150
- subject: event_1.subject,
151
- to: (_h = event_1.to) !== null && _h !== void 0 ? _h : null,
152
- redirectto: (_j = event_1.redirectto) !== null && _j !== void 0 ? _j : null,
153
- dataschema: (_k = event_1.dataschema) !== null && _k !== void 0 ? _k : null,
154
- }, config.data, extensions);
155
- }
156
- catch (error) {
157
- throw new Error("Failed to import ArvoEvent from binary: ".concat(error.message));
158
- }
90
+ var _a, _b;
91
+ this.validateContentType((_b = (_a = config.headers['content-type']) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '', 'application/json');
92
+ var event = this.extractEventFieldsFromHeaders(config.headers);
93
+ this.validateRequiredFields(event, true);
94
+ var extensions = this.extractExtensions(config.headers);
95
+ return this.createArvoEvent(event, config.data, extensions);
159
96
  };
160
97
  /**
161
98
  * Imports an ArvoEvent from a structured-mode HTTP representation.
@@ -164,35 +101,164 @@ var ArvoEventHttp = /** @class */ (function () {
164
101
  * @throws {Error} If required fields are missing or if there's an error parsing the input.
165
102
  */
166
103
  ArvoEventHttp.importFromStructured = function (config) {
167
- try {
168
- if (config.headers['content-type'] !== schema_1.ArvoDataContentType) {
169
- throw new Error("Invalid content-type: ".concat(config.headers['content-type'], ". Expected: ").concat(schema_1.ArvoDataContentType));
170
- }
171
- var eventData = config.data;
172
- if (!eventData.type || !eventData.source || !eventData.subject) {
173
- throw new Error('Missing required fields: type, source, or subject');
174
- }
175
- var id = eventData.id, type = eventData.type, source = eventData.source, subject = eventData.subject, time = eventData.time, datacontenttype = eventData.datacontenttype, specversion = eventData.specversion, dataschema = eventData.dataschema, data = eventData.data, to = eventData.to, accesscontrol = eventData.accesscontrol, redirectto = eventData.redirectto, executionunits = eventData.executionunits, traceparent = eventData.traceparent, tracestate = eventData.tracestate, extensions = __rest(eventData, ["id", "type", "source", "subject", "time", "datacontenttype", "specversion", "dataschema", "data", "to", "accesscontrol", "redirectto", "executionunits", "traceparent", "tracestate"]);
176
- return new ArvoEvent_1.default({
177
- id: id !== null && id !== void 0 ? id : (0, uuid_1.v4)(),
178
- type: type,
179
- source: source,
180
- subject: subject,
181
- time: time !== null && time !== void 0 ? time : (0, utils_1.createTimestamp)(),
182
- datacontenttype: datacontenttype !== null && datacontenttype !== void 0 ? datacontenttype : schema_1.ArvoDataContentType,
183
- specversion: specversion !== null && specversion !== void 0 ? specversion : '1.0',
184
- dataschema: dataschema !== null && dataschema !== void 0 ? dataschema : null,
185
- to: to !== null && to !== void 0 ? to : null,
186
- accesscontrol: accesscontrol !== null && accesscontrol !== void 0 ? accesscontrol : null,
187
- redirectto: redirectto !== null && redirectto !== void 0 ? redirectto : null,
188
- executionunits: executionunits ? Number(executionunits) : null,
189
- traceparent: traceparent !== null && traceparent !== void 0 ? traceparent : null,
190
- tracestate: tracestate !== null && tracestate !== void 0 ? tracestate : null,
191
- }, data !== null && data !== void 0 ? data : {}, extensions);
104
+ var _a, _b;
105
+ this.validateContentType((_b = (_a = config.headers['content-type']) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '', schema_1.ArvoDataContentType);
106
+ var eventData = config.data;
107
+ this.validateRequiredFields(eventData, false);
108
+ return this.createArvoEventFromStructured(eventData);
109
+ };
110
+ /**
111
+ * Validates the content type of the HTTP request.
112
+ * @param actual - The actual content type from the request.
113
+ * @param expected - The expected content type.
114
+ * @throws {Error} If the actual content type doesn't match the expected one.
115
+ */
116
+ ArvoEventHttp.validateContentType = function (actual, expected) {
117
+ if (actual !== expected) {
118
+ throw new Error("Invalid content-type: ".concat(actual, ". Expected: ").concat(expected));
192
119
  }
193
- catch (error) {
194
- throw new Error("Failed to import ArvoEvent from structured: ".concat(error.message));
120
+ };
121
+ /**
122
+ * Extracts event fields from the HTTP headers.
123
+ * @param headers - The HTTP headers containing event information.
124
+ * @returns An object with extracted event fields.
125
+ */
126
+ ArvoEventHttp.extractEventFieldsFromHeaders = function (headers) {
127
+ var eventFields = [
128
+ 'id',
129
+ 'type',
130
+ 'accesscontrol',
131
+ 'executionunits',
132
+ 'traceparent',
133
+ 'tracestate',
134
+ 'datacontenttype',
135
+ 'specversion',
136
+ 'time',
137
+ 'source',
138
+ 'subject',
139
+ 'to',
140
+ 'redirectto',
141
+ 'dataschema',
142
+ ];
143
+ return Object.fromEntries(eventFields
144
+ .map(function (field) { return ["ce-".concat(field), headers["ce-".concat(field)]]; })
145
+ .filter(function (_a) {
146
+ var value = _a[1];
147
+ return value !== undefined;
148
+ })
149
+ .map(function (_a) {
150
+ var key = _a[0], value = _a[1];
151
+ return [key.slice(3), value];
152
+ }));
153
+ };
154
+ /**
155
+ * Creates an error message for a missing required field.
156
+ * @param type - The type of the missing field.
157
+ * @param isHeader - A flag to distinguish between headers and structured payload
158
+ * @returns A formatted error message string.
159
+ */
160
+ ArvoEventHttp.createErrorMessageForMissingField = function (type, isHeader) {
161
+ if (isHeader) {
162
+ return "Missing required header field(s): ".concat(type);
195
163
  }
164
+ return "Missing required field(s): ".concat(type);
165
+ };
166
+ /**
167
+ * Validates that all required fields are present in the event data.
168
+ * @param event - The event data to validate.
169
+ * @param isHeader - A flag to distinguish between headers and structured payload
170
+ * @throws {Error} If any required field is missing.
171
+ */
172
+ ArvoEventHttp.validateRequiredFields = function (event, isHeader) {
173
+ ['type', 'source', 'subject'].forEach(function (field) {
174
+ if (!event[field]) {
175
+ throw new Error(ArvoEventHttp.createErrorMessageForMissingField(isHeader ? "ce-".concat(field) : field, isHeader));
176
+ }
177
+ });
178
+ };
179
+ /**
180
+ * Extracts extension fields from the HTTP headers.
181
+ * @param headers - The HTTP headers containing event information.
182
+ * @returns An object with extracted extension fields.
183
+ */
184
+ ArvoEventHttp.extractExtensions = function (headers) {
185
+ var eventFields = [
186
+ 'id',
187
+ 'type',
188
+ 'accesscontrol',
189
+ 'executionunits',
190
+ 'traceparent',
191
+ 'tracestate',
192
+ 'datacontenttype',
193
+ 'specversion',
194
+ 'time',
195
+ 'source',
196
+ 'subject',
197
+ 'to',
198
+ 'redirectto',
199
+ 'dataschema',
200
+ ];
201
+ return Object.fromEntries(Object.entries(headers)
202
+ .filter(function (_a) {
203
+ var key = _a[0];
204
+ return key.startsWith('ce-') && !eventFields.includes(key.slice(3));
205
+ })
206
+ .map(function (_a) {
207
+ var key = _a[0], value = _a[1];
208
+ return [key.slice(3), value];
209
+ }));
210
+ };
211
+ /**
212
+ * Creates an ArvoEvent instance from extracted event data, payload, and extensions.
213
+ * @param event - The extracted event data.
214
+ * @param data - The event payload.
215
+ * @param extensions - The extracted extension fields.
216
+ * @returns A new ArvoEvent instance.
217
+ */
218
+ ArvoEventHttp.createArvoEvent = function (event, data, extensions) {
219
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
220
+ return new ArvoEvent_1.default({
221
+ id: (_a = event.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
222
+ type: event.type,
223
+ accesscontrol: (_b = event.accesscontrol) !== null && _b !== void 0 ? _b : null,
224
+ executionunits: event.executionunits
225
+ ? Number(event.executionunits)
226
+ : null,
227
+ traceparent: (_c = event.traceparent) !== null && _c !== void 0 ? _c : null,
228
+ tracestate: (_d = event.tracestate) !== null && _d !== void 0 ? _d : null,
229
+ datacontenttype: (_e = event.datacontenttype) !== null && _e !== void 0 ? _e : schema_1.ArvoDataContentType,
230
+ specversion: (_f = event.specversion) !== null && _f !== void 0 ? _f : '1.0',
231
+ time: (_g = event.time) !== null && _g !== void 0 ? _g : (0, utils_1.createTimestamp)(),
232
+ source: event.source,
233
+ subject: event.subject,
234
+ to: (_h = event.to) !== null && _h !== void 0 ? _h : null,
235
+ redirectto: (_j = event.redirectto) !== null && _j !== void 0 ? _j : null,
236
+ dataschema: (_k = event.dataschema) !== null && _k !== void 0 ? _k : null,
237
+ }, data, extensions);
238
+ };
239
+ /**
240
+ * Creates an ArvoEvent instance from structured event data.
241
+ * @param eventData - The structured event data.
242
+ * @returns A new ArvoEvent instance.
243
+ */
244
+ ArvoEventHttp.createArvoEventFromStructured = function (eventData) {
245
+ var id = eventData.id, type = eventData.type, source = eventData.source, subject = eventData.subject, time = eventData.time, datacontenttype = eventData.datacontenttype, specversion = eventData.specversion, dataschema = eventData.dataschema, data = eventData.data, to = eventData.to, accesscontrol = eventData.accesscontrol, redirectto = eventData.redirectto, executionunits = eventData.executionunits, traceparent = eventData.traceparent, tracestate = eventData.tracestate, extensions = __rest(eventData, ["id", "type", "source", "subject", "time", "datacontenttype", "specversion", "dataschema", "data", "to", "accesscontrol", "redirectto", "executionunits", "traceparent", "tracestate"]);
246
+ return ArvoEventHttp.createArvoEvent({
247
+ id: id !== null && id !== void 0 ? id : (0, uuid_1.v4)(),
248
+ type: type,
249
+ source: source,
250
+ subject: subject,
251
+ time: time !== null && time !== void 0 ? time : (0, utils_1.createTimestamp)(),
252
+ datacontenttype: datacontenttype !== null && datacontenttype !== void 0 ? datacontenttype : schema_1.ArvoDataContentType,
253
+ specversion: specversion !== null && specversion !== void 0 ? specversion : '1.0',
254
+ dataschema: dataschema !== null && dataschema !== void 0 ? dataschema : null,
255
+ to: to !== null && to !== void 0 ? to : null,
256
+ accesscontrol: accesscontrol !== null && accesscontrol !== void 0 ? accesscontrol : null,
257
+ redirectto: redirectto !== null && redirectto !== void 0 ? redirectto : null,
258
+ executionunits: executionunits ? Number(executionunits) : null,
259
+ traceparent: traceparent !== null && traceparent !== void 0 ? traceparent : null,
260
+ tracestate: tracestate !== null && tracestate !== void 0 ? tracestate : null,
261
+ }, data !== null && data !== void 0 ? data : {}, extensions);
196
262
  };
197
263
  return ArvoEventHttp;
198
264
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
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": {