awaitly 2.0.0 → 3.0.0

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.
Files changed (88) hide show
  1. package/dist/di-Biw_plBn.d.cts +15 -0
  2. package/dist/di-DxyH2N3i.d.ts +15 -0
  3. package/dist/durable.cjs +4670 -0
  4. package/dist/durable.cjs.map +1 -0
  5. package/dist/durable.d.cts +9 -0
  6. package/dist/durable.d.ts +9 -0
  7. package/dist/durable.js +4610 -0
  8. package/dist/durable.js.map +1 -0
  9. package/dist/{di-BbFFfO8y.d.ts → duration-BjVn3QpB.d.cts} +1 -15
  10. package/dist/{di-BDlT7InM.d.cts → duration-BjVn3QpB.d.ts} +1 -15
  11. package/dist/engine.cjs +4501 -0
  12. package/dist/engine.cjs.map +1 -0
  13. package/dist/engine.d.cts +113 -0
  14. package/dist/engine.d.ts +113 -0
  15. package/dist/engine.js +4474 -0
  16. package/dist/engine.js.map +1 -0
  17. package/dist/{errors-DtXvrCiO.d.cts → errors-sDSeaZBO.d.cts} +1 -1
  18. package/dist/{errors-DtXvrCiO.d.ts → errors-sDSeaZBO.d.ts} +1 -1
  19. package/dist/guards-CwQZro1F.d.ts +72 -0
  20. package/dist/guards-DhhOJZda.d.cts +72 -0
  21. package/dist/hitl-BnnnLzh2.d.cts +468 -0
  22. package/dist/hitl-pYfkQMv_.d.ts +468 -0
  23. package/dist/hitl.cjs +646 -0
  24. package/dist/hitl.cjs.map +1 -0
  25. package/dist/hitl.d.cts +440 -0
  26. package/dist/hitl.d.ts +440 -0
  27. package/dist/hitl.js +605 -0
  28. package/dist/hitl.js.map +1 -0
  29. package/dist/index-Bew12SZ8.d.ts +417 -0
  30. package/dist/index-Bwt-iz50.d.cts +417 -0
  31. package/dist/{types-B8NfNRGX.d.ts → index-Cv4K_3sZ.d.ts} +2 -1162
  32. package/dist/{types-BZ2f4MRR.d.cts → index-ICmfQi08.d.cts} +2 -1162
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +11 -1527
  35. package/dist/index.d.ts +11 -1527
  36. package/dist/persistence.cjs +552 -0
  37. package/dist/persistence.cjs.map +1 -0
  38. package/dist/persistence.d.cts +254 -0
  39. package/dist/persistence.d.ts +254 -0
  40. package/dist/persistence.js +499 -0
  41. package/dist/persistence.js.map +1 -0
  42. package/dist/reliability.cjs +1651 -0
  43. package/dist/reliability.cjs.map +1 -0
  44. package/dist/reliability.d.cts +1442 -0
  45. package/dist/reliability.d.ts +1442 -0
  46. package/dist/reliability.js +1589 -0
  47. package/dist/reliability.js.map +1 -0
  48. package/dist/result.d.cts +1 -1
  49. package/dist/result.d.ts +1 -1
  50. package/dist/run.cjs +2283 -0
  51. package/dist/run.cjs.map +1 -0
  52. package/dist/run.d.cts +90 -0
  53. package/dist/run.d.ts +90 -0
  54. package/dist/run.js +2247 -0
  55. package/dist/run.js.map +1 -0
  56. package/dist/saga.cjs +3699 -0
  57. package/dist/saga.cjs.map +1 -0
  58. package/dist/saga.d.cts +162 -0
  59. package/dist/saga.d.ts +162 -0
  60. package/dist/saga.js +3670 -0
  61. package/dist/saga.js.map +1 -0
  62. package/dist/store-contract-BI98VYmX.d.ts +66 -0
  63. package/dist/store-contract-C2HyR_o6.d.cts +66 -0
  64. package/dist/streaming.cjs +895 -0
  65. package/dist/streaming.cjs.map +1 -0
  66. package/dist/streaming.d.cts +594 -0
  67. package/dist/streaming.d.ts +594 -0
  68. package/dist/streaming.js +832 -0
  69. package/dist/streaming.js.map +1 -0
  70. package/dist/testing.d.cts +4 -2
  71. package/dist/testing.d.ts +4 -2
  72. package/dist/types-BDTxgKKc.d.ts +845 -0
  73. package/dist/types-C9Y71dua.d.cts +845 -0
  74. package/dist/types-d8q8iQlk.d.cts +323 -0
  75. package/dist/types-sObbY4mX.d.ts +323 -0
  76. package/dist/webhook.cjs +461 -0
  77. package/dist/webhook.cjs.map +1 -0
  78. package/dist/webhook.d.cts +497 -0
  79. package/dist/webhook.d.ts +497 -0
  80. package/dist/webhook.js +422 -0
  81. package/dist/webhook.js.map +1 -0
  82. package/dist/workflow.cjs +2 -2614
  83. package/dist/workflow.cjs.map +1 -1
  84. package/dist/workflow.d.cts +12 -3023
  85. package/dist/workflow.d.ts +12 -3023
  86. package/dist/workflow.js +2 -2535
  87. package/dist/workflow.js.map +1 -1
  88. package/package.json +46 -1
