@amqp-contract/worker 0.7.0 → 0.8.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/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L35)
11
+ Defined in: [packages/worker/src/errors.ts:35](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L36)
27
+ Defined in: [packages/worker/src/errors.ts:36](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L38) |
51
+ | <a id="consumername"></a> `consumerName` | `readonly` | `string` | - | - | [packages/worker/src/errors.ts:37](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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.3/node\_modules/@types/node/globals.d.ts:67 |
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.3/node\_modules/@types/node/globals.d.ts:51
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.3/node\_modules/@types/node/globals.d.ts:55
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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L22)
468
+ Defined in: [packages/worker/src/errors.ts:22](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L23)
485
+ Defined in: [packages/worker/src/errors.ts:23](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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-1"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:25](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/errors.ts#L25) |
203
- | <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 |
204
- | <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 |
205
- | <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 |
206
- | <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.3/node\_modules/@types/node/globals.d.ts:67 |
508
+ | <a id="cause-3"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:25](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/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.3/node\_modules/@types/node/globals.d.ts:51
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.3/node\_modules/@types/node/globals.d.ts:55
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:130](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L130)
618
+ Defined in: [packages/worker/src/worker.ts:175](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L175)
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:244](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L244)
672
+ Defined in: [packages/worker/src/worker.ts:306](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L306)
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:205](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L205)
700
+ Defined in: [packages/worker/src/worker.ts:264](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L264)
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 workerResult = await TypedAmqpWorker.create({
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:77](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L77)
750
+ Defined in: [packages/worker/src/worker.ts:115](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L115)
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,301 @@ 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:85](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L85) |
496
- | <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:79](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L79) |
497
- | <a id="handlers"></a> `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | Handlers for each consumer defined in the contract. Can be a function or a tuple of [handler, options] | [packages/worker/src/worker.ts:81](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L81) |
498
- | <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:87](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L87) |
499
- | <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:83](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/worker.ts#L83) |
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:128](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L128) |
805
+ | <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:117](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L117) |
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:124](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L124) |
807
+ | <a id="logger"></a> `logger?` | `Logger` | Optional logger for logging message consumption and errors | [packages/worker/src/worker.ts:130](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L130) |
808
+ | <a id="retry"></a> `retry?` | [`RetryOptions`](#retryoptions) | Retry configuration - when undefined, uses legacy behavior (immediate requeue) | [packages/worker/src/worker.ts:132](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L132) |
809
+ | <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:126](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L126) |
810
+
811
+ ***
812
+
813
+ ### HandlerError
814
+
815
+ ```ts
816
+ type HandlerError =
817
+ | RetryableError
818
+ | NonRetryableError;
819
+ ```
820
+
821
+ Defined in: [packages/worker/src/errors.ts:83](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/errors.ts#L83)
822
+
823
+ Union type representing all handler errors.
824
+ Use this type when defining handlers that explicitly signal error outcomes.
825
+
826
+ ***
827
+
828
+ ### RetryOptions
829
+
830
+ ```ts
831
+ type RetryOptions = object;
832
+ ```
833
+
834
+ Defined in: [packages/worker/src/worker.ts:47](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L47)
835
+
836
+ Retry configuration options for handling failed message processing.
837
+
838
+ When enabled, the worker will automatically retry failed messages using
839
+ RabbitMQ's native TTL + Dead Letter Exchange (DLX) pattern.
840
+
841
+ #### Properties
842
+
843
+ | Property | Type | Description | Defined in |
844
+ | ------ | ------ | ------ | ------ |
845
+ | <a id="backoffmultiplier"></a> `backoffMultiplier?` | `number` | Exponential backoff multiplier (default: 2) | [packages/worker/src/worker.ts:55](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L55) |
846
+ | <a id="initialdelayms"></a> `initialDelayMs?` | `number` | Initial delay in ms before first retry (default: 1000) | [packages/worker/src/worker.ts:51](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L51) |
847
+ | <a id="jitter"></a> `jitter?` | `boolean` | Add jitter to prevent thundering herd (default: true) | [packages/worker/src/worker.ts:57](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L57) |
848
+ | <a id="maxdelayms"></a> `maxDelayMs?` | `number` | Maximum delay in ms between retries (default: 30000) | [packages/worker/src/worker.ts:53](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L53) |
849
+ | <a id="maxretries"></a> `maxRetries?` | `number` | Maximum retry attempts before sending to DLQ (default: 3) | [packages/worker/src/worker.ts:49](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/worker.ts#L49) |
850
+
851
+ ***
852
+
853
+ ### ~~WorkerInferConsumerBatchHandler~~
854
+
855
+ ```ts
856
+ type WorkerInferConsumerBatchHandler<TContract, TName> = WorkerInferUnsafeConsumerBatchHandler<TContract, TName>;
857
+ ```
858
+
859
+ Defined in: [packages/worker/src/types.ts:195](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L195)
860
+
861
+ #### Type Parameters
862
+
863
+ | Type Parameter |
864
+ | ------ |
865
+ | `TContract` *extends* `ContractDefinition` |
866
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
867
+
868
+ #### Deprecated
869
+
870
+ Use WorkerInferUnsafeConsumerBatchHandler instead
871
+
872
+ ***
873
+
874
+ ### ~~WorkerInferConsumerHandler~~
875
+
876
+ ```ts
877
+ type WorkerInferConsumerHandler<TContract, TName> = WorkerInferUnsafeConsumerHandler<TContract, TName>;
878
+ ```
879
+
880
+ Defined in: [packages/worker/src/types.ts:187](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L187)
881
+
882
+ #### Type Parameters
883
+
884
+ | Type Parameter |
885
+ | ------ |
886
+ | `TContract` *extends* `ContractDefinition` |
887
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
888
+
889
+ #### Deprecated
890
+
891
+ Use WorkerInferUnsafeConsumerHandler instead
892
+
893
+ ***
894
+
895
+ ### ~~WorkerInferConsumerHandlerEntry~~
896
+
897
+ ```ts
898
+ type WorkerInferConsumerHandlerEntry<TContract, TName> = WorkerInferUnsafeConsumerHandlerEntry<TContract, TName>;
899
+ ```
900
+
901
+ Defined in: [packages/worker/src/types.ts:203](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L203)
902
+
903
+ #### Type Parameters
904
+
905
+ | Type Parameter |
906
+ | ------ |
907
+ | `TContract` *extends* `ContractDefinition` |
908
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
909
+
910
+ #### Deprecated
911
+
912
+ Use WorkerInferUnsafeConsumerHandlerEntry instead
913
+
914
+ ***
915
+
916
+ ### ~~WorkerInferConsumerHandlers~~
917
+
918
+ ```ts
919
+ type WorkerInferConsumerHandlers<TContract> = WorkerInferUnsafeConsumerHandlers<TContract>;
920
+ ```
921
+
922
+ Defined in: [packages/worker/src/types.ts:211](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L211)
923
+
924
+ #### Type Parameters
925
+
926
+ | Type Parameter |
927
+ | ------ |
928
+ | `TContract` *extends* `ContractDefinition` |
929
+
930
+ #### Deprecated
931
+
932
+ Use WorkerInferUnsafeConsumerHandlers instead
933
+
934
+ ***
935
+
936
+ ### WorkerInferConsumerInput
937
+
938
+ ```ts
939
+ type WorkerInferConsumerInput<TContract, TName> = ConsumerInferInput<InferConsumer<TContract, TName>>;
940
+ ```
941
+
942
+ Defined in: [packages/worker/src/types.ts:39](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L39)
943
+
944
+ Worker perspective types - for consuming messages
945
+
946
+ #### Type Parameters
947
+
948
+ | Type Parameter |
949
+ | ------ |
950
+ | `TContract` *extends* `ContractDefinition` |
951
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
952
+
953
+ ***
954
+
955
+ ### WorkerInferSafeConsumerBatchHandler()
956
+
957
+ ```ts
958
+ type WorkerInferSafeConsumerBatchHandler<TContract, TName> = (messages) => Future<Result<void, HandlerError>>;
959
+ ```
960
+
961
+ Defined in: [packages/worker/src/types.ts:80](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L80)
962
+
963
+ Safe consumer handler type for batch processing.
964
+ Returns a `Future<Result<void, HandlerError>>` for explicit error handling.
965
+
966
+ #### Type Parameters
967
+
968
+ | Type Parameter |
969
+ | ------ |
970
+ | `TContract` *extends* `ContractDefinition` |
971
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
972
+
973
+ #### Parameters
974
+
975
+ | Parameter | Type |
976
+ | ------ | ------ |
977
+ | `messages` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\>[] |
978
+
979
+ #### Returns
980
+
981
+ `Future`\<`Result`\<`void`, [`HandlerError`](#handlererror)\>\>
982
+
983
+ #### Example
984
+
985
+ ```typescript
986
+ const handler: WorkerInferSafeConsumerBatchHandler<typeof contract, 'processOrders'> =
987
+ (messages) => Future.value(Result.Ok(undefined));
988
+ ```
989
+
990
+ ***
991
+
992
+ ### WorkerInferSafeConsumerHandler()
993
+
994
+ ```ts
995
+ type WorkerInferSafeConsumerHandler<TContract, TName> = (message) => Future<Result<void, HandlerError>>;
996
+ ```
997
+
998
+ Defined in: [packages/worker/src/types.ts:65](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L65)
999
+
1000
+ Safe consumer handler type for a specific consumer.
1001
+ Returns a `Future<Result<void, HandlerError>>` for explicit error handling.
1002
+
1003
+ **Recommended over unsafe handlers** for better error control:
1004
+ - RetryableError: Message will be retried with exponential backoff
1005
+ - NonRetryableError: Message will be immediately sent to DLQ
1006
+
1007
+ #### Type Parameters
1008
+
1009
+ | Type Parameter |
1010
+ | ------ |
1011
+ | `TContract` *extends* `ContractDefinition` |
1012
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
1013
+
1014
+ #### Parameters
1015
+
1016
+ | Parameter | Type |
1017
+ | ------ | ------ |
1018
+ | `message` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\> |
1019
+
1020
+ #### Returns
1021
+
1022
+ `Future`\<`Result`\<`void`, [`HandlerError`](#handlererror)\>\>
1023
+
1024
+ #### Example
1025
+
1026
+ ```typescript
1027
+ const handler: WorkerInferSafeConsumerHandler<typeof contract, 'processOrder'> =
1028
+ (message) => Future.value(Result.Ok(undefined));
1029
+ ```
1030
+
1031
+ ***
1032
+
1033
+ ### WorkerInferSafeConsumerHandlerEntry
1034
+
1035
+ ```ts
1036
+ type WorkerInferSafeConsumerHandlerEntry<TContract, TName> =
1037
+ | WorkerInferSafeConsumerHandler<TContract, TName>
1038
+ | readonly [WorkerInferSafeConsumerHandler<TContract, TName>, {
1039
+ batchSize?: never;
1040
+ batchTimeout?: never;
1041
+ prefetch?: number;
1042
+ }]
1043
+ | readonly [WorkerInferSafeConsumerBatchHandler<TContract, TName>, {
1044
+ batchSize: number;
1045
+ batchTimeout?: number;
1046
+ prefetch?: number;
1047
+ }];
1048
+ ```
1049
+
1050
+ Defined in: [packages/worker/src/types.ts:95](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L95)
1051
+
1052
+ Safe handler entry for a consumer - either a function or a tuple of [handler, options].
1053
+
1054
+ Three patterns are supported:
1055
+ 1. Simple handler: `(message) => Future.value(Result.Ok(undefined))`
1056
+ 2. Handler with prefetch: `[(message) => ..., { prefetch: 10 }]`
1057
+ 3. Batch handler: `[(messages) => ..., { batchSize: 5, batchTimeout: 1000 }]`
1058
+
1059
+ #### Type Parameters
1060
+
1061
+ | Type Parameter |
1062
+ | ------ |
1063
+ | `TContract` *extends* `ContractDefinition` |
1064
+ | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
1065
+
1066
+ ***
1067
+
1068
+ ### WorkerInferSafeConsumerHandlers
1069
+
1070
+ ```ts
1071
+ type WorkerInferSafeConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferSafeConsumerHandlerEntry<TContract, K> };
1072
+ ```
1073
+
1074
+ Defined in: [packages/worker/src/types.ts:113](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L113)
1075
+
1076
+ Safe consumer handlers for a contract.
1077
+ All handlers return `Future<Result<void, HandlerError>>` for explicit error control.
1078
+
1079
+ #### Type Parameters
1080
+
1081
+ | Type Parameter |
1082
+ | ------ |
1083
+ | `TContract` *extends* `ContractDefinition` |
500
1084
 
501
1085
  ***
502
1086
 
503
- ### WorkerInferConsumerBatchHandler()
1087
+ ### ~~WorkerInferUnsafeConsumerBatchHandler()~~
504
1088
 
505
1089
  ```ts
