@codeinkit/flows-hoa 0.2.0-alpha.0 → 0.2.0-alpha.1

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/README.md CHANGED
@@ -136,11 +136,41 @@ What happens:
136
136
  - **Repeat while the first run is still going**: `IdempotencyInProgressError`.
137
137
  - **The action throws**: the key is deleted, so a retry runs the action again. The error is rethrown.
138
138
 
139
- | Error | `code` | `status` |
140
- | --- | --- | --- |
141
- | `IdempotencyKeyRequiredError` | `EIDEMPOTENCY_KEY_REQUIRED` | 400 |
142
- | `IdempotencyConflictError` | `EIDEMPOTENCY_CONFLICT` | 422 |
143
- | `IdempotencyInProgressError` | `EIDEMPOTENCY_IN_PROGRESS` | 409 |
139
+ ### Errors
140
+
141
+ All three errors extend `IdempotencyError` (which extends `Error`) and carry plain, readonly fields:
142
+
143
+ | Error | `status` | `code` | `description` |
144
+ | --- | --- | --- | --- |
145
+ | `IdempotencyKeyRequiredError` | 400 | `EIDEMPOTENCY_KEY_REQUIRED` | `An Idempotency-Key is required for this request.` |
146
+ | `IdempotencyConflictError` | 422 | `EIDEMPOTENCY_CONFLICT` | `This Idempotency-Key was already used with a different request.` |
147
+ | `IdempotencyInProgressError` | 409 | `EIDEMPOTENCY_IN_PROGRESS` | `A request with this Idempotency-Key is still in progress. Try again later.` |
148
+
149
+ | Field | |
150
+ | --- | --- |
151
+ | `status` | The HTTP status that fits the error. |
152
+ | `code` | Stable machine-readable code (type `IdempotencyErrorCode`). |
153
+ | `description` | Short text that is safe to send to a client. It never holds the idempotency key, the scope or a stored result. |
154
+ | `message` | For logs. It **does** hold the scope and the key, so do not send it to a client. |
155
+ | `scope` | The scope of the `withIdempotency` that threw. |
156
+ | `key` | The idempotency key (`IdempotencyConflictError` and `IdempotencyInProgressError` only). |
157
+
158
+ **In `@codeinkit/http-trigger`**: a thrown error with a `status` is answered with that status and the body `err.response ?? err.description ?? 'Internal server error'`. So a flow that lets these errors through answers 400 / 422 / 409 with the `description` as the body, with no mapping of your own. This package does not depend on http-trigger, the fields are plain properties any HTTP layer can read.
159
+
160
+ To tell them apart from other errors use `instanceof IdempotencyError` or the `isIdempotencyError(e)` type guard:
161
+
162
+ ```ts
163
+ import { isIdempotencyError } from '@codeinkit/flows-hoa';
164
+
165
+ try {
166
+ await flows.execute('checkout', input);
167
+ } catch (e) {
168
+ if (isIdempotencyError(e)) return { status: e.status, body: { code: e.code, error: e.description } };
169
+ throw e;
170
+ }
171
+ ```
172
+
173
+ `isIdempotencyError` also accepts an `Error` that has one of the three codes with its matching `status` and a string `description`. That keeps it working when two copies of this package are installed, where `instanceof` fails.
144
174
 
145
175
  The fingerprint is a JSON text with sorted object keys, hashed with FNV-1a (32 bit, not cryptographic). Key order in the body does not matter, array order does.
146
176
 
@@ -32,26 +32,52 @@ export interface IdempotencyOptions<D = any> {
32
32
  /** the part of the input that identifies the request. Default: `data.body ?? data.input ?? data.detail ?? null` */
33
33
  fingerprint?: (data: Partial<D>) => unknown;
34
34
  }