@@ -0,0 +1,461 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/webhook-entry.ts
21
+ var webhook_entry_exports = {};
22
+ __export(webhook_entry_exports, {
23
+ composeValidators: () => composeValidators,
24
+ createEventHandler: () => createEventHandler,
25
+ createExpressHandler: () => createExpressHandler,
26
+ createResultMapper: () => createResultMapper,
27
+ createSimpleHandler: () => createSimpleHandler,
28
+ createWebhookHandler: () => createWebhookHandler,
29
+ defaultUnexpectedErrorMapper: () => defaultUnexpectedErrorMapper,
30
+ defaultValidationErrorMapper: () => defaultValidationErrorMapper,
31
+ isValidationError: () => isValidationError,
32
+ requireFields: () => requireFields,
33
+ sendWebhookResponse: () => sendWebhookResponse,
34
+ toWebhookRequest: () => toWebhookRequest,
35
+ validationError: () => validationError
36
+ });
37
+ module.exports = __toCommonJS(webhook_entry_exports);
38
+
39
+ // src/slugs.ts
40
+ var AWAITLY_SLUGS = {
41
+ // --- step-* ---
42
+ "step-require-id": "step-require-id",
43
+ "step-no-immediate-execution": "step-no-immediate-execution",
44
+ "step-require-thunk-for-key": "step-require-thunk-for-key",
45
+ "step-no-bare-await": "step-no-bare-await",
46
+ "step-no-try-catch-wrap": "step-no-try-catch-wrap",
47
+ "step-stable-cache-keys": "step-stable-cache-keys",
48
+ // --- workflow-* ---
49
+ "workflow-no-floating": "workflow-no-floating",
50
+ "workflow-options-position": "workflow-options-position",
51
+ "workflow-callback-shape": "workflow-callback-shape",
52
+ "workflow-no-callable-form": "workflow-no-callable-form",
53
+ "workflow-no-dynamic-import": "workflow-no-dynamic-import",
54
+ "workflow-prefer-step-if": "workflow-prefer-step-if",
55
+ "workflow-prefer-step-foreach": "workflow-prefer-step-foreach",
56
+ // --- result-* ---
57
+ "result-no-floating": "result-no-floating",
58
+ "result-require-handling": "result-require-handling",
59
+ "result-no-double-wrap": "result-no-double-wrap",
60
+ "result-no-manual-propagation": "result-no-manual-propagation",
61
+ "result-no-direct-ok-err": "result-no-direct-ok-err",
62
+ // --- error-* ---
63
+ "error-check-unexpected-first": "error-check-unexpected-first",
64
+ "error-access-cause": "error-access-cause",
65
+ "error-normalize": "error-normalize",
66
+ "error-no-throw-in-deps": "error-no-throw-in-deps",
67
+ // --- concurrency-* ---
68
+ "concurrency-no-promise-all": "concurrency-no-promise-all",
69
+ "concurrency-no-promise-race": "concurrency-no-promise-race",
70
+ "concurrency-no-promise-allsettled": "concurrency-no-promise-allsettled",
71
+ // --- runtime-* ---
72
+ "runtime-step-timeout": "runtime-step-timeout",
73
+ "runtime-step-aborted": "runtime-step-aborted",
74
+ "runtime-retry-exhausted": "runtime-retry-exhausted",
75
+ "runtime-rate-limit": "runtime-rate-limit",
76
+ "runtime-circuit-open": "runtime-circuit-open",
77
+ "runtime-unexpected": "runtime-unexpected",
78
+ "runtime-resolver-not-found": "runtime-resolver-not-found",
79
+ "runtime-saga-compensation": "runtime-saga-compensation"
80
+ };
81
+ function slugDocsUrl(slug) {
82
+ return `https://jagreehal.github.io/awaitly/rules/#${slug}`;
83
+ }
84
+ var ALL_SLUGS = Object.keys(
85
+ AWAITLY_SLUGS
86
+ );
87
+
88
+ // src/tagged-error.ts
89
+ var InternalTaggedErrorBase = class extends Error {
90
+ type;
91
+ /** @deprecated Use `type`. */
92
+ _tag;
93
+ };
94
+ function TaggedError(tag, options) {
95
+ return class extends InternalTaggedErrorBase {
96
+ type = tag;
97
+ /** @deprecated Use `type`. */
98
+ _tag = tag;
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ constructor(props, errorOptions) {
101
+ const message = options?.message ? options.message(props ?? {}) : tag;
102
+ super(message);
103
+ this.name = tag;
104
+ if (options?.slug !== void 0) {
105
+ if (!options.hint) {
106
+ throw new TypeError(
107
+ `TaggedError: 'hint' is required when 'slug' is set (slug: "${options.slug}")`
108
+ );
109
+ }
110
+ Object.defineProperty(this, "code", {
111
+ value: options.slug,
112
+ enumerable: true,
113
+ writable: false,
114
+ configurable: false
115
+ });
116
+ Object.defineProperty(this, "hint", {
117
+ value: options.hint,
118
+ enumerable: true,
119
+ writable: false,
120
+ configurable: false
121
+ });
122
+ Object.defineProperty(this, "docsUrl", {
123
+ value: slugDocsUrl(options.slug),
124
+ enumerable: true,
125
+ writable: false,
126
+ configurable: false
127
+ });
128
+ }
129
+ Object.setPrototypeOf(this, new.target.prototype);
130
+ if (props && typeof props === "object") {
131
+ let safeProps;
132
+ if (options?.slug !== void 0) {
133
+ const {
134
+ _tag: _,
135
+ type: _t,
136
+ name: _n,
137
+ message: _m,
138
+ stack: _s,
139
+ code: _c,
140
+ hint: _h,
141
+ docsUrl: _d,
142
+ ...rest
143
+ } = props;
144
+ safeProps = rest;
145
+ } else {
146
+ const {
147
+ _tag: _,
148
+ type: _t,
149
+ name: _n,
150
+ message: _m,
151
+ stack: _s,
152
+ ...rest
153
+ } = props;
154
+ safeProps = rest;
155
+ }
156
+ const hasUserCause = Object.prototype.hasOwnProperty.call(
157
+ safeProps,
158
+ "cause"
159
+ );
160
+ const userCause = hasUserCause ? safeProps.cause : void 0;
161
+ if (hasUserCause) {
162
+ delete safeProps.cause;
163
+ }
164
+ const hasOptionsCause = errorOptions?.cause !== void 0;
165
+ if (hasUserCause && hasOptionsCause) {
166
+ throw new TypeError(
167
+ "TaggedError: cannot provide 'cause' in props when also setting ErrorOptions.cause"
168
+ );
169
+ }
170
+ Object.assign(this, safeProps);
171
+ if (hasUserCause) {
172
+ this.cause = userCause;
173
+ }
174
+ if (hasOptionsCause) {
175
+ this.cause = errorOptions?.cause;
176
+ }
177
+ } else if (errorOptions?.cause !== void 0) {
178
+ this.cause = errorOptions.cause;
179
+ }
180
+ }
181
+ };
182
+ }
183
+ Object.defineProperty(TaggedError, Symbol.hasInstance, {
184
+ value: (instance) => instance instanceof InternalTaggedErrorBase
185
+ });
186
+ ((TaggedError2) => {
187
+ function isError(value) {
188
+ return value instanceof Error;
189
+ }
190
+ TaggedError2.isError = isError;
191
+ function isTaggedError(value) {
192
+ return value instanceof InternalTaggedErrorBase;
193
+ }
194
+ TaggedError2.isTaggedError = isTaggedError;
195
+ function match(error, handlers) {
196
+ const tag = error._tag;
197
+ const handler = handlers[tag];
198
+ return handler(
199
+ error
200
+ );
201
+ }
202
+ TaggedError2.match = match;
203
+ function matchPartial(error, handlers, otherwise) {
204
+ const tag = error._tag;
205
+ const handler = handlers[tag];
206
+ if (handler) {
207
+ return handler(
208
+ error
209
+ );
210
+ }
211
+ return otherwise(error);
212
+ }
213
+ TaggedError2.matchPartial = matchPartial;
214
+ })(TaggedError || (TaggedError = {}));
215
+
216
+ // src/errors.ts
217
+ var UnexpectedError = class extends (/* @__PURE__ */ TaggedError("UnexpectedError", {
218
+ slug: "runtime-unexpected",
219
+ hint: "An unexpected exception escaped a step. Inspect cause; consider returning a typed Result instead of throwing.",
220
+ message: (p) => `UnexpectedError: ${p.cause instanceof Error ? p.cause.message : String(p.cause ?? "unknown")}`
221
+ })) {
222
+ };
223
+
224
+ // src/core/index.ts
225
+ function ok(value) {
226
+ return { ok: true, value };
227
+ }
228
+ function err(error, options) {
229
+ const cause = options?.cause;
230
+ return { ok: false, error, ...cause !== void 0 ? { cause } : {} };
231
+ }
232
+ var isOk = (r) => r.ok;
233
+ var isUnexpectedError = (e) => e instanceof UnexpectedError || typeof e === "object" && e !== null && "_tag" in e && e._tag === "UnexpectedError";
234
+
235
+ // src/webhook/index.ts
236
+ function isValidationError(e) {
237
+ return typeof e === "object" && e !== null && e.type === "VALIDATION_ERROR";
238
+ }
239
+ function defaultValidationErrorMapper(error) {
240
+ return {
241
+ status: 400,
242
+ body: {
243
+ error: {
244
+ type: error.type,
245
+ message: error.message,
246
+ details: error.field ? { field: error.field } : error.details
247
+ }
248
+ }
249
+ };
250
+ }
251
+ function defaultUnexpectedErrorMapper(error) {
252
+ return {
253
+ status: 500,
254
+ body: {
255
+ error: {
256
+ type: "INTERNAL_ERROR",
257
+ message: error instanceof Error ? error.message : "An unexpected error occurred"
258
+ }
259
+ }
260
+ };
261
+ }
262
+ function createWebhookHandler(workflow, workflowFn, config) {
263
+ const {
264
+ validateInput,
265
+ mapResult,
266
+ mapValidationError = defaultValidationErrorMapper,
267
+ mapUnexpectedError = defaultUnexpectedErrorMapper,
268
+ beforeValidation,
269
+ afterResponse
270
+ } = config;
271
+ return async (req) => {
272
+ try {
273
+ const processedReq = beforeValidation ? await beforeValidation(req) : req;
274
+ const validationResult = await validateInput(processedReq);
275
+ if (!isOk(validationResult)) {
276
+ const response2 = mapValidationError(validationResult.error, processedReq);
277
+ return afterResponse ? await afterResponse(response2, processedReq) : response2;
278
+ }
279
+ const args = validationResult.value;
280
+ const workflowResult = await workflow.run(
281
+ async ({ step, deps }) => workflowFn({ step, deps, args })
282
+ );
283
+ const response = mapResult(workflowResult, processedReq);
284
+ return afterResponse ? await afterResponse(response, processedReq) : response;
285
+ } catch (error) {
286
+ const response = mapUnexpectedError(error, req);
287
+ return afterResponse ? await afterResponse(response, req) : response;
288
+ }
289
+ };
290
+ }
291
+ function createSimpleHandler(config) {
292
+ const {
293
+ validateInput,
294
+ handler,
295
+ mapResult,
296
+ mapValidationError = defaultValidationErrorMapper,
297
+ mapUnexpectedError = defaultUnexpectedErrorMapper
298
+ } = config;
299
+ return async (req) => {
300
+ try {
301
+ const validationResult = await validateInput(req);
302
+ if (!isOk(validationResult)) {
303
+ return mapValidationError(validationResult.error, req);
304
+ }
305
+ const result = await handler(validationResult.value, req);
306
+ return mapResult(result, req);
307
+ } catch (error) {
308
+ return mapUnexpectedError(error, req);
309
+ }
310
+ };
311
+ }
312
+ function createResultMapper(mappings, options = {}) {
313
+ const { defaultStatus = 500, successStatus = 200 } = options;
314
+ const errorMap = /* @__PURE__ */ new Map();
315
+ for (const mapping of mappings) {
316
+ errorMap.set(mapping.error, mapping);
317
+ }
318
+ return (result) => {
319
+ if (result.ok) {
320
+ return {
321
+ status: successStatus,
322
+ body: result.value
323
+ };
324
+ }
325
+ if (isUnexpectedError(result.error)) {
326
+ return {
327
+ status: 500,
328
+ body: {
329
+ error: {
330
+ type: "INTERNAL_ERROR",
331
+ message: result.error instanceof Error ? result.error.message : "An unexpected error occurred"
332
+ }
333
+ }
334
+ };
335
+ }
336
+ const mapping = errorMap.get(result.error);
337
+ if (mapping) {
338
+ return {
339
+ status: mapping.status,
340
+ body: {
341
+ error: {
342
+ type: String(mapping.error),
343
+ message: mapping.message
344
+ }
345
+ }
346
+ };
347
+ }
348
+ return {
349
+ status: defaultStatus,
350
+ body: {
351
+ error: {
352
+ type: String(result.error)
353
+ }
354
+ }
355
+ };
356
+ };
357
+ }
358
+ function toWebhookRequest(req) {
359
+ return {
360
+ method: req.method,
361
+ path: req.path,
362
+ headers: req.headers,
363
+ body: req.body,
364
+ query: req.query,
365
+ params: req.params,
366
+ raw: req
367
+ };
368
+ }
369
+ function sendWebhookResponse(res, response) {
370
+ if (response.headers) {
371
+ res.set(response.headers);
372
+ }
373
+ res.status(response.status).json(response.body);
374
+ }
375
+ function createExpressHandler(handler) {
376
+ return async (req, res) => {
377
+ const webhookReq = toWebhookRequest(req);
378
+ const response = await handler(webhookReq);
379
+ sendWebhookResponse(res, response);
380
+ };
381
+ }
382
+ function validationError(message, field, details) {
383
+ return {
384
+ type: "VALIDATION_ERROR",
385
+ message,
386
+ field,
387
+ details
388
+ };
389
+ }
390
+ function requireFields(fields) {
391
+ return (body) => {
392
+ for (const field of fields) {
393
+ if (body[field] === void 0 || body[field] === null || body[field] === "") {
394
+ return err(validationError(`Missing required field: ${field}`, field));
395
+ }
396
+ }
397
+ return ok(void 0);
398
+ };
399
+ }
400
+ function composeValidators(...validators) {
401
+ return (input) => {
402
+ for (const validator of validators) {
403
+ const result = validator(input);
404
+ if (!result.ok) return result;
405
+ }
406
+ return ok(void 0);
407
+ };
408
+ }
409
+ function createEventHandler(workflow, workflowFn, config) {
410
+ const { validatePayload, mapResult } = config;
411
+ return async (event) => {
412
+ try {
413
+ const validationResult = validatePayload(event);
414
+ if (!isOk(validationResult)) {
415
+ return {
416
+ success: false,
417
+ ack: true,
418
+ // Don't retry validation errors
419
+ error: {
420
+ type: validationResult.error.type,
421
+ message: validationResult.error.message,
422
+ retryable: false
423
+ }
424
+ };
425
+ }
426
+ const args = validationResult.value;
427
+ const workflowResult = await workflow.run(
428
+ async ({ step, deps }) => workflowFn({ step, deps, args })
429
+ );
430
+ return mapResult(workflowResult, event);
431
+ } catch (error) {
432
+ return {
433
+ success: false,
434
+ ack: false,
435
+ // Retry unexpected errors
436
+ error: {
437
+ type: "UnexpectedError",
438
+ message: error instanceof Error ? error.message : String(error),
439
+ retryable: true
440
+ }
441
+ };
442
+ }
443
+ };
444
+ }
445
+ // Annotate the CommonJS export names for ESM import in node:
446
+ 0 && (module.exports = {
447
+ composeValidators,
448
+ createEventHandler,
449
+ createExpressHandler,
450
+ createResultMapper,
451
+ createSimpleHandler,
452
+ createWebhookHandler,
453
+ defaultUnexpectedErrorMapper,
454
+ defaultValidationErrorMapper,
455
+ isValidationError,
456
+ requireFields,
457
+ sendWebhookResponse,
458
+ toWebhookRequest,
459
+ validationError
460
+ });
461
+ //# sourceMappingURL=webhook.cjs.map