@amqp-contract/worker-nestjs 0.4.0 → 0.6.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/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/docs/index.md +212 -91
- package/package.json +8 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AmqpConnectionManagerOptions, ConnectionUrl } from "amqp-connection-manager";
|
|
2
2
|
import { DynamicModule, ModuleMetadata, OnModuleDestroy, OnModuleInit, Type } from "@nestjs/common";
|
|
3
|
-
import {
|
|
3
|
+
import { WorkerInferConsumerHandlers, WorkerInferConsumerHandlers as WorkerInferConsumerHandlers$1, defineHandler, defineHandlers } from "@amqp-contract/worker";
|
|
4
4
|
import { ContractDefinition } from "@amqp-contract/contract";
|
|
5
5
|
|
|
6
6
|
//#region src/worker.service.d.ts
|
|
@@ -190,5 +190,5 @@ declare class AmqpWorkerModule {
|
|
|
190
190
|
*/
|
|
191
191
|
declare const MODULE_OPTIONS_TOKEN: unique symbol;
|
|
192
192
|
//#endregion
|
|
193
|
-
export { AmqpWorkerModule, type AmqpWorkerModuleAsyncOptions, type AmqpWorkerModuleOptions, AmqpWorkerService, MODULE_OPTIONS_TOKEN, type
|
|
193
|
+
export { AmqpWorkerModule, type AmqpWorkerModuleAsyncOptions, type AmqpWorkerModuleOptions, AmqpWorkerService, MODULE_OPTIONS_TOKEN, type WorkerInferConsumerHandlers, defineHandler, defineHandlers };
|
|
194
194
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DynamicModule, ModuleMetadata, OnModuleDestroy, OnModuleInit, Type } from "@nestjs/common";
|
|
2
|
-
import {
|
|
2
|
+
import { WorkerInferConsumerHandlers, WorkerInferConsumerHandlers as WorkerInferConsumerHandlers$1, defineHandler, defineHandlers } from "@amqp-contract/worker";
|
|
3
3
|
import { AmqpConnectionManagerOptions, ConnectionUrl } from "amqp-connection-manager";
|
|
4
4
|
import { ContractDefinition } from "@amqp-contract/contract";
|
|
5
5
|
|
|
@@ -190,5 +190,5 @@ declare class AmqpWorkerModule {
|
|
|
190
190
|
*/
|
|
191
191
|
declare const MODULE_OPTIONS_TOKEN: unique symbol;
|
|
192
192
|
//#endregion
|
|
193
|
-
export { AmqpWorkerModule, type AmqpWorkerModuleAsyncOptions, type AmqpWorkerModuleOptions, AmqpWorkerService, MODULE_OPTIONS_TOKEN, type
|
|
193
|
+
export { AmqpWorkerModule, type AmqpWorkerModuleAsyncOptions, type AmqpWorkerModuleOptions, AmqpWorkerService, MODULE_OPTIONS_TOKEN, type WorkerInferConsumerHandlers, defineHandler, defineHandlers };
|
|
194
194
|
//# sourceMappingURL=index.d.mts.map
|
package/docs/index.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
### AmqpWorkerModule
|
|
10
10
|
|
|
11
|
-
Defined in: [worker-nestjs/src/worker.module.ts:88](https://github.com/btravers/amqp-contract/blob/
|
|
11
|
+
Defined in: [worker-nestjs/src/worker.module.ts:88](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L88)
|
|
12
12
|
|
|
13
13
|
NestJS module for AMQP worker integration
|
|
14
14
|
This module provides type-safe AMQP worker functionality using @amqp-contract/worker
|
|
@@ -79,7 +79,7 @@ new AmqpWorkerModule(): AmqpWorkerModule;
|
|
|
79
79
|
static forRoot<TContract>(options): DynamicModule;
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
-
Defined in: [worker-nestjs/src/worker.module.ts:95](https://github.com/btravers/amqp-contract/blob/
|
|
82
|
+
Defined in: [worker-nestjs/src/worker.module.ts:95](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L95)
|
|
83
83
|
|
|
84
84
|
Register the AMQP worker module with synchronous configuration
|
|
85
85
|
|
|
@@ -107,7 +107,7 @@ A dynamic module for NestJS
|
|
|
107
107
|
static forRootAsync<TContract>(options): DynamicModule;
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
Defined in: [worker-nestjs/src/worker.module.ts:117](https://github.com/btravers/amqp-contract/blob/
|
|
110
|
+
Defined in: [worker-nestjs/src/worker.module.ts:117](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L117)
|
|
111
111
|
|
|
112
112
|
Register the AMQP worker module with asynchronous configuration
|
|
113
113
|
|
|
@@ -133,11 +133,11 @@ A dynamic module for NestJS
|
|
|
133
133
|
|
|
134
134
|
### AmqpWorkerService
|
|
135
135
|
|
|
136
|
-
Defined in: [worker-nestjs/src/worker.service.ts:74](https://github.com/btravers/amqp-contract/blob/
|
|
136
|
+
Defined in: [worker-nestjs/src/worker.service.ts:74](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L74)
|
|
137
137
|
|
|
138
138
|
Type-safe AMQP worker service for NestJS applications.
|
|
139
139
|
|
|
140
|
-
This service wraps TypedAmqpWorker and integrates it with the NestJS
|
|
140
|
+
This service wraps [TypedAmqpWorker](https://btravers.github.io/amqp-contract/api/worker#typedamqpworker) and integrates it with the NestJS
|
|
141
141
|
lifecycle, automatically starting message consumption on module init and
|
|
142
142
|
cleaning up resources on module destroy.
|
|
143
143
|
|
|
@@ -186,7 +186,7 @@ export class AppModule {}
|
|
|
186
186
|
new AmqpWorkerService<TContract>(options): AmqpWorkerService<TContract>;
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
-
Defined in: [worker-nestjs/src/worker.service.ts:79](https://github.com/btravers/amqp-contract/blob/
|
|
189
|
+
Defined in: [worker-nestjs/src/worker.service.ts:79](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L79)
|
|
190
190
|
|
|
191
191
|
###### Parameters
|
|
192
192
|
|
|
@@ -206,7 +206,7 @@ Defined in: [worker-nestjs/src/worker.service.ts:79](https://github.com/btravers
|
|
|
206
206
|
onModuleDestroy(): Promise<void>;
|
|
207
207
|
```
|
|
208
208
|
|
|
209
|
-
Defined in: [worker-nestjs/src/worker.service.ts:105](https://github.com/btravers/amqp-contract/blob/
|
|
209
|
+
Defined in: [worker-nestjs/src/worker.service.ts:105](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L105)
|
|
210
210
|
|
|
211
211
|
Close the AMQP worker when the NestJS module is destroyed.
|
|
212
212
|
|
|
@@ -230,7 +230,7 @@ OnModuleDestroy.onModuleDestroy
|
|
|
230
230
|
onModuleInit(): Promise<void>;
|
|
231
231
|
```
|
|
232
232
|
|
|
233
|
-
Defined in: [worker-nestjs/src/worker.service.ts:94](https://github.com/btravers/amqp-contract/blob/
|
|
233
|
+
Defined in: [worker-nestjs/src/worker.service.ts:94](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L94)
|
|
234
234
|
|
|
235
235
|
Initialize the AMQP worker when the NestJS module starts.
|
|
236
236
|
|
|
@@ -261,7 +261,7 @@ OnModuleInit.onModuleInit
|
|
|
261
261
|
type AmqpWorkerModuleAsyncOptions<TContract> = object;
|
|
262
262
|
```
|
|
263
263
|
|
|
264
|
-
Defined in: [worker-nestjs/src/worker.module.ts:22](https://github.com/btravers/amqp-contract/blob/
|
|
264
|
+
Defined in: [worker-nestjs/src/worker.module.ts:22](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L22)
|
|
265
265
|
|
|
266
266
|
Options for async module configuration using factory pattern
|
|
267
267
|
|
|
@@ -275,9 +275,9 @@ Options for async module configuration using factory pattern
|
|
|
275
275
|
|
|
276
276
|
| Property | Type | Description | Defined in |
|
|
277
277
|
| ------ | ------ | ------ | ------ |
|
|
278
|
-
| <a id="imports"></a> `imports?` | `ModuleMetadata`\[`"imports"`\] | Optional list of imported modules that export providers needed by the factory | [worker-nestjs/src/worker.module.ts:37](https://github.com/btravers/amqp-contract/blob/
|
|
279
|
-
| <a id="inject"></a> `inject?` | (`string` \| `symbol` \| `Type`\<`unknown`\>)[] | Optional dependencies to inject into the factory function. Can be a token (string/symbol) a class or a reference to a provider. | [worker-nestjs/src/worker.module.ts:33](https://github.com/btravers/amqp-contract/blob/
|
|
280
|
-
| <a id="usefactory"></a> `useFactory` | (...`args`) => `AmqpWorkerModuleOptionsFactory`\<`TContract`\> | Factory function that returns the module options. Can use injected dependencies to create configuration. | [worker-nestjs/src/worker.module.ts:28](https://github.com/btravers/amqp-contract/blob/
|
|
278
|
+
| <a id="imports"></a> `imports?` | `ModuleMetadata`\[`"imports"`\] | Optional list of imported modules that export providers needed by the factory | [worker-nestjs/src/worker.module.ts:37](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L37) |
|
|
279
|
+
| <a id="inject"></a> `inject?` | (`string` \| `symbol` \| `Type`\<`unknown`\>)[] | Optional dependencies to inject into the factory function. Can be a token (string/symbol) a class or a reference to a provider. | [worker-nestjs/src/worker.module.ts:33](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L33) |
|
|
280
|
+
| <a id="usefactory"></a> `useFactory` | (...`args`) => `AmqpWorkerModuleOptionsFactory`\<`TContract`\> | Factory function that returns the module options. Can use injected dependencies to create configuration. | [worker-nestjs/src/worker.module.ts:28](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module.ts#L28) |
|
|
281
281
|
|
|
282
282
|
***
|
|
283
283
|
|
|
@@ -287,7 +287,7 @@ Options for async module configuration using factory pattern
|
|
|
287
287
|
type AmqpWorkerModuleOptions<TContract> = object;
|
|
288
288
|
```
|
|
289
289
|
|
|
290
|
-
Defined in: [worker-nestjs/src/worker.service.ts:28](https://github.com/btravers/amqp-contract/blob/
|
|
290
|
+
Defined in: [worker-nestjs/src/worker.service.ts:28](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L28)
|
|
291
291
|
|
|
292
292
|
Configuration options for the AMQP worker NestJS module.
|
|
293
293
|
|
|
@@ -318,183 +318,304 @@ const options: AmqpWorkerModuleOptions<typeof contract> = {
|
|
|
318
318
|
|
|
319
319
|
| Property | Type | Description | Defined in |
|
|
320
320
|
| ------ | ------ | ------ | ------ |
|
|
321
|
-
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [worker-nestjs/src/worker.service.ts:36](https://github.com/btravers/amqp-contract/blob/
|
|
322
|
-
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [worker-nestjs/src/worker.service.ts:30](https://github.com/btravers/amqp-contract/blob/
|
|
323
|
-
| <a id="handlers"></a> `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | Message handlers for each consumer defined in the contract | [worker-nestjs/src/worker.service.ts:32](https://github.com/btravers/amqp-contract/blob/
|
|
324
|
-
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [worker-nestjs/src/worker.service.ts:34](https://github.com/btravers/amqp-contract/blob/
|
|
321
|
+
| <a id="connectionoptions"></a> `connectionOptions?` | `AmqpConnectionManagerOptions` | Optional connection configuration (heartbeat, reconnect settings, etc.) | [worker-nestjs/src/worker.service.ts:36](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L36) |
|
|
322
|
+
| <a id="contract"></a> `contract` | `TContract` | The AMQP contract definition specifying consumers and their message schemas | [worker-nestjs/src/worker.service.ts:30](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L30) |
|
|
323
|
+
| <a id="handlers"></a> `handlers` | [`WorkerInferConsumerHandlers`](#workerinferconsumerhandlers)\<`TContract`\> | Message handlers for each consumer defined in the contract | [worker-nestjs/src/worker.service.ts:32](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L32) |
|
|
324
|
+
| <a id="urls"></a> `urls` | `ConnectionUrl`[] | AMQP broker URL(s). Multiple URLs provide failover support | [worker-nestjs/src/worker.service.ts:34](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.service.ts#L34) |
|
|
325
325
|
|
|
326
326
|
***
|
|
327
327
|
|
|
328
|
-
###
|
|
328
|
+
### WorkerInferConsumerHandlers
|
|
329
329
|
|
|
330
330
|
```ts
|
|
331
|
-
type
|
|
331
|
+
type WorkerInferConsumerHandlers<TContract> = { [K in InferConsumerNames<TContract>]: WorkerInferConsumerHandlerEntry<TContract, K> };
|
|
332
332
|
```
|
|
333
333
|
|
|
334
|
-
Defined in: worker/dist/index.d.mts:
|
|
334
|
+
Defined in: worker/dist/index.d.mts:84
|
|
335
335
|
|
|
336
|
-
Infer consumer
|
|
336
|
+
Infer all consumer handlers for a contract.
|
|
337
|
+
Handlers can be either single-message handlers, batch handlers, or a tuple of [handler, options].
|
|
337
338
|
|
|
338
339
|
#### Type Parameters
|
|
339
340
|
|
|
340
341
|
| Type Parameter |
|
|
341
342
|
| ------ |
|
|
342
343
|
| `TContract` *extends* `ContractDefinition` |
|
|
343
|
-
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
344
344
|
|
|
345
|
-
|
|
345
|
+
## Variables
|
|
346
346
|
|
|
347
|
-
|
|
348
|
-
| ------ | ------ |
|
|
349
|
-
| `message` | [`WorkerInferConsumerInput`](#workerinferconsumerinput)\<`TContract`, `TName`\> |
|
|
347
|
+
### MODULE\_OPTIONS\_TOKEN
|
|
350
348
|
|
|
351
|
-
|
|
349
|
+
```ts
|
|
350
|
+
const MODULE_OPTIONS_TOKEN: typeof MODULE_OPTIONS_TOKEN;
|
|
351
|
+
```
|
|
352
352
|
|
|
353
|
-
|
|
353
|
+
Defined in: [worker-nestjs/src/worker.module-definition.ts:5](https://github.com/btravers/amqp-contract/blob/8711ad20abb1d4e537ec473d43e85fac3ebd4d0b/packages/worker-nestjs/src/worker.module-definition.ts#L5)
|
|
354
354
|
|
|
355
|
-
|
|
355
|
+
Injection token for AMQP worker module options
|
|
356
|
+
Used by NestJS DI system to inject configuration into AmqpWorkerService
|
|
356
357
|
|
|
357
|
-
|
|
358
|
+
## Functions
|
|
359
|
+
|
|
360
|
+
### defineHandler()
|
|
361
|
+
|
|
362
|
+
#### Call Signature
|
|
358
363
|
|
|
359
364
|
```ts
|
|
360
|
-
|
|
365
|
+
function defineHandler<TContract, TName>(
|
|
366
|
+
contract,
|
|
367
|
+
consumerName,
|
|
368
|
+
handler): WorkerInferConsumerHandlerEntry<TContract, TName>;
|
|
361
369
|
```
|
|
362
370
|
|
|
363
|
-
Defined in: worker/dist/index.d.mts:
|
|
371
|
+
Defined in: worker/dist/index.d.mts:323
|
|
364
372
|
|
|
365
|
-
|
|
373
|
+
Define a type-safe handler for a specific consumer in a contract.
|
|
366
374
|
|
|
367
|
-
|
|
375
|
+
This utility allows you to define handlers outside of the worker creation,
|
|
376
|
+
providing better code organization and reusability.
|
|
368
377
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
378
|
+
Supports three patterns:
|
|
379
|
+
1. Simple handler: just the function (single message handler)
|
|
380
|
+
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
381
|
+
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
372
382
|
|
|
373
|
-
|
|
383
|
+
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
384
|
+
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
374
385
|
|
|
375
|
-
|
|
386
|
+
##### Type Parameters
|
|
376
387
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
388
|
+
| Type Parameter | Description |
|
|
389
|
+
| ------ | ------ |
|
|
390
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
391
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
380
392
|
|
|
381
|
-
|
|
393
|
+
##### Parameters
|
|
382
394
|
|
|
383
|
-
|
|
395
|
+
| Parameter | Type | Description |
|
|
396
|
+
| ------ | ------ | ------ |
|
|
397
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
398
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
399
|
+
| `handler` | `WorkerInferConsumerHandler`\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
|
|
384
400
|
|
|
385
|
-
|
|
401
|
+
##### Returns
|
|
386
402
|
|
|
387
|
-
|
|
388
|
-
| ------ |
|
|
389
|
-
| `TContract` *extends* `ContractDefinition` |
|
|
390
|
-
| `TName` *extends* `InferConsumerNames`\<`TContract`\> |
|
|
403
|
+
`WorkerInferConsumerHandlerEntry`\<`TContract`, `TName`\>
|
|
391
404
|
|
|
392
|
-
|
|
405
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
393
406
|
|
|
394
|
-
|
|
407
|
+
##### Example
|
|
395
408
|
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
|
|
409
|
+
```typescript
|
|
410
|
+
import { defineHandler } from '@amqp-contract/worker';
|
|
411
|
+
import { orderContract } from './contract';
|
|
399
412
|
|
|
400
|
-
|
|
413
|
+
// Simple single-message handler without options
|
|
414
|
+
const processOrderHandler = defineHandler(
|
|
415
|
+
orderContract,
|
|
416
|
+
'processOrder',
|
|
417
|
+
async (message) => {
|
|
418
|
+
console.log('Processing order:', message.orderId);
|
|
419
|
+
await processPayment(message);
|
|
420
|
+
}
|
|
421
|
+
);
|
|
401
422
|
|
|
402
|
-
|
|
403
|
-
|
|
423
|
+
// Single-message handler with prefetch
|
|
424
|
+
const processOrderWithPrefetch = defineHandler(
|
|
425
|
+
orderContract,
|
|
426
|
+
'processOrder',
|
|
427
|
+
async (message) => {
|
|
428
|
+
await processOrder(message);
|
|
429
|
+
},
|
|
430
|
+
{ prefetch: 10 }
|
|
431
|
+
);
|
|
404
432
|
|
|
405
|
-
|
|
433
|
+
// Batch handler - MUST include batchSize
|
|
434
|
+
const processBatchOrders = defineHandler(
|
|
435
|
+
orderContract,
|
|
436
|
+
'processOrders',
|
|
437
|
+
async (messages) => {
|
|
438
|
+
// messages is an array - batchSize configuration is REQUIRED
|
|
439
|
+
await db.insertMany(messages);
|
|
440
|
+
},
|
|
441
|
+
{ batchSize: 5, batchTimeout: 1000 }
|
|
442
|
+
);
|
|
443
|
+
```
|
|
406
444
|
|
|
407
|
-
|
|
445
|
+
#### Call Signature
|
|
408
446
|
|
|
409
447
|
```ts
|
|
410
448
|
function defineHandler<TContract, TName>(
|
|
411
449
|
contract,
|
|
412
450
|
consumerName,
|
|
413
|
-
handler
|
|
451
|
+
handler,
|
|
452
|
+
options): WorkerInferConsumerHandlerEntry<TContract, TName>;
|
|
414
453
|
```
|
|
415
454
|
|
|
416
|
-
Defined in: worker/dist/index.d.mts:
|
|
455
|
+
Defined in: worker/dist/index.d.mts:324
|
|
417
456
|
|
|
418
457
|
Define a type-safe handler for a specific consumer in a contract.
|
|
419
458
|
|
|
420
459
|
This utility allows you to define handlers outside of the worker creation,
|
|
421
460
|
providing better code organization and reusability.
|
|
422
461
|
|
|
423
|
-
|
|
462
|
+
Supports three patterns:
|
|
463
|
+
1. Simple handler: just the function (single message handler)
|
|
464
|
+
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
465
|
+
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
466
|
+
|
|
467
|
+
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
468
|
+
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
469
|
+
|
|
470
|
+
##### Type Parameters
|
|
424
471
|
|
|
425
472
|
| Type Parameter | Description |
|
|
426
473
|
| ------ | ------ |
|
|
427
474
|
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
428
475
|
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
429
476
|
|
|
430
|
-
|
|
477
|
+
##### Parameters
|
|
431
478
|
|
|
432
479
|
| Parameter | Type | Description |
|
|
433
480
|
| ------ | ------ | ------ |
|
|
434
481
|
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
435
482
|
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
436
|
-
| `handler` |
|
|
483
|
+
| `handler` | `WorkerInferConsumerHandler`\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
|
|
484
|
+
| `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 |
|
|
485
|
+
| `options.batchSize?` | `undefined` | - |
|
|
486
|
+
| `options.batchTimeout?` | `undefined` | - |
|
|
487
|
+
| `options.prefetch?` | `number` | - |
|
|
437
488
|
|
|
438
|
-
|
|
489
|
+
##### Returns
|
|
439
490
|
|
|
440
|
-
|
|
491
|
+
`WorkerInferConsumerHandlerEntry`\<`TContract`, `TName`\>
|
|
441
492
|
|
|
442
493
|
A type-safe handler that can be used with TypedAmqpWorker
|
|
443
494
|
|
|
444
|
-
|
|
495
|
+
##### Example
|
|
445
496
|
|
|
446
497
|
```typescript
|
|
447
498
|
import { defineHandler } from '@amqp-contract/worker';
|
|
448
499
|
import { orderContract } from './contract';
|
|
449
500
|
|
|
450
|
-
//
|
|
501
|
+
// Simple single-message handler without options
|
|
451
502
|
const processOrderHandler = defineHandler(
|
|
452
503
|
orderContract,
|
|
453
504
|
'processOrder',
|
|
454
505
|
async (message) => {
|
|
455
|
-
// message is fully typed based on the contract
|
|
456
506
|
console.log('Processing order:', message.orderId);
|
|
457
507
|
await processPayment(message);
|
|
458
508
|
}
|
|
459
509
|
);
|
|
460
510
|
|
|
461
|
-
//
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
511
|
+
// Single-message handler with prefetch
|
|
512
|
+
const processOrderWithPrefetch = defineHandler(
|
|
513
|
+
orderContract,
|
|
514
|
+
'processOrder',
|
|
515
|
+
async (message) => {
|
|
516
|
+
await processOrder(message);
|
|
466
517
|
},
|
|
467
|
-
|
|
468
|
-
|
|
518
|
+
{ prefetch: 10 }
|
|
519
|
+
);
|
|
520
|
+
|
|
521
|
+
// Batch handler - MUST include batchSize
|
|
522
|
+
const processBatchOrders = defineHandler(
|
|
523
|
+
orderContract,
|
|
524
|
+
'processOrders',
|
|
525
|
+
async (messages) => {
|
|
526
|
+
// messages is an array - batchSize configuration is REQUIRED
|
|
527
|
+
await db.insertMany(messages);
|
|
528
|
+
},
|
|
529
|
+
{ batchSize: 5, batchTimeout: 1000 }
|
|
530
|
+
);
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
#### Call Signature
|
|
534
|
+
|
|
535
|
+
```ts
|
|
536
|
+
function defineHandler<TContract, TName>(
|
|
537
|
+
contract,
|
|
538
|
+
consumerName,
|
|
539
|
+
handler,
|
|
540
|
+
options): WorkerInferConsumerHandlerEntry<TContract, TName>;
|
|
469
541
|
```
|
|
470
542
|
|
|
543
|
+
Defined in: worker/dist/index.d.mts:329
|
|
544
|
+
|
|
545
|
+
Define a type-safe handler for a specific consumer in a contract.
|
|
546
|
+
|
|
547
|
+
This utility allows you to define handlers outside of the worker creation,
|
|
548
|
+
providing better code organization and reusability.
|
|
549
|
+
|
|
550
|
+
Supports three patterns:
|
|
551
|
+
1. Simple handler: just the function (single message handler)
|
|
552
|
+
2. Handler with prefetch: [handler, { prefetch: 10 }] (single message handler with config)
|
|
553
|
+
3. Batch handler: [batchHandler, { batchSize: 5, batchTimeout: 1000 }] (REQUIRES batchSize config)
|
|
554
|
+
|
|
555
|
+
**Important**: Batch handlers (handlers that accept an array of messages) MUST include
|
|
556
|
+
batchSize configuration. You cannot create a batch handler without specifying batchSize.
|
|
557
|
+
|
|
558
|
+
##### Type Parameters
|
|
559
|
+
|
|
560
|
+
| Type Parameter | Description |
|
|
561
|
+
| ------ | ------ |
|
|
562
|
+
| `TContract` *extends* `ContractDefinition` | The contract definition type |
|
|
563
|
+
| `TName` *extends* `string` \| `number` \| `symbol` | The consumer name from the contract |
|
|
564
|
+
|
|
565
|
+
##### Parameters
|
|
566
|
+
|
|
567
|
+
| Parameter | Type | Description |
|
|
568
|
+
| ------ | ------ | ------ |
|
|
569
|
+
| `contract` | `TContract` | The contract definition containing the consumer |
|
|
570
|
+
| `consumerName` | `TName` | The name of the consumer from the contract |
|
|
571
|
+
| `handler` | `WorkerInferConsumerBatchHandler`\<`TContract`, `TName`\> | The async handler function that processes messages (single or batch) |
|
|
572
|
+
| `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 |
|
|
573
|
+
| `options.batchSize` | `number` | - |
|
|
574
|
+
| `options.batchTimeout?` | `number` | - |
|
|
575
|
+
| `options.prefetch?` | `number` | - |
|
|
576
|
+
|
|
577
|
+
##### Returns
|
|
578
|
+
|
|
579
|
+
`WorkerInferConsumerHandlerEntry`\<`TContract`, `TName`\>
|
|
580
|
+
|
|
581
|
+
A type-safe handler that can be used with TypedAmqpWorker
|
|
582
|
+
|
|
583
|
+
##### Example
|
|
584
|
+
|
|
471
585
|
```typescript
|
|
472
|
-
|
|
586
|
+
import { defineHandler } from '@amqp-contract/worker';
|
|
587
|
+
import { orderContract } from './contract';
|
|
588
|
+
|
|
589
|
+
// Simple single-message handler without options
|
|
473
590
|
const processOrderHandler = defineHandler(
|
|
474
591
|
orderContract,
|
|
475
592
|
'processOrder',
|
|
476
593
|
async (message) => {
|
|
477
|
-
|
|
594
|
+
console.log('Processing order:', message.orderId);
|
|
595
|
+
await processPayment(message);
|
|
478
596
|
}
|
|
479
597
|
);
|
|
480
598
|
|
|
481
|
-
|
|
599
|
+
// Single-message handler with prefetch
|
|
600
|
+
const processOrderWithPrefetch = defineHandler(
|
|
482
601
|
orderContract,
|
|
483
|
-
'
|
|
602
|
+
'processOrder',
|
|
484
603
|
async (message) => {
|
|
485
|
-
await
|
|
486
|
-
}
|
|
604
|
+
await processOrder(message);
|
|
605
|
+
},
|
|
606
|
+
{ prefetch: 10 }
|
|
487
607
|
);
|
|
488
608
|
|
|
489
|
-
//
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
609
|
+
// Batch handler - MUST include batchSize
|
|
610
|
+
const processBatchOrders = defineHandler(
|
|
611
|
+
orderContract,
|
|
612
|
+
'processOrders',
|
|
613
|
+
async (messages) => {
|
|
614
|
+
// messages is an array - batchSize configuration is REQUIRED
|
|
615
|
+
await db.insertMany(messages);
|
|
495
616
|
},
|
|
496
|
-
|
|
497
|
-
|
|
617
|
+
{ batchSize: 5, batchTimeout: 1000 }
|
|
618
|
+
);
|
|
498
619
|
```
|
|
499
620
|
|
|
500
621
|
***
|
|
@@ -505,7 +626,7 @@ const worker = await TypedAmqpWorker.create({
|
|
|
505
626
|
function defineHandlers<TContract>(contract, handlers): WorkerInferConsumerHandlers<TContract>;
|
|
506
627
|
```
|
|
507
628
|
|
|
508
|
-
Defined in: worker/dist/index.d.mts:
|
|
629
|
+
Defined in: worker/dist/index.d.mts:390
|
|
509
630
|
|
|
510
631
|
Define multiple type-safe handlers for consumers in a contract.
|
|
511
632
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amqp-contract/worker-nestjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "NestJS integration for @amqp-contract/worker",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"amqp",
|
|
@@ -43,11 +43,13 @@
|
|
|
43
43
|
"docs"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@amqp-contract/contract": "0.
|
|
47
|
-
"@amqp-contract/worker": "0.
|
|
46
|
+
"@amqp-contract/contract": "0.6.0",
|
|
47
|
+
"@amqp-contract/worker": "0.6.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@nestjs/common": "11.1.10",
|
|
51
|
+
"@nestjs/core": "11.1.10",
|
|
52
|
+
"@nestjs/testing": "11.1.10",
|
|
51
53
|
"@swan-io/boxed": "3.2.1",
|
|
52
54
|
"@types/node": "25.0.3",
|
|
53
55
|
"@vitest/coverage-v8": "4.0.16",
|
|
@@ -55,12 +57,13 @@
|
|
|
55
57
|
"amqplib": "0.10.9",
|
|
56
58
|
"reflect-metadata": "0.2.2",
|
|
57
59
|
"rxjs": "7.8.2",
|
|
58
|
-
"tsdown": "0.18.
|
|
60
|
+
"tsdown": "0.18.3",
|
|
59
61
|
"typedoc": "0.28.15",
|
|
60
62
|
"typedoc-plugin-markdown": "4.9.0",
|
|
61
63
|
"typescript": "5.9.3",
|
|
62
64
|
"vitest": "4.0.16",
|
|
63
65
|
"zod": "4.2.1",
|
|
66
|
+
"@amqp-contract/testing": "0.6.0",
|
|
64
67
|
"@amqp-contract/tsconfig": "0.0.0",
|
|
65
68
|
"@amqp-contract/typedoc": "0.0.1"
|
|
66
69
|
},
|
|
@@ -75,6 +78,7 @@
|
|
|
75
78
|
"build:docs": "typedoc",
|
|
76
79
|
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
77
80
|
"test": "vitest run --project unit",
|
|
81
|
+
"test:integration": "vitest run --project integration",
|
|
78
82
|
"test:watch": "vitest --project unit",
|
|
79
83
|
"typecheck": "tsc --noEmit"
|
|
80
84
|
}
|