506
- type WorkerInferConsumerBatchHandler<TContract, TName> = (messages) => Promise<void>;
1090
+ type WorkerInferUnsafeConsumerBatchHandler<TContract, TName> = (messages) => Promise<void>;
507
1091
  ```
508
1092
 
509
- Defined in: [packages/worker/src/types.ts:56](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/types.ts#L56)
1093
+ Defined in: [packages/worker/src/types.ts:147](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L147)
510
1094
 
511
- Infer consumer handler type for batch processing.
512
- Batch handlers receive an array of messages.
1095
+ Unsafe consumer handler type for batch processing.
1096
+ Returns a `Promise<void>` - throws exceptions on error.
513
1097
 
514
1098
  #### Type Parameters
515
1099
 
@@ -528,19 +1112,23 @@ Batch handlers receive an array of messages.
528
1112
 
529
1113
  `Promise`\<`void`\>
530
1114
 
1115
+ #### Deprecated
1116
+
1117
+ Prefer using safe handlers (WorkerInferSafeConsumerBatchHandler) that return
1118
+ `Future<Result<void, HandlerError>>` for better error handling.
1119
+
531
1120
  ***
532
1121
 
533
- ### WorkerInferConsumerHandler()
1122
+ ### ~~WorkerInferUnsafeConsumerHandler()~~
534
1123
 
535
1124
  ```ts
