@amqp-contract/worker 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -54
- package/dist/index.cjs +257 -89
- package/dist/index.d.cts +294 -45
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +294 -45
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +256 -90
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +719 -0
- package/package.json +14 -9
package/docs/index.md
ADDED
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
**@amqp-contract/worker**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @amqp-contract/worker
|
|
6
|
+
|
|
7
|
+
## Classes
|
|
8
|
+
|
|
9
|
+
### MessageValidationError
|
|
10
|
+
|
|
11
|
+
Defined in: [packages/worker/src/errors.ts:35](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/errors.ts#L35)
|
|
12
|
+
|
|
13
|
+
Error thrown when message validation fails
|
|
14
|
+
|
|
15
|
+
#### Extends
|
|
16
|
+
|
|
17
|
+
- `WorkerError`
|
|
18
|
+
|
|
19
|
+
#### Constructors
|
|
20
|
+
|
|
21
|
+
##### Constructor
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
new MessageValidationError(consumerName, issues): MessageValidationError;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Defined in: [packages/worker/src/errors.ts:36](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/errors.ts#L36)
|
|
28
|
+
|
|
29
|
+
###### Parameters
|
|
30
|
+
|
|
31
|
+
| Parameter | Type |
|
|
32
|
+
| ------ | ------ |
|
|
33
|
+
| `consumerName` | `string` |
|
|
34
|
+
| `issues` | `unknown` |
|
|
35
|
+
|
|
36
|
+
###### Returns
|
|
37
|
+
|
|
38
|
+
[`MessageValidationError`](#messagevalidationerror)
|
|
39
|
+
|
|
40
|
+
###### Overrides
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
WorkerError.constructor
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### Properties
|
|
47
|
+
|
|
48
|
+
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
49
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
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/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/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/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/errors.ts#L38) |
|
|
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
|
+
| <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
|
+
| <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 |
|
|
57
|
+
|
|
58
|
+
#### Methods
|
|
59
|
+
|
|
60
|
+
##### captureStackTrace()
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.3/node\_modules/@types/node/globals.d.ts:51
|
|
67
|
+
|
|
68
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
69
|
+
a string representing the location in the code at which
|
|
70
|
+
`Error.captureStackTrace()` was called.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const myObject = {};
|
|
74
|
+
Error.captureStackTrace(myObject);
|
|
75
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The first line of the trace will be prefixed with
|
|
79
|
+
`${myObject.name}: ${myObject.message}`.
|
|
80
|
+
|
|
81
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
82
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
83
|
+
generated stack trace.
|
|
84
|
+
|
|
85
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
86
|
+
details of error generation from the user. For instance:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
function a() {
|
|
90
|
+
b();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function b() {
|
|
94
|
+
c();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function c() {
|
|
98
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
99
|
+
const { stackTraceLimit } = Error;
|
|
100
|
+
Error.stackTraceLimit = 0;
|
|
101
|
+
const error = new Error();
|
|
102
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
103
|
+
|
|
104
|
+
// Capture the stack trace above function b
|
|
105
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
a();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
###### Parameters
|
|
113
|
+
|
|
114
|
+
| Parameter | Type |
|
|
115
|
+
| ------ | ------ |
|
|
116
|
+
| `targetObject` | `object` |
|
|
117
|
+
| `constructorOpt?` | `Function` |
|
|
118
|
+
|
|
119
|
+
###### Returns
|
|
120
|
+
|
|
121
|
+
`void`
|
|
122
|
+
|
|
123
|
+
###### Inherited from
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
WorkerError.captureStackTrace
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
##### prepareStackTrace()
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
static prepareStackTrace(err, stackTraces): any;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.3/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
|
+
### TechnicalError
|
|
161
|
+
|
|
162
|
+
Defined in: [packages/worker/src/errors.ts:22](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/errors.ts#L22)
|
|
163
|
+
|
|
164
|
+
Error for technical/runtime failures in worker operations
|
|
165
|
+
This includes validation failures, parsing failures, and processing failures
|
|
166
|
+
|
|
167
|
+
#### Extends
|
|
168
|
+
|
|
169
|
+
- `WorkerError`
|
|
170
|
+
|
|
171
|
+
#### Constructors
|
|
172
|
+
|
|
173
|
+
##### Constructor
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
new TechnicalError(message, cause?): TechnicalError;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Defined in: [packages/worker/src/errors.ts:23](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/errors.ts#L23)
|
|
180
|
+
|
|
181
|
+
###### Parameters
|
|
182
|
+
|
|
183
|
+
| Parameter | Type |
|
|
184
|
+
| ------ | ------ |
|
|
185
|
+
| `message` | `string` |
|
|
186
|
+
| `cause?` | `unknown` |
|
|
187
|
+
|
|
188
|
+
###### Returns
|
|
189
|
+
|
|
190
|
+
[`TechnicalError`](#technicalerror)
|
|
191
|
+
|
|
192
|
+
###### Overrides
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
WorkerError.constructor
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Properties
|
|
199
|
+
|
|
200
|
+
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
201
|
+
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
202
|
+
| <a id="cause-1"></a> `cause?` | `readonly` | `unknown` | - | `WorkerError.cause` | [packages/worker/src/errors.ts:25](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/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 |
|
|
207
|
+
|
|
208
|
+
#### Methods
|
|
209
|
+
|
|
210
|
+
##### captureStackTrace()
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.3/node\_modules/@types/node/globals.d.ts:51
|
|
217
|
+
|
|
218
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
219
|
+
a string representing the location in the code at which
|
|
220
|
+
`Error.captureStackTrace()` was called.
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
const myObject = {};
|
|
224
|
+
Error.captureStackTrace(myObject);
|
|
225
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The first line of the trace will be prefixed with
|
|
229
|
+
`${myObject.name}: ${myObject.message}`.
|
|
230
|
+
|
|
231
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
232
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
233
|
+
generated stack trace.
|
|
234
|
+
|
|
235
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
236
|
+
details of error generation from the user. For instance:
|
|
237
|
+
|
|
238
|
+
```js
|
|
239
|
+
function a() {
|
|
240
|
+
b();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function b() {
|
|
244
|
+
c();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function c() {
|
|
248
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
249
|
+
const { stackTraceLimit } = Error;
|
|
250
|
+
Error.stackTraceLimit = 0;
|
|
251
|
+
const error = new Error();
|
|
252
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
253
|
+
|
|
254
|
+
// Capture the stack trace above function b
|
|
255
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
a();
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
###### Parameters
|
|
263
|
+
|
|
264
|
+
| Parameter | Type |
|
|
265
|
+
| ------ | ------ |
|
|
266
|
+
| `targetObject` | `object` |
|
|
267
|
+
| `constructorOpt?` | `Function` |
|
|
268
|
+
|
|
269
|
+
###### Returns
|
|
270
|
+
|
|
271
|
+
`void`
|
|
272
|
+
|
|
273
|
+
###### Inherited from
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
WorkerError.captureStackTrace
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
##### prepareStackTrace()
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
static prepareStackTrace(err, stackTraces): any;
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Defined in: node\_modules/.pnpm/@types+node@25.0.3/node\_modules/@types/node/globals.d.ts:55
|
|
286
|
+
|
|
287
|
+
###### Parameters
|
|
288
|
+
|
|
289
|
+
| Parameter | Type |
|
|
290
|
+
| ------ | ------ |
|
|
291
|
+
| `err` | `Error` |
|
|
292
|
+
| `stackTraces` | `CallSite`[] |
|
|
293
|
+
|
|
294
|
+
###### Returns
|
|
295
|
+
|
|
296
|
+
`any`
|
|
297
|
+
|
|
298
|
+
###### See
|
|
299
|
+
|
|
300
|
+
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
301
|
+
|
|
302
|
+
###### Inherited from
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
WorkerError.prepareStackTrace
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
***
|
|
309
|
+
|
|
310
|
+
### TypedAmqpWorker
|
|
311
|
+
|
|
312
|
+
Defined in: [packages/worker/src/worker.ts:80](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L80)
|
|
313
|
+
|
|
314
|
+
Type-safe AMQP worker for consuming messages from RabbitMQ.
|
|
315
|
+
|
|
316
|
+
This class provides automatic message validation, connection management,
|
|
317
|
+
and error handling for consuming messages based on a contract definition.
|
|
318
|
+
|
|
319
|
+
#### Example
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { TypedAmqpWorker } from '@amqp-contract/worker';
|
|
323
|
+
import { z } from 'zod';
|
|
324
|
+
|
|
325
|
+
const contract = defineContract({
|
|
326
|
+
queues: {
|
|
327
|
+
orderProcessing: defineQueue('order-processing', { durable: true })
|
|
328
|
+
},
|
|
329
|
+
consumers: {
|
|
330
|
+
processOrder: defineConsumer('order-processing', z.object({
|
|
331
|
+
orderId: z.string(),
|
|
332
|
+
amount: z.number()
|
|
333
|
+
}))
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const worker = await TypedAmqpWorker.create({
|
|
338
|
+
contract,
|
|
339
|
+
handlers: {
|
|
340
|
+
processOrder: async (message) => {
|
|
341
|
+
console.log('Processing order', message.orderId);
|
|
342
|
+
// Process the order...
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
urls: ['amqp://localhost']
|
|
346
|
+
}).resultToPromise();
|
|
347
|
+
|
|
348
|
+
// Close when done
|
|
349
|
+
await worker.close().resultToPromise();
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
#### Type Parameters
|
|
353
|
+
|
|
354
|
+
| Type Parameter | Description |
|
|
355
|
+
| ------ | ------ |
|
|
356
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
357
|
+
|
|
358
|
+
#### Methods
|
|
359
|
+
|
|
360
|
+
##### close()
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
close(): Future<Result<void, TechnicalError>>;
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
Defined in: [packages/worker/src/worker.ts:146](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L146)
|
|
367
|
+
|
|
368
|
+
Close the AMQP channel and connection.
|
|
369
|
+
|
|
370
|
+
This gracefully closes the connection to the AMQP broker,
|
|
371
|
+
stopping all message consumption and cleaning up resources.
|
|
372
|
+
|
|
373
|
+
###### Returns
|
|
374
|
+
|
|
375
|
+
`Future`\<`Result`\<`void`, [`TechnicalError`](#technicalerror)\>\>
|
|
376
|
+
|
|
377
|
+
A Future that resolves to a Result indicating success or failure
|
|
378
|
+
|
|
379
|
+
###### Example
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
const closeResult = await worker.close().resultToPromise();
|
|
383
|
+
if (closeResult.isOk()) {
|
|
384
|
+
console.log('Worker closed successfully');
|
|
385
|
+
}
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
##### create()
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
static create<TContract>(options): Future<Result<TypedAmqpWorker<TContract>, TechnicalError>>;
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Defined in: [packages/worker/src/worker.ts:113](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L113)
|
|
395
|
+
|
|
396
|
+
Create a type-safe AMQP worker from a contract.
|
|
397
|
+
|
|
398
|
+
Connection management (including automatic reconnection) is handled internally
|
|
399
|
+
by amqp-connection-manager via the AmqpClient. The worker will set up
|
|
400
|
+
consumers for all contract-defined handlers asynchronously in the background
|
|
401
|
+
once the underlying connection and channels are ready.
|
|
402
|
+
|
|
403
|
+
###### Type Parameters
|
|
404
|
+
|
|
405
|
+
| Type Parameter |
|
|
406
|
+
| ------ |
|
|
407
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
408
|
+
|
|
409
|
+
###### Parameters
|
|
410
|
+
|
|
411
|
+
| Parameter | Type | Description |
|
|
412
|
+
| ------ | ------ | ------ |
|
|
413
|
+
| `options` | [`CreateWorkerOptions`](#createworkeroptions)\<`TContract`\> | Configuration options for the worker |
|
|
414
|
+
|
|
415
|
+
###### Returns
|
|
416
|
+
|
|
417
|
+
`Future`\<`Result`\<[`TypedAmqpWorker`](#typedamqpworker)\<`TContract`\>, [`TechnicalError`](#technicalerror)\>\>
|
|
418
|
+
|
|
419
|
+
A Future that resolves to a Result containing the worker or an error
|
|
420
|
+
|
|
421
|
+
###### Example
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
const workerResult = await TypedAmqpWorker.create({
|
|
425
|
+
contract: myContract,
|
|
426
|
+
handlers: {
|
|
427
|
+
processOrder: async (msg) => console.log('Order:', msg.orderId)
|
|
428
|
+
},
|
|
429
|
+
urls: ['amqp://localhost']
|
|
430
|
+
}).resultToPromise();
|
|
431
|
+
|
|
432
|
+
if (workerResult.isError()) {
|
|
433
|
+
console.error('Failed to create worker:', workerResult.error);
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## Type Aliases
|
|
438
|
+
|
|
439
|
+
### CreateWorkerOptions
|
|
440
|
+
|
|
441
|
+
```ts
|
|
442
|
+
type CreateWorkerOptions<TContract> = object;
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Defined in: [packages/worker/src/worker.ts:29](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L29)
|
|
446
|
+
|
|
447
|
+
Options for creating a type-safe AMQP worker.
|
|
448
|
+
|
|
449
|
+
#### Example
|
|
450
|
+
|
|
451
|
+
```typescript
|
|
452
|
+
const options: CreateWorkerOptions<typeof contract> = {
|
|
453
|
+
contract: myContract,
|
|
454
|
+
handlers: {
|
|
455
|
+
processOrder: async (message) => {
|
|
456
|
+
console.log('Processing order:', message.orderId);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
urls: ['amqp://localhost'],
|
|
460
|
+
connectionOptions: {
|
|
461
|
+
heartbeatIntervalInSeconds: 30
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
#### Type Parameters
|
|
467
|
+
|
|
468
|
+
| Type Parameter | Description |
|
|
469
|
+
| ------ | ------ |
|
|
470
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
471
|
+
|
|
472
|
+
#### Properties
|
|
473
|
+
|
|
474
|
+
| Property | Type | Description | Defined in |
|
|
475
|
+
| ------ | ------ | ------ | ------ |
|
|
476
|
+
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [packages/worker/src/worker.ts:37](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L37) |
|
|
477
|
+
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [packages/worker/src/worker.ts:31](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L31) |
|
|
478
|
+
| <a id="handlers"></a> `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | Handlers for each consumer defined in the contract | [packages/worker/src/worker.ts:33](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L33) |
|
|
479
|
+
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [packages/worker/src/worker.ts:35](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/worker.ts#L35) |
|
|
480
|
+
|
|
481
|
+
***
|
|
482
|
+
|
|
483
|
+
### WorkerInferConsumerHandler()
|
|
484
|
+
|
|
485
|
+
```ts
|
|
486
|
+
type WorkerInferConsumerHandler<TContract, TName> = (message) => Promise<void>;
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
Defined in: [packages/worker/src/types.ts:45](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/types.ts#L45)
|
|
490
|
+
|
|
491
|
+
Infer consumer handler type for a specific consumer
|
|
492
|
+
|
|
493
|
+
#### Type Parameters
|
|
494
|
+
|
|
495
|
+
| Type Parameter |
|
|
496
|
+
| ------ |
|
|
497
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
498
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
499
|
+
|
|
500
|
+
#### Parameters
|
|
501
|
+
|
|
502
|
+
| Parameter | Type |
|
|
503
|
+
| ------ | ------ |
|
|
504
|
+
| `message` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\> |
|
|
505
|
+
|
|
506
|
+
#### Returns
|
|
507
|
+
|
|
508
|
+
`Promise`\<`void`\>
|
|
509
|
+
|
|
510
|
+
***
|
|
511
|
+
|
|
512
|
+
### WorkerInferConsumerHandlers
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
type WorkerInferConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferConsumerHandler<TContract, K> };
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
Defined in: [packages/worker/src/types.ts:53](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/types.ts#L53)
|
|
519
|
+
|
|
520
|
+
Infer all consumer handlers for a contract
|
|
521
|
+
|
|
522
|
+
#### Type Parameters
|
|
523
|
+
|
|
524
|
+
| Type Parameter |
|
|
525
|
+
| ------ |
|
|
526
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
527
|
+
|
|
528
|
+
***
|
|
529
|
+
|
|
530
|
+
### WorkerInferConsumerInput
|
|
531
|
+
|
|
532
|
+
```ts
|
|
533
|
+
type WorkerInferConsumerInput<TContract, TName> = ConsumerInferInput<InferConsumer<TContract, TName>>;
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
Defined in: [packages/worker/src/types.ts:37](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/types.ts#L37)
|
|
537
|
+
|
|
538
|
+
Worker perspective types - for consuming messages
|
|
539
|
+
|
|
540
|
+
#### Type Parameters
|
|
541
|
+
|
|
542
|
+
| Type Parameter |
|
|
543
|
+
| ------ |
|
|
544
|
+
| `TContract` *extends* `ContractDefinition` |
|
|
545
|
+
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
546
|
+
|
|
547
|
+
## Functions
|
|
548
|
+
|
|
549
|
+
### defineHandler()
|
|
550
|
+
|
|
551
|
+
```ts
|
|
552
|
+
function defineHandler<TContract, TName>(
|
|
553
|
+
contract,
|
|
554
|
+
consumerName,
|
|
555
|
+
handler): WorkerInferConsumerHandler<TContract, TName>;
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
Defined in: [packages/worker/src/handlers.ts:73](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/handlers.ts#L73)
|
|
559
|
+
|
|
560
|
+
Define a type-safe handler for a specific consumer in a contract.
|
|
561
|
+
|
|
562
|
+
This utility allows you to define handlers outside of the worker creation,
|
|
563
|
+
providing better code organization and reusability.
|
|
564
|
+
|
|
565
|
+
#### Type Parameters
|
|
566
|
+
|
|
567
|
+
| Type Parameter | Description |
|
|
568
|
+
| ------ | ------ |
|
|
569
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
570
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
571
|
+
|
|
572
|
+
#### Parameters
|
|
573
|
+
|
|
574
|
+
| Parameter | Type | Description |
|
|
575
|
+
| ------ | ------ | ------ |
|
|
576
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
577
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
578
|
+
| `handler` | [`WorkerInferConsumerHandler`](#workerinferconsumerhandler)\<`TContract`, `TName`\> | The async handler function that processes messages |
|
|
579
|
+
|
|
580
|
+
#### Returns
|
|
581
|
+
|
|
582
|
+
[`WorkerInferConsumerHandler`](#workerinferconsumerhandler)\<`TContract`, `TName`\>
|
|
583
|
+
|
|
584
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
585
|
+
|
|
586
|
+
#### Examples
|
|
587
|
+
|
|
588
|
+
```typescript
|
|
589
|
+
import { defineHandler } from '@amqp-contract/worker';
|
|
590
|
+
import { orderContract } from './contract';
|
|
591
|
+
|
|
592
|
+
// Define handler outside of worker creation
|
|
593
|
+
const processOrderHandler = defineHandler(
|
|
594
|
+
orderContract,
|
|
595
|
+
'processOrder',
|
|
596
|
+
async (message) => {
|
|
597
|
+
// message is fully typed based on the contract
|
|
598
|
+
console.log('Processing order:', message.orderId);
|
|
599
|
+
await processPayment(message);
|
|
600
|
+
}
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
// Use the handler in worker
|
|
604
|
+
const worker = await TypedAmqpWorker.create({
|
|
605
|
+
contract: orderContract,
|
|
606
|
+
handlers: {
|
|
607
|
+
processOrder: processOrderHandler,
|
|
608
|
+
},
|
|
609
|
+
connection: 'amqp://localhost',
|
|
610
|
+
});
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
```typescript
|
|
614
|
+
// Define multiple handlers
|
|
615
|
+
const processOrderHandler = defineHandler(
|
|
616
|
+
orderContract,
|
|
617
|
+
'processOrder',
|
|
618
|
+
async (message) => {
|
|
619
|
+
await processOrder(message);
|
|
620
|
+
}
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
const notifyOrderHandler = defineHandler(
|
|
624
|
+
orderContract,
|
|
625
|
+
'notifyOrder',
|
|
626
|
+
async (message) => {
|
|
627
|
+
await sendNotification(message);
|
|
628
|
+
}
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
// Compose handlers
|
|
632
|
+
const worker = await TypedAmqpWorker.create({
|
|
633
|
+
contract: orderContract,
|
|
634
|
+
handlers: {
|
|
635
|
+
processOrder: processOrderHandler,
|
|
636
|
+
notifyOrder: notifyOrderHandler,
|
|
637
|
+
},
|
|
638
|
+
connection: 'amqp://localhost',
|
|
639
|
+
});
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
***
|
|
643
|
+
|
|
644
|
+
### defineHandlers()
|
|
645
|
+
|
|
646
|
+
```ts
|
|
647
|
+
function defineHandlers<TContract>(contract, handlers): WorkerInferConsumerHandlers<TContract>;
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
Defined in: [packages/worker/src/handlers.ts:152](https://github.com/btravers/amqp-contract/blob/f0945f098387fd3a6a40beac8cbe2ed7a4de210a/packages/worker/src/handlers.ts#L152)
|
|
651
|
+
|
|
652
|
+
Define multiple type-safe handlers for consumers in a contract.
|
|
653
|
+
|
|
654
|
+
This utility allows you to define all handlers at once outside of the worker creation,
|
|
655
|
+
ensuring type safety and providing better code organization.
|
|
656
|
+
|
|
657
|
+
#### Type Parameters
|
|
658
|
+
|
|
659
|
+
| Type Parameter | Description |
|
|
660
|
+
| ------ | ------ |
|
|
661
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
662
|
+
|
|
663
|
+
#### Parameters
|
|
664
|
+
|
|
665
|
+
| Parameter | Type | Description |
|
|
666
|
+
| ------ | ------ | ------ |
|
|
667
|
+
| `contract` | `TContract` | The contract definition containing the consumers |
|
|
668
|
+
| `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | An object with async handler functions for each consumer |
|
|
669
|
+
|
|
670
|
+
#### Returns
|
|
671
|
+
|
|
672
|
+
[`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\>
|
|
673
|
+
|
|
674
|
+
A type-safe handlers object that can be used with TypedAmqpWorker
|
|
675
|
+
|
|
676
|
+
#### Examples
|
|
677
|
+
|
|
678
|
+
```typescript
|
|
679
|
+
import { defineHandlers } from '@amqp-contract/worker';
|
|
680
|
+
import { orderContract } from './contract';
|
|
681
|
+
|
|
682
|
+
// Define all handlers at once
|
|
683
|
+
const handlers = defineHandlers(orderContract, {
|
|
684
|
+
processOrder: async (message) => {
|
|
685
|
+
// message is fully typed based on the contract
|
|
686
|
+
console.log('Processing order:', message.orderId);
|
|
687
|
+
await processPayment(message);
|
|
688
|
+
},
|
|
689
|
+
notifyOrder: async (message) => {
|
|
690
|
+
await sendNotification(message);
|
|
691
|
+
},
|
|
692
|
+
shipOrder: async (message) => {
|
|
693
|
+
await prepareShipment(message);
|
|
694
|
+
},
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
// Use the handlers in worker
|
|
698
|
+
const worker = await TypedAmqpWorker.create({
|
|
699
|
+
contract: orderContract,
|
|
700
|
+
handlers,
|
|
701
|
+
connection: 'amqp://localhost',
|
|
702
|
+
});
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
```typescript
|
|
706
|
+
// Separate handler definitions for better organization
|
|
707
|
+
async function handleProcessOrder(message: WorkerInferConsumerInput<typeof orderContract, 'processOrder'>) {
|
|
708
|
+
await processOrder(message);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
async function handleNotifyOrder(message: WorkerInferConsumerInput<typeof orderContract, 'notifyOrder'>) {
|
|
712
|
+
await sendNotification(message);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const handlers = defineHandlers(orderContract, {
|
|
716
|
+
processOrder: handleProcessOrder,
|
|
717
|
+
notifyOrder: handleNotifyOrder,
|
|
718
|
+
});
|
|
719
|
+
```
|