@amqp-contract/worker 0.24.0 → 1.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.
- package/README.md +18 -18
- package/dist/index.cjs +227 -159
- package/dist/index.d.cts +269 -88
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +269 -88
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +221 -153
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +499 -466
- package/package.json +18 -15
package/docs/index.md
CHANGED
|
@@ -8,24 +8,34 @@
|
|
|
8
8
|
|
|
9
9
|
### MessageValidationError
|
|
10
10
|
|
|
11
|
-
Defined in: packages/core/dist/index.d.mts:
|
|
11
|
+
Defined in: packages/core/dist/index.d.mts:40
|
|
12
12
|
|
|
13
13
|
Error thrown when message validation fails (payload or headers).
|
|
14
14
|
|
|
15
15
|
Used by both the client (publish-time payload validation) and the worker
|
|
16
|
-
(consume-time payload and headers validation).
|
|
16
|
+
(consume-time payload and headers validation). Carries a `_tag` of
|
|
17
|
+
`"@amqp-contract/MessageValidationError"` (namespaced to avoid collisions);
|
|
18
|
+
the `Error.name` is kept bare (`"MessageValidationError"`).
|
|
17
19
|
|
|
18
20
|
#### Param
|
|
19
21
|
|
|
22
|
+
**source**
|
|
23
|
+
|
|
20
24
|
The name of the publisher or consumer that triggered the validation
|
|
21
25
|
|
|
22
26
|
#### Param
|
|
23
27
|
|
|
28
|
+
**issues**
|
|
29
|
+
|
|
24
30
|
The validation issues from the Standard Schema validation
|
|
25
31
|
|
|
26
32
|
#### Extends
|
|
27
33
|
|
|
28
|
-
- `
|
|
34
|
+
- `MessageValidationError_base`<\{
|
|
35
|
+
`issues`: `unknown`;
|
|
36
|
+
`message`: `string`;
|
|
37
|
+
`source`: `string`;
|
|
38
|
+
\}>
|
|
29
39
|
|
|
30
40
|
#### Constructors
|
|
31
41
|
|
|
@@ -35,7 +45,7 @@ The validation issues from the Standard Schema validation
|
|
|
35
45
|
new MessageValidationError(source, issues): MessageValidationError;
|
|
36
46
|
```
|
|
37
47
|
|
|
38
|
-
Defined in: packages/core/dist/index.d.mts:
|
|
48
|
+
Defined in: packages/core/dist/index.d.mts:45
|
|
39
49
|
|
|
40
50
|
###### Parameters
|
|
41
51
|
|
|
@@ -51,136 +61,45 @@ Defined in: packages/core/dist/index.d.mts:30
|
|
|
51
61
|
###### Overrides
|
|
52
62
|
|
|
53
63
|
```ts
|
|
54
|
-
|
|
64
|
+
MessageValidationError_base<{
|
|
65
|
+
message: string;
|
|
66
|
+
source: string;
|
|
67
|
+
issues: unknown;
|
|
68
|
+
}>.constructor
|
|
55
69
|
```
|
|
56
70
|
|
|
57
71
|
#### Properties
|
|
58
72
|
|
|
59
|
-
| Property | Modifier | Type |
|
|
60
|
-
| ------ | ------ | ------ | ------ | ------ |
|
|
61
|
-
| <a id="
|
|
62
|
-
| <a id="
|
|
63
|
-
| <a id="
|
|
64
|
-
| <a id="
|
|
65
|
-
| <a id="
|
|
66
|
-
| <a id="
|
|
67
|
-
| <a id="
|
|
68
|
-
|
|
69
|
-
#### Methods
|
|
70
|
-
|
|
71
|
-
##### captureStackTrace()
|
|
72
|
-
|
|
73
|
-
```ts
|
|
74
|
-
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:52
|
|
78
|
-
|
|
79
|
-
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
80
|
-
a string representing the location in the code at which
|
|
81
|
-
`Error.captureStackTrace()` was called.
|
|
82
|
-
|
|
83
|
-
```js
|
|
84
|
-
const myObject = {};
|
|
85
|
-
Error.captureStackTrace(myObject);
|
|
86
|
-
myObject.stack; // Similar to `new Error().stack`
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
The first line of the trace will be prefixed with
|
|
90
|
-
`${myObject.name}: ${myObject.message}`.
|
|
91
|
-
|
|
92
|
-
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
93
|
-
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
94
|
-
generated stack trace.
|
|
95
|
-
|
|
96
|
-
The `constructorOpt` argument is useful for hiding implementation
|
|
97
|
-
details of error generation from the user. For instance:
|
|
98
|
-
|
|
99
|
-
```js
|
|
100
|
-
function a() {
|
|
101
|
-
b();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function b() {
|
|
105
|
-
c();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function c() {
|
|
109
|
-
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
110
|
-
const { stackTraceLimit } = Error;
|
|
111
|
-
Error.stackTraceLimit = 0;
|
|
112
|
-
const error = new Error();
|
|
113
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
114
|
-
|
|
115
|
-
// Capture the stack trace above function b
|
|
116
|
-
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
a();
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
###### Parameters
|
|
124
|
-
|
|
125
|
-
| Parameter | Type |
|
|
126
|
-
| ------ | ------ |
|
|
127
|
-
| `targetObject` | `object` |
|
|
128
|
-
| `constructorOpt?` | `Function` |
|
|
129
|
-
|
|
130
|
-
###### Returns
|
|
131
|
-
|
|
132
|
-
`void`
|
|
133
|
-
|
|
134
|
-
###### Inherited from
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
Error.captureStackTrace
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
##### prepareStackTrace()
|
|
141
|
-
|
|
142
|
-
```ts
|
|
143
|
-
static prepareStackTrace(err, stackTraces): any;
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:56
|
|
147
|
-
|
|
148
|
-
###### Parameters
|
|
149
|
-
|
|
150
|
-
| Parameter | Type |
|
|
151
|
-
| ------ | ------ |
|
|
152
|
-
| `err` | `Error` |
|
|
153
|
-
| `stackTraces` | `CallSite`[] |
|
|
154
|
-
|
|
155
|
-
###### Returns
|
|
156
|
-
|
|
157
|
-
`any`
|
|
158
|
-
|
|
159
|
-
###### See
|
|
160
|
-
|
|
161
|
-
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
162
|
-
|
|
163
|
-
###### Inherited from
|
|
164
|
-
|
|
165
|
-
```ts
|
|
166
|
-
Error.prepareStackTrace
|
|
167
|
-
```
|
|
73
|
+
| Property | Modifier | Type | Inherited from | Defined in |
|
|
74
|
+
| ------ | ------ | ------ | ------ | ------ |
|
|
75
|
+
| <a id="_tag"></a> `_tag` | `readonly` | `"@amqp-contract/MessageValidationError"` | `MessageValidationError_base._tag` | node\_modules/.pnpm/unthrown@0.2.0/node\_modules/unthrown/dist/index.d.mts:638 |
|
|
76
|
+
| <a id="cause"></a> `cause?` | `public` | `unknown` | `MessageValidationError_base.cause` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
77
|
+
| <a id="issues"></a> `issues` | `readonly` | `unknown` | `MessageValidationError_base.issues` | packages/core/dist/index.d.mts:43 |
|
|
78
|
+
| <a id="message"></a> `message` | `public` | `string` | `MessageValidationError_base.message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
79
|
+
| <a id="name"></a> `name` | `public` | `string` | `MessageValidationError_base.name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
80
|
+
| <a id="source"></a> `source` | `readonly` | `string` | `MessageValidationError_base.source` | packages/core/dist/index.d.mts:42 |
|
|
81
|
+
| <a id="stack"></a> `stack?` | `public` | `string` | `MessageValidationError_base.stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
168
82
|
|
|
169
83
|
***
|
|
170
84
|
|
|
171
85
|
### NonRetryableError
|
|
172
86
|
|
|
173
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
87
|
+
Defined in: [packages/worker/src/errors.ts:37](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L37)
|
|
174
88
|
|
|
175
89
|
Non-retryable errors - permanent failures that should not be retried
|
|
176
90
|
Examples: invalid data, business rule violations, permanent external failures
|
|
177
91
|
|
|
178
92
|
Use this error type when retrying would not help - the message will be
|
|
179
|
-
immediately sent to the dead letter queue (DLQ) if configured.
|
|
93
|
+
immediately sent to the dead letter queue (DLQ) if configured. Carries a
|
|
94
|
+
namespaced `_tag` of `"@amqp-contract/NonRetryableError"`; the `Error.name` is
|
|
95
|
+
kept bare (`"NonRetryableError"`).
|
|
180
96
|
|
|
181
97
|
#### Extends
|
|
182
98
|
|
|
183
|
-
- `
|
|
99
|
+
- `TaggedErrorInstance`<`"@amqp-contract/NonRetryableError"`, \{
|
|
100
|
+
`cause?`: `unknown`;
|
|
101
|
+
`message`: `string`;
|
|
102
|
+
\}>
|
|
184
103
|
|
|
185
104
|
#### Constructors
|
|
186
105
|
|
|
@@ -190,7 +109,7 @@ immediately sent to the dead letter queue (DLQ) if configured.
|
|
|
190
109
|
new NonRetryableError(message, cause?): NonRetryableError;
|
|
191
110
|
```
|
|
192
111
|
|
|
193
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
112
|
+
Defined in: [packages/worker/src/errors.ts:43](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L43)
|
|
194
113
|
|
|
195
114
|
###### Parameters
|
|
196
115
|
|
|
@@ -206,124 +125,29 @@ Defined in: [packages/worker/src/errors.ts:35](https://github.com/btravers/amqp-
|
|
|
206
125
|
###### Overrides
|
|
207
126
|
|
|
208
127
|
```ts
|
|
209
|
-
|
|
128
|
+
TaggedError("@amqp-contract/NonRetryableError", {
|
|
129
|
+
name: "NonRetryableError",
|
|
130
|
+
})<{
|
|
131
|
+
message: string;
|
|
132
|
+
cause?: unknown;
|
|
133
|
+
}>.constructor
|
|
210
134
|
```
|
|
211
135
|
|
|
212
136
|
#### Properties
|
|
213
137
|
|
|
214
|
-
| Property | Modifier | Type |
|
|
215
|
-
| ------ | ------ | ------ | ------ | ------ |
|
|
216
|
-
| <a id="
|
|
217
|
-
| <a id="
|
|
218
|
-
| <a id="
|
|
219
|
-
| <a id="
|
|
220
|
-
| <a id="
|
|
221
|
-
|
|
222
|
-
#### Methods
|
|
223
|
-
|
|
224
|
-
##### captureStackTrace()
|
|
225
|
-
|
|
226
|
-
```ts
|
|
227
|
-
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:52
|
|
231
|
-
|
|
232
|
-
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
233
|
-
a string representing the location in the code at which
|
|
234
|
-
`Error.captureStackTrace()` was called.
|
|
235
|
-
|
|
236
|
-
```js
|
|
237
|
-
const myObject = {};
|
|
238
|
-
Error.captureStackTrace(myObject);
|
|
239
|
-
myObject.stack; // Similar to `new Error().stack`
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
The first line of the trace will be prefixed with
|
|
243
|
-
`${myObject.name}: ${myObject.message}`.
|
|
244
|
-
|
|
245
|
-
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
246
|
-
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
247
|
-
generated stack trace.
|
|
248
|
-
|
|
249
|
-
The `constructorOpt` argument is useful for hiding implementation
|
|
250
|
-
details of error generation from the user. For instance:
|
|
251
|
-
|
|
252
|
-
```js
|
|
253
|
-
function a() {
|
|
254
|
-
b();
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function b() {
|
|
258
|
-
c();
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function c() {
|
|
262
|
-
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
263
|
-
const { stackTraceLimit } = Error;
|
|
264
|
-
Error.stackTraceLimit = 0;
|
|
265
|
-
const error = new Error();
|
|
266
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
267
|
-
|
|
268
|
-
// Capture the stack trace above function b
|
|
269
|
-
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
270
|
-
throw error;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
a();
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
###### Parameters
|
|
277
|
-
|
|
278
|
-
| Parameter | Type |
|
|
279
|
-
| ------ | ------ |
|
|
280
|
-
| `targetObject` | `object` |
|
|
281
|
-
| `constructorOpt?` | `Function` |
|
|
282
|
-
|
|
283
|
-
###### Returns
|
|
284
|
-
|
|
285
|
-
`void`
|
|
286
|
-
|
|
287
|
-
###### Inherited from
|
|
288
|
-
|
|
289
|
-
```ts
|
|
290
|
-
Error.captureStackTrace
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
##### prepareStackTrace()
|
|
294
|
-
|
|
295
|
-
```ts
|
|
296
|
-
static prepareStackTrace(err, stackTraces): any;
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:56
|
|
300
|
-
|
|
301
|
-
###### Parameters
|
|
302
|
-
|
|
303
|
-
| Parameter | Type |
|
|
304
|
-
| ------ | ------ |
|
|
305
|
-
| `err` | `Error` |
|
|
306
|
-
| `stackTraces` | `CallSite`[] |
|
|
307
|
-
|
|
308
|
-
###### Returns
|
|
309
|
-
|
|
310
|
-
`any`
|
|
311
|
-
|
|
312
|
-
###### See
|
|
313
|
-
|
|
314
|
-
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
315
|
-
|
|
316
|
-
###### Inherited from
|
|
317
|
-
|
|
318
|
-
```ts
|
|
319
|
-
Error.prepareStackTrace
|
|
320
|
-
```
|
|
138
|
+
| Property | Modifier | Type | Inherited from | Defined in |
|
|
139
|
+
| ------ | ------ | ------ | ------ | ------ |
|
|
140
|
+
| <a id="_tag-1"></a> `_tag` | `readonly` | `"@amqp-contract/NonRetryableError"` | `TaggedError("@amqp-contract/NonRetryableError", { name: "NonRetryableError", })._tag` | node\_modules/.pnpm/unthrown@0.2.0/node\_modules/unthrown/dist/index.d.mts:638 |
|
|
141
|
+
| <a id="cause-1"></a> `cause?` | `public` | `unknown` | [`MessageValidationError`](#messagevalidationerror).[`cause`](#cause) | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
142
|
+
| <a id="message-1"></a> `message` | `public` | `string` | `TaggedError("@amqp-contract/NonRetryableError", { name: "NonRetryableError", }).message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
143
|
+
| <a id="name-1"></a> `name` | `public` | `string` | `TaggedError("@amqp-contract/NonRetryableError", { name: "NonRetryableError", }).name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
144
|
+
| <a id="stack-1"></a> `stack?` | `public` | `string` | `TaggedError("@amqp-contract/NonRetryableError", { name: "NonRetryableError", }).stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
321
145
|
|
|
322
146
|
***
|
|
323
147
|
|
|
324
148
|
### RetryableError
|
|
325
149
|
|
|
326
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
150
|
+
Defined in: [packages/worker/src/errors.ts:17](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L17)
|
|
327
151
|
|
|
328
152
|
Retryable errors - transient failures that may succeed on retry
|
|
329
153
|
Examples: network timeouts, rate limiting, temporary service unavailability
|
|
@@ -331,9 +155,17 @@ Examples: network timeouts, rate limiting, temporary service unavailability
|
|
|
331
155
|
Use this error type when the operation might succeed if retried.
|
|
332
156
|
The worker will apply exponential backoff and retry the message.
|
|
333
157
|
|
|
158
|
+
Built on unthrown's [TaggedError](https://github.com/btravstack/unthrown), so it carries a namespaced `_tag` of
|
|
159
|
+
`"@amqp-contract/RetryableError"` (to avoid colliding with other libraries'
|
|
160
|
+
tags in a shared `matchTags`) for exhaustive dispatch; the `Error.name` is
|
|
161
|
+
kept bare (`"RetryableError"`).
|
|
162
|
+
|
|
334
163
|
#### Extends
|
|
335
164
|
|
|
336
|
-
- `
|
|
165
|
+
- `TaggedErrorInstance`<`"@amqp-contract/RetryableError"`, \{
|
|
166
|
+
`cause?`: `unknown`;
|
|
167
|
+
`message`: `string`;
|
|
168
|
+
\}>
|
|
337
169
|
|
|
338
170
|
#### Constructors
|
|
339
171
|
|
|
@@ -343,7 +175,7 @@ The worker will apply exponential backoff and retry the message.
|
|
|
343
175
|
new RetryableError(message, cause?): RetryableError;
|
|
344
176
|
```
|
|
345
177
|
|
|
346
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
178
|
+
Defined in: [packages/worker/src/errors.ts:23](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L23)
|
|
347
179
|
|
|
348
180
|
###### Parameters
|
|
349
181
|
|
|
@@ -359,124 +191,29 @@ Defined in: [packages/worker/src/errors.ts:11](https://github.com/btravers/amqp-
|
|
|
359
191
|
###### Overrides
|
|
360
192
|
|
|
361
193
|
```ts
|
|
362
|
-
|
|
194
|
+
TaggedError("@amqp-contract/RetryableError", {
|
|
195
|
+
name: "RetryableError",
|
|
196
|
+
})<{
|
|
197
|
+
message: string;
|
|
198
|
+
cause?: unknown;
|
|
199
|
+
}>.constructor
|
|
363
200
|
```
|
|
364
201
|
|
|
365
202
|
#### Properties
|
|
366
203
|
|
|
367
|
-
| Property | Modifier | Type |
|
|
368
|
-
| ------ | ------ | ------ | ------ | ------ |
|
|
369
|
-
| <a id="
|
|
370
|
-
| <a id="
|
|
371
|
-
| <a id="
|
|
372
|
-
| <a id="
|
|
373
|
-
| <a id="
|
|
374
|
-
|
|
375
|
-
#### Methods
|
|
376
|
-
|
|
377
|
-
##### captureStackTrace()
|
|
378
|
-
|
|
379
|
-
```ts
|
|
380
|
-
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:52
|
|
384
|
-
|
|
385
|
-
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
386
|
-
a string representing the location in the code at which
|
|
387
|
-
`Error.captureStackTrace()` was called.
|
|
388
|
-
|
|
389
|
-
```js
|
|
390
|
-
const myObject = {};
|
|
391
|
-
Error.captureStackTrace(myObject);
|
|
392
|
-
myObject.stack; // Similar to `new Error().stack`
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
The first line of the trace will be prefixed with
|
|
396
|
-
`${myObject.name}: ${myObject.message}`.
|
|
397
|
-
|
|
398
|
-
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
399
|
-
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
400
|
-
generated stack trace.
|
|
401
|
-
|
|
402
|
-
The `constructorOpt` argument is useful for hiding implementation
|
|
403
|
-
details of error generation from the user. For instance:
|
|
404
|
-
|
|
405
|
-
```js
|
|
406
|
-
function a() {
|
|
407
|
-
b();
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
function b() {
|
|
411
|
-
c();
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
function c() {
|
|
415
|
-
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
416
|
-
const { stackTraceLimit } = Error;
|
|
417
|
-
Error.stackTraceLimit = 0;
|
|
418
|
-
const error = new Error();
|
|
419
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
420
|
-
|
|
421
|
-
// Capture the stack trace above function b
|
|
422
|
-
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
423
|
-
throw error;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
a();
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
###### Parameters
|
|
430
|
-
|
|
431
|
-
| Parameter | Type |
|
|
432
|
-
| ------ | ------ |
|
|
433
|
-
| `targetObject` | `object` |
|
|
434
|
-
| `constructorOpt?` | `Function` |
|
|
435
|
-
|
|
436
|
-
###### Returns
|
|
437
|
-
|
|
438
|
-
`void`
|
|
439
|
-
|
|
440
|
-
###### Inherited from
|
|
441
|
-
|
|
442
|
-
```ts
|
|
443
|
-
Error.captureStackTrace
|
|
444
|
-
```
|
|
445
|
-
|
|
446
|
-
##### prepareStackTrace()
|
|
447
|
-
|
|
448
|
-
```ts
|
|
449
|
-
static prepareStackTrace(err, stackTraces): any;
|
|
450
|
-
```
|
|
451
|
-
|
|
452
|
-
Defined in: node\_modules/.pnpm/@types+node@24.12.2/node\_modules/@types/node/globals.d.ts:56
|
|
453
|
-
|
|
454
|
-
###### Parameters
|
|
455
|
-
|
|
456
|
-
| Parameter | Type |
|
|
457
|
-
| ------ | ------ |
|
|
458
|
-
| `err` | `Error` |
|
|
459
|
-
| `stackTraces` | `CallSite`[] |
|
|
460
|
-
|
|
461
|
-
###### Returns
|
|
462
|
-
|
|
463
|
-
`any`
|
|
464
|
-
|
|
465
|
-
###### See
|
|
466
|
-
|
|
467
|
-
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
468
|
-
|
|
469
|
-
###### Inherited from
|
|
470
|
-
|
|
471
|
-
```ts
|
|
472
|
-
Error.prepareStackTrace
|
|
473
|
-
```
|
|
204
|
+
| Property | Modifier | Type | Inherited from | Defined in |
|
|
205
|
+
| ------ | ------ | ------ | ------ | ------ |
|
|
206
|
+
| <a id="_tag-2"></a> `_tag` | `readonly` | `"@amqp-contract/RetryableError"` | `TaggedError("@amqp-contract/RetryableError", { name: "RetryableError", })._tag` | node\_modules/.pnpm/unthrown@0.2.0/node\_modules/unthrown/dist/index.d.mts:638 |
|
|
207
|
+
| <a id="cause-2"></a> `cause?` | `public` | `unknown` | [`MessageValidationError`](#messagevalidationerror).[`cause`](#cause) | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
208
|
+
| <a id="message-2"></a> `message` | `public` | `string` | `TaggedError("@amqp-contract/RetryableError", { name: "RetryableError", }).message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
209
|
+
| <a id="name-2"></a> `name` | `public` | `string` | `TaggedError("@amqp-contract/RetryableError", { name: "RetryableError", }).name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
210
|
+
| <a id="stack-2"></a> `stack?` | `public` | `string` | `TaggedError("@amqp-contract/RetryableError", { name: "RetryableError", }).stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
474
211
|
|
|
475
212
|
***
|
|
476
213
|
|
|
477
214
|
### TypedAmqpWorker
|
|
478
215
|
|
|
479
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
216
|
+
Defined in: [packages/worker/src/worker.ts:189](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L189)
|
|
480
217
|
|
|
481
218
|
Type-safe AMQP worker for consuming messages from RabbitMQ.
|
|
482
219
|
|
|
@@ -488,7 +225,7 @@ and error handling for consuming messages based on a contract definition.
|
|
|
488
225
|
```typescript
|
|
489
226
|
import { TypedAmqpWorker } from '@amqp-contract/worker';
|
|
490
227
|
import { defineQueue, defineMessage, defineContract, defineConsumer } from '@amqp-contract/contract';
|
|
491
|
-
import {
|
|
228
|
+
import { ok } from 'unthrown';
|
|
492
229
|
import { z } from 'zod';
|
|
493
230
|
|
|
494
231
|
const orderQueue = defineQueue('order-processing');
|
|
@@ -508,14 +245,13 @@ const result = await TypedAmqpWorker.create({
|
|
|
508
245
|
handlers: {
|
|
509
246
|
processOrder: ({ payload }) => {
|
|
510
247
|
console.log('Processing order', payload.orderId);
|
|
511
|
-
return
|
|
248
|
+
return ok(undefined).toAsync();
|
|
512
249
|
},
|
|
513
250
|
},
|
|
514
251
|
urls: ['amqp://localhost'],
|
|
515
252
|
});
|
|
516
253
|
|
|
517
|
-
|
|
518
|
-
const worker = result.value;
|
|
254
|
+
const worker = result.unwrap();
|
|
519
255
|
|
|
520
256
|
// Close when done
|
|
521
257
|
await worker.close();
|
|
@@ -532,10 +268,10 @@ await worker.close();
|
|
|
532
268
|
##### close()
|
|
533
269
|
|
|
534
270
|
```ts
|
|
535
|
-
close():
|
|
271
|
+
close(): AsyncResult<void, TechnicalError>;
|
|
536
272
|
```
|
|
537
273
|
|
|
538
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
274
|
+
Defined in: [packages/worker/src/worker.ts:350](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L350)
|
|
539
275
|
|
|
540
276
|
Close the AMQP channel and connection.
|
|
541
277
|
|
|
@@ -544,7 +280,7 @@ stopping all message consumption and cleaning up resources.
|
|
|
544
280
|
|
|
545
281
|
###### Returns
|
|
546
282
|
|
|
547
|
-
`
|
|
283
|
+
`AsyncResult`<`void`, `TechnicalError`>
|
|
548
284
|
|
|
549
285
|
###### Example
|
|
550
286
|
|
|
@@ -558,15 +294,15 @@ if (closeResult.isOk()) {
|
|
|
558
294
|
##### create()
|
|
559
295
|
|
|
560
296
|
```ts
|
|
561
|
-
static create<TContract>(__namedParameters):
|
|
297
|
+
static create<TContract>(__namedParameters): AsyncResult<TypedAmqpWorker<TContract>, TechnicalError>;
|
|
562
298
|
```
|
|
563
299
|
|
|
564
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
300
|
+
Defined in: [packages/worker/src/worker.ts:288](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L288)
|
|
565
301
|
|
|
566
302
|
Create a type-safe AMQP worker from a contract.
|
|
567
303
|
|
|
568
304
|
Connection management (including automatic reconnection) is handled internally
|
|
569
|
-
by amqp-connection-manager via the [AmqpClient](https://
|
|
305
|
+
by amqp-connection-manager via the [AmqpClient](https://btravstack.github.io/amqp-contract/api/core#amqpclient). The worker will set up
|
|
570
306
|
consumers for all contract-defined handlers asynchronously in the background
|
|
571
307
|
once the underlying connection and channels are ready.
|
|
572
308
|
|
|
@@ -587,9 +323,9 @@ URLs and connection options, following RabbitMQ best practices.
|
|
|
587
323
|
|
|
588
324
|
###### Returns
|
|
589
325
|
|
|
590
|
-
`
|
|
326
|
+
`AsyncResult`<[`TypedAmqpWorker`](#typedamqpworker)<`TContract`>, `TechnicalError`>
|
|
591
327
|
|
|
592
|
-
A
|
|
328
|
+
A AsyncResult that resolves to the worker or a TechnicalError.
|
|
593
329
|
|
|
594
330
|
###### Example
|
|
595
331
|
|
|
@@ -597,7 +333,7 @@ A ResultAsync that resolves to the worker or a TechnicalError.
|
|
|
597
333
|
const result = await TypedAmqpWorker.create({
|
|
598
334
|
contract: myContract,
|
|
599
335
|
handlers: {
|
|
600
|
-
processOrder: ({ payload }) =>
|
|
336
|
+
processOrder: ({ payload }) => ok(undefined).toAsync(),
|
|
601
337
|
},
|
|
602
338
|
urls: ['amqp://localhost'],
|
|
603
339
|
});
|
|
@@ -611,7 +347,7 @@ const result = await TypedAmqpWorker.create({
|
|
|
611
347
|
type ConsumerOptions = AmqpClientConsumerOptions;
|
|
612
348
|
```
|
|
613
349
|
|
|
614
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
350
|
+
Defined in: [packages/worker/src/worker.ts:58](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L58)
|
|
615
351
|
|
|
616
352
|
***
|
|
617
353
|
|
|
@@ -621,7 +357,7 @@ Defined in: [packages/worker/src/worker.ts:49](https://github.com/btravers/amqp-
|
|
|
621
357
|
type CreateWorkerOptions<TContract> = object;
|
|
622
358
|
```
|
|
623
359
|
|
|
624
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
360
|
+
Defined in: [packages/worker/src/worker.ts:105](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L105)
|
|
625
361
|
|
|
626
362
|
Options for creating a type-safe AMQP worker.
|
|
627
363
|
|
|
@@ -634,13 +370,13 @@ const options: CreateWorkerOptions<typeof contract> = {
|
|
|
634
370
|
// Simple handler
|
|
635
371
|
processOrder: ({ payload }) => {
|
|
636
372
|
console.log('Processing order:', payload.orderId);
|
|
637
|
-
return
|
|
373
|
+
return ok(undefined).toAsync();
|
|
638
374
|
},
|
|
639
375
|
// Handler with prefetch configuration
|
|
640
376
|
processPayment: [
|
|
641
377
|
({ payload }) => {
|
|
642
378
|
console.log('Processing payment:', payload.paymentId);
|
|
643
|
-
return
|
|
379
|
+
return ok(undefined).toAsync();
|
|
644
380
|
},
|
|
645
381
|
{ prefetch: 10 }
|
|
646
382
|
]
|
|
@@ -669,14 +405,14 @@ not at the handler level. See `QueueDefinition.retry` for configuration options.
|
|
|
669
405
|
|
|
670
406
|
| Property | Type | Description | Defined in |
|
|
671
407
|
| ------ | ------ | ------ | ------ |
|
|
672
|
-
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [packages/worker/src/worker.ts:
|
|
673
|
-
| <a id="connecttimeoutms"></a> `connectTimeoutMs?` | `number` \| `null` | Maximum time in ms to wait for the AMQP connection to become ready before `create()` resolves to an `err(TechnicalError)`. Defaults to 30s (the [AmqpClient](https://
|
|
674
|
-
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:
|
|
675
|
-
| <a id="defaultconsumeroptions"></a> `defaultConsumerOptions?` | [`ConsumerOptions`](#consumeroptions) | Optional default consumer options applied to all consumer handlers. Handler-specific options provided in tuple form override these defaults. | [packages/worker/src/worker.ts:
|
|
676
|
-
| <a id="handlers"></a> `handlers` | `WorkerInferHandlers
|
|
677
|
-
| <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:
|
|
678
|
-
| <a id="telemetry"></a> `telemetry?` | `TelemetryProvider` | Optional telemetry provider for tracing and metrics. If not provided, uses the default provider which attempts to load OpenTelemetry. OpenTelemetry instrumentation is automatically enabled if @opentelemetry/api is installed. | [packages/worker/src/worker.ts:
|
|
679
|
-
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:
|
|
408
|
+
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [packages/worker/src/worker.ts:122](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L122) |
|
|
409
|
+
| <a id="connecttimeoutms"></a> `connectTimeoutMs?` | `number` \| `null` | Maximum time in ms to wait for the AMQP connection to become ready before `create()` resolves to an `err(TechnicalError)`. Defaults to 30s (the [AmqpClient](https://btravstack.github.io/amqp-contract/api/core#amqpclient)'s `DEFAULT_CONNECT_TIMEOUT_MS`). Pass `null` to disable the timeout and let amqp-connection-manager retry indefinitely. | [packages/worker/src/worker.ts:142](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L142) |
|
|
410
|
+
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:107](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L107) |
|
|
411
|
+
| <a id="defaultconsumeroptions"></a> `defaultConsumerOptions?` | [`ConsumerOptions`](#consumeroptions) | Optional default consumer options applied to all consumer handlers. Handler-specific options provided in tuple form override these defaults. | [packages/worker/src/worker.ts:135](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L135) |
|
|
412
|
+
| <a id="handlers"></a> `handlers` | [`WorkerInferHandlers`](#workerinferhandlers)<`TContract`> | Handlers for each `consumers` and `rpcs` entry in the contract. - Regular consumers return `AsyncResult<void, HandlerError>`. - RPC handlers return `AsyncResult<TResponse, HandlerError>` where `TResponse` is inferred from the RPC's response message schema. Use `defineHandler` / `defineHandlers` to create handlers with full type inference. | [packages/worker/src/worker.ts:118](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L118) |
|
|
413
|
+
| <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:124](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L124) |
|
|
414
|
+
| <a id="telemetry"></a> `telemetry?` | `TelemetryProvider` | Optional telemetry provider for tracing and metrics. If not provided, uses the default provider which attempts to load OpenTelemetry. OpenTelemetry instrumentation is automatically enabled if @opentelemetry/api is installed. | [packages/worker/src/worker.ts:130](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L130) |
|
|
415
|
+
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:120](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/worker.ts#L120) |
|
|
680
416
|
|
|
681
417
|
***
|
|
682
418
|
|
|
@@ -688,10 +424,16 @@ type HandlerError =
|
|
|
688
424
|
| NonRetryableError;
|
|
689
425
|
```
|
|
690
426
|
|
|
691
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
427
|
+
Defined in: [packages/worker/src/errors.ts:58](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L58)
|
|
428
|
+
|
|
429
|
+
Any handler-signalled error — the union a handler may put in the `Err`
|
|
430
|
+
channel of its `AsyncResult`. Discriminate on `_tag`
|
|
431
|
+
(`"@amqp-contract/RetryableError"` / `"@amqp-contract/NonRetryableError"`),
|
|
432
|
+
e.g. with `matchTags`.
|
|
692
433
|
|
|
693
|
-
|
|
694
|
-
|
|
434
|
+
Previously an abstract base class; now a tagged union, because unthrown's
|
|
435
|
+
`TaggedError` mints a distinct base class per tag. Use [isHandlerError](#ishandlererror)
|
|
436
|
+
for runtime narrowing instead of `instanceof HandlerError`.
|
|
695
437
|
|
|
696
438
|
***
|
|
697
439
|
|
|
@@ -701,7 +443,7 @@ Use this type when defining handlers that explicitly signal error outcomes.
|
|
|
701
443
|
type WorkerConsumedMessage<TPayload, THeaders> = object;
|
|
702
444
|
```
|
|
703
445
|
|
|
704
|
-
Defined in: [packages/worker/src/types.ts:156](https://github.com/
|
|
446
|
+
Defined in: [packages/worker/src/types.ts:156](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L156)
|
|
705
447
|
|
|
706
448
|
A consumed message containing parsed payload and headers.
|
|
707
449
|
|
|
@@ -715,7 +457,7 @@ const handler = defineHandler(contract, 'processOrder', (message, rawMessage) =>
|
|
|
715
457
|
console.log(message.payload.orderId); // Typed payload
|
|
716
458
|
console.log(message.headers?.priority); // Typed headers (if defined)
|
|
717
459
|
console.log(rawMessage.fields.deliveryTag); // Raw AMQP message
|
|
718
|
-
return
|
|
460
|
+
return ok(undefined).toAsync();
|
|
719
461
|
});
|
|
720
462
|
```
|
|
721
463
|
|
|
@@ -730,8 +472,8 @@ const handler = defineHandler(contract, 'processOrder', (message, rawMessage) =>
|
|
|
730
472
|
|
|
731
473
|
| Property | Type | Description | Defined in |
|
|
732
474
|
| ------ | ------ | ------ | ------ |
|
|
733
|
-
| <a id="headers"></a> `headers` | `THeaders` *extends* `undefined` ? `undefined` : `THeaders` | The validated message headers (present only when headers schema is defined) | [packages/worker/src/types.ts:160](https://github.com/
|
|
734
|
-
| <a id="payload"></a> `payload` | `TPayload` | The validated message payload | [packages/worker/src/types.ts:158](https://github.com/
|
|
475
|
+
| <a id="headers"></a> `headers` | `THeaders` *extends* `undefined` ? `undefined` : `THeaders` | The validated message headers (present only when headers schema is defined) | [packages/worker/src/types.ts:160](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L160) |
|
|
476
|
+
| <a id="payload"></a> `payload` | `TPayload` | The validated message payload | [packages/worker/src/types.ts:158](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L158) |
|
|
735
477
|
|
|
736
478
|
***
|
|
737
479
|
|
|
@@ -741,7 +483,7 @@ const handler = defineHandler(contract, 'processOrder', (message, rawMessage) =>
|
|
|
741
483
|
type WorkerInferConsumedMessage<TContract, TName> = WorkerConsumedMessage<WorkerInferConsumerPayload<TContract, TName>, WorkerInferConsumerHeaders<TContract, TName>>;
|
|
742
484
|
```
|
|
743
485
|
|
|
744
|
-
Defined in: [packages/worker/src/types.ts:166](https://github.com/
|
|
486
|
+
Defined in: [packages/worker/src/types.ts:166](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L166)
|
|
745
487
|
|
|
746
488
|
Infer the full consumed message type for a regular consumer.
|
|
747
489
|
|
|
@@ -757,13 +499,13 @@ Infer the full consumed message type for a regular consumer.
|
|
|
757
499
|
### WorkerInferConsumerHandler
|
|
758
500
|
|
|
759
501
|
```ts
|
|
760
|
-
type WorkerInferConsumerHandler<TContract, TName> = (message, rawMessage) =>
|
|
502
|
+
type WorkerInferConsumerHandler<TContract, TName> = (message, rawMessage) => AsyncResult<void, HandlerError>;
|
|
761
503
|
```
|
|
762
504
|
|
|
763
|
-
Defined in: [packages/worker/src/types.ts:197](https://github.com/
|
|
505
|
+
Defined in: [packages/worker/src/types.ts:197](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L197)
|
|
764
506
|
|
|
765
507
|
Handler signature for a regular consumer (event/command). Returns
|
|
766
|
-
`
|
|
508
|
+
`AsyncResult<void, HandlerError>` — there is no response message.
|
|
767
509
|
|
|
768
510
|
#### Type Parameters
|
|
769
511
|
|
|
@@ -781,7 +523,7 @@ Handler signature for a regular consumer (event/command). Returns
|
|
|
781
523
|
|
|
782
524
|
#### Returns
|
|
783
525
|
|
|
784
|
-
`
|
|
526
|
+
`AsyncResult`<`void`, [`HandlerError`](#handlererror)>
|
|
785
527
|
|
|
786
528
|
***
|
|
787
529
|
|
|
@@ -793,7 +535,7 @@ type WorkerInferConsumerHandlerEntry<TContract, TName> =
|
|
|
793
535
|
| readonly [WorkerInferConsumerHandler<TContract, TName>, ConsumerOptions];
|
|
794
536
|
```
|
|
795
537
|
|
|
796
|
-
Defined in: [packages/worker/src/types.ts:223](https://github.com/
|
|
538
|
+
Defined in: [packages/worker/src/types.ts:223](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L223)
|
|
797
539
|
|
|
798
540
|
Handler entry for a regular consumer — function or `[handler, options]`.
|
|
799
541
|
|
|
@@ -806,13 +548,37 @@ Handler entry for a regular consumer — function or `[handler, options]`.
|
|
|
806
548
|
|
|
807
549
|
***
|
|
808
550
|
|
|
809
|
-
###
|
|
551
|
+
### WorkerInferConsumerHeaders
|
|
552
|
+
|
|
553
|
+
```ts
|
|
554
|
+
type WorkerInferConsumerHeaders<TContract, TName> = ConsumerInferHeadersOutput<InferConsumer<TContract, TName>>;
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
Defined in: [packages/worker/src/types.ts:85](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L85)
|
|
558
|
+
|
|
559
|
+
Infer the headers type for a regular consumer.
|
|
560
|
+
Returns undefined if no headers schema is defined.
|
|
561
|
+
|
|
562
|
+
#### Type Parameters
|
|
563
|
+
|
|
564
|
+
| Type Parameter |
|
|
565
|
+
| ------ |
|
|
566
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
567
|
+
| `TName` *extends* `InferConsumerNames`<`TContract`> |
|
|
568
|
+
|
|
569
|
+
***
|
|
570
|
+
|
|
571
|
+
### WorkerInferHandlers
|
|
810
572
|
|
|
811
573
|
```ts
|
|
812
|
-
type
|
|
574
|
+
type WorkerInferHandlers<TContract> = [InferConsumerNames<TContract>] extends [never] ? object : { [K in InferConsumerNames<TContract>]: WorkerInferConsumerHandlerEntry<TContract, K> } & [InferRpcNames<TContract>] extends [never] ? object : { [K in InferRpcNames<TContract>]: WorkerInferRpcHandlerEntry<TContract, K> };
|
|
813
575
|
```
|
|
814
576
|
|
|
815
|
-
Defined in: [packages/worker/src/types.ts:
|
|
577
|
+
Defined in: [packages/worker/src/types.ts:257](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L257)
|
|
578
|
+
|
|
579
|
+
All handlers for a contract: one entry per `consumers` key plus one entry
|
|
580
|
+
per `rpcs` key. The two name spaces are disjoint so the resulting object
|
|
581
|
+
type is unambiguous.
|
|
816
582
|
|
|
817
583
|
#### Type Parameters
|
|
818
584
|
|
|
@@ -820,29 +586,152 @@ Defined in: [packages/worker/src/types.ts:269](https://github.com/btravers/amqp-
|
|
|
820
586
|
| ------ |
|
|
821
587
|
| `TContract` *extends* `ContractDefinition` |
|
|
822
588
|
|
|
823
|
-
####
|
|
589
|
+
#### Example
|
|
824
590
|
|
|
825
|
-
|
|
591
|
+
```typescript
|
|
592
|
+
const handlers: WorkerInferHandlers<typeof contract> = {
|
|
593
|
+
processOrder: ({ payload }) =>
|
|
594
|
+
fromPromise(
|
|
595
|
+
processPayment(payload),
|
|
596
|
+
(error) => new RetryableError('Payment failed', error),
|
|
597
|
+
).map(() => undefined),
|
|
598
|
+
calculate: ({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
599
|
+
};
|
|
600
|
+
```
|
|
826
601
|
|
|
827
602
|
***
|
|
828
603
|
|
|
829
|
-
###
|
|
604
|
+
### WorkerInferRpcConsumedMessage
|
|
830
605
|
|
|
831
606
|
```ts
|
|
832
|
-
type
|
|
607
|
+
type WorkerInferRpcConsumedMessage<TContract, TName> = WorkerConsumedMessage<WorkerInferRpcRequest<TContract, TName>, WorkerInferRpcHeaders<TContract, TName>>;
|
|
833
608
|
```
|
|
834
609
|
|
|
835
|
-
Defined in: [packages/worker/src/types.ts:
|
|
610
|
+
Defined in: [packages/worker/src/types.ts:178](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L178)
|
|
836
611
|
|
|
837
|
-
Infer the
|
|
838
|
-
|
|
612
|
+
Infer the consumed message type for an RPC handler — payload + headers from
|
|
613
|
+
the request side of the RPC.
|
|
839
614
|
|
|
840
615
|
#### Type Parameters
|
|
841
616
|
|
|
842
617
|
| Type Parameter |
|
|
843
618
|
| ------ |
|
|
844
619
|
| `TContract` *extends* `ContractDefinition` |
|
|
845
|
-
| `TName` *extends* `
|
|
620
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
621
|
+
|
|
622
|
+
***
|
|
623
|
+
|
|
624
|
+
### WorkerInferRpcHandler
|
|
625
|
+
|
|
626
|
+
```ts
|
|
627
|
+
type WorkerInferRpcHandler<TContract, TName> = (message, rawMessage) => AsyncResult<WorkerInferRpcResponse<TContract, TName>, HandlerError>;
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
Defined in: [packages/worker/src/types.ts:212](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L212)
|
|
631
|
+
|
|
632
|
+
Handler signature for an RPC. Returns
|
|
633
|
+
`AsyncResult<TResponse, HandlerError>` where `TResponse` is the inferred
|
|
634
|
+
response payload. The worker validates the response against the RPC's
|
|
635
|
+
response schema and publishes it back to `msg.properties.replyTo` with the
|
|
636
|
+
same `correlationId`.
|
|
637
|
+
|
|
638
|
+
#### Type Parameters
|
|
639
|
+
|
|
640
|
+
| Type Parameter |
|
|
641
|
+
| ------ |
|
|
642
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
643
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
644
|
+
|
|
645
|
+
#### Parameters
|
|
646
|
+
|
|
647
|
+
| Parameter | Type |
|
|
648
|
+
| ------ | ------ |
|
|
649
|
+
| `message` | [`WorkerInferRpcConsumedMessage`](#workerinferrpcconsumedmessage)<`TContract`, `TName`> |
|
|
650
|
+
| `rawMessage` | `ConsumeMessage` |
|
|
651
|
+
|
|
652
|
+
#### Returns
|
|
653
|
+
|
|
654
|
+
`AsyncResult`<[`WorkerInferRpcResponse`](#workerinferrpcresponse)<`TContract`, `TName`>, [`HandlerError`](#handlererror)>
|
|
655
|
+
|
|
656
|
+
***
|
|
657
|
+
|
|
658
|
+
### WorkerInferRpcHandlerEntry
|
|
659
|
+
|
|
660
|
+
```ts
|
|
661
|
+
type WorkerInferRpcHandlerEntry<TContract, TName> =
|
|
662
|
+
| WorkerInferRpcHandler<TContract, TName>
|
|
663
|
+
| readonly [WorkerInferRpcHandler<TContract, TName>, ConsumerOptions];
|
|
664
|
+
```
|
|
665
|
+
|
|
666
|
+
Defined in: [packages/worker/src/types.ts:233](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L233)
|
|
667
|
+
|
|
668
|
+
Handler entry for an RPC — function or `[handler, options]`.
|
|
669
|
+
|
|
670
|
+
#### Type Parameters
|
|
671
|
+
|
|
672
|
+
| Type Parameter |
|
|
673
|
+
| ------ |
|
|
674
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
675
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
676
|
+
|
|
677
|
+
***
|
|
678
|
+
|
|
679
|
+
### WorkerInferRpcHeaders
|
|
680
|
+
|
|
681
|
+
```ts
|
|
682
|
+
type WorkerInferRpcHeaders<TContract, TName> = InferRpc<TContract, TName> extends RpcDefinition<infer TRequest, MessageDefinition> ? TRequest extends MessageDefinition<infer _TPayload, infer THeaders> ? THeaders extends StandardSchemaV1<Record<string, unknown>> ? InferSchemaOutput<THeaders> : undefined : undefined : undefined;
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
Defined in: [packages/worker/src/types.ts:107](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L107)
|
|
686
|
+
|
|
687
|
+
Infer the request headers type for an RPC. Returns undefined unless the RPC's
|
|
688
|
+
request `MessageDefinition` declares a headers schema.
|
|
689
|
+
|
|
690
|
+
#### Type Parameters
|
|
691
|
+
|
|
692
|
+
| Type Parameter |
|
|
693
|
+
| ------ |
|
|
694
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
695
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
696
|
+
|
|
697
|
+
***
|
|
698
|
+
|
|
699
|
+
### WorkerInferRpcRequest
|
|
700
|
+
|
|
701
|
+
```ts
|
|
702
|
+
type WorkerInferRpcRequest<TContract, TName> = InferRpc<TContract, TName> extends RpcDefinition<infer TRequest, MessageDefinition> ? TRequest extends MessageDefinition ? InferSchemaOutput<TRequest["payload"]> : never : never;
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
Defined in: [packages/worker/src/types.ts:93](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L93)
|
|
706
|
+
|
|
707
|
+
Infer the request payload type for an RPC.
|
|
708
|
+
|
|
709
|
+
#### Type Parameters
|
|
710
|
+
|
|
711
|
+
| Type Parameter |
|
|
712
|
+
| ------ |
|
|
713
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
714
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
715
|
+
|
|
716
|
+
***
|
|
717
|
+
|
|
718
|
+
### WorkerInferRpcResponse
|
|
719
|
+
|
|
720
|
+
```ts
|
|
721
|
+
type WorkerInferRpcResponse<TContract, TName> = InferRpc<TContract, TName> extends RpcDefinition<MessageDefinition, infer TResponse> ? TResponse extends MessageDefinition ? InferSchemaOutput<TResponse["payload"]> : never : never;
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
Defined in: [packages/worker/src/types.ts:123](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/types.ts#L123)
|
|
725
|
+
|
|
726
|
+
Infer the response payload type for an RPC. The handler must return a
|
|
727
|
+
`AsyncResult<TResponse, HandlerError>` matching this shape.
|
|
728
|
+
|
|
729
|
+
#### Type Parameters
|
|
730
|
+
|
|
731
|
+
| Type Parameter |
|
|
732
|
+
| ------ |
|
|
733
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
734
|
+
| `TName` *extends* `InferRpcNames`<`TContract`> |
|
|
846
735
|
|
|
847
736
|
## Functions
|
|
848
737
|
|
|
@@ -853,35 +742,37 @@ Returns undefined if no headers schema is defined.
|
|
|
853
742
|
```ts
|
|
854
743
|
function defineHandler<TContract, TName>(
|
|
855
744
|
contract,
|
|
856
|
-
|
|
745
|
+
name,
|
|
857
746
|
handler): WorkerInferConsumerHandlerEntry<TContract, TName>;
|
|
858
747
|
```
|
|
859
748
|
|
|
860
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
749
|
+
Defined in: [packages/worker/src/handlers.ts:121](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/handlers.ts#L121)
|
|
861
750
|
|
|
862
|
-
Define a type-safe handler for a specific consumer in a contract.
|
|
751
|
+
Define a type-safe handler for a specific consumer or RPC in a contract.
|
|
863
752
|
|
|
864
|
-
**Recommended:** This function creates handlers that return
|
|
865
|
-
|
|
753
|
+
**Recommended:** This function creates handlers that return
|
|
754
|
+
`AsyncResult<void, HandlerError>` (consumers) or
|
|
755
|
+
`AsyncResult<TResponse, HandlerError>` (RPCs), providing explicit error
|
|
756
|
+
handling and better control over retry behavior.
|
|
866
757
|
|
|
867
758
|
Supports two patterns:
|
|
868
759
|
1. Simple handler: just the function
|
|
869
|
-
2. Handler with options: [handler,
|
|
760
|
+
2. Handler with options: `[handler, { prefetch: 10 }]`
|
|
870
761
|
|
|
871
762
|
##### Type Parameters
|
|
872
763
|
|
|
873
764
|
| Type Parameter | Description |
|
|
874
765
|
| ------ | ------ |
|
|
875
766
|
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
876
|
-
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
767
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer or RPC name from the contract |
|
|
877
768
|
|
|
878
769
|
##### Parameters
|
|
879
770
|
|
|
880
771
|
| Parameter | Type | Description |
|
|
881
772
|
| ------ | ------ | ------ |
|
|
882
|
-
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
883
|
-
| `
|
|
884
|
-
| `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)<`TContract`, `TName`> | The handler function
|
|
773
|
+
| `contract` | `TContract` | The contract definition containing the consumer or RPC |
|
|
774
|
+
| `name` | `TName` | The name of the consumer or RPC from the contract |
|
|
775
|
+
| `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)<`TContract`, `TName`> | The handler function — for consumers, returns `AsyncResult<void, HandlerError>`; for RPCs, returns `AsyncResult<TResponse, HandlerError>`. |
|
|
885
776
|
|
|
886
777
|
##### Returns
|
|
887
778
|
|
|
@@ -889,35 +780,32 @@ Supports two patterns:
|
|
|
889
780
|
|
|
890
781
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
891
782
|
|
|
892
|
-
#####
|
|
783
|
+
##### Examples
|
|
784
|
+
|
|
785
|
+
**Consumer handler**
|
|
893
786
|
|
|
894
787
|
```typescript
|
|
895
788
|
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
896
|
-
import {
|
|
897
|
-
import { orderContract } from './contract';
|
|
789
|
+
import { fromPromise, ok } from 'unthrown';
|
|
898
790
|
|
|
899
|
-
// Simple handler with explicit error handling
|
|
900
791
|
const processOrderHandler = defineHandler(
|
|
901
792
|
orderContract,
|
|
902
793
|
'processOrder',
|
|
903
794
|
({ payload }) =>
|
|
904
|
-
|
|
795
|
+
fromPromise(
|
|
905
796
|
processPayment(payload),
|
|
906
797
|
(error) => new RetryableError('Payment failed', error),
|
|
907
798
|
).map(() => undefined),
|
|
908
799
|
);
|
|
800
|
+
```
|
|
909
801
|
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
return errAsync(new NonRetryableError('Invalid order amount'));
|
|
918
|
-
}
|
|
919
|
-
return okAsync(undefined);
|
|
920
|
-
},
|
|
802
|
+
**RPC handler**
|
|
803
|
+
|
|
804
|
+
```typescript
|
|
805
|
+
const calculateHandler = defineHandler(
|
|
806
|
+
rpcContract,
|
|
807
|
+
'calculate',
|
|
808
|
+
({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
921
809
|
);
|
|
922
810
|
```
|
|
923
811
|
|
|
@@ -926,36 +814,38 @@ const validateOrderHandler = defineHandler(
|
|
|
926
814
|
```ts
|
|
927
815
|
function defineHandler<TContract, TName>(
|
|
928
816
|
contract,
|
|
929
|
-
|
|
817
|
+
name,
|
|
930
818
|
handler,
|
|
931
819
|
options): WorkerInferConsumerHandlerEntry<TContract, TName>;
|
|
932
820
|
```
|
|
933
821
|
|
|
934
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
822
|
+
Defined in: [packages/worker/src/handlers.ts:129](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/handlers.ts#L129)
|
|
935
823
|
|
|
936
|
-
Define a type-safe handler for a specific consumer in a contract.
|
|
824
|
+
Define a type-safe handler for a specific consumer or RPC in a contract.
|
|
937
825
|
|
|
938
|
-
**Recommended:** This function creates handlers that return
|
|
939
|
-
|
|
826
|
+
**Recommended:** This function creates handlers that return
|
|
827
|
+
`AsyncResult<void, HandlerError>` (consumers) or
|
|
828
|
+
`AsyncResult<TResponse, HandlerError>` (RPCs), providing explicit error
|
|
829
|
+
handling and better control over retry behavior.
|
|
940
830
|
|
|
941
831
|
Supports two patterns:
|
|
942
832
|
1. Simple handler: just the function
|
|
943
|
-
2. Handler with options: [handler,
|
|
833
|
+
2. Handler with options: `[handler, { prefetch: 10 }]`
|
|
944
834
|
|
|
945
835
|
##### Type Parameters
|
|
946
836
|
|
|
947
837
|
| Type Parameter | Description |
|
|
948
838
|
| ------ | ------ |
|
|
949
839
|
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
950
|
-
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
840
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer or RPC name from the contract |
|
|
951
841
|
|
|
952
842
|
##### Parameters
|
|
953
843
|
|
|
954
844
|
| Parameter | Type | Description |
|
|
955
845
|
| ------ | ------ | ------ |
|
|
956
|
-
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
957
|
-
| `
|
|
958
|
-
| `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)<`TContract`, `TName`> | The handler function
|
|
846
|
+
| `contract` | `TContract` | The contract definition containing the consumer or RPC |
|
|
847
|
+
| `name` | `TName` | The name of the consumer or RPC from the contract |
|
|
848
|
+
| `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)<`TContract`, `TName`> | The handler function — for consumers, returns `AsyncResult<void, HandlerError>`; for RPCs, returns `AsyncResult<TResponse, HandlerError>`. |
|
|
959
849
|
| `options` | `ConsumerOptions` | Optional consumer options (prefetch) |
|
|
960
850
|
|
|
961
851
|
##### Returns
|
|
@@ -964,35 +854,178 @@ Supports two patterns:
|
|
|
964
854
|
|
|
965
855
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
966
856
|
|
|
967
|
-
#####
|
|
857
|
+
##### Examples
|
|
858
|
+
|
|
859
|
+
**Consumer handler**
|
|
968
860
|
|
|
969
861
|
```typescript
|
|
970
862
|
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
971
|
-
import {
|
|
972
|
-
import { orderContract } from './contract';
|
|
863
|
+
import { fromPromise, ok } from 'unthrown';
|
|
973
864
|
|
|
974
|
-
// Simple handler with explicit error handling
|
|
975
865
|
const processOrderHandler = defineHandler(
|
|
976
866
|
orderContract,
|
|
977
867
|
'processOrder',
|
|
978
868
|
({ payload }) =>
|
|
979
|
-
|
|
869
|
+
fromPromise(
|
|
980
870
|
processPayment(payload),
|
|
981
871
|
(error) => new RetryableError('Payment failed', error),
|
|
982
872
|
).map(() => undefined),
|
|
983
873
|
);
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
**RPC handler**
|
|
877
|
+
|
|
878
|
+
```typescript
|
|
879
|
+
const calculateHandler = defineHandler(
|
|
880
|
+
rpcContract,
|
|
881
|
+
'calculate',
|
|
882
|
+
({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
883
|
+
);
|
|
884
|
+
```
|
|
984
885
|
|
|
985
|
-
|
|
986
|
-
|
|
886
|
+
#### Call Signature
|
|
887
|
+
|
|
888
|
+
```ts
|
|
889
|
+
function defineHandler<TContract, TName>(
|
|
890
|
+
contract,
|
|
891
|
+
name,
|
|
892
|
+
handler): WorkerInferRpcHandlerEntry<TContract, TName>;
|
|
893
|
+
```
|
|
894
|
+
|
|
895
|
+
Defined in: [packages/worker/src/handlers.ts:138](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/handlers.ts#L138)
|
|
896
|
+
|
|
897
|
+
Define a type-safe handler for a specific consumer or RPC in a contract.
|
|
898
|
+
|
|
899
|
+
**Recommended:** This function creates handlers that return
|
|
900
|
+
`AsyncResult<void, HandlerError>` (consumers) or
|
|
901
|
+
`AsyncResult<TResponse, HandlerError>` (RPCs), providing explicit error
|
|
902
|
+
handling and better control over retry behavior.
|
|
903
|
+
|
|
904
|
+
Supports two patterns:
|
|
905
|
+
1. Simple handler: just the function
|
|
906
|
+
2. Handler with options: `[handler, { prefetch: 10 }]`
|
|
907
|
+
|
|
908
|
+
##### Type Parameters
|
|
909
|
+
|
|
910
|
+
| Type Parameter | Description |
|
|
911
|
+
| ------ | ------ |
|
|
912
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
913
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer or RPC name from the contract |
|
|
914
|
+
|
|
915
|
+
##### Parameters
|
|
916
|
+
|
|
917
|
+
| Parameter | Type | Description |
|
|
918
|
+
| ------ | ------ | ------ |
|
|
919
|
+
| `contract` | `TContract` | The contract definition containing the consumer or RPC |
|
|
920
|
+
| `name` | `TName` | The name of the consumer or RPC from the contract |
|
|
921
|
+
| `handler` | [`WorkerInferRpcHandler`](#workerinferrpchandler)<`TContract`, `TName`> | The handler function — for consumers, returns `AsyncResult<void, HandlerError>`; for RPCs, returns `AsyncResult<TResponse, HandlerError>`. |
|
|
922
|
+
|
|
923
|
+
##### Returns
|
|
924
|
+
|
|
925
|
+
[`WorkerInferRpcHandlerEntry`](#workerinferrpchandlerentry)<`TContract`, `TName`>
|
|
926
|
+
|
|
927
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
928
|
+
|
|
929
|
+
##### Examples
|
|
930
|
+
|
|
931
|
+
**Consumer handler**
|
|
932
|
+
|
|
933
|
+
```typescript
|
|
934
|
+
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
935
|
+
import { fromPromise, ok } from 'unthrown';
|
|
936
|
+
|
|
937
|
+
const processOrderHandler = defineHandler(
|
|
987
938
|
orderContract,
|
|
988
|
-
'
|
|
989
|
-
({ payload }) =>
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
939
|
+
'processOrder',
|
|
940
|
+
({ payload }) =>
|
|
941
|
+
fromPromise(
|
|
942
|
+
processPayment(payload),
|
|
943
|
+
(error) => new RetryableError('Payment failed', error),
|
|
944
|
+
).map(() => undefined),
|
|
945
|
+
);
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
**RPC handler**
|
|
949
|
+
|
|
950
|
+
```typescript
|
|
951
|
+
const calculateHandler = defineHandler(
|
|
952
|
+
rpcContract,
|
|
953
|
+
'calculate',
|
|
954
|
+
({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
955
|
+
);
|
|
956
|
+
```
|
|
957
|
+
|
|
958
|
+
#### Call Signature
|
|
959
|
+
|
|
960
|
+
```ts
|
|
961
|
+
function defineHandler<TContract, TName>(
|
|
962
|
+
contract,
|
|
963
|
+
name,
|
|
964
|
+
handler,
|
|
965
|
+
options): WorkerInferRpcHandlerEntry<TContract, TName>;
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
Defined in: [packages/worker/src/handlers.ts:146](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/handlers.ts#L146)
|
|
969
|
+
|
|
970
|
+
Define a type-safe handler for a specific consumer or RPC in a contract.
|
|
971
|
+
|
|
972
|
+
**Recommended:** This function creates handlers that return
|
|
973
|
+
`AsyncResult<void, HandlerError>` (consumers) or
|
|
974
|
+
`AsyncResult<TResponse, HandlerError>` (RPCs), providing explicit error
|
|
975
|
+
handling and better control over retry behavior.
|
|
976
|
+
|
|
977
|
+
Supports two patterns:
|
|
978
|
+
1. Simple handler: just the function
|
|
979
|
+
2. Handler with options: `[handler, { prefetch: 10 }]`
|
|
980
|
+
|
|
981
|
+
##### Type Parameters
|
|
982
|
+
|
|
983
|
+
| Type Parameter | Description |
|
|
984
|
+
| ------ | ------ |
|
|
985
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
986
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer or RPC name from the contract |
|
|
987
|
+
|
|
988
|
+
##### Parameters
|
|
989
|
+
|
|
990
|
+
| Parameter | Type | Description |
|
|
991
|
+
| ------ | ------ | ------ |
|
|
992
|
+
| `contract` | `TContract` | The contract definition containing the consumer or RPC |
|
|
993
|
+
| `name` | `TName` | The name of the consumer or RPC from the contract |
|
|
994
|
+
| `handler` | [`WorkerInferRpcHandler`](#workerinferrpchandler)<`TContract`, `TName`> | The handler function — for consumers, returns `AsyncResult<void, HandlerError>`; for RPCs, returns `AsyncResult<TResponse, HandlerError>`. |
|
|
995
|
+
| `options` | `ConsumerOptions` | Optional consumer options (prefetch) |
|
|
996
|
+
|
|
997
|
+
##### Returns
|
|
998
|
+
|
|
999
|
+
[`WorkerInferRpcHandlerEntry`](#workerinferrpchandlerentry)<`TContract`, `TName`>
|
|
1000
|
+
|
|
1001
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
1002
|
+
|
|
1003
|
+
##### Examples
|
|
1004
|
+
|
|
1005
|
+
**Consumer handler**
|
|
1006
|
+
|
|
1007
|
+
```typescript
|
|
1008
|
+
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
1009
|
+
import { fromPromise, ok } from 'unthrown';
|
|
1010
|
+
|
|
1011
|
+
const processOrderHandler = defineHandler(
|
|
1012
|
+
orderContract,
|
|
1013
|
+
'processOrder',
|
|
1014
|
+
({ payload }) =>
|
|
1015
|
+
fromPromise(
|
|
1016
|
+
processPayment(payload),
|
|
1017
|
+
(error) => new RetryableError('Payment failed', error),
|
|
1018
|
+
).map(() => undefined),
|
|
1019
|
+
);
|
|
1020
|
+
```
|
|
1021
|
+
|
|
1022
|
+
**RPC handler**
|
|
1023
|
+
|
|
1024
|
+
```typescript
|
|
1025
|
+
const calculateHandler = defineHandler(
|
|
1026
|
+
rpcContract,
|
|
1027
|
+
'calculate',
|
|
1028
|
+
({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
996
1029
|
);
|
|
997
1030
|
```
|
|
998
1031
|
|
|
@@ -1001,15 +1034,20 @@ const validateOrderHandler = defineHandler(
|
|
|
1001
1034
|
### defineHandlers()
|
|
1002
1035
|
|
|
1003
1036
|
```ts
|
|
1004
|
-
function defineHandlers<TContract>(contract, handlers):
|
|
1037
|
+
function defineHandlers<TContract>(contract, handlers): WorkerInferHandlers<TContract>;
|
|
1005
1038
|
```
|
|
1006
1039
|
|
|
1007
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
1040
|
+
Defined in: [packages/worker/src/handlers.ts:198](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/handlers.ts#L198)
|
|
1041
|
+
|
|
1042
|
+
Define multiple type-safe handlers for consumers and RPCs in a contract.
|
|
1008
1043
|
|
|
1009
|
-
|
|
1044
|
+
**Recommended:** This function creates handlers that return
|
|
1045
|
+
`AsyncResult<void, HandlerError>` (consumers) or
|
|
1046
|
+
`AsyncResult<TResponse, HandlerError>` (RPCs), providing explicit error
|
|
1047
|
+
handling and better control over retry behavior.
|
|
1010
1048
|
|
|
1011
|
-
|
|
1012
|
-
|
|
1049
|
+
The handlers object must contain exactly one entry per `consumers` and
|
|
1050
|
+
`rpcs` key in the contract — see [WorkerInferHandlers](#workerinferhandlers).
|
|
1013
1051
|
|
|
1014
1052
|
#### Type Parameters
|
|
1015
1053
|
|
|
@@ -1021,12 +1059,12 @@ providing explicit error handling and better control over retry behavior.
|
|
|
1021
1059
|
|
|
1022
1060
|
| Parameter | Type | Description |
|
|
1023
1061
|
| ------ | ------ | ------ |
|
|
1024
|
-
| `contract` | `TContract` | The contract definition containing the consumers |
|
|
1025
|
-
| `handlers` | [`
|
|
1062
|
+
| `contract` | `TContract` | The contract definition containing the consumers and RPCs |
|
|
1063
|
+
| `handlers` | [`WorkerInferHandlers`](#workerinferhandlers)<`TContract`> | An object with handler functions for each consumer and RPC |
|
|
1026
1064
|
|
|
1027
1065
|
#### Returns
|
|
1028
1066
|
|
|
1029
|
-
[`
|
|
1067
|
+
[`WorkerInferHandlers`](#workerinferhandlers)<`TContract`>
|
|
1030
1068
|
|
|
1031
1069
|
A type-safe handlers object that can be used with TypedAmqpWorker
|
|
1032
1070
|
|
|
@@ -1034,20 +1072,15 @@ A type-safe handlers object that can be used with TypedAmqpWorker
|
|
|
1034
1072
|
|
|
1035
1073
|
```typescript
|
|
1036
1074
|
import { defineHandlers, RetryableError } from '@amqp-contract/worker';
|
|
1037
|
-
import {
|
|
1038
|
-
import { orderContract } from './contract';
|
|
1075
|
+
import { fromPromise, ok } from 'unthrown';
|
|
1039
1076
|
|
|
1040
1077
|
const handlers = defineHandlers(orderContract, {
|
|
1041
1078
|
processOrder: ({ payload }) =>
|
|
1042
|
-
|
|
1079
|
+
fromPromise(
|
|
1043
1080
|
processPayment(payload),
|
|
1044
1081
|
(error) => new RetryableError('Payment failed', error),
|
|
1045
1082
|
).map(() => undefined),
|
|
1046
|
-
|
|
1047
|
-
ResultAsync.fromPromise(
|
|
1048
|
-
sendNotification(payload),
|
|
1049
|
-
(error) => new RetryableError('Notification failed', error),
|
|
1050
|
-
).map(() => undefined),
|
|
1083
|
+
calculate: ({ payload }) => ok({ sum: payload.a + payload.b }).toAsync(),
|
|
1051
1084
|
});
|
|
1052
1085
|
```
|
|
1053
1086
|
|
|
@@ -1059,7 +1092,7 @@ const handlers = defineHandlers(orderContract, {
|
|
|
1059
1092
|
function isHandlerError(error): error is HandlerError;
|
|
1060
1093
|
```
|
|
1061
1094
|
|
|
1062
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
1095
|
+
Defined in: [packages/worker/src/errors.ts:134](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L134)
|
|
1063
1096
|
|
|
1064
1097
|
Type guard to check if an error is any HandlerError (RetryableError or NonRetryableError).
|
|
1065
1098
|
|
|
@@ -1096,7 +1129,7 @@ function handleError(error: unknown) {
|
|
|
1096
1129
|
function isNonRetryableError(error): error is NonRetryableError;
|
|
1097
1130
|
```
|
|
1098
1131
|
|
|
1099
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
1132
|
+
Defined in: [packages/worker/src/errors.ts:112](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L112)
|
|
1100
1133
|
|
|
1101
1134
|
Type guard to check if an error is a NonRetryableError.
|
|
1102
1135
|
|
|
@@ -1136,7 +1169,7 @@ try {
|
|
|
1136
1169
|
function isRetryableError(error): error is RetryableError;
|
|
1137
1170
|
```
|
|
1138
1171
|
|
|
1139
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
1172
|
+
Defined in: [packages/worker/src/errors.ts:87](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L87)
|
|
1140
1173
|
|
|
1141
1174
|
Type guard to check if an error is a RetryableError.
|
|
1142
1175
|
|
|
@@ -1178,7 +1211,7 @@ try {
|
|
|
1178
1211
|
function nonRetryable(message, cause?): NonRetryableError;
|
|
1179
1212
|
```
|
|
1180
1213
|
|
|
1181
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
1214
|
+
Defined in: [packages/worker/src/errors.ts:197](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L197)
|
|
1182
1215
|
|
|
1183
1216
|
Create a NonRetryableError with less verbosity.
|
|
1184
1217
|
|
|
@@ -1202,17 +1235,17 @@ A new NonRetryableError instance
|
|
|
1202
1235
|
|
|
1203
1236
|
```typescript
|
|
1204
1237
|
import { nonRetryable } from '@amqp-contract/worker';
|
|
1205
|
-
import {
|
|
1238
|
+
import { err, ok } from 'unthrown';
|
|
1206
1239
|
|
|
1207
1240
|
const handler = ({ payload }) => {
|
|
1208
1241
|
if (!isValidPayload(payload)) {
|
|
1209
|
-
return
|
|
1242
|
+
return err(nonRetryable('Invalid payload format')).toAsync();
|
|
1210
1243
|
}
|
|
1211
|
-
return
|
|
1244
|
+
return ok(undefined).toAsync();
|
|
1212
1245
|
};
|
|
1213
1246
|
|
|
1214
1247
|
// Equivalent to:
|
|
1215
|
-
// return
|
|
1248
|
+
// return err(new NonRetryableError('Invalid payload format')).toAsync();
|
|
1216
1249
|
```
|
|
1217
1250
|
|
|
1218
1251
|
***
|
|
@@ -1223,7 +1256,7 @@ const handler = ({ payload }) => {
|
|
|
1223
1256
|
function retryable(message, cause?): RetryableError;
|
|
1224
1257
|
```
|
|
1225
1258
|
|
|
1226
|
-
Defined in: [packages/worker/src/errors.ts:
|
|
1259
|
+
Defined in: [packages/worker/src/errors.ts:167](https://github.com/btravstack/amqp-contract/blob/5bdec81d9ab13131c5b7f699c426616cfbed2b5f/packages/worker/src/errors.ts#L167)
|
|
1227
1260
|
|
|
1228
1261
|
Create a RetryableError with less verbosity.
|
|
1229
1262
|
|
|
@@ -1247,14 +1280,14 @@ A new RetryableError instance
|
|
|
1247
1280
|
|
|
1248
1281
|
```typescript
|
|
1249
1282
|
import { retryable } from '@amqp-contract/worker';
|
|
1250
|
-
import {
|
|
1283
|
+
import { fromPromise } from 'unthrown';
|
|
1251
1284
|
|
|
1252
1285
|
const handler = ({ payload }) =>
|
|
1253
|
-
|
|
1286
|
+
fromPromise(
|
|
1254
1287
|
processPayment(payload),
|
|
1255
1288
|
(e) => retryable('Payment service unavailable', e),
|
|
1256
1289
|
).map(() => undefined);
|
|
1257
1290
|
|
|
1258
1291
|
// Equivalent to:
|
|
1259
|
-
//
|
|
1292
|
+
// fromPromise(processPayment(payload), (e) => new RetryableError('...', e))
|
|
1260
1293
|
```
|