@amqp-contract/worker 0.7.0 → 0.9.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 +33 -4
- package/dist/index.cjs +397 -95
- package/dist/index.d.cts +362 -99
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +362 -99
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +394 -96
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +995 -213
- package/package.json +8 -8
package/docs/index.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
### MessageValidationError
|
|
10
10
|
|
|
11
|
-
Defined in: [packages/worker/src/errors.ts:35](https://github.com/btravers/amqp-contract/blob/
|
|
11
|
+
Defined in: [packages/worker/src/errors.ts:35](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L35)
|
|
12
12
|
|
|
13
13
|
Error thrown when message validation fails
|
|
14
14
|
|
|
@@ -24,7 +24,7 @@ Error thrown when message validation fails
|
|
|
24
24
|
new MessageValidationError(consumerName, issues): MessageValidationError;
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
Defined in: [packages/worker/src/errors.ts:36](https://github.com/btravers/amqp-contract/blob/
|
|
27
|
+
Defined in: [packages/worker/src/errors.ts:36](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L36)
|
|
28
28
|
|
|
29
29
|
###### Parameters
|
|
30
30
|
|
|
@@ -48,12 +48,12 @@ WorkerError.constructor
|
|
|
48
48
|
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
49
49
|
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
50
50
|
| <a id="cause"></a> `cause?` | `public` | `unknown` | - | `WorkerError.cause` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:26 |
|
|
51
|
-
| <a id="consumername"></a> `consumerName` | `readonly` | `string` | - | - | [packages/worker/src/errors.ts:37](https://github.com/btravers/amqp-contract/blob/
|
|
52
|
-
| <a id="issues"></a> `issues` | `readonly` | `unknown` | - | - | [packages/worker/src/errors.ts:38](https://github.com/btravers/amqp-contract/blob/
|
|
51
|
+
| <a id="consumername"></a> `consumerName` | `readonly` | `string` | - | - | [packages/worker/src/errors.ts:37](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L37) |
|
|
52
|
+
| <a id="issues"></a> `issues` | `readonly` | `unknown` | - | - | [packages/worker/src/errors.ts:38](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L38) |
|
|
53
53
|
| <a id="message"></a> `message` | `public` | `string` | - | `WorkerError.message` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1077 |
|
|
54
54
|
| <a id="name"></a> `name` | `public` | `string` | - | `WorkerError.name` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
55
55
|
| <a id="stack"></a> `stack?` | `public` | `string` | - | `WorkerError.stack` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1078 |
|
|
56
|
-
| <a id="stacktracelimit"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `WorkerError.stackTraceLimit` | node\_modules/.pnpm/@types+node@25.0.
|
|
56
|
+
| <a id="stacktracelimit"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `WorkerError.stackTraceLimit` | node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:67 |
|
|
57
57
|
|
|
58
58
|
#### Methods
|
|
59
59
|
|
|
@@ -63,7 +63,7 @@ WorkerError.constructor
|
|
|
63
63
|
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
Defined in: node\_modules/.pnpm/@types+node@25.0.
|
|
66
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:51
|
|
67
67
|
|
|
68
68
|
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
69
69
|
a string representing the location in the code at which
|
|
@@ -132,7 +132,313 @@ WorkerError.captureStackTrace
|
|
|
132
132
|
static prepareStackTrace(err, stackTraces): any;
|
|
133
133
|
```
|
|
134
134
|
|
|
135
|
-
Defined in: node\_modules/.pnpm/@types+node@25.0.
|
|
135
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:55
|
|
136
|
+
|
|
137
|
+
###### Parameters
|
|
138
|
+
|
|
139
|
+
| Parameter | Type |
|
|
140
|
+
| ------ | ------ |
|
|
141
|
+
| `err` | `Error` |
|
|
142
|
+
| `stackTraces` | `CallSite`[] |
|
|
143
|
+
|
|
144
|
+
###### Returns
|
|
145
|
+
|
|
146
|
+
`any`
|
|
147
|
+
|
|
148
|
+
###### See
|
|
149
|
+
|
|
150
|
+
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
151
|
+
|
|
152
|
+
###### Inherited from
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
WorkerError.prepareStackTrace
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
***
|
|
159
|
+
|
|
160
|
+
### NonRetryableError
|
|
161
|
+
|
|
162
|
+
Defined in: [packages/worker/src/errors.ts:69](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L69)
|
|
163
|
+
|
|
164
|
+
Non-retryable errors - permanent failures that should not be retried
|
|
165
|
+
Examples: invalid data, business rule violations, permanent external failures
|
|
166
|
+
|
|
167
|
+
Use this error type when retrying would not help - the message will be
|
|
168
|
+
immediately sent to the dead letter queue (DLQ) if configured.
|
|
169
|
+
|
|
170
|
+
#### Extends
|
|
171
|
+
|
|
172
|
+
- `WorkerError`
|
|
173
|
+
|
|
174
|
+
#### Constructors
|
|
175
|
+
|
|
176
|
+
##### Constructor
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
new NonRetryableError(message, cause?): NonRetryableError;
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Defined in: [packages/worker/src/errors.ts:70](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L70)
|
|
183
|
+
|
|
184
|
+
###### Parameters
|
|
185
|
+
|
|
186
|
+
| Parameter | Type |
|
|
187
|
+
| ------ | ------ |
|
|
188
|
+
| `message` | `string` |
|
|
189
|
+
| `cause?` | `unknown` |
|
|
190
|
+
|
|
191
|
+
###### Returns
|
|
192
|
+
|
|
193
|
+
[`NonRetryableError`](#nonretryableerror)
|
|
194
|
+
|
|
195
|
+
###### Overrides
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
WorkerError.constructor
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### Properties
|
|
202
|
+
|
|
203
|
+
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
204
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
205
|
+
| <a id="cause-1"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:72](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L72) |
|
|
206
|
+
| <a id="message-1"></a> `message` | `public` | `string` | - | `WorkerError.message` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1077 |
|
|
207
|
+
| <a id="name-1"></a> `name` | `public` | `string` | - | `WorkerError.name` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
208
|
+
| <a id="stack-1"></a> `stack?` | `public` | `string` | - | `WorkerError.stack` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1078 |
|
|
209
|
+
| <a id="stacktracelimit-1"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `WorkerError.stackTraceLimit` | node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:67 |
|
|
210
|
+
|
|
211
|
+
#### Methods
|
|
212
|
+
|
|
213
|
+
##### captureStackTrace()
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:51
|
|
220
|
+
|
|
221
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
222
|
+
a string representing the location in the code at which
|
|
223
|
+
`Error.captureStackTrace()` was called.
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
const myObject = {};
|
|
227
|
+
Error.captureStackTrace(myObject);
|
|
228
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The first line of the trace will be prefixed with
|
|
232
|
+
`${myObject.name}: ${myObject.message}`.
|
|
233
|
+
|
|
234
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
235
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
236
|
+
generated stack trace.
|
|
237
|
+
|
|
238
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
239
|
+
details of error generation from the user. For instance:
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
function a() {
|
|
243
|
+
b();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function b() {
|
|
247
|
+
c();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function c() {
|
|
251
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
252
|
+
const { stackTraceLimit } = Error;
|
|
253
|
+
Error.stackTraceLimit = 0;
|
|
254
|
+
const error = new Error();
|
|
255
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
256
|
+
|
|
257
|
+
// Capture the stack trace above function b
|
|
258
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
a();
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
###### Parameters
|
|
266
|
+
|
|
267
|
+
| Parameter | Type |
|
|
268
|
+
| ------ | ------ |
|
|
269
|
+
| `targetObject` | `object` |
|
|
270
|
+
| `constructorOpt?` | `Function` |
|
|
271
|
+
|
|
272
|
+
###### Returns
|
|
273
|
+
|
|
274
|
+
`void`
|
|
275
|
+
|
|
276
|
+
###### Inherited from
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
WorkerError.captureStackTrace
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
##### prepareStackTrace()
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
static prepareStackTrace(err, stackTraces): any;
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:55
|
|
289
|
+
|
|
290
|
+
###### Parameters
|
|
291
|
+
|
|
292
|
+
| Parameter | Type |
|
|
293
|
+
| ------ | ------ |
|
|
294
|
+
| `err` | `Error` |
|
|
295
|
+
| `stackTraces` | `CallSite`[] |
|
|
296
|
+
|
|
297
|
+
###### Returns
|
|
298
|
+
|
|
299
|
+
`any`
|
|
300
|
+
|
|
301
|
+
###### See
|
|
302
|
+
|
|
303
|
+
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
304
|
+
|
|
305
|
+
###### Inherited from
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
WorkerError.prepareStackTrace
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
***
|
|
312
|
+
|
|
313
|
+
### RetryableError
|
|
314
|
+
|
|
315
|
+
Defined in: [packages/worker/src/errors.ts:52](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L52)
|
|
316
|
+
|
|
317
|
+
Retryable errors - transient failures that may succeed on retry
|
|
318
|
+
Examples: network timeouts, rate limiting, temporary service unavailability
|
|
319
|
+
|
|
320
|
+
Use this error type when the operation might succeed if retried.
|
|
321
|
+
The worker will apply exponential backoff and retry the message.
|
|
322
|
+
|
|
323
|
+
#### Extends
|
|
324
|
+
|
|
325
|
+
- `WorkerError`
|
|
326
|
+
|
|
327
|
+
#### Constructors
|
|
328
|
+
|
|
329
|
+
##### Constructor
|
|
330
|
+
|
|
331
|
+
```ts
|
|
332
|
+
new RetryableError(message, cause?): RetryableError;
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Defined in: [packages/worker/src/errors.ts:53](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L53)
|
|
336
|
+
|
|
337
|
+
###### Parameters
|
|
338
|
+
|
|
339
|
+
| Parameter | Type |
|
|
340
|
+
| ------ | ------ |
|
|
341
|
+
| `message` | `string` |
|
|
342
|
+
| `cause?` | `unknown` |
|
|
343
|
+
|
|
344
|
+
###### Returns
|
|
345
|
+
|
|
346
|
+
[`RetryableError`](#retryableerror)
|
|
347
|
+
|
|
348
|
+
###### Overrides
|
|
349
|
+
|
|
350
|
+
```ts
|
|
351
|
+
WorkerError.constructor
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
#### Properties
|
|
355
|
+
|
|
356
|
+
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
357
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
358
|
+
| <a id="cause-2"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:55](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L55) |
|
|
359
|
+
| <a id="message-2"></a> `message` | `public` | `string` | - | `WorkerError.message` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1077 |
|
|
360
|
+
| <a id="name-2"></a> `name` | `public` | `string` | - | `WorkerError.name` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
361
|
+
| <a id="stack-2"></a> `stack?` | `public` | `string` | - | `WorkerError.stack` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1078 |
|
|
362
|
+
| <a id="stacktracelimit-2"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `WorkerError.stackTraceLimit` | node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:67 |
|
|
363
|
+
|
|
364
|
+
#### Methods
|
|
365
|
+
|
|
366
|
+
##### captureStackTrace()
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:51
|
|
373
|
+
|
|
374
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
375
|
+
a string representing the location in the code at which
|
|
376
|
+
`Error.captureStackTrace()` was called.
|
|
377
|
+
|
|
378
|
+
```js
|
|
379
|
+
const myObject = {};
|
|
380
|
+
Error.captureStackTrace(myObject);
|
|
381
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
The first line of the trace will be prefixed with
|
|
385
|
+
`${myObject.name}: ${myObject.message}`.
|
|
386
|
+
|
|
387
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
388
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
389
|
+
generated stack trace.
|
|
390
|
+
|
|
391
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
392
|
+
details of error generation from the user. For instance:
|
|
393
|
+
|
|
394
|
+
```js
|
|
395
|
+
function a() {
|
|
396
|
+
b();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function b() {
|
|
400
|
+
c();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function c() {
|
|
404
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
405
|
+
const { stackTraceLimit } = Error;
|
|
406
|
+
Error.stackTraceLimit = 0;
|
|
407
|
+
const error = new Error();
|
|
408
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
409
|
+
|
|
410
|
+
// Capture the stack trace above function b
|
|
411
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
412
|
+
throw error;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
a();
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
###### Parameters
|
|
419
|
+
|
|
420
|
+
| Parameter | Type |
|
|
421
|
+
| ------ | ------ |
|
|
422
|
+
| `targetObject` | `object` |
|
|
423
|
+
| `constructorOpt?` | `Function` |
|
|
424
|
+
|
|
425
|
+
###### Returns
|
|
426
|
+
|
|
427
|
+
`void`
|
|
428
|
+
|
|
429
|
+
###### Inherited from
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
WorkerError.captureStackTrace
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
##### prepareStackTrace()
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
static prepareStackTrace(err, stackTraces): any;
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:55
|
|
136
442
|
|
|
137
443
|
###### Parameters
|
|
138
444
|
|
|
@@ -159,7 +465,7 @@ WorkerError.prepareStackTrace
|
|
|
159
465
|
|
|
160
466
|
### TechnicalError
|
|
161
467
|
|
|
162
|
-
Defined in: [packages/worker/src/errors.ts:22](https://github.com/btravers/amqp-contract/blob/
|
|
468
|
+
Defined in: [packages/worker/src/errors.ts:22](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L22)
|
|
163
469
|
|
|
164
470
|
Error for technical/runtime failures in worker operations
|
|
165
471
|
This includes validation failures, parsing failures, and processing failures
|
|
@@ -176,7 +482,7 @@ This includes validation failures, parsing failures, and processing failures
|
|
|
176
482
|
new TechnicalError(message, cause?): TechnicalError;
|
|
177
483
|
```
|
|
178
484
|
|
|
179
|
-
Defined in: [packages/worker/src/errors.ts:23](https://github.com/btravers/amqp-contract/blob/
|
|
485
|
+
Defined in: [packages/worker/src/errors.ts:23](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L23)
|
|
180
486
|
|
|
181
487
|
###### Parameters
|
|
182
488
|
|
|
@@ -199,11 +505,11 @@ WorkerError.constructor
|
|
|
199
505
|
|
|
200
506
|
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
201
507
|
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
202
|
-
| <a id="cause-
|
|
203
|
-
| <a id="message-
|
|
204
|
-
| <a id="name-
|
|
205
|
-
| <a id="stack-
|
|
206
|
-
| <a id="stacktracelimit-
|
|
508
|
+
| <a id="cause-3"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:25](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L25) |
|
|
509
|
+
| <a id="message-3"></a> `message` | `public` | `string` | - | `WorkerError.message` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1077 |
|
|
510
|
+
| <a id="name-3"></a> `name` | `public` | `string` | - | `WorkerError.name` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
511
|
+
| <a id="stack-3"></a> `stack?` | `public` | `string` | - | `WorkerError.stack` | node\_modules/.pnpm/typescript@5.9.3/node\_modules/typescript/lib/lib.es5.d.ts:1078 |
|
|
512
|
+
| <a id="stacktracelimit-3"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `WorkerError.stackTraceLimit` | node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:67 |
|
|
207
513
|
|
|
208
514
|
#### Methods
|
|
209
515
|
|
|
@@ -213,7 +519,7 @@ WorkerError.constructor
|
|
|
213
519
|
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
214
520
|
```
|
|
215
521
|
|
|
216
|
-
Defined in: node\_modules/.pnpm/@types+node@25.0.
|
|
522
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:51
|
|
217
523
|
|
|
218
524
|
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
219
525
|
a string representing the location in the code at which
|
|
@@ -282,7 +588,7 @@ WorkerError.captureStackTrace
|
|
|
282
588
|
static prepareStackTrace(err, stackTraces): any;
|
|
283
589
|
```
|
|
284
590
|
|
|
285
|
-
Defined in: node\_modules/.pnpm/@types+node@25.0.
|
|
591
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.5/node\_modules/@types/node/globals.d.ts:55
|
|
286
592
|
|
|
287
593
|
###### Parameters
|
|
288
594
|
|
|
@@ -309,7 +615,7 @@ WorkerError.prepareStackTrace
|
|
|
309
615
|
|
|
310
616
|
### TypedAmqpWorker
|
|
311
617
|
|
|
312
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
618
|
+
Defined in: [packages/worker/src/worker.ts:190](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L190)
|
|
313
619
|
|
|
314
620
|
Type-safe AMQP worker for consuming messages from RabbitMQ.
|
|
315
621
|
|
|
@@ -363,7 +669,7 @@ await worker.close().resultToPromise();
|
|
|
363
669
|
close(): Future<Result<void, TechnicalError>>;
|
|
364
670
|
```
|
|
365
671
|
|
|
366
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
672
|
+
Defined in: [packages/worker/src/worker.ts:327](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L327)
|
|
367
673
|
|
|
368
674
|
Close the AMQP channel and connection.
|
|
369
675
|
|
|
@@ -391,7 +697,7 @@ if (closeResult.isOk()) {
|
|
|
391
697
|
static create<TContract>(options): Future<Result<TypedAmqpWorker<TContract>, TechnicalError>>;
|
|
392
698
|
```
|
|
393
699
|
|
|
394
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
700
|
+
Defined in: [packages/worker/src/worker.ts:283](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L283)
|
|
395
701
|
|
|
396
702
|
Create a type-safe AMQP worker from a contract.
|
|
397
703
|
|
|
@@ -424,17 +730,13 @@ A Future that resolves to a Result containing the worker or an error
|
|
|
424
730
|
###### Example
|
|
425
731
|
|
|
426
732
|
```typescript
|
|
427
|
-
const
|
|
733
|
+
const worker = await TypedAmqpWorker.create({
|
|
428
734
|
contract: myContract,
|
|
429
735
|
handlers: {
|
|
430
736
|
processOrder: async (msg) => console.log('Order:', msg.orderId)
|
|
431
737
|
},
|
|
432
738
|
urls: ['amqp://localhost']
|
|
433
739
|
}).resultToPromise();
|
|
434
|
-
|
|
435
|
-
if (workerResult.isError()) {
|
|
436
|
-
console.error('Failed to create worker:', workerResult.error);
|
|
437
|
-
}
|
|
438
740
|
```
|
|
439
741
|
|
|
440
742
|
## Type Aliases
|
|
@@ -445,7 +747,7 @@ if (workerResult.isError()) {
|
|
|
445
747
|
type CreateWorkerOptions<TContract> = object;
|
|
446
748
|
```
|
|
447
749
|
|
|
448
|
-
Defined in: [packages/worker/src/worker.ts:
|
|
750
|
+
Defined in: [packages/worker/src/worker.ts:124](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L124)
|
|
449
751
|
|
|
450
752
|
Options for creating a type-safe AMQP worker.
|
|
451
753
|
|
|
@@ -478,7 +780,14 @@ const options: CreateWorkerOptions<typeof contract> = {
|
|
|
478
780
|
connectionOptions: {
|
|
479
781
|
heartbeatIntervalInSeconds: 30
|
|
480
782
|
},
|
|
481
|
-
logger: myLogger
|
|
783
|
+
logger: myLogger,
|
|
784
|
+
retry: {
|
|
785
|
+
maxRetries: 3,
|
|
786
|
+
initialDelayMs: 1000,
|
|
787
|
+
maxDelayMs: 30000,
|
|
788
|
+
backoffMultiplier: 2,
|
|
789
|
+
jitter: true
|
|
790
|
+
}
|
|
482
791
|
};
|
|
483
792
|
```
|
|
484
793
|
|
|
@@ -490,26 +799,302 @@ const options: CreateWorkerOptions<typeof contract> = {
|
|
|
490
799
|
|
|
491
800
|
#### Properties
|
|
492
801
|
|
|
493
|
-
| Property | Type | Description | Defined in |
|
|
494
|
-
| ------ | ------ | ------ | ------ |
|
|
495
|
-
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [packages/worker/src/worker.ts:
|
|
496
|
-
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:
|
|
497
|
-
| <a id="handlers"></a> `handlers` | [`
|
|
498
|
-
| <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:
|
|
499
|
-
| <a id="
|
|
802
|
+
| Property | Type | Description | Defined in |
|
|
803
|
+
| ------ | ------ | ------ | ------ |
|
|
804
|
+
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [packages/worker/src/worker.ts:137](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L137) |
|
|
805
|
+
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:126](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L126) |
|
|
806
|
+
| <a id="handlers"></a> `handlers` | [`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\> | Handlers for each consumer defined in the contract. Handlers must return `Future<Result<void, HandlerError>>` for explicit error handling. Use defineHandler() to create safe handlers, or defineUnsafeHandler() which wraps Promise-based handlers into safe handlers internally. | [packages/worker/src/worker.ts:133](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L133) |
|
|
807
|
+
| <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:139](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L139) |
|
|
808
|
+
| <a id="retry"></a> `retry?` | [`RetryOptions`](#retryoptions) | Retry configuration - when undefined, uses legacy behavior (immediate requeue) | [packages/worker/src/worker.ts:141](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L141) |
|
|
809
|
+
| <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:147](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L147) |
|
|
810
|
+
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:135](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L135) |
|
|
811
|
+
|
|
812
|
+
***
|
|
813
|
+
|
|
814
|
+
### HandlerError
|
|
815
|
+
|
|
816
|
+
```ts
|
|
817
|
+
type HandlerError =
|
|
818
|
+
| RetryableError
|
|
819
|
+
| NonRetryableError;
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
Defined in: [packages/worker/src/errors.ts:83](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/errors.ts#L83)
|
|
823
|
+
|
|
824
|
+
Union type representing all handler errors.
|
|
825
|
+
Use this type when defining handlers that explicitly signal error outcomes.
|
|
826
|
+
|
|
827
|
+
***
|
|
828
|
+
|
|
829
|
+
### RetryOptions
|
|
830
|
+
|
|
831
|
+
```ts
|
|
832
|
+
type RetryOptions = object;
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
Defined in: [packages/worker/src/worker.ts:56](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L56)
|
|
836
|
+
|
|
837
|
+
Retry configuration options for handling failed message processing.
|
|
838
|
+
|
|
839
|
+
When enabled, the worker will automatically retry failed messages using
|
|
840
|
+
RabbitMQ's native TTL + Dead Letter Exchange (DLX) pattern.
|
|
841
|
+
|
|
842
|
+
#### Properties
|
|
843
|
+
|
|
844
|
+
| Property | Type | Description | Defined in |
|
|
845
|
+
| ------ | ------ | ------ | ------ |
|
|
846
|
+
| <a id="backoffmultiplier"></a> `backoffMultiplier?` | `number` | Exponential backoff multiplier (default: 2) | [packages/worker/src/worker.ts:64](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L64) |
|
|
847
|
+
| <a id="initialdelayms"></a> `initialDelayMs?` | `number` | Initial delay in ms before first retry (default: 1000) | [packages/worker/src/worker.ts:60](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L60) |
|
|
848
|
+
| <a id="jitter"></a> `jitter?` | `boolean` | Add jitter to prevent thundering herd (default: true) | [packages/worker/src/worker.ts:66](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L66) |
|
|
849
|
+
| <a id="maxdelayms"></a> `maxDelayMs?` | `number` | Maximum delay in ms between retries (default: 30000) | [packages/worker/src/worker.ts:62](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L62) |
|
|
850
|
+
| <a id="maxretries"></a> `maxRetries?` | `number` | Maximum retry attempts before sending to DLQ (default: 3) | [packages/worker/src/worker.ts:58](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/worker.ts#L58) |
|
|
851
|
+
|
|
852
|
+
***
|
|
853
|
+
|
|
854
|
+
### ~~WorkerInferConsumerBatchHandler~~
|
|
855
|
+
|
|
856
|
+
```ts
|
|
857
|
+
type WorkerInferConsumerBatchHandler<TContract, TName> = WorkerInferUnsafeConsumerBatchHandler<TContract, TName>;
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
Defined in: [packages/worker/src/types.ts:195](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L195)
|
|
861
|
+
|
|
862
|
+
#### Type Parameters
|
|
863
|
+
|
|
864
|
+
| Type Parameter |
|
|
865
|
+
| ------ |
|
|
866
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
867
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
868
|
+
|
|
869
|
+
#### Deprecated
|
|
870
|
+
|
|
871
|
+
Use WorkerInferUnsafeConsumerBatchHandler instead
|
|
872
|
+
|
|
873
|
+
***
|
|
874
|
+
|
|
875
|
+
### ~~WorkerInferConsumerHandler~~
|
|
876
|
+
|
|
877
|
+
```ts
|
|
878
|
+
type WorkerInferConsumerHandler<TContract, TName> = WorkerInferUnsafeConsumerHandler<TContract, TName>;
|
|
879
|
+
```
|
|
880
|
+
|
|
881
|
+
Defined in: [packages/worker/src/types.ts:187](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L187)
|
|
882
|
+
|
|
883
|
+
#### Type Parameters
|
|
884
|
+
|
|
885
|
+
| Type Parameter |
|
|
886
|
+
| ------ |
|
|
887
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
888
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
889
|
+
|
|
890
|
+
#### Deprecated
|
|
891
|
+
|
|
892
|
+
Use WorkerInferUnsafeConsumerHandler instead
|
|
893
|
+
|
|
894
|
+
***
|
|
895
|
+
|
|
896
|
+
### ~~WorkerInferConsumerHandlerEntry~~
|
|
897
|
+
|
|
898
|
+
```ts
|
|
899
|
+
type WorkerInferConsumerHandlerEntry<TContract, TName> = WorkerInferUnsafeConsumerHandlerEntry<TContract, TName>;
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
Defined in: [packages/worker/src/types.ts:203](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L203)
|
|
903
|
+
|
|
904
|
+
#### Type Parameters
|
|
905
|
+
|
|
906
|
+
| Type Parameter |
|
|
907
|
+
| ------ |
|
|
908
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
909
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
910
|
+
|
|
911
|
+
#### Deprecated
|
|
912
|
+
|
|
913
|
+
Use WorkerInferUnsafeConsumerHandlerEntry instead
|
|
914
|
+
|
|
915
|
+
***
|
|
916
|
+
|
|
917
|
+
### ~~WorkerInferConsumerHandlers~~
|
|
918
|
+
|
|
919
|
+
```ts
|
|
920
|
+
type WorkerInferConsumerHandlers<TContract> = WorkerInferUnsafeConsumerHandlers<TContract>;
|
|
921
|
+
```
|
|
922
|
+
|
|
923
|
+
Defined in: [packages/worker/src/types.ts:211](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L211)
|
|
924
|
+
|
|
925
|
+
#### Type Parameters
|
|
926
|
+
|
|
927
|
+
| Type Parameter |
|
|
928
|
+
| ------ |
|
|
929
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
930
|
+
|
|
931
|
+
#### Deprecated
|
|
932
|
+
|
|
933
|
+
Use WorkerInferUnsafeConsumerHandlers instead
|
|
934
|
+
|
|
935
|
+
***
|
|
936
|
+
|
|
937
|
+
### WorkerInferConsumerInput
|
|
938
|
+
|
|
939
|
+
```ts
|
|
940
|
+
type WorkerInferConsumerInput<TContract, TName> = ConsumerInferInput<InferConsumer<TContract, TName>>;
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
Defined in: [packages/worker/src/types.ts:39](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L39)
|
|
944
|
+
|
|
945
|
+
Worker perspective types - for consuming messages
|
|
946
|
+
|
|
947
|
+
#### Type Parameters
|
|
948
|
+
|
|
949
|
+
| Type Parameter |
|
|
950
|
+
| ------ |
|
|
951
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
952
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
953
|
+
|
|
954
|
+
***
|
|
955
|
+
|
|
956
|
+
### WorkerInferSafeConsumerBatchHandler()
|
|
957
|
+
|
|
958
|
+
```ts
|
|
959
|
+
type WorkerInferSafeConsumerBatchHandler<TContract, TName> = (messages) => Future<Result<void, HandlerError>>;
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
Defined in: [packages/worker/src/types.ts:80](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L80)
|
|
963
|
+
|
|
964
|
+
Safe consumer handler type for batch processing.
|
|
965
|
+
Returns a `Future<Result<void, HandlerError>>` for explicit error handling.
|
|
966
|
+
|
|
967
|
+
#### Type Parameters
|
|
968
|
+
|
|
969
|
+
| Type Parameter |
|
|
970
|
+
| ------ |
|
|
971
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
972
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
973
|
+
|
|
974
|
+
#### Parameters
|
|
975
|
+
|
|
976
|
+
| Parameter | Type |
|
|
977
|
+
| ------ | ------ |
|
|
978
|
+
| `messages` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\>[] |
|
|
979
|
+
|
|
980
|
+
#### Returns
|
|
981
|
+
|
|
982
|
+
`Future`\<`Result`\<`void`, [`HandlerError`](#handlererror)\>\>
|
|
983
|
+
|
|
984
|
+
#### Example
|
|
985
|
+
|
|
986
|
+
```typescript
|
|
987
|
+
const handler: WorkerInferSafeConsumerBatchHandler<typeof contract, 'processOrders'> =
|
|
988
|
+
(messages) => Future.value(Result.Ok(undefined));
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
***
|
|
992
|
+
|
|
993
|
+
### WorkerInferSafeConsumerHandler()
|
|
994
|
+
|
|
995
|
+
```ts
|
|
996
|
+
type WorkerInferSafeConsumerHandler<TContract, TName> = (message) => Future<Result<void, HandlerError>>;
|
|
997
|
+
```
|
|
998
|
+
|
|
999
|
+
Defined in: [packages/worker/src/types.ts:65](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L65)
|
|
1000
|
+
|
|
1001
|
+
Safe consumer handler type for a specific consumer.
|
|
1002
|
+
Returns a `Future<Result<void, HandlerError>>` for explicit error handling.
|
|
1003
|
+
|
|
1004
|
+
**Recommended over unsafe handlers** for better error control:
|
|
1005
|
+
- RetryableError: Message will be retried with exponential backoff
|
|
1006
|
+
- NonRetryableError: Message will be immediately sent to DLQ
|
|
1007
|
+
|
|
1008
|
+
#### Type Parameters
|
|
1009
|
+
|
|
1010
|
+
| Type Parameter |
|
|
1011
|
+
| ------ |
|
|
1012
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
1013
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
1014
|
+
|
|
1015
|
+
#### Parameters
|
|
1016
|
+
|
|
1017
|
+
| Parameter | Type |
|
|
1018
|
+
| ------ | ------ |
|
|
1019
|
+
| `message` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\> |
|
|
1020
|
+
|
|
1021
|
+
#### Returns
|
|
1022
|
+
|
|
1023
|
+
`Future`\<`Result`\<`void`, [`HandlerError`](#handlererror)\>\>
|
|
1024
|
+
|
|
1025
|
+
#### Example
|
|
1026
|
+
|
|
1027
|
+
```typescript
|
|
1028
|
+
const handler: WorkerInferSafeConsumerHandler<typeof contract, 'processOrder'> =
|
|
1029
|
+
(message) => Future.value(Result.Ok(undefined));
|
|
1030
|
+
```
|
|
1031
|
+
|
|
1032
|
+
***
|
|
1033
|
+
|
|
1034
|
+
### WorkerInferSafeConsumerHandlerEntry
|
|
1035
|
+
|
|
1036
|
+
```ts
|
|
1037
|
+
type WorkerInferSafeConsumerHandlerEntry<TContract, TName> =
|
|
1038
|
+
| WorkerInferSafeConsumerHandler<TContract, TName>
|
|
1039
|
+
| readonly [WorkerInferSafeConsumerHandler<TContract, TName>, {
|
|
1040
|
+
batchSize?: never;
|
|
1041
|
+
batchTimeout?: never;
|
|
1042
|
+
prefetch?: number;
|
|
1043
|
+
}]
|
|
1044
|
+
| readonly [WorkerInferSafeConsumerBatchHandler<TContract, TName>, {
|
|
1045
|
+
batchSize: number;
|
|
1046
|
+
batchTimeout?: number;
|
|
1047
|
+
prefetch?: number;
|
|
1048
|
+
}];
|
|
1049
|
+
```
|
|
1050
|
+
|
|
1051
|
+
Defined in: [packages/worker/src/types.ts:95](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L95)
|
|
1052
|
+
|
|
1053
|
+
Safe handler entry for a consumer - either a function or a tuple of [handler, options].
|
|
1054
|
+
|
|
1055
|
+
Three patterns are supported:
|
|
1056
|
+
1. Simple handler: `(message) => Future.value(Result.Ok(undefined))`
|
|
1057
|
+
2. Handler with prefetch: `[(message) => ..., { prefetch: 10 }]`
|
|
1058
|
+
3. Batch handler: `[(messages) => ..., { batchSize: 5, batchTimeout: 1000 }]`
|
|
1059
|
+
|
|
1060
|
+
#### Type Parameters
|
|
1061
|
+
|
|
1062
|
+
| Type Parameter |
|
|
1063
|
+
| ------ |
|
|
1064
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
1065
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
1066
|
+
|
|
1067
|
+
***
|
|
1068
|
+
|
|
1069
|
+
### WorkerInferSafeConsumerHandlers
|
|
1070
|
+
|
|
1071
|
+
```ts
|
|
1072
|
+
type WorkerInferSafeConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferSafeConsumerHandlerEntry<TContract, K> };
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
Defined in: [packages/worker/src/types.ts:113](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L113)
|
|
1076
|
+
|
|
1077
|
+
Safe consumer handlers for a contract.
|
|
1078
|
+
All handlers return `Future<Result<void, HandlerError>>` for explicit error control.
|
|
1079
|
+
|
|
1080
|
+
#### Type Parameters
|
|
1081
|
+
|
|
1082
|
+
| Type Parameter |
|
|
1083
|
+
| ------ |
|
|
1084
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
500
1085
|
|
|
501
1086
|
***
|
|
502
1087
|
|
|
503
|
-
###
|
|
1088
|
+
### ~~WorkerInferUnsafeConsumerBatchHandler()~~
|
|
504
1089
|
|
|
505
1090
|
```ts
|
|
506
|
-
type
|
|
1091
|
+
type WorkerInferUnsafeConsumerBatchHandler<TContract, TName> = (messages) => Promise<void>;
|
|
507
1092
|
```
|
|
508
1093
|
|
|
509
|
-
Defined in: [packages/worker/src/types.ts:
|
|
1094
|
+
Defined in: [packages/worker/src/types.ts:147](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L147)
|
|
510
1095
|
|
|
511
|
-
|
|
512
|
-
|
|
1096
|
+
Unsafe consumer handler type for batch processing.
|
|
1097
|
+
Returns a `Promise<void>` - throws exceptions on error.
|
|
513
1098
|
|
|
514
1099
|
#### Type Parameters
|
|
515
1100
|
|
|
@@ -528,19 +1113,23 @@ Batch handlers receive an array of messages.
|
|
|
528
1113
|
|
|
529
1114
|
`Promise`\<`void`\>
|
|
530
1115
|
|
|
1116
|
+
#### Deprecated
|
|
1117
|
+
|
|
1118
|
+
Prefer using safe handlers (WorkerInferSafeConsumerBatchHandler) that return
|
|
1119
|
+
`Future<Result<void, HandlerError>>` for better error handling.
|
|
1120
|
+
|
|
531
1121
|
***
|
|
532
1122
|
|
|
533
|
-
###
|
|
1123
|
+
### ~~WorkerInferUnsafeConsumerHandler()~~
|
|
534
1124
|
|
|
535
1125
|
```ts
|
|
536
|
-
type
|
|
1126
|
+
type WorkerInferUnsafeConsumerHandler<TContract, TName> = (message) => Promise<void>;
|
|
537
1127
|
```
|
|
538
1128
|
|
|
539
|
-
Defined in: [packages/worker/src/types.ts:
|
|
1129
|
+
Defined in: [packages/worker/src/types.ts:135](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L135)
|
|
540
1130
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
For batch processing, use consumerOptions to configure batch behavior.
|
|
1131
|
+
Unsafe consumer handler type for a specific consumer.
|
|
1132
|
+
Returns a `Promise<void>` - throws exceptions on error.
|
|
544
1133
|
|
|
545
1134
|
#### Type Parameters
|
|
546
1135
|
|
|
@@ -559,33 +1148,37 @@ For batch processing, use consumerOptions to configure batch behavior.
|
|
|
559
1148
|
|
|
560
1149
|
`Promise`\<`void`\>
|
|
561
1150
|
|
|
1151
|
+
#### Deprecated
|
|
1152
|
+
|
|
1153
|
+
Prefer using safe handlers (WorkerInferSafeConsumerHandler) that return
|
|
1154
|
+
`Future<Result<void, HandlerError>>` for better error handling.
|
|
1155
|
+
|
|
1156
|
+
**Note:** When using unsafe handlers:
|
|
1157
|
+
- All thrown errors are treated as retryable by default (when retry is configured)
|
|
1158
|
+
- Use RetryableError or NonRetryableError to control retry behavior explicitly
|
|
1159
|
+
|
|
562
1160
|
***
|
|
563
1161
|
|
|
564
|
-
###
|
|
1162
|
+
### ~~WorkerInferUnsafeConsumerHandlerEntry~~
|
|
565
1163
|
|
|
566
1164
|
```ts
|
|
567
|
-
type
|
|
568
|
-
|
|
|
569
|
-
| readonly [
|
|
1165
|
+
type WorkerInferUnsafeConsumerHandlerEntry<TContract, TName> =
|
|
1166
|
+
| WorkerInferUnsafeConsumerHandler<TContract, TName>
|
|
1167
|
+
| readonly [WorkerInferUnsafeConsumerHandler<TContract, TName>, {
|
|
570
1168
|
batchSize?: never;
|
|
571
1169
|
batchTimeout?: never;
|
|
572
1170
|
prefetch?: number;
|
|
573
1171
|
}]
|
|
574
|
-
| readonly [
|
|
1172
|
+
| readonly [WorkerInferUnsafeConsumerBatchHandler<TContract, TName>, {
|
|
575
1173
|
batchSize: number;
|
|
576
1174
|
batchTimeout?: number;
|
|
577
1175
|
prefetch?: number;
|
|
578
1176
|
}];
|
|
579
1177
|
```
|
|
580
1178
|
|
|
581
|
-
Defined in: [packages/worker/src/types.ts:
|
|
1179
|
+
Defined in: [packages/worker/src/types.ts:157](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L157)
|
|
582
1180
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
Three patterns are supported:
|
|
586
|
-
1. Simple handler: `async (message) => { ... }`
|
|
587
|
-
2. Handler with prefetch: `[async (message) => { ... }, { prefetch: 10 }]`
|
|
588
|
-
3. Batch handler: `[async (messages) => { ... }, { batchSize: 5, batchTimeout: 1000 }]`
|
|
1181
|
+
Unsafe handler entry for a consumer - either a function or a tuple of [handler, options].
|
|
589
1182
|
|
|
590
1183
|
#### Type Parameters
|
|
591
1184
|
|
|
@@ -594,43 +1187,31 @@ Three patterns are supported:
|
|
|
594
1187
|
| `TContract` *extends* `ContractDefinition` |
|
|
595
1188
|
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
596
1189
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
### WorkerInferConsumerHandlers
|
|
600
|
-
|
|
601
|
-
```ts
|
|
602
|
-
type WorkerInferConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferConsumerHandlerEntry<TContract, K> };
|
|
603
|
-
```
|
|
604
|
-
|
|
605
|
-
Defined in: [packages/worker/src/types.ts:87](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/types.ts#L87)
|
|
606
|
-
|
|
607
|
-
Infer all consumer handlers for a contract.
|
|
608
|
-
Handlers can be either single-message handlers, batch handlers, or a tuple of [handler, options].
|
|
609
|
-
|
|
610
|
-
#### Type Parameters
|
|
1190
|
+
#### Deprecated
|
|
611
1191
|
|
|
612
|
-
|
|
613
|
-
| ------ |
|
|
614
|
-
| `TContract` *extends* `ContractDefinition` |
|
|
1192
|
+
Prefer using safe handler entries (WorkerInferSafeConsumerHandlerEntry).
|
|
615
1193
|
|
|
616
1194
|
***
|
|
617
1195
|
|
|
618
|
-
###
|
|
1196
|
+
### ~~WorkerInferUnsafeConsumerHandlers~~
|
|
619
1197
|
|
|
620
1198
|
```ts
|
|
621
|
-
type
|
|
1199
|
+
type WorkerInferUnsafeConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferUnsafeConsumerHandlerEntry<TContract, K> };
|
|
622
1200
|
```
|
|
623
1201
|
|
|
624
|
-
Defined in: [packages/worker/src/types.ts:
|
|
1202
|
+
Defined in: [packages/worker/src/types.ts:176](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/types.ts#L176)
|
|
625
1203
|
|
|
626
|
-
|
|
1204
|
+
Unsafe consumer handlers for a contract.
|
|
627
1205
|
|
|
628
1206
|
#### Type Parameters
|
|
629
1207
|
|
|
630
1208
|
| Type Parameter |
|
|
631
1209
|
| ------ |
|
|
632
1210
|
| `TContract` *extends* `ContractDefinition` |
|
|
633
|
-
|
|
1211
|
+
|
|
1212
|
+
#### Deprecated
|
|
1213
|
+
|
|
1214
|
+
Prefer using safe handlers (WorkerInferSafeConsumerHandlers).
|
|
634
1215
|
|
|
635
1216
|
## Functions
|
|
636
1217
|
|
|
@@ -642,24 +1223,21 @@ Worker perspective types - for consuming messages
|
|
|
642
1223
|
function defineHandler<TContract, TName>(
|
|
643
1224
|
contract,
|
|
644
1225
|
consumerName,
|
|
645
|
-
handler):
|
|
1226
|
+
handler): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
646
1227
|
```
|
|
647
1228
|
|
|
648
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
1229
|
+
Defined in: [packages/worker/src/handlers.ts:134](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L134)
|
|
649
1230
|
|
|
650
1231
|
Define a type-safe handler for a specific consumer in a contract.
|
|
651
1232
|
|
|
652
|
-
This
|
|
653
|
-
providing
|
|
1233
|
+
**Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
|
|
1234
|
+
providing explicit error handling and better control over retry behavior.
|
|
654
1235
|
|
|
655
1236
|
Supports three patterns:
|
|
656
1237
|
1. Simple handler: just the function (single message handler)
|
|
657
1238
|
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
658
1239
|
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
659
1240
|
|
|
660
|
-
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
661
|
-
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
662
|
-
|
|
663
1241
|
##### Type Parameters
|
|
664
1242
|
|
|
665
1243
|
| Type Parameter | Description |
|
|
@@ -673,49 +1251,42 @@ batchSize configuration. You cannot create a batch handler without specifying ba
|
|
|
673
1251
|
| ------ | ------ | ------ |
|
|
674
1252
|
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
675
1253
|
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
676
|
-
| `handler` | [`
|
|
1254
|
+
| `handler` | [`WorkerInferSafeConsumerHandler`](#workerinfersafeconsumerhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
|
|
677
1255
|
|
|
678
1256
|
##### Returns
|
|
679
1257
|
|
|
680
|
-
[`
|
|
1258
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
681
1259
|
|
|
682
1260
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
683
1261
|
|
|
684
1262
|
##### Example
|
|
685
1263
|
|
|
686
1264
|
```typescript
|
|
687
|
-
import { defineHandler } from '@amqp-contract/worker';
|
|
1265
|
+
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
1266
|
+
import { Future, Result } from '@swan-io/boxed';
|
|
688
1267
|
import { orderContract } from './contract';
|
|
689
1268
|
|
|
690
|
-
// Simple
|
|
1269
|
+
// Simple handler with explicit error handling using mapError
|
|
691
1270
|
const processOrderHandler = defineHandler(
|
|
692
1271
|
orderContract,
|
|
693
1272
|
'processOrder',
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
);
|
|
699
|
-
|
|
700
|
-
// Single-message handler with prefetch
|
|
701
|
-
const processOrderWithPrefetch = defineHandler(
|
|
702
|
-
orderContract,
|
|
703
|
-
'processOrder',
|
|
704
|
-
async (message) => {
|
|
705
|
-
await processOrder(message);
|
|
706
|
-
},
|
|
707
|
-
{ prefetch: 10 }
|
|
1273
|
+
(message) =>
|
|
1274
|
+
Future.fromPromise(processPayment(message))
|
|
1275
|
+
.mapOk(() => undefined)
|
|
1276
|
+
.mapError((error) => new RetryableError('Payment failed', error))
|
|
708
1277
|
);
|
|
709
1278
|
|
|
710
|
-
//
|
|
711
|
-
const
|
|
1279
|
+
// Handler with validation (non-retryable error)
|
|
1280
|
+
const validateOrderHandler = defineHandler(
|
|
712
1281
|
orderContract,
|
|
713
|
-
'
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
1282
|
+
'validateOrder',
|
|
1283
|
+
(message) => {
|
|
1284
|
+
if (message.amount < 1) {
|
|
1285
|
+
// Won't be retried - goes directly to DLQ
|
|
1286
|
+
return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
|
|
1287
|
+
}
|
|
1288
|
+
return Future.value(Result.Ok(undefined));
|
|
1289
|
+
}
|
|
719
1290
|
);
|
|
720
1291
|
```
|
|
721
1292
|
|
|
@@ -726,24 +1297,21 @@ function defineHandler<TContract, TName>(
|
|
|
726
1297
|
contract,
|
|
727
1298
|
consumerName,
|
|
728
1299
|
handler,
|
|
729
|
-
options):
|
|
1300
|
+
options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
730
1301
|
```
|
|
731
1302
|
|
|
732
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
1303
|
+
Defined in: [packages/worker/src/handlers.ts:142](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L142)
|
|
733
1304
|
|
|
734
1305
|
Define a type-safe handler for a specific consumer in a contract.
|
|
735
1306
|
|
|
736
|
-
This
|
|
737
|
-
providing
|
|
1307
|
+
**Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
|
|
1308
|
+
providing explicit error handling and better control over retry behavior.
|
|
738
1309
|
|
|
739
1310
|
Supports three patterns:
|
|
740
1311
|
1. Simple handler: just the function (single message handler)
|
|
741
1312
|
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
742
1313
|
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
743
1314
|
|
|
744
|
-
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
745
|
-
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
746
|
-
|
|
747
1315
|
##### Type Parameters
|
|
748
1316
|
|
|
749
1317
|
| Type Parameter | Description |
|
|
@@ -757,53 +1325,46 @@ batchSize configuration. You cannot create a batch handler without specifying ba
|
|
|
757
1325
|
| ------ | ------ | ------ |
|
|
758
1326
|
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
759
1327
|
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
760
|
-
| `handler` | [`
|
|
761
|
-
| `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout)
|
|
1328
|
+
| `handler` | [`WorkerInferSafeConsumerHandler`](#workerinfersafeconsumerhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
|
|
1329
|
+
| `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
|
|
762
1330
|
| `options.batchSize?` | `undefined` | - |
|
|
763
1331
|
| `options.batchTimeout?` | `undefined` | - |
|
|
764
1332
|
| `options.prefetch?` | `number` | - |
|
|
765
1333
|
|
|
766
1334
|
##### Returns
|
|
767
1335
|
|
|
768
|
-
[`
|
|
1336
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
769
1337
|
|
|
770
1338
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
771
1339
|
|
|
772
1340
|
##### Example
|
|
773
1341
|
|
|
774
1342
|
```typescript
|
|
775
|
-
import { defineHandler } from '@amqp-contract/worker';
|
|
1343
|
+
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
1344
|
+
import { Future, Result } from '@swan-io/boxed';
|
|
776
1345
|
import { orderContract } from './contract';
|
|
777
1346
|
|
|
778
|
-
// Simple
|
|
1347
|
+
// Simple handler with explicit error handling using mapError
|
|
779
1348
|
const processOrderHandler = defineHandler(
|
|
780
1349
|
orderContract,
|
|
781
1350
|
'processOrder',
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
);
|
|
787
|
-
|
|
788
|
-
// Single-message handler with prefetch
|
|
789
|
-
const processOrderWithPrefetch = defineHandler(
|
|
790
|
-
orderContract,
|
|
791
|
-
'processOrder',
|
|
792
|
-
async (message) => {
|
|
793
|
-
await processOrder(message);
|
|
794
|
-
},
|
|
795
|
-
{ prefetch: 10 }
|
|
1351
|
+
(message) =>
|
|
1352
|
+
Future.fromPromise(processPayment(message))
|
|
1353
|
+
.mapOk(() => undefined)
|
|
1354
|
+
.mapError((error) => new RetryableError('Payment failed', error))
|
|
796
1355
|
);
|
|
797
1356
|
|
|
798
|
-
//
|
|
799
|
-
const
|
|
1357
|
+
// Handler with validation (non-retryable error)
|
|
1358
|
+
const validateOrderHandler = defineHandler(
|
|
800
1359
|
orderContract,
|
|
801
|
-
'
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1360
|
+
'validateOrder',
|
|
1361
|
+
(message) => {
|
|
1362
|
+
if (message.amount < 1) {
|
|
1363
|
+
// Won't be retried - goes directly to DLQ
|
|
1364
|
+
return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
|
|
1365
|
+
}
|
|
1366
|
+
return Future.value(Result.Ok(undefined));
|
|
1367
|
+
}
|
|
807
1368
|
);
|
|
808
1369
|
```
|
|
809
1370
|
|
|
@@ -814,24 +1375,21 @@ function defineHandler<TContract, TName>(
|
|
|
814
1375
|
contract,
|
|
815
1376
|
consumerName,
|
|
816
1377
|
handler,
|
|
817
|
-
options):
|
|
1378
|
+
options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
818
1379
|
```
|
|
819
1380
|
|
|
820
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
1381
|
+
Defined in: [packages/worker/src/handlers.ts:151](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L151)
|
|
821
1382
|
|
|
822
1383
|
Define a type-safe handler for a specific consumer in a contract.
|
|
823
1384
|
|
|
824
|
-
This
|
|
825
|
-
providing
|
|
1385
|
+
**Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
|
|
1386
|
+
providing explicit error handling and better control over retry behavior.
|
|
826
1387
|
|
|
827
1388
|
Supports three patterns:
|
|
828
1389
|
1. Simple handler: just the function (single message handler)
|
|
829
1390
|
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
830
1391
|
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
831
1392
|
|
|
832
|
-
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
833
|
-
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
834
|
-
|
|
835
1393
|
##### Type Parameters
|
|
836
1394
|
|
|
837
1395
|
| Type Parameter | Description |
|
|
@@ -845,70 +1403,313 @@ batchSize configuration. You cannot create a batch handler without specifying ba
|
|
|
845
1403
|
| ------ | ------ | ------ |
|
|
846
1404
|
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
847
1405
|
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
848
|
-
| `handler` | [`
|
|
849
|
-
| `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout)
|
|
1406
|
+
| `handler` | [`WorkerInferSafeConsumerBatchHandler`](#workerinfersafeconsumerbatchhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
|
|
1407
|
+
| `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
|
|
850
1408
|
| `options.batchSize` | `number` | - |
|
|
851
1409
|
| `options.batchTimeout?` | `number` | - |
|
|
852
1410
|
| `options.prefetch?` | `number` | - |
|
|
853
1411
|
|
|
854
1412
|
##### Returns
|
|
855
1413
|
|
|
856
|
-
[`
|
|
1414
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
857
1415
|
|
|
858
1416
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
859
1417
|
|
|
860
1418
|
##### Example
|
|
861
1419
|
|
|
862
1420
|
```typescript
|
|
863
|
-
import { defineHandler } from '@amqp-contract/worker';
|
|
1421
|
+
import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
|
|
1422
|
+
import { Future, Result } from '@swan-io/boxed';
|
|
864
1423
|
import { orderContract } from './contract';
|
|
865
1424
|
|
|
866
|
-
// Simple
|
|
1425
|
+
// Simple handler with explicit error handling using mapError
|
|
867
1426
|
const processOrderHandler = defineHandler(
|
|
1427
|
+
orderContract,
|
|
1428
|
+
'processOrder',
|
|
1429
|
+
(message) =>
|
|
1430
|
+
Future.fromPromise(processPayment(message))
|
|
1431
|
+
.mapOk(() => undefined)
|
|
1432
|
+
.mapError((error) => new RetryableError('Payment failed', error))
|
|
1433
|
+
);
|
|
1434
|
+
|
|
1435
|
+
// Handler with validation (non-retryable error)
|
|
1436
|
+
const validateOrderHandler = defineHandler(
|
|
1437
|
+
orderContract,
|
|
1438
|
+
'validateOrder',
|
|
1439
|
+
(message) => {
|
|
1440
|
+
if (message.amount < 1) {
|
|
1441
|
+
// Won't be retried - goes directly to DLQ
|
|
1442
|
+
return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
|
|
1443
|
+
}
|
|
1444
|
+
return Future.value(Result.Ok(undefined));
|
|
1445
|
+
}
|
|
1446
|
+
);
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
***
|
|
1450
|
+
|
|
1451
|
+
### defineHandlers()
|
|
1452
|
+
|
|
1453
|
+
```ts
|
|
1454
|
+
function defineHandlers<TContract>(contract, handlers): WorkerInferSafeConsumerHandlers<TContract>;
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1457
|
+
Defined in: [packages/worker/src/handlers.ts:208](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L208)
|
|
1458
|
+
|
|
1459
|
+
Define multiple type-safe handlers for consumers in a contract.
|
|
1460
|
+
|
|
1461
|
+
**Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
|
|
1462
|
+
providing explicit error handling and better control over retry behavior.
|
|
1463
|
+
|
|
1464
|
+
#### Type Parameters
|
|
1465
|
+
|
|
1466
|
+
| Type Parameter | Description |
|
|
1467
|
+
| ------ | ------ |
|
|
1468
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
1469
|
+
|
|
1470
|
+
#### Parameters
|
|
1471
|
+
|
|
1472
|
+
| Parameter | Type | Description |
|
|
1473
|
+
| ------ | ------ | ------ |
|
|
1474
|
+
| `contract` | `TContract` | The contract definition containing the consumers |
|
|
1475
|
+
| `handlers` | [`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\> | An object with handler functions for each consumer |
|
|
1476
|
+
|
|
1477
|
+
#### Returns
|
|
1478
|
+
|
|
1479
|
+
[`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\>
|
|
1480
|
+
|
|
1481
|
+
A type-safe handlers object that can be used with TypedAmqpWorker
|
|
1482
|
+
|
|
1483
|
+
#### Example
|
|
1484
|
+
|
|
1485
|
+
```typescript
|
|
1486
|
+
import { defineHandlers, RetryableError } from '@amqp-contract/worker';
|
|
1487
|
+
import { Future } from '@swan-io/boxed';
|
|
1488
|
+
import { orderContract } from './contract';
|
|
1489
|
+
|
|
1490
|
+
const handlers = defineHandlers(orderContract, {
|
|
1491
|
+
processOrder: (message) =>
|
|
1492
|
+
Future.fromPromise(processPayment(message))
|
|
1493
|
+
.mapOk(() => undefined)
|
|
1494
|
+
.mapError((error) => new RetryableError('Payment failed', error)),
|
|
1495
|
+
notifyOrder: (message) =>
|
|
1496
|
+
Future.fromPromise(sendNotification(message))
|
|
1497
|
+
.mapOk(() => undefined)
|
|
1498
|
+
.mapError((error) => new RetryableError('Notification failed', error)),
|
|
1499
|
+
});
|
|
1500
|
+
```
|
|
1501
|
+
|
|
1502
|
+
***
|
|
1503
|
+
|
|
1504
|
+
### ~~defineUnsafeHandler()~~
|
|
1505
|
+
|
|
1506
|
+
#### Call Signature
|
|
1507
|
+
|
|
1508
|
+
```ts
|
|
1509
|
+
function defineUnsafeHandler<TContract, TName>(
|
|
1510
|
+
contract,
|
|
1511
|
+
consumerName,
|
|
1512
|
+
handler): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
1513
|
+
```
|
|
1514
|
+
|
|
1515
|
+
Defined in: [packages/worker/src/handlers.ts:272](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L272)
|
|
1516
|
+
|
|
1517
|
+
Define an unsafe handler for a specific consumer in a contract.
|
|
1518
|
+
|
|
1519
|
+
##### Type Parameters
|
|
1520
|
+
|
|
1521
|
+
| Type Parameter | Description |
|
|
1522
|
+
| ------ | ------ |
|
|
1523
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
1524
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
1525
|
+
|
|
1526
|
+
##### Parameters
|
|
1527
|
+
|
|
1528
|
+
| Parameter | Type | Description |
|
|
1529
|
+
| ------ | ------ | ------ |
|
|
1530
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
1531
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
1532
|
+
| `handler` | `UnsafeHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
|
|
1533
|
+
|
|
1534
|
+
##### Returns
|
|
1535
|
+
|
|
1536
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
1537
|
+
|
|
1538
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
1539
|
+
|
|
1540
|
+
##### Deprecated
|
|
1541
|
+
|
|
1542
|
+
Use `defineHandler` instead for explicit error handling with `Future<Result>`.
|
|
1543
|
+
|
|
1544
|
+
**Warning:** Unsafe handlers use exception-based error handling:
|
|
1545
|
+
- All thrown errors are treated as retryable by default
|
|
1546
|
+
- Harder to reason about which errors should be retried
|
|
1547
|
+
- May lead to unexpected retry behavior
|
|
1548
|
+
|
|
1549
|
+
**Note:** Internally, this function wraps the Promise-based handler into a Future-based
|
|
1550
|
+
safe handler for consistent processing in the worker.
|
|
1551
|
+
|
|
1552
|
+
##### Example
|
|
1553
|
+
|
|
1554
|
+
```typescript
|
|
1555
|
+
import { defineUnsafeHandler } from '@amqp-contract/worker';
|
|
1556
|
+
|
|
1557
|
+
// ⚠️ Consider using defineHandler for better error handling
|
|
1558
|
+
const processOrderHandler = defineUnsafeHandler(
|
|
868
1559
|
orderContract,
|
|
869
1560
|
'processOrder',
|
|
870
1561
|
async (message) => {
|
|
871
|
-
|
|
1562
|
+
// Throws on error - will be retried
|
|
872
1563
|
await processPayment(message);
|
|
873
1564
|
}
|
|
874
1565
|
);
|
|
1566
|
+
```
|
|
1567
|
+
|
|
1568
|
+
#### Call Signature
|
|
1569
|
+
|
|
1570
|
+
```ts
|
|
1571
|
+
function defineUnsafeHandler<TContract, TName>(
|
|
1572
|
+
contract,
|
|
1573
|
+
consumerName,
|
|
1574
|
+
handler,
|
|
1575
|
+
options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
1576
|
+
```
|
|
1577
|
+
|
|
1578
|
+
Defined in: [packages/worker/src/handlers.ts:280](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L280)
|
|
1579
|
+
|
|
1580
|
+
Define an unsafe handler for a specific consumer in a contract.
|
|
1581
|
+
|
|
1582
|
+
##### Type Parameters
|
|
1583
|
+
|
|
1584
|
+
| Type Parameter | Description |
|
|
1585
|
+
| ------ | ------ |
|
|
1586
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
1587
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
1588
|
+
|
|
1589
|
+
##### Parameters
|
|
1590
|
+
|
|
1591
|
+
| Parameter | Type | Description |
|
|
1592
|
+
| ------ | ------ | ------ |
|
|
1593
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
1594
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
1595
|
+
| `handler` | `UnsafeHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
|
|
1596
|
+
| `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
|
|
1597
|
+
| `options.batchSize?` | `undefined` | - |
|
|
1598
|
+
| `options.batchTimeout?` | `undefined` | - |
|
|
1599
|
+
| `options.prefetch?` | `number` | - |
|
|
1600
|
+
|
|
1601
|
+
##### Returns
|
|
1602
|
+
|
|
1603
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
1604
|
+
|
|
1605
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
875
1606
|
|
|
876
|
-
|
|
877
|
-
|
|
1607
|
+
##### Deprecated
|
|
1608
|
+
|
|
1609
|
+
Use `defineHandler` instead for explicit error handling with `Future<Result>`.
|
|
1610
|
+
|
|
1611
|
+
**Warning:** Unsafe handlers use exception-based error handling:
|
|
1612
|
+
- All thrown errors are treated as retryable by default
|
|
1613
|
+
- Harder to reason about which errors should be retried
|
|
1614
|
+
- May lead to unexpected retry behavior
|
|
1615
|
+
|
|
1616
|
+
**Note:** Internally, this function wraps the Promise-based handler into a Future-based
|
|
1617
|
+
safe handler for consistent processing in the worker.
|
|
1618
|
+
|
|
1619
|
+
##### Example
|
|
1620
|
+
|
|
1621
|
+
```typescript
|
|
1622
|
+
import { defineUnsafeHandler } from '@amqp-contract/worker';
|
|
1623
|
+
|
|
1624
|
+
// ⚠️ Consider using defineHandler for better error handling
|
|
1625
|
+
const processOrderHandler = defineUnsafeHandler(
|
|
878
1626
|
orderContract,
|
|
879
1627
|
'processOrder',
|
|
880
1628
|
async (message) => {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
1629
|
+
// Throws on error - will be retried
|
|
1630
|
+
await processPayment(message);
|
|
1631
|
+
}
|
|
884
1632
|
);
|
|
1633
|
+
```
|
|
1634
|
+
|
|
1635
|
+
#### Call Signature
|
|
1636
|
+
|
|
1637
|
+
```ts
|
|
1638
|
+
function defineUnsafeHandler<TContract, TName>(
|
|
1639
|
+
contract,
|
|
1640
|
+
consumerName,
|
|
1641
|
+
handler,
|
|
1642
|
+
options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
|
|
1643
|
+
```
|
|
1644
|
+
|
|
1645
|
+
Defined in: [packages/worker/src/handlers.ts:289](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L289)
|
|
1646
|
+
|
|
1647
|
+
Define an unsafe handler for a specific consumer in a contract.
|
|
1648
|
+
|
|
1649
|
+
##### Type Parameters
|
|
1650
|
+
|
|
1651
|
+
| Type Parameter | Description |
|
|
1652
|
+
| ------ | ------ |
|
|
1653
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
1654
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
1655
|
+
|
|
1656
|
+
##### Parameters
|
|
1657
|
+
|
|
1658
|
+
| Parameter | Type | Description |
|
|
1659
|
+
| ------ | ------ | ------ |
|
|
1660
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
1661
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
1662
|
+
| `handler` | `UnsafeBatchHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
|
|
1663
|
+
| `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
|
|
1664
|
+
| `options.batchSize` | `number` | - |
|
|
1665
|
+
| `options.batchTimeout?` | `number` | - |
|
|
1666
|
+
| `options.prefetch?` | `number` | - |
|
|
1667
|
+
|
|
1668
|
+
##### Returns
|
|
1669
|
+
|
|
1670
|
+
[`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
|
|
1671
|
+
|
|
1672
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
1673
|
+
|
|
1674
|
+
##### Deprecated
|
|
1675
|
+
|
|
1676
|
+
Use `defineHandler` instead for explicit error handling with `Future<Result>`.
|
|
1677
|
+
|
|
1678
|
+
**Warning:** Unsafe handlers use exception-based error handling:
|
|
1679
|
+
- All thrown errors are treated as retryable by default
|
|
1680
|
+
- Harder to reason about which errors should be retried
|
|
1681
|
+
- May lead to unexpected retry behavior
|
|
1682
|
+
|
|
1683
|
+
**Note:** Internally, this function wraps the Promise-based handler into a Future-based
|
|
1684
|
+
safe handler for consistent processing in the worker.
|
|
885
1685
|
|
|
886
|
-
|
|
887
|
-
|
|
1686
|
+
##### Example
|
|
1687
|
+
|
|
1688
|
+
```typescript
|
|
1689
|
+
import { defineUnsafeHandler } from '@amqp-contract/worker';
|
|
1690
|
+
|
|
1691
|
+
// ⚠️ Consider using defineHandler for better error handling
|
|
1692
|
+
const processOrderHandler = defineUnsafeHandler(
|
|
888
1693
|
orderContract,
|
|
889
|
-
'
|
|
890
|
-
async (
|
|
891
|
-
//
|
|
892
|
-
await
|
|
893
|
-
}
|
|
894
|
-
{ batchSize: 5, batchTimeout: 1000 }
|
|
1694
|
+
'processOrder',
|
|
1695
|
+
async (message) => {
|
|
1696
|
+
// Throws on error - will be retried
|
|
1697
|
+
await processPayment(message);
|
|
1698
|
+
}
|
|
895
1699
|
);
|
|
896
1700
|
```
|
|
897
1701
|
|
|
898
1702
|
***
|
|
899
1703
|
|
|
900
|
-
###
|
|
1704
|
+
### ~~defineUnsafeHandlers()~~
|
|
901
1705
|
|
|
902
1706
|
```ts
|
|
903
|
-
function
|
|
1707
|
+
function defineUnsafeHandlers<TContract>(contract, handlers): WorkerInferSafeConsumerHandlers<TContract>;
|
|
904
1708
|
```
|
|
905
1709
|
|
|
906
|
-
Defined in: [packages/worker/src/handlers.ts:
|
|
907
|
-
|
|
908
|
-
Define multiple type-safe handlers for consumers in a contract.
|
|
1710
|
+
Defined in: [packages/worker/src/handlers.ts:373](https://github.com/btravers/amqp-contract/blob/f058a24938d9644a82812e57d7995cb683cfd6b5/packages/worker/src/handlers.ts#L373)
|
|
909
1711
|
|
|
910
|
-
|
|
911
|
-
ensuring type safety and providing better code organization.
|
|
1712
|
+
Define multiple unsafe handlers for consumers in a contract.
|
|
912
1713
|
|
|
913
1714
|
#### Type Parameters
|
|
914
1715
|
|
|
@@ -921,55 +1722,36 @@ ensuring type safety and providing better code organization.
|
|
|
921
1722
|
| Parameter | Type | Description |
|
|
922
1723
|
| ------ | ------ | ------ |
|
|
923
1724
|
| `contract` | `TContract` | The contract definition containing the consumers |
|
|
924
|
-
| `handlers` |
|
|
1725
|
+
| `handlers` | `UnsafeHandlers`\<`TContract`\> | An object with async handler functions for each consumer |
|
|
925
1726
|
|
|
926
1727
|
#### Returns
|
|
927
1728
|
|
|
928
|
-
[`
|
|
1729
|
+
[`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\>
|
|
929
1730
|
|
|
930
1731
|
A type-safe handlers object that can be used with TypedAmqpWorker
|
|
931
1732
|
|
|
932
|
-
####
|
|
1733
|
+
#### Deprecated
|
|
1734
|
+
|
|
1735
|
+
Use `defineHandlers` instead for explicit error handling with `Future<Result>`.
|
|
1736
|
+
|
|
1737
|
+
**Warning:** Unsafe handlers use exception-based error handling.
|
|
1738
|
+
Consider migrating to safe handlers for better error control.
|
|
1739
|
+
|
|
1740
|
+
**Note:** Internally, this function wraps all Promise-based handlers into Future-based
|
|
1741
|
+
safe handlers for consistent processing in the worker.
|
|
1742
|
+
|
|
1743
|
+
#### Example
|
|
933
1744
|
|
|
934
1745
|
```typescript
|
|
935
|
-
import {
|
|
936
|
-
import { orderContract } from './contract';
|
|
1746
|
+
import { defineUnsafeHandlers } from '@amqp-contract/worker';
|
|
937
1747
|
|
|
938
|
-
//
|
|
939
|
-
const handlers =
|
|
1748
|
+
// ⚠️ Consider using defineHandlers for better error handling
|
|
1749
|
+
const handlers = defineUnsafeHandlers(orderContract, {
|
|
940
1750
|
processOrder: async (message) => {
|
|
941
|
-
// message is fully typed based on the contract
|
|
942
|
-
console.log('Processing order:', message.orderId);
|
|
943
1751
|
await processPayment(message);
|
|
944
1752
|
},
|
|
945
1753
|
notifyOrder: async (message) => {
|
|
946
1754
|
await sendNotification(message);
|
|
947
1755
|
},
|
|
948
|
-
shipOrder: async (message) => {
|
|
949
|
-
await prepareShipment(message);
|
|
950
|
-
},
|
|
951
|
-
});
|
|
952
|
-
|
|
953
|
-
// Use the handlers in worker
|
|
954
|
-
const worker = await TypedAmqpWorker.create({
|
|
955
|
-
contract: orderContract,
|
|
956
|
-
handlers,
|
|
957
|
-
connection: 'amqp://localhost',
|
|
958
|
-
});
|
|
959
|
-
```
|
|
960
|
-
|
|
961
|
-
```typescript
|
|
962
|
-
// Separate handler definitions for better organization
|
|
963
|
-
async function handleProcessOrder(message: WorkerInferConsumerInput<typeof orderContract, 'processOrder'>) {
|
|
964
|
-
await processOrder(message);
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
async function handleNotifyOrder(message: WorkerInferConsumerInput<typeof orderContract, 'notifyOrder'>) {
|
|
968
|
-
await sendNotification(message);
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
const handlers = defineHandlers(orderContract, {
|
|
972
|
-
processOrder: handleProcessOrder,
|
|
973
|
-
notifyOrder: handleNotifyOrder,
|
|
974
1756
|
});
|
|
975
1757
|
```
|