536
- type WorkerInferConsumerHandler<TContract, TName> = (message) => Promise<void>;
1125
+ type WorkerInferUnsafeConsumerHandler<TContract, TName> = (message) => Promise<void>;
537
1126
  ```
538
1127
 
539
- Defined in: [packages/worker/src/types.ts:47](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/types.ts#L47)
1128
+ Defined in: [packages/worker/src/types.ts:135](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L135)
540
1129
 
541
- Infer consumer handler type for a specific consumer.
542
- Handlers always receive a single message by default.
543
- For batch processing, use consumerOptions to configure batch behavior.
1130
+ Unsafe consumer handler type for a specific consumer.
1131
+ Returns a `Promise<void>` - throws exceptions on error.
544
1132
 
545
1133
  #### Type Parameters
546
1134
 
@@ -559,33 +1147,37 @@ For batch processing, use consumerOptions to configure batch behavior.
559
1147
 
560
1148
  `Promise`\<`void`\>
561
1149
 
1150
+ #### Deprecated
1151
+
1152
+ Prefer using safe handlers (WorkerInferSafeConsumerHandler) that return
1153
+ `Future<Result<void, HandlerError>>` for better error handling.
1154
+
1155
+ **Note:** When using unsafe handlers:
1156
+ - All thrown errors are treated as retryable by default (when retry is configured)
1157
+ - Use RetryableError or NonRetryableError to control retry behavior explicitly
1158
+
562
1159
  ***
563
1160
 
564
- ### WorkerInferConsumerHandlerEntry
1161
+ ### ~~WorkerInferUnsafeConsumerHandlerEntry~~
565
1162
 
566
1163
  ```ts