35
- export declare class IdempotencyKeyRequiredError extends Error {
36
- code: string;
37
- status: number;
38
- scope: string;
35
+ export type IdempotencyErrorCode = 'EIDEMPOTENCY_KEY_REQUIRED' | 'EIDEMPOTENCY_CONFLICT' | 'EIDEMPOTENCY_IN_PROGRESS';
36
+ /**
37
+ * Base class of the errors `withIdempotency` throws. Every one carries plain `status`, `code` and
38
+ * `description` fields, so an HTTP layer can answer from them without knowing this package
39
+ * (`@codeinkit/http-trigger` answers `status` with `description` as the body).
40
+ *
41
+ * `description` is safe to show to a client: it never holds the idempotency key, the scope or a
42
+ * stored result. `message` is for logs and does hold the key and the scope.
43
+ */
44
+ export declare class IdempotencyError extends Error {
45
+ /** HTTP status that fits the error: 400, 422 or 409 */
46
+ readonly status: number;
47
+ readonly code: IdempotencyErrorCode;
48
+ /** short, client-safe text. No key, no scope, no stored result */
49
+ readonly description: string;
50
+ readonly scope: string;
51
+ constructor(message: string, fields: {
52
+ status: number;
53
+ code: IdempotencyErrorCode;
54
+ description: string;
55
+ scope: string;
56
+ });
57
+ }
58
+ export declare class IdempotencyKeyRequiredError extends IdempotencyError {
59
+ readonly status: 400;
60
+ readonly code: 'EIDEMPOTENCY_KEY_REQUIRED';
39
61
  constructor(scope: string);
40
62
  }
41
- export declare class IdempotencyConflictError extends Error {
42
- code: string;
43
- status: number;
44
- scope: string;
45
- key: string;
63
+ export declare class IdempotencyConflictError extends IdempotencyError {
64
+ readonly status: 422;
65
+ readonly code: 'EIDEMPOTENCY_CONFLICT';
66
+ readonly key: string;
46
67
  constructor(scope: string, key: string);
47
68
  }
48
- export declare class IdempotencyInProgressError extends Error {
49
- code: string;
50
- status: number;
51
- scope: string;
52
- key: string;
69
+ export declare class IdempotencyInProgressError extends IdempotencyError {
70
+ readonly status: 409;
71
+ readonly code: 'EIDEMPOTENCY_IN_PROGRESS';
72
+ readonly key: string;
53
73
  constructor(scope: string, key: string);
54
74
  }
75
+ /**
76
+ * True for the errors `withIdempotency` throws. Besides `instanceof IdempotencyError` it accepts
77
+ * an error with the same `code`, `status` and a string `description`, so it also works when two
78
+ * copies of this package are installed (where `instanceof` fails).
79
+ */
80
+ export declare function isIdempotencyError(e: unknown): e is IdempotencyError;
55
81
  /**
56
82
  * Dedupes repeat runs that carry the same Idempotency-Key. The first run claims the key in the
57
83
  * store, runs the action and stores its (JSON-cloned) result. A repeat with the same key and the
@@ -1,44 +1,88 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMemoryIdempotencyStore = exports.withIdempotency = exports.IdempotencyInProgressError = exports.IdempotencyConflictError = exports.IdempotencyKeyRequiredError = void 0;
3
+ exports.createMemoryIdempotencyStore = exports.withIdempotency = exports.isIdempotencyError = exports.IdempotencyInProgressError = exports.IdempotencyConflictError = exports.IdempotencyKeyRequiredError = exports.IdempotencyError = void 0;
4
4
  const hoa_1 = require("./hoa");
5
5
  const internal_1 = require("./internal");
6
- /* ---------- errors ---------- */
7
- class IdempotencyKeyRequiredError extends Error {
6
+ /**
7
+ * Base class of the errors `withIdempotency` throws. Every one carries plain `status`, `code` and
8
+ * `description` fields, so an HTTP layer can answer from them without knowing this package
9
+ * (`@codeinkit/http-trigger` answers `status` with `description` as the body).
10
+ *
11
+ * `description` is safe to show to a client: it never holds the idempotency key, the scope or a
12
+ * stored result. `message` is for logs and does hold the key and the scope.
13
+ */
14
+ class IdempotencyError extends Error {
15
+ constructor(message, fields) {
16
+ super(message);
17
+ Object.setPrototypeOf(this, new.target.prototype);
18
+ this.name = 'IdempotencyError';
19
+ this.status = fields.status;
20
+ this.code = fields.code;
21
+ this.description = fields.description;
22
+ this.scope = fields.scope;
23
+ }
24
+ }
25
+ exports.IdempotencyError = IdempotencyError;
26
+ class IdempotencyKeyRequiredError extends IdempotencyError {
8
27
  constructor(scope) {
9
- super(`an idempotency key is required for ${scope}`);
10
- this.code = 'EIDEMPOTENCY_KEY_REQUIRED';
11
- this.status = 400;
12
- Object.setPrototypeOf(this, IdempotencyKeyRequiredError.prototype);
28
+ super(`an idempotency key is required for ${scope}`, {
29
+ status: 400,
30
+ code: 'EIDEMPOTENCY_KEY_REQUIRED',
31
+ description: 'An Idempotency-Key is required for this request.',
32
+ scope,
33
+ });
13
34
  this.name = 'IdempotencyKeyRequiredError';
14
- this.scope = scope;
15
35
  }
16
36
  }
17
37
  exports.IdempotencyKeyRequiredError = IdempotencyKeyRequiredError;
18
- class IdempotencyConflictError extends Error {
38
+ class IdempotencyConflictError extends IdempotencyError {
19
39
  constructor(scope, key) {
20
- super(`idempotency key "${key}" was already used for ${scope} with a different request`);
21
- this.code = 'EIDEMPOTENCY_CONFLICT';
22
- this.status = 422;
23
- Object.setPrototypeOf(this, IdempotencyConflictError.prototype);
40
+ super(`idempotency key "${key}" was already used for ${scope} with a different request`, {
41
+ status: 422,
42
+ code: 'EIDEMPOTENCY_CONFLICT',
43
+ description: 'This Idempotency-Key was already used with a different request.',
44
+ scope,
45
+ });
24
46
  this.name = 'IdempotencyConflictError';
25
- this.scope = scope;
26
47
  this.key = key;
27
48
  }
28
49
  }
29
50
  exports.IdempotencyConflictError = IdempotencyConflictError;
30
- class IdempotencyInProgressError extends Error {
51
+ class IdempotencyInProgressError extends IdempotencyError {
31
52
  constructor(scope, key) {
32
- super(`a request with idempotency key "${key}" is still in progress for ${scope}`);
33
- this.code = 'EIDEMPOTENCY_IN_PROGRESS';
34
- this.status = 409;
35
- Object.setPrototypeOf(this, IdempotencyInProgressError.prototype);
53
+ super(`a request with idempotency key "${key}" is still in progress for ${scope}`, {
54
+ status: 409,
55
+ code: 'EIDEMPOTENCY_IN_PROGRESS',
56
+ description: 'A request with this Idempotency-Key is still in progress. Try again later.',
57
+ scope,
58
+ });
36
59
  this.name = 'IdempotencyInProgressError';
37
- this.scope = scope;
38
60
  this.key = key;
39
61
  }
40
62
  }
41
63
  exports.IdempotencyInProgressError = IdempotencyInProgressError;
64
+ const STATUS_BY_CODE = {
65
+ EIDEMPOTENCY_KEY_REQUIRED: 400,
66
+ EIDEMPOTENCY_CONFLICT: 422,
67
+ EIDEMPOTENCY_IN_PROGRESS: 409,
68
+ };
69
+ /**
70
+ * True for the errors `withIdempotency` throws. Besides `instanceof IdempotencyError` it accepts
71
+ * an error with the same `code`, `status` and a string `description`, so it also works when two
72
+ * copies of this package are installed (where `instanceof` fails).
73
+ */
74
+ function isIdempotencyError(e) {
75
+ if (e instanceof IdempotencyError)
76
+ return true;
77
+ if (!(e instanceof Error))
78
+ return false;
79
+ const { code, status, description } = e;
80
+ return (typeof code === 'string' &&
81
+ Object.prototype.hasOwnProperty.call(STATUS_BY_CODE, code) &&
82
+ STATUS_BY_CODE[code] === status &&
83
+ typeof description === 'string');
84
+ }
85
+ exports.isIdempotencyError = isIdempotencyError;
42
86
  /* ---------- key and fingerprint ---------- */
43
87
  const DAY_MS = 24 * 60 * 60 * 1000;
44
88
  const asKey = (v) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeinkit/flows-hoa",
3
- "version": "0.2.0-alpha.0",
3
+ "version": "0.2.0-alpha.1",
4
4
  "description": "Higher-Order Actions (retry, fallback, timeout with cancellation, guard, idempotency, compensation) for @codeinkit/flows v4",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",