567
- type WorkerInferConsumerHandlerEntry<TContract, TName> =
568
- | WorkerInferConsumerHandler<TContract, TName>
569
- | readonly [WorkerInferConsumerHandler<TContract, TName>, {
1164
+ type WorkerInferUnsafeConsumerHandlerEntry<TContract, TName> =
1165
+ | WorkerInferUnsafeConsumerHandler<TContract, TName>
1166
+ | readonly [WorkerInferUnsafeConsumerHandler<TContract, TName>, {
570
1167
  batchSize?: never;
571
1168
  batchTimeout?: never;
572
1169
  prefetch?: number;
573
1170
  }]
574
- | readonly [WorkerInferConsumerBatchHandler<TContract, TName>, {
1171
+ | readonly [WorkerInferUnsafeConsumerBatchHandler<TContract, TName>, {
575
1172
  batchSize: number;
576
1173
  batchTimeout?: number;
577
1174
  prefetch?: number;
578
1175
  }];
579
1176
  ```
580
1177
 
581
- Defined in: [packages/worker/src/types.ts:69](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/types.ts#L69)
1178
+ Defined in: [packages/worker/src/types.ts:157](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L157)
582
1179
 
583
- Infer handler entry for a consumer - either a function or a tuple of [handler, options].
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 }]`
1180
+ Unsafe handler entry for a consumer - either a function or a tuple of [handler, options].
589
1181
 
590
1182
  #### Type Parameters
591
1183
 
@@ -594,43 +1186,31 @@ Three patterns are supported:
594
1186
  | `TContract` *extends* `ContractDefinition` |
595
1187
  | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
596
1188
 
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
1189
+ #### Deprecated
611
1190
 
612
- | Type Parameter |
613
- | ------ |
614
- | `TContract` *extends* `ContractDefinition` |
1191
+ Prefer using safe handler entries (WorkerInferSafeConsumerHandlerEntry).
615
1192
 
616
1193
  ***
617
1194
 
618
- ### WorkerInferConsumerInput
1195
+ ### ~~WorkerInferUnsafeConsumerHandlers~~
619
1196
 
620
1197
  ```ts
621
- type WorkerInferConsumerInput<TContract, TName> = ConsumerInferInput<InferConsumer<TContract, TName>>;
1198
+ type WorkerInferUnsafeConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferUnsafeConsumerHandlerEntry<TContract, K> };
622
1199
  ```
623
1200
 
624
- Defined in: [packages/worker/src/types.ts:37](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/types.ts#L37)
1201
+ Defined in: [packages/worker/src/types.ts:176](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/types.ts#L176)
625
1202
 
626
- Worker perspective types - for consuming messages
1203
+ Unsafe consumer handlers for a contract.
627
1204
 
628
1205
  #### Type Parameters
629
1206
 
630
1207
  | Type Parameter |
631
1208
  | ------ |
632
1209
  | `TContract` *extends* `ContractDefinition` |
633
- | `TName` *extends* `InferConsumerNames`\<`TContract`\> |
1210
+
1211
+ #### Deprecated
1212
+
1213
+ Prefer using safe handlers (WorkerInferSafeConsumerHandlers).
634
1214
 
635
1215
  ## Functions
636
1216
 
@@ -642,24 +1222,21 @@ Worker perspective types - for consuming messages
642
1222
  function defineHandler<TContract, TName>(
643
1223
  contract,
644
1224
  consumerName,
645
- handler): WorkerInferConsumerHandlerEntry<TContract, TName>;
1225
+ handler): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
646
1226
  ```
647
1227
 
648
- Defined in: [packages/worker/src/handlers.ts:70](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/handlers.ts#L70)
1228
+ Defined in: [packages/worker/src/handlers.ts:134](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L134)
649
1229
 
650
1230
  Define a type-safe handler for a specific consumer in a contract.
651
1231
 
652
- This utility allows you to define handlers outside of the worker creation,
653
- providing better code organization and reusability.
1232
+ **Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
1233
+ providing explicit error handling and better control over retry behavior.
654
1234
 
655
1235
  Supports three patterns:
656
1236
  1. Simple handler: just the function (single message handler)
657
1237
  2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
658
1238
  3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
659
1239
 
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
1240
  ##### Type Parameters
664
1241
 
665
1242
  | Type Parameter | Description |
@@ -673,49 +1250,42 @@ batchSize configuration. You cannot create a batch handler without specifying ba
673
1250
  | ------ | ------ | ------ |
674
1251
  | `contract` | `TContract` | The contract definition containing the consumer |
675
1252
  | `consumerName` | `TName` | The name of the consumer from the contract |
676
- | `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
1253
+ | `handler` | [`WorkerInferSafeConsumerHandler`](#workerinfersafeconsumerhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
677
1254
 
678
1255
  ##### Returns
679
1256
 
680
- [`WorkerInferConsumerHandlerEntry`](#workerinferconsumerhandlerentry)\<`TContract`, `TName`\>
1257
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
681
1258
 
682
1259
  A type-safe handler that can be used with TypedAmqpWorker
683
1260
 
684
1261
  ##### Example
685
1262
 
686
1263
  ```typescript
687
- import { defineHandler } from '@amqp-contract/worker';
1264
+ import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
1265
+ import { Future, Result } from '@swan-io/boxed';
688
1266
  import { orderContract } from './contract';
689
1267
 
690
- // Simple single-message handler without options
1268
+ // Simple handler with explicit error handling using mapError
691
1269
  const processOrderHandler = defineHandler(
692
1270
  orderContract,
693
1271
  'processOrder',
694
- async (message) => {
695
- console.log('Processing order:', message.orderId);
696
- await processPayment(message);
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 }
1272
+ (message) =>
1273
+ Future.fromPromise(processPayment(message))
1274
+ .mapOk(() => undefined)
1275
+ .mapError((error) => new RetryableError('Payment failed', error))
708
1276
  );
709
1277
 
710
- // Batch handler - MUST include batchSize
711
- const processBatchOrders = defineHandler(
1278
+ // Handler with validation (non-retryable error)
1279
+ const validateOrderHandler = defineHandler(
712
1280
  orderContract,
713
- 'processOrders',
714
- async (messages) => {
715
- // messages is an array - batchSize configuration is REQUIRED
716
- await db.insertMany(messages);
717
- },
718
- { batchSize: 5, batchTimeout: 1000 }
1281
+ 'validateOrder',
1282
+ (message) => {
1283
+ if (message.amount < 1) {
1284
+ // Won't be retried - goes directly to DLQ
1285
+ return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
1286
+ }
1287
+ return Future.value(Result.Ok(undefined));
1288
+ }
719
1289
  );
720
1290
  ```
721
1291
 
@@ -726,24 +1296,21 @@ function defineHandler<TContract, TName>(
726
1296
  contract,
727
1297
  consumerName,
728
1298
  handler,
729
- options): WorkerInferConsumerHandlerEntry<TContract, TName>;
1299
+ options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
730
1300
  ```
731
1301
 
732
- Defined in: [packages/worker/src/handlers.ts:78](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/handlers.ts#L78)
1302
+ Defined in: [packages/worker/src/handlers.ts:142](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L142)
733
1303
 
734
1304
  Define a type-safe handler for a specific consumer in a contract.
735
1305
 
736
- This utility allows you to define handlers outside of the worker creation,
737
- providing better code organization and reusability.
1306
+ **Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
1307
+ providing explicit error handling and better control over retry behavior.
738
1308
 
739
1309
  Supports three patterns:
740
1310
  1. Simple handler: just the function (single message handler)
741
1311
  2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
742
1312
  3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
743
1313
 
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
1314
  ##### Type Parameters
748
1315
 
749
1316
  | Type Parameter | Description |
@@ -757,53 +1324,46 @@ batchSize configuration. You cannot create a batch handler without specifying ba
757
1324
  | ------ | ------ | ------ |
758
1325
  | `contract` | `TContract` | The contract definition containing the consumer |
759
1326
  | `consumerName` | `TName` | The name of the consumer from the contract |
760
- | `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
761
- | `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) - For single-message handlers: { prefetch?: number } is optional - For batch handlers: { batchSize: number, batchTimeout?: number } is REQUIRED |
1327
+ | `handler` | [`WorkerInferSafeConsumerHandler`](#workerinfersafeconsumerhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
1328
+ | `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
762
1329
  | `options.batchSize?` | `undefined` | - |
763
1330
  | `options.batchTimeout?` | `undefined` | - |
764
1331
  | `options.prefetch?` | `number` | - |
765
1332
 
766
1333
  ##### Returns
767
1334
 
768
- [`WorkerInferConsumerHandlerEntry`](#workerinferconsumerhandlerentry)\<`TContract`, `TName`\>
1335
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
769
1336
 
770
1337
  A type-safe handler that can be used with TypedAmqpWorker
771
1338
 
772
1339
  ##### Example
773
1340
 
774
1341
  ```typescript
775
- import { defineHandler } from '@amqp-contract/worker';
1342
+ import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
1343
+ import { Future, Result } from '@swan-io/boxed';
776
1344
  import { orderContract } from './contract';
777
1345
 
778
- // Simple single-message handler without options
1346
+ // Simple handler with explicit error handling using mapError
779
1347
  const processOrderHandler = defineHandler(
780
1348
  orderContract,
781
1349
  'processOrder',
782
- async (message) => {
783
- console.log('Processing order:', message.orderId);
784
- await processPayment(message);
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 }
1350
+ (message) =>
1351
+ Future.fromPromise(processPayment(message))
1352
+ .mapOk(() => undefined)
1353
+ .mapError((error) => new RetryableError('Payment failed', error))
796
1354
  );
797
1355
 
798
- // Batch handler - MUST include batchSize
799
- const processBatchOrders = defineHandler(
1356
+ // Handler with validation (non-retryable error)
1357
+ const validateOrderHandler = defineHandler(
800
1358
  orderContract,
801
- 'processOrders',
802
- async (messages) => {
803
- // messages is an array - batchSize configuration is REQUIRED
804
- await db.insertMany(messages);
805
- },
806
- { batchSize: 5, batchTimeout: 1000 }
1359
+ 'validateOrder',
1360
+ (message) => {
1361
+ if (message.amount < 1) {
1362
+ // Won't be retried - goes directly to DLQ
1363
+ return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
1364
+ }
1365
+ return Future.value(Result.Ok(undefined));
1366
+ }
807
1367
  );
808
1368
  ```
809
1369
 
@@ -814,24 +1374,21 @@ function defineHandler<TContract, TName>(
814
1374
  contract,
815
1375
  consumerName,
816
1376
  handler,
817
- options): WorkerInferConsumerHandlerEntry<TContract, TName>;
1377
+ options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
818
1378
  ```
819
1379
 
820
- Defined in: [packages/worker/src/handlers.ts:87](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/handlers.ts#L87)
1380
+ Defined in: [packages/worker/src/handlers.ts:151](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L151)
821
1381
 
822
1382
  Define a type-safe handler for a specific consumer in a contract.
823
1383
 
824
- This utility allows you to define handlers outside of the worker creation,
825
- providing better code organization and reusability.
1384
+ **Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
1385
+ providing explicit error handling and better control over retry behavior.
826
1386
 
827
1387
  Supports three patterns:
828
1388
  1. Simple handler: just the function (single message handler)
829
1389
  2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
830
1390
  3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
831
1391
 
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
1392
  ##### Type Parameters
836
1393
 
837
1394
  | Type Parameter | Description |
@@ -845,70 +1402,313 @@ batchSize configuration. You cannot create a batch handler without specifying ba
845
1402
  | ------ | ------ | ------ |
846
1403
  | `contract` | `TContract` | The contract definition containing the consumer |
847
1404
  | `consumerName` | `TName` | The name of the consumer from the contract |
848
- | `handler` | [`WorkerInferConsumerBatchHandler`](#workerinferconsumerbatchhandler)\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
849
- | `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) - For single-message handlers: { prefetch?: number } is optional - For batch handlers: { batchSize: number, batchTimeout?: number } is REQUIRED |
1405
+ | `handler` | [`WorkerInferSafeConsumerBatchHandler`](#workerinfersafeconsumerbatchhandler)\<`TContract`, `TName`\> | The handler function that returns `Future<Result<void, HandlerError>>` |
1406
+ | `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
850
1407
  | `options.batchSize` | `number` | - |
851
1408
  | `options.batchTimeout?` | `number` | - |
852
1409
  | `options.prefetch?` | `number` | - |
853
1410
 
854
1411
  ##### Returns
855
1412
 
856
- [`WorkerInferConsumerHandlerEntry`](#workerinferconsumerhandlerentry)\<`TContract`, `TName`\>
1413
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
857
1414
 
858
1415
  A type-safe handler that can be used with TypedAmqpWorker
859
1416
 
860
1417
  ##### Example
861
1418
 
862
1419
  ```typescript
863
- import { defineHandler } from '@amqp-contract/worker';
1420
+ import { defineHandler, RetryableError, NonRetryableError } from '@amqp-contract/worker';
1421
+ import { Future, Result } from '@swan-io/boxed';
864
1422
  import { orderContract } from './contract';
865
1423
 
866
- // Simple single-message handler without options
1424
+ // Simple handler with explicit error handling using mapError
867
1425
  const processOrderHandler = defineHandler(
1426
+ orderContract,
1427
+ 'processOrder',
1428
+ (message) =>
1429
+ Future.fromPromise(processPayment(message))
1430
+ .mapOk(() => undefined)
1431
+ .mapError((error) => new RetryableError('Payment failed', error))
1432
+ );
1433
+
1434
+ // Handler with validation (non-retryable error)
1435
+ const validateOrderHandler = defineHandler(
1436
+ orderContract,
1437
+ 'validateOrder',
1438
+ (message) => {
1439
+ if (message.amount < 1) {
1440
+ // Won't be retried - goes directly to DLQ
1441
+ return Future.value(Result.Error(new NonRetryableError('Invalid order amount')));
1442
+ }
1443
+ return Future.value(Result.Ok(undefined));
1444
+ }
1445
+ );
1446
+ ```
1447
+
1448
+ ***
1449
+
1450
+ ### defineHandlers()
1451
+
1452
+ ```ts
1453
+ function defineHandlers<TContract>(contract, handlers): WorkerInferSafeConsumerHandlers<TContract>;
1454
+ ```
1455
+
1456
+ Defined in: [packages/worker/src/handlers.ts:208](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L208)
1457
+
1458
+ Define multiple type-safe handlers for consumers in a contract.
1459
+
1460
+ **Recommended:** This function creates handlers that return `Future<Result<void, HandlerError>>`,
1461
+ providing explicit error handling and better control over retry behavior.
1462
+
1463
+ #### Type Parameters
1464
+
1465
+ | Type Parameter | Description |
1466
+ | ------ | ------ |
1467
+ | `TContract` *extends* `ContractDefinition` | The contract definition type |
1468
+
1469
+ #### Parameters
1470
+
1471
+ | Parameter | Type | Description |
1472
+ | ------ | ------ | ------ |
1473
+ | `contract` | `TContract` | The contract definition containing the consumers |
1474
+ | `handlers` | [`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\> | An object with handler functions for each consumer |
1475
+
1476
+ #### Returns
1477
+
1478
+ [`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\>
1479
+
1480
+ A type-safe handlers object that can be used with TypedAmqpWorker
1481
+
1482
+ #### Example
1483
+
1484
+ ```typescript
1485
+ import { defineHandlers, RetryableError } from '@amqp-contract/worker';
1486
+ import { Future } from '@swan-io/boxed';
1487
+ import { orderContract } from './contract';
1488
+
1489
+ const handlers = defineHandlers(orderContract, {
1490
+ processOrder: (message) =>
1491
+ Future.fromPromise(processPayment(message))
1492
+ .mapOk(() => undefined)
1493
+ .mapError((error) => new RetryableError('Payment failed', error)),
1494
+ notifyOrder: (message) =>
1495
+ Future.fromPromise(sendNotification(message))
1496
+ .mapOk(() => undefined)
1497
+ .mapError((error) => new RetryableError('Notification failed', error)),
1498
+ });
1499
+ ```
1500
+
1501
+ ***
1502
+
1503
+ ### ~~defineUnsafeHandler()~~
1504
+
1505
+ #### Call Signature
1506
+
1507
+ ```ts
1508
+ function defineUnsafeHandler<TContract, TName>(
1509
+ contract,
1510
+ consumerName,
1511
+ handler): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
1512
+ ```
1513
+
1514
+ Defined in: [packages/worker/src/handlers.ts:272](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L272)
1515
+
1516
+ Define an unsafe handler for a specific consumer in a contract.
1517
+
1518
+ ##### Type Parameters
1519
+
1520
+ | Type Parameter | Description |
1521
+ | ------ | ------ |
1522
+ | `TContract` *extends* `ContractDefinition` | The contract definition type |
1523
+ | `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
1524
+
1525
+ ##### Parameters
1526
+
1527
+ | Parameter | Type | Description |
1528
+ | ------ | ------ | ------ |
1529
+ | `contract` | `TContract` | The contract definition containing the consumer |
1530
+ | `consumerName` | `TName` | The name of the consumer from the contract |
1531
+ | `handler` | `UnsafeHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
1532
+
1533
+ ##### Returns
1534
+
1535
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
1536
+
1537
+ A type-safe handler that can be used with TypedAmqpWorker
1538
+
1539
+ ##### Deprecated
1540
+
1541
+ Use `defineHandler` instead for explicit error handling with `Future<Result>`.
1542
+
1543
+ **Warning:** Unsafe handlers use exception-based error handling:
1544
+ - All thrown errors are treated as retryable by default
1545
+ - Harder to reason about which errors should be retried
1546
+ - May lead to unexpected retry behavior
1547
+
1548
+ **Note:** Internally, this function wraps the Promise-based handler into a Future-based
1549
+ safe handler for consistent processing in the worker.
1550
+
1551
+ ##### Example
1552
+
1553
+ ```typescript
1554
+ import { defineUnsafeHandler } from '@amqp-contract/worker';
1555
+
1556
+ // ⚠️ Consider using defineHandler for better error handling
1557
+ const processOrderHandler = defineUnsafeHandler(
868
1558
  orderContract,
869
1559
  'processOrder',
870
1560
  async (message) => {
871
- console.log('Processing order:', message.orderId);
1561
+ // Throws on error - will be retried
872
1562
  await processPayment(message);
873
1563
  }
874
1564
  );
1565
+ ```
1566
+
1567
+ #### Call Signature
1568
+
1569
+ ```ts
1570
+ function defineUnsafeHandler<TContract, TName>(
1571
+ contract,
1572
+ consumerName,
1573
+ handler,
1574
+ options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
1575
+ ```
1576
+
1577
+ Defined in: [packages/worker/src/handlers.ts:280](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L280)
1578
+
1579
+ Define an unsafe handler for a specific consumer in a contract.
1580
+
1581
+ ##### Type Parameters
1582
+
1583
+ | Type Parameter | Description |
1584
+ | ------ | ------ |
1585
+ | `TContract` *extends* `ContractDefinition` | The contract definition type |
1586
+ | `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
1587
+
1588
+ ##### Parameters
1589
+
1590
+ | Parameter | Type | Description |
1591
+ | ------ | ------ | ------ |
1592
+ | `contract` | `TContract` | The contract definition containing the consumer |
1593
+ | `consumerName` | `TName` | The name of the consumer from the contract |
1594
+ | `handler` | `UnsafeHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
1595
+ | `options` | \{ `batchSize?`: `undefined`; `batchTimeout?`: `undefined`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
1596
+ | `options.batchSize?` | `undefined` | - |
1597
+ | `options.batchTimeout?` | `undefined` | - |
1598
+ | `options.prefetch?` | `number` | - |
1599
+
1600
+ ##### Returns
1601
+
1602
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
1603
+
1604
+ A type-safe handler that can be used with TypedAmqpWorker
875
1605
 
876
- // Single-message handler with prefetch
877
- const processOrderWithPrefetch = defineHandler(
1606
+ ##### Deprecated
1607
+
1608
+ Use `defineHandler` instead for explicit error handling with `Future<Result>`.
1609
+
1610
+ **Warning:** Unsafe handlers use exception-based error handling:
1611
+ - All thrown errors are treated as retryable by default
1612
+ - Harder to reason about which errors should be retried
1613
+ - May lead to unexpected retry behavior
1614
+
1615
+ **Note:** Internally, this function wraps the Promise-based handler into a Future-based
1616
+ safe handler for consistent processing in the worker.
1617
+
1618
+ ##### Example
1619
+
1620
+ ```typescript
1621
+ import { defineUnsafeHandler } from '@amqp-contract/worker';
1622
+
1623
+ // ⚠️ Consider using defineHandler for better error handling
1624
+ const processOrderHandler = defineUnsafeHandler(
878
1625
  orderContract,
879
1626
  'processOrder',
880
1627
  async (message) => {
881
- await processOrder(message);
882
- },
883
- { prefetch: 10 }
1628
+ // Throws on error - will be retried
1629
+ await processPayment(message);
1630
+ }
884
1631
  );
1632
+ ```
1633
+
1634
+ #### Call Signature
1635
+
1636
+ ```ts
1637
+ function defineUnsafeHandler<TContract, TName>(
1638
+ contract,
1639
+ consumerName,
1640
+ handler,
1641
+ options): WorkerInferSafeConsumerHandlerEntry<TContract, TName>;
1642
+ ```
1643
+
1644
+ Defined in: [packages/worker/src/handlers.ts:289](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L289)
1645
+
1646
+ Define an unsafe handler for a specific consumer in a contract.
1647
+
1648
+ ##### Type Parameters
1649
+
1650
+ | Type Parameter | Description |
1651
+ | ------ | ------ |
1652
+ | `TContract` *extends* `ContractDefinition` | The contract definition type |
1653
+ | `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
1654
+
1655
+ ##### Parameters
1656
+
1657
+ | Parameter | Type | Description |
1658
+ | ------ | ------ | ------ |
1659
+ | `contract` | `TContract` | The contract definition containing the consumer |
1660
+ | `consumerName` | `TName` | The name of the consumer from the contract |
1661
+ | `handler` | `UnsafeBatchHandler`\<`TContract`, `TName`\> | The async handler function that processes messages |
1662
+ | `options` | \{ `batchSize`: `number`; `batchTimeout?`: `number`; `prefetch?`: `number`; \} | Optional consumer options (prefetch, batchSize, batchTimeout) |
1663
+ | `options.batchSize` | `number` | - |
1664
+ | `options.batchTimeout?` | `number` | - |
1665
+ | `options.prefetch?` | `number` | - |
1666
+
1667
+ ##### Returns
1668
+
1669
+ [`WorkerInferSafeConsumerHandlerEntry`](#workerinfersafeconsumerhandlerentry)\<`TContract`, `TName`\>
1670
+
1671
+ A type-safe handler that can be used with TypedAmqpWorker
1672
+
1673
+ ##### Deprecated
1674
+
1675
+ Use `defineHandler` instead for explicit error handling with `Future<Result>`.
1676
+
1677
+ **Warning:** Unsafe handlers use exception-based error handling:
1678
+ - All thrown errors are treated as retryable by default
1679
+ - Harder to reason about which errors should be retried
1680
+ - May lead to unexpected retry behavior
1681
+
1682
+ **Note:** Internally, this function wraps the Promise-based handler into a Future-based
1683
+ safe handler for consistent processing in the worker.
885
1684
 
886
- // Batch handler - MUST include batchSize
887
- const processBatchOrders = defineHandler(
1685
+ ##### Example
1686
+
1687
+ ```typescript
1688
+ import { defineUnsafeHandler } from '@amqp-contract/worker';
1689
+
1690
+ // ⚠️ Consider using defineHandler for better error handling
1691
+ const processOrderHandler = defineUnsafeHandler(
888
1692
  orderContract,
889
- 'processOrders',
890
- async (messages) => {
891
- // messages is an array - batchSize configuration is REQUIRED
892
- await db.insertMany(messages);
893
- },
894
- { batchSize: 5, batchTimeout: 1000 }
1693
+ 'processOrder',
1694
+ async (message) => {
1695
+ // Throws on error - will be retried
1696
+ await processPayment(message);
1697
+ }
895
1698
  );
896
1699
  ```
897
1700
 
898
1701
  ***
899
1702
 
900
- ### defineHandlers()
1703
+ ### ~~defineUnsafeHandlers()~~
901
1704
 
902
1705
  ```ts
903
- function defineHandlers<TContract>(contract, handlers): WorkerInferConsumerHandlers<TContract>;
1706
+ function defineUnsafeHandlers<TContract>(contract, handlers): WorkerInferSafeConsumerHandlers<TContract>;
904
1707
  ```
905
1708
 
906
- Defined in: [packages/worker/src/handlers.ts:181](https://github.com/btravers/amqp-contract/blob/382c6d2fbfc563c9f6f16cb71cb33fd4a3d41d77/packages/worker/src/handlers.ts#L181)
907
-
908
- Define multiple type-safe handlers for consumers in a contract.
1709
+ Defined in: [packages/worker/src/handlers.ts:373](https://github.com/btravers/amqp-contract/blob/7897a0ef6745684800b11ec9bbc26bfab645f2a7/packages/worker/src/handlers.ts#L373)
909
1710
 
910
- This utility allows you to define all handlers at once outside of the worker creation,
911
- ensuring type safety and providing better code organization.
1711
+ Define multiple unsafe handlers for consumers in a contract.
912
1712
 
913
1713
  #### Type Parameters
914
1714
 
@@ -921,55 +1721,36 @@ ensuring type safety and providing better code organization.
921
1721
  | Parameter | Type | Description |
922
1722
  | ------ | ------ | ------ |
923
1723
  | `contract` | `TContract` | The contract definition containing the consumers |
924
- | `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | An object with async handler functions for each consumer |
1724
+ | `handlers` | `UnsafeHandlers`\<`TContract`\> | An object with async handler functions for each consumer |
925
1725
 
926
1726
  #### Returns
927
1727
 
928
- [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\>
1728
+ [`WorkerInferSafeConsumerHandlers`](#workerinfersafeconsumerhandlers)\<`TContract`\>
929
1729
 
930
1730
  A type-safe handlers object that can be used with TypedAmqpWorker
931
1731
 
932
- #### Examples
1732
+ #### Deprecated
1733
+
1734
+ Use `defineHandlers` instead for explicit error handling with `Future<Result>`.
1735
+
1736
+ **Warning:** Unsafe handlers use exception-based error handling.
1737
+ Consider migrating to safe handlers for better error control.
1738
+
1739
+ **Note:** Internally, this function wraps all Promise-based handlers into Future-based
1740
+ safe handlers for consistent processing in the worker.
1741
+
1742
+ #### Example
933
1743
 
934
1744
  ```typescript
935
- import { defineHandlers } from '@amqp-contract/worker';
936
- import { orderContract } from './contract';
1745
+ import { defineUnsafeHandlers } from '@amqp-contract/worker';
937
1746
 
938
- // Define all handlers at once
939
- const handlers = defineHandlers(orderContract, {
1747
+ // ⚠️ Consider using defineHandlers for better error handling
1748
+ const handlers = defineUnsafeHandlers(orderContract, {
940
1749
  processOrder: async (message) => {
941
- // message is fully typed based on the contract
942
- console.log('Processing order:', message.orderId);
943
1750
  await processPayment(message);
944
1751
  },
945
1752
  notifyOrder: async (message) => {
946
1753
  await sendNotification(message);
947
1754
  },
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
1755
  });
975
1756
  ```