@lindorm/iris 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +885 -191
- package/dist/classes/IrisSession.d.ts +28 -0
- package/dist/classes/IrisSession.d.ts.map +1 -0
- package/dist/classes/IrisSession.js +42 -0
- package/dist/classes/IrisSession.js.map +1 -0
- package/dist/classes/IrisSource.d.ts +6 -5
- package/dist/classes/IrisSource.d.ts.map +1 -1
- package/dist/classes/IrisSource.js +17 -33
- package/dist/classes/IrisSource.js.map +1 -1
- package/dist/classes/index.d.ts +1 -0
- package/dist/classes/index.d.ts.map +1 -1
- package/dist/classes/index.js +1 -0
- package/dist/classes/index.js.map +1 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +35 -0
- package/dist/cli.js.map +1 -0
- package/dist/interfaces/IrisDriver.d.ts +4 -2
- package/dist/interfaces/IrisDriver.d.ts.map +1 -1
- package/dist/interfaces/IrisMessagingProvider.d.ts +21 -0
- package/dist/interfaces/IrisMessagingProvider.d.ts.map +1 -0
- package/dist/interfaces/IrisMessagingProvider.js +3 -0
- package/dist/interfaces/IrisMessagingProvider.js.map +1 -0
- package/dist/interfaces/IrisSession.d.ts +4 -0
- package/dist/interfaces/IrisSession.d.ts.map +1 -0
- package/dist/{internal/types/iris-source-init.js → interfaces/IrisSession.js} +1 -1
- package/dist/interfaces/IrisSession.js.map +1 -0
- package/dist/interfaces/IrisSource.d.ts +8 -19
- package/dist/interfaces/IrisSource.d.ts.map +1 -1
- package/dist/interfaces/index.d.ts +2 -0
- package/dist/interfaces/index.d.ts.map +1 -1
- package/dist/interfaces/index.js +2 -0
- package/dist/interfaces/index.js.map +1 -1
- package/dist/internal/cli/commands/generate-message.d.ts +7 -0
- package/dist/internal/cli/commands/generate-message.d.ts.map +1 -0
- package/dist/internal/cli/commands/generate-message.js +77 -0
- package/dist/internal/cli/commands/generate-message.js.map +1 -0
- package/dist/internal/cli/commands/init.d.ts +8 -0
- package/dist/internal/cli/commands/init.d.ts.map +1 -0
- package/dist/internal/cli/commands/init.js +112 -0
- package/dist/internal/cli/commands/init.js.map +1 -0
- package/dist/internal/cli/commands/register-generate.d.ts +3 -0
- package/dist/internal/cli/commands/register-generate.d.ts.map +1 -0
- package/dist/internal/cli/commands/register-generate.js +20 -0
- package/dist/internal/cli/commands/register-generate.js.map +1 -0
- package/dist/internal/cli/commands/register-init.d.ts +3 -0
- package/dist/internal/cli/commands/register-init.d.ts.map +1 -0
- package/dist/internal/cli/commands/register-init.js +16 -0
- package/dist/internal/cli/commands/register-init.js.map +1 -0
- package/dist/internal/drivers/kafka/classes/KafkaDriver.d.ts +5 -3
- package/dist/internal/drivers/kafka/classes/KafkaDriver.d.ts.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaDriver.js +12 -16
- package/dist/internal/drivers/kafka/classes/KafkaDriver.js.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaMessageBus.d.ts.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaMessageBus.js.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaRpcServer.js.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaWorkerQueue.d.ts.map +1 -1
- package/dist/internal/drivers/kafka/classes/KafkaWorkerQueue.js.map +1 -1
- package/dist/internal/drivers/kafka/utils/create-kafka-consumer.js +1 -1
- package/dist/internal/drivers/kafka/utils/create-kafka-consumer.js.map +1 -1
- package/dist/internal/drivers/kafka/utils/stop-kafka-consumer.d.ts +3 -1
- package/dist/internal/drivers/kafka/utils/stop-kafka-consumer.d.ts.map +1 -1
- package/dist/internal/drivers/kafka/utils/stop-kafka-consumer.js +24 -5
- package/dist/internal/drivers/kafka/utils/stop-kafka-consumer.js.map +1 -1
- package/dist/internal/drivers/memory/classes/MemoryDriver.d.ts +5 -3
- package/dist/internal/drivers/memory/classes/MemoryDriver.d.ts.map +1 -1
- package/dist/internal/drivers/memory/classes/MemoryDriver.js +11 -14
- package/dist/internal/drivers/memory/classes/MemoryDriver.js.map +1 -1
- package/dist/internal/drivers/nats/classes/NatsDriver.d.ts +5 -3
- package/dist/internal/drivers/nats/classes/NatsDriver.d.ts.map +1 -1
- package/dist/internal/drivers/nats/classes/NatsDriver.js +12 -15
- package/dist/internal/drivers/nats/classes/NatsDriver.js.map +1 -1
- package/dist/internal/drivers/rabbit/classes/RabbitDriver.d.ts +5 -3
- package/dist/internal/drivers/rabbit/classes/RabbitDriver.d.ts.map +1 -1
- package/dist/internal/drivers/rabbit/classes/RabbitDriver.js +11 -14
- package/dist/internal/drivers/rabbit/classes/RabbitDriver.js.map +1 -1
- package/dist/internal/drivers/redis/classes/RedisDriver.d.ts +5 -3
- package/dist/internal/drivers/redis/classes/RedisDriver.d.ts.map +1 -1
- package/dist/internal/drivers/redis/classes/RedisDriver.js +11 -14
- package/dist/internal/drivers/redis/classes/RedisDriver.js.map +1 -1
- package/dist/internal/drivers/redis/classes/RedisRpcServer.js.map +1 -1
- package/dist/internal/drivers/redis/utils/create-consumer-loop.js +1 -1
- package/dist/internal/drivers/redis/utils/create-consumer-loop.js.map +1 -1
- package/dist/internal/drivers/redis/utils/stop-consumer-loop.js +2 -2
- package/dist/internal/drivers/redis/utils/stop-consumer-loop.js.map +1 -1
- package/dist/internal/message/utils/encrypt.d.ts +1 -1
- package/dist/internal/message/utils/encrypt.d.ts.map +1 -1
- package/dist/internal/message/utils/encrypt.js +4 -3
- package/dist/internal/message/utils/encrypt.js.map +1 -1
- package/dist/internal/message/utils/prepare-inbound.js +1 -1
- package/dist/internal/message/utils/prepare-inbound.js.map +1 -1
- package/dist/internal/types/index.d.ts +0 -1
- package/dist/internal/types/index.d.ts.map +1 -1
- package/dist/internal/types/index.js +0 -1
- package/dist/internal/types/index.js.map +1 -1
- package/dist/mocks/create-mock-iris-session.d.ts +4 -0
- package/dist/mocks/create-mock-iris-session.d.ts.map +1 -0
- package/dist/mocks/create-mock-iris-session.js +20 -0
- package/dist/mocks/create-mock-iris-session.js.map +1 -0
- package/dist/mocks/create-mock-iris-source.d.ts +2 -1
- package/dist/mocks/create-mock-iris-source.d.ts.map +1 -1
- package/dist/mocks/create-mock-iris-source.js +5 -2
- package/dist/mocks/create-mock-iris-source.js.map +1 -1
- package/dist/mocks/index.d.ts +2 -1
- package/dist/mocks/index.d.ts.map +1 -1
- package/dist/mocks/index.js +5 -1
- package/dist/mocks/index.js.map +1 -1
- package/dist/types/events.d.ts +5 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +3 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/source-options.d.ts +1 -1
- package/dist/types/source-options.d.ts.map +1 -1
- package/package.json +21 -16
- package/dist/internal/types/iris-source-init.d.ts +0 -29
- package/dist/internal/types/iris-source-init.d.ts.map +0 -1
- package/dist/internal/types/iris-source-init.js.map +0 -1
package/README.md
CHANGED
|
@@ -92,6 +92,73 @@ await source.drain();
|
|
|
92
92
|
await source.disconnect();
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
+
## Table of Contents
|
|
96
|
+
|
|
97
|
+
- [Messaging Patterns](#messaging-patterns)
|
|
98
|
+
- [Publisher (Fire-and-Forget)](#publisher-fire-and-forget)
|
|
99
|
+
- [Message Bus (Pub/Sub + Queues)](#message-bus-pubsub--queues)
|
|
100
|
+
- [Worker Queue (Competing Consumers)](#worker-queue-competing-consumers)
|
|
101
|
+
- [RPC (Request/Response)](#rpc-requestresponse)
|
|
102
|
+
- [Stream Processor (Pipelines)](#stream-processor-pipelines)
|
|
103
|
+
- [Field Types](#field-types)
|
|
104
|
+
- [Decorators](#decorators)
|
|
105
|
+
- [Class-Level Decorators](#class-level-decorators)
|
|
106
|
+
- [`@Message`](#message)
|
|
107
|
+
- [`@AbstractMessage`](#abstractmessage)
|
|
108
|
+
- [`@Namespace`](#namespace)
|
|
109
|
+
- [`@Version`](#version)
|
|
110
|
+
- [`@Topic`](#topic)
|
|
111
|
+
- [`@Broadcast`](#broadcast)
|
|
112
|
+
- [`@Persistent`](#persistent)
|
|
113
|
+
- [`@Priority`](#priority)
|
|
114
|
+
- [`@Delay`](#delay)
|
|
115
|
+
- [`@Expiry`](#expiry)
|
|
116
|
+
- [`@Encrypted`](#encrypted)
|
|
117
|
+
- [`@Compressed`](#compressed)
|
|
118
|
+
- [`@Retry`](#retry)
|
|
119
|
+
- [`@DeadLetter`](#deadletter)
|
|
120
|
+
- [Field Decorators](#field-decorators)
|
|
121
|
+
- [`@Field`](#field)
|
|
122
|
+
- [`@IdentifierField`](#identifierfield)
|
|
123
|
+
- [`@CorrelationField`](#correlationfield)
|
|
124
|
+
- [`@TimestampField`](#timestampfield)
|
|
125
|
+
- [`@MandatoryField`](#mandatoryfield)
|
|
126
|
+
- [`@PersistentField`](#persistentfield)
|
|
127
|
+
- [Field Modifiers](#field-modifiers)
|
|
128
|
+
- [`@Generated`](#generated)
|
|
129
|
+
- [`@Header`](#header)
|
|
130
|
+
- [`@Enum`](#enum)
|
|
131
|
+
- [`@Min` / `@Max`](#min--max)
|
|
132
|
+
- [`@Schema`](#schema)
|
|
133
|
+
- [`@Transform`](#transform)
|
|
134
|
+
- [Lifecycle Hook Decorators](#lifecycle-hook-decorators)
|
|
135
|
+
- [`@OnCreate`](#oncreate)
|
|
136
|
+
- [`@OnHydrate`](#onhydrate)
|
|
137
|
+
- [`@OnValidate`](#onvalidate)
|
|
138
|
+
- [`@BeforePublish` / `@AfterPublish`](#beforepublish--afterpublish)
|
|
139
|
+
- [`@BeforeConsume` / `@AfterConsume`](#beforeconsume--afterconsume)
|
|
140
|
+
- [`@OnConsumeError`](#onconsumeerror)
|
|
141
|
+
- [Retry and Dead Letter](#retry-and-dead-letter)
|
|
142
|
+
- [Dynamic Topics](#dynamic-topics)
|
|
143
|
+
- [Encryption and Compression](#encryption-and-compression)
|
|
144
|
+
- [Message Subscribers](#message-subscribers)
|
|
145
|
+
- [Hook Execution Order](#hook-execution-order)
|
|
146
|
+
- [Consume Envelope](#consume-envelope)
|
|
147
|
+
- [Message Manipulation](#message-manipulation)
|
|
148
|
+
- [Publish Options](#publish-options)
|
|
149
|
+
- [Zod Validation](#zod-validation)
|
|
150
|
+
- [Cloning](#cloning)
|
|
151
|
+
- [Driver Configuration](#driver-configuration)
|
|
152
|
+
- [Memory](#memory)
|
|
153
|
+
- [RabbitMQ](#rabbitmq)
|
|
154
|
+
- [Kafka](#kafka)
|
|
155
|
+
- [NATS](#nats)
|
|
156
|
+
- [Redis Streams](#redis-streams)
|
|
157
|
+
- [Persistence (Delay and Dead Letter Stores)](#persistence-delay-and-dead-letter-stores)
|
|
158
|
+
- [Connection State](#connection-state)
|
|
159
|
+
- [Testing with Mocks](#testing-with-mocks)
|
|
160
|
+
- [Error Classes](#error-classes)
|
|
161
|
+
|
|
95
162
|
## Messaging Patterns
|
|
96
163
|
|
|
97
164
|
### Publisher (Fire-and-Forget)
|
|
@@ -190,141 +257,794 @@ await server.serve(async (req) => {
|
|
|
190
257
|
return res;
|
|
191
258
|
});
|
|
192
259
|
|
|
193
|
-
// Client: send request
|
|
194
|
-
const req = new GetPrice();
|
|
195
|
-
req.sku = "WIDGET-42";
|
|
260
|
+
// Client: send request
|
|
261
|
+
const req = new GetPrice();
|
|
262
|
+
req.sku = "WIDGET-42";
|
|
263
|
+
|
|
264
|
+
const res = await client.request(req);
|
|
265
|
+
console.log(`${res.price} ${res.currency}`); // 29.99 USD
|
|
266
|
+
|
|
267
|
+
// With timeout
|
|
268
|
+
const res2 = await client.request(req, { timeout: 5000 });
|
|
269
|
+
|
|
270
|
+
// Clean up
|
|
271
|
+
await client.close();
|
|
272
|
+
await server.unserveAll();
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Stream Processor (Pipelines)
|
|
276
|
+
|
|
277
|
+
Declarative stream processing with an immutable builder pattern.
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
@Message()
|
|
281
|
+
class RawEvent {
|
|
282
|
+
@Field("string") type!: string;
|
|
283
|
+
@Field("float") value!: number;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
@Message()
|
|
287
|
+
class AggregatedEvent {
|
|
288
|
+
@Field("float") sum!: number;
|
|
289
|
+
@Field("integer") count!: number;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const pipeline = source
|
|
293
|
+
.stream()
|
|
294
|
+
.from(RawEvent)
|
|
295
|
+
.filter((msg) => msg.value > 0)
|
|
296
|
+
.map((msg) => {
|
|
297
|
+
const out = new AggregatedEvent();
|
|
298
|
+
out.sum = msg.value;
|
|
299
|
+
out.count = 1;
|
|
300
|
+
return out;
|
|
301
|
+
})
|
|
302
|
+
.to(AggregatedEvent);
|
|
303
|
+
|
|
304
|
+
await pipeline.start();
|
|
305
|
+
// pipeline.isRunning() === true
|
|
306
|
+
|
|
307
|
+
await pipeline.pause();
|
|
308
|
+
await pipeline.resume();
|
|
309
|
+
await pipeline.stop();
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Field Types
|
|
313
|
+
|
|
314
|
+
The `@Field()` decorator accepts the following type identifiers:
|
|
315
|
+
|
|
316
|
+
| Category | Types |
|
|
317
|
+
| -------------- | -------------------------------- |
|
|
318
|
+
| Boolean | `boolean` |
|
|
319
|
+
| Integer | `integer`, `bigint` |
|
|
320
|
+
| Floating Point | `float` |
|
|
321
|
+
| String | `string`, `email`, `url`, `uuid` |
|
|
322
|
+
| Enum | `enum` |
|
|
323
|
+
| Date/Time | `date` |
|
|
324
|
+
| Structured | `object`, `array` |
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
@Message()
|
|
328
|
+
class FullExample {
|
|
329
|
+
@IdentifierField() id!: string;
|
|
330
|
+
@CorrelationField() correlationId!: string;
|
|
331
|
+
@TimestampField() createdAt!: Date;
|
|
332
|
+
|
|
333
|
+
@Field("string") name!: string;
|
|
334
|
+
@Field("integer") count!: number;
|
|
335
|
+
@Field("float") price!: number;
|
|
336
|
+
@Field("boolean") active!: boolean;
|
|
337
|
+
@Field("date") expiresAt!: Date;
|
|
338
|
+
@Field("uuid") referenceId!: string;
|
|
339
|
+
@Field("email") contactEmail!: string;
|
|
340
|
+
@Field("url") callbackUrl!: string;
|
|
341
|
+
@Field("array") tags!: Array<string>;
|
|
342
|
+
@Field("object") metadata!: Record<string, unknown>;
|
|
343
|
+
|
|
344
|
+
@Field("string", { nullable: true }) description!: string | null;
|
|
345
|
+
@Field("string", { optional: true }) nickname?: string;
|
|
346
|
+
@Field("integer", { default: 0 }) retryCount!: number;
|
|
347
|
+
@Field("string", { default: () => "generated" }) code!: string;
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
## Decorators
|
|
352
|
+
|
|
353
|
+
All decorators use the TC39 (stage 3) decorator specification. Class decorators receive `ClassDecoratorContext`, field decorators receive `ClassFieldDecoratorContext`. Metadata flows through the `Symbol.metadata` prototype chain, so abstract base class decorators are inherited by concrete subclasses.
|
|
354
|
+
|
|
355
|
+
### Class-Level Decorators
|
|
356
|
+
|
|
357
|
+
These decorators are applied to classes and configure message-wide behavior.
|
|
358
|
+
|
|
359
|
+
#### `@Message`
|
|
360
|
+
|
|
361
|
+
Marks a class as a concrete message type registered in the global message registry. Every message class must have exactly one of `@Message` or `@AbstractMessage`.
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
@Message()
|
|
365
|
+
class OrderPlaced {
|
|
366
|
+
/* ... */
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
@Message({ name: "order-placed" }) // custom message name
|
|
370
|
+
class OrderPlaced {
|
|
371
|
+
/* ... */
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
**Options:** `{ name?: string }` — Override the message name. Defaults to the class name with any trailing version suffix (`_v1`, `_V2`) stripped. Must not conflict with other registered message names.
|
|
376
|
+
|
|
377
|
+
#### `@AbstractMessage`
|
|
378
|
+
|
|
379
|
+
Marks a class as an abstract message base. It is **not** registered in the global message registry. Fields, hooks, and metadata are inherited by `@Message()` subclasses via the `Symbol.metadata` prototype chain.
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
@AbstractMessage()
|
|
383
|
+
class BaseEvent {
|
|
384
|
+
@IdentifierField() id!: string;
|
|
385
|
+
@TimestampField() createdAt!: Date;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
@Message()
|
|
389
|
+
@Namespace("orders")
|
|
390
|
+
class OrderPlaced extends BaseEvent {
|
|
391
|
+
@Field("string") orderId!: string;
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Cannot be combined with `@Message` on the same class.
|
|
396
|
+
|
|
397
|
+
#### `@Namespace`
|
|
398
|
+
|
|
399
|
+
Places the message in a named namespace for logical grouping and routing.
|
|
400
|
+
|
|
401
|
+
```typescript
|
|
402
|
+
@Namespace("orders")
|
|
403
|
+
@Message()
|
|
404
|
+
class OrderPlaced {
|
|
405
|
+
/* ... */
|
|
406
|
+
}
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
**Argument:** `string` — must be non-empty. Throws `IrisMetadataError` if empty or whitespace-only.
|
|
410
|
+
|
|
411
|
+
#### `@Version`
|
|
412
|
+
|
|
413
|
+
Sets the message schema version. Useful for evolving message formats while maintaining backward compatibility.
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
@Version(1)
|
|
417
|
+
@Message()
|
|
418
|
+
class OrderPlaced {
|
|
419
|
+
/* ... */
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
**Argument:** `number` — must be a positive integer (>= 1). Throws `IrisMetadataError` otherwise.
|
|
424
|
+
|
|
425
|
+
#### `@Topic`
|
|
426
|
+
|
|
427
|
+
Provides a dynamic topic resolution callback. Instead of using the message class name as the topic, the callback computes the topic from the message content at publish time.
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
@Topic((msg: any) => `events.${msg.region}.${msg.type}`)
|
|
431
|
+
@Message()
|
|
432
|
+
class RegionalEvent {
|
|
433
|
+
@Field("string") region!: string;
|
|
434
|
+
@Field("string") type!: string;
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
**Argument:** `(message: any) => string` — receives the message instance, returns the routing topic string.
|
|
439
|
+
|
|
440
|
+
#### `@Broadcast`
|
|
441
|
+
|
|
442
|
+
Marks a message for broadcast delivery. When published, the message is delivered to **all** subscribers rather than being distributed round-robin to one consumer per queue.
|
|
443
|
+
|
|
444
|
+
```typescript
|
|
445
|
+
@Broadcast()
|
|
446
|
+
@Message()
|
|
447
|
+
class SystemNotification {
|
|
448
|
+
@Field("string") text!: string;
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
No arguments.
|
|
453
|
+
|
|
454
|
+
#### `@Persistent`
|
|
455
|
+
|
|
456
|
+
Marks a message as persistent/durable. Persistent messages survive broker restarts (where supported by the driver).
|
|
457
|
+
|
|
458
|
+
```typescript
|
|
459
|
+
@Persistent()
|
|
460
|
+
@Message()
|
|
461
|
+
class PaymentCharge {
|
|
462
|
+
@Field("string") chargeId!: string;
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
No arguments.
|
|
467
|
+
|
|
468
|
+
#### `@Priority`
|
|
469
|
+
|
|
470
|
+
Sets the default priority for a message type. Higher priority messages are delivered before lower priority ones (where supported by the driver).
|
|
471
|
+
|
|
472
|
+
```typescript
|
|
473
|
+
@Priority(8)
|
|
474
|
+
@Message()
|
|
475
|
+
class UrgentAlert {
|
|
476
|
+
@Field("string") text!: string;
|
|
477
|
+
}
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
**Argument:** `number` — integer between 0 and 10 inclusive. Throws `IrisMetadataError` if out of range or not an integer.
|
|
481
|
+
|
|
482
|
+
#### `@Delay`
|
|
483
|
+
|
|
484
|
+
Sets a default delivery delay. The message is held for the specified duration before being delivered to consumers.
|
|
485
|
+
|
|
486
|
+
```typescript
|
|
487
|
+
@Delay(5000) // 5 seconds
|
|
488
|
+
@Message()
|
|
489
|
+
class ScheduledReminder {
|
|
490
|
+
@Field("string") text!: string;
|
|
491
|
+
}
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
**Argument:** `number` — non-negative integer in milliseconds. Throws `IrisMetadataError` if negative or not an integer.
|
|
495
|
+
|
|
496
|
+
Can be overridden per-publish via `PublishOptions.delay`.
|
|
497
|
+
|
|
498
|
+
#### `@Expiry`
|
|
499
|
+
|
|
500
|
+
Sets a default message TTL (time-to-live). Messages that are not consumed within this window are discarded.
|
|
501
|
+
|
|
502
|
+
```typescript
|
|
503
|
+
@Expiry(60000) // 1 minute
|
|
504
|
+
@Message()
|
|
505
|
+
class TemporaryOffer {
|
|
506
|
+
@Field("float") discount!: number;
|
|
507
|
+
}
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
**Argument:** `number` — non-negative integer in milliseconds. Throws `IrisMetadataError` if negative or not an integer.
|
|
511
|
+
|
|
512
|
+
Can be overridden per-publish via `PublishOptions.expiry`.
|
|
513
|
+
|
|
514
|
+
#### `@Encrypted`
|
|
515
|
+
|
|
516
|
+
Enables payload encryption via `@lindorm/amphora`. The entire message payload is encrypted before publishing and decrypted on consume. Requires an `IAmphora` instance configured on `IrisSource`.
|
|
517
|
+
|
|
518
|
+
```typescript
|
|
519
|
+
@Encrypted() // encrypt with any available key
|
|
520
|
+
@Message()
|
|
521
|
+
class SensitivePayload {
|
|
522
|
+
@Field("string") ssn!: string;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
@Encrypted({ purpose: "pii" }) // filter keys by purpose
|
|
526
|
+
@Message()
|
|
527
|
+
class MedicalRecord {
|
|
528
|
+
@Field("json") data!: Record<string, unknown>;
|
|
529
|
+
}
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
**Argument:** `AmphoraPredicate` (optional, defaults to `{}`) — a predicate object to filter which encryption key to use from the key store. Supports fields like `algorithm`, `encryption`, `purpose`, `type`, `ownerId`, and standard predicate operators (`$eq`, `$in`, `$neq`, etc.).
|
|
533
|
+
|
|
534
|
+
#### `@Compressed`
|
|
535
|
+
|
|
536
|
+
Enables payload compression before publishing and decompression on consume.
|
|
537
|
+
|
|
538
|
+
```typescript
|
|
539
|
+
@Compressed() // gzip (default)
|
|
540
|
+
@Compressed("brotli") // brotli compression
|
|
541
|
+
@Message()
|
|
542
|
+
class LargePayload {
|
|
543
|
+
@Field("object") data!: Record<string, unknown>;
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
**Argument:** `"gzip" | "deflate" | "brotli"` (optional, defaults to `"gzip"`).
|
|
548
|
+
|
|
549
|
+
When combined with `@Encrypted`, compression is applied first, then encryption.
|
|
550
|
+
|
|
551
|
+
#### `@Retry`
|
|
552
|
+
|
|
553
|
+
Configures automatic retry behavior when a consume callback throws. Failed messages are retried with configurable backoff strategies.
|
|
554
|
+
|
|
555
|
+
```typescript
|
|
556
|
+
@Retry() // defaults: 3 retries, constant 1s delay
|
|
557
|
+
@Message()
|
|
558
|
+
class ProcessOrder {
|
|
559
|
+
@Field("string") orderId!: string;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
@Retry({
|
|
563
|
+
maxRetries: 5,
|
|
564
|
+
strategy: "exponential",
|
|
565
|
+
delay: 1000,
|
|
566
|
+
delayMax: 30000,
|
|
567
|
+
multiplier: 2,
|
|
568
|
+
jitter: true,
|
|
569
|
+
})
|
|
570
|
+
@Message()
|
|
571
|
+
class PaymentCharge {
|
|
572
|
+
@Field("string") chargeId!: string;
|
|
573
|
+
}
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
**Options:**
|
|
577
|
+
|
|
578
|
+
| Field | Type | Default | Description |
|
|
579
|
+
| ------------ | --------------------------------------------- | ------------ | ----------------------------------------- |
|
|
580
|
+
| `maxRetries` | `number` | `3` | Maximum number of retry attempts |
|
|
581
|
+
| `strategy` | `"constant"` \| `"linear"` \| `"exponential"` | `"constant"` | Backoff strategy |
|
|
582
|
+
| `delay` | `number` | `1000` | Initial delay in milliseconds |
|
|
583
|
+
| `delayMax` | `number` | `30000` | Maximum delay cap in milliseconds |
|
|
584
|
+
| `multiplier` | `number` | `2` | Multiplier for exponential backoff |
|
|
585
|
+
| `jitter` | `boolean` | `false` | Add randomness to prevent thundering herd |
|
|
586
|
+
|
|
587
|
+
**Retry strategies:**
|
|
588
|
+
|
|
589
|
+
| Strategy | Delay pattern (base=1000, multiplier=2) |
|
|
590
|
+
| --------------- | ------------------------------------------- |
|
|
591
|
+
| `"constant"` | 1000, 1000, 1000, ... |
|
|
592
|
+
| `"linear"` | 1000, 2000, 3000, ... |
|
|
593
|
+
| `"exponential"` | 1000, 2000, 4000, 8000, ... (capped at max) |
|
|
594
|
+
|
|
595
|
+
#### `@DeadLetter`
|
|
596
|
+
|
|
597
|
+
Routes messages that have exhausted all retry attempts to the dead letter store. Requires `@Retry` and a dead letter store configured via `IrisSource.persistence.deadLetter`.
|
|
598
|
+
|
|
599
|
+
```typescript
|
|
600
|
+
@Retry({ maxRetries: 3 })
|
|
601
|
+
@DeadLetter()
|
|
602
|
+
@Message()
|
|
603
|
+
class PaymentCharge {
|
|
604
|
+
@Field("string") chargeId!: string;
|
|
605
|
+
}
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
No arguments.
|
|
609
|
+
|
|
610
|
+
---
|
|
611
|
+
|
|
612
|
+
### Field Decorators
|
|
613
|
+
|
|
614
|
+
These decorators are applied to class properties and declare message fields. Each field decorator creates a complete field definition with its type, default value, and nullability.
|
|
615
|
+
|
|
616
|
+
#### `@Field`
|
|
617
|
+
|
|
618
|
+
The foundational field decorator. Declares a message field with an explicit type.
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
@Field("string")
|
|
622
|
+
name!: string;
|
|
623
|
+
|
|
624
|
+
@Field("integer")
|
|
625
|
+
count!: number;
|
|
626
|
+
|
|
627
|
+
@Field("float")
|
|
628
|
+
price!: number;
|
|
629
|
+
|
|
630
|
+
@Field("boolean")
|
|
631
|
+
active!: boolean;
|
|
632
|
+
|
|
633
|
+
@Field("date")
|
|
634
|
+
expiresAt!: Date;
|
|
635
|
+
|
|
636
|
+
@Field("uuid")
|
|
637
|
+
referenceId!: string;
|
|
638
|
+
|
|
639
|
+
@Field("email")
|
|
640
|
+
contactEmail!: string;
|
|
641
|
+
|
|
642
|
+
@Field("url")
|
|
643
|
+
callbackUrl!: string;
|
|
644
|
+
|
|
645
|
+
@Field("array")
|
|
646
|
+
tags!: Array<string>;
|
|
647
|
+
|
|
648
|
+
@Field("object")
|
|
649
|
+
metadata!: Record<string, unknown>;
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
**Arguments:** `(type: MetaFieldType, options?: FieldDecoratorOptions)`.
|
|
653
|
+
|
|
654
|
+
**Options:**
|
|
655
|
+
|
|
656
|
+
| Field | Type | Default | Description |
|
|
657
|
+
| ----------- | -------------------------------- | ------- | ----------------------------------------- |
|
|
658
|
+
| `nullable` | `boolean` | `false` | Allow `null` values |
|
|
659
|
+
| `optional` | `boolean` | `false` | Field may be omitted |
|
|
660
|
+
| `default` | `value \| (() => value) \| null` | `null` | Default value applied on create |
|
|
661
|
+
| `transform` | `MetaTransform \| null` | `null` | Inline serialisation/deserialisation pair |
|
|
662
|
+
|
|
663
|
+
```typescript
|
|
664
|
+
@Field("string", { nullable: true })
|
|
665
|
+
description!: string | null;
|
|
666
|
+
|
|
667
|
+
@Field("string", { optional: true })
|
|
668
|
+
nickname?: string;
|
|
669
|
+
|
|
670
|
+
@Field("integer", { default: 0 })
|
|
671
|
+
retryCount!: number;
|
|
672
|
+
|
|
673
|
+
@Field("string", { default: () => "generated" })
|
|
674
|
+
code!: string;
|
|
675
|
+
|
|
676
|
+
@Field("float", {
|
|
677
|
+
transform: {
|
|
678
|
+
to: (value: number) => Math.round(value * 100),
|
|
679
|
+
from: (raw: number) => raw / 100,
|
|
680
|
+
},
|
|
681
|
+
})
|
|
682
|
+
price!: number;
|
|
683
|
+
```
|
|
684
|
+
|
|
685
|
+
#### `@IdentifierField`
|
|
686
|
+
|
|
687
|
+
Shorthand for a UUID primary identifier field. Auto-generates a UUID v4 on message creation.
|
|
688
|
+
|
|
689
|
+
```typescript
|
|
690
|
+
@IdentifierField()
|
|
691
|
+
id!: string;
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
Equivalent to `@Field("uuid", { default: () => randomUUID() })`. Non-nullable, non-optional.
|
|
695
|
+
|
|
696
|
+
No arguments.
|
|
697
|
+
|
|
698
|
+
#### `@CorrelationField`
|
|
699
|
+
|
|
700
|
+
Shorthand for a UUID correlation tracking field. Auto-generates a UUID v4 on message creation. Used to trace related messages across publish/consume chains.
|
|
701
|
+
|
|
702
|
+
```typescript
|
|
703
|
+
@CorrelationField()
|
|
704
|
+
correlationId!: string;
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
Equivalent to `@Field("uuid", { default: () => randomUUID() })`. Non-nullable, non-optional.
|
|
708
|
+
|
|
709
|
+
No arguments.
|
|
710
|
+
|
|
711
|
+
#### `@TimestampField`
|
|
712
|
+
|
|
713
|
+
Shorthand for a timestamp field. Auto-generates the current `Date` on message creation.
|
|
714
|
+
|
|
715
|
+
```typescript
|
|
716
|
+
@TimestampField()
|
|
717
|
+
createdAt!: Date;
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
Equivalent to `@Field("date", { default: () => new Date() })`. Non-nullable, non-optional.
|
|
721
|
+
|
|
722
|
+
No arguments.
|
|
723
|
+
|
|
724
|
+
#### `@MandatoryField`
|
|
725
|
+
|
|
726
|
+
Shorthand for a boolean flag that defaults to `false`. Commonly used for acknowledgement or processing flags.
|
|
727
|
+
|
|
728
|
+
```typescript
|
|
729
|
+
@MandatoryField()
|
|
730
|
+
requiresApproval!: boolean;
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
Equivalent to `@Field("boolean", { default: false })`. Non-nullable, non-optional.
|
|
734
|
+
|
|
735
|
+
No arguments.
|
|
736
|
+
|
|
737
|
+
#### `@PersistentField`
|
|
738
|
+
|
|
739
|
+
Shorthand for a boolean persistence flag that defaults to `false`. Commonly used to mark whether a message should be durably stored.
|
|
740
|
+
|
|
741
|
+
```typescript
|
|
742
|
+
@PersistentField()
|
|
743
|
+
shouldPersist!: boolean;
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
Equivalent to `@Field("boolean", { default: false })`. Non-nullable, non-optional.
|
|
747
|
+
|
|
748
|
+
No arguments.
|
|
749
|
+
|
|
750
|
+
---
|
|
751
|
+
|
|
752
|
+
### Field Modifiers
|
|
753
|
+
|
|
754
|
+
These decorators modify the behavior of a field declared with `@Field` or one of the shorthand field decorators. Stack them on the same property. Modifier decorators must appear alongside a field decorator on the same property.
|
|
755
|
+
|
|
756
|
+
#### `@Generated`
|
|
757
|
+
|
|
758
|
+
Marks a field for automatic value generation on message creation.
|
|
759
|
+
|
|
760
|
+
```typescript
|
|
761
|
+
@Generated("uuid") // UUID v4
|
|
762
|
+
@Field("uuid")
|
|
763
|
+
traceId!: string;
|
|
764
|
+
|
|
765
|
+
@Generated("date") // current timestamp
|
|
766
|
+
@Field("date")
|
|
767
|
+
processedAt!: Date;
|
|
768
|
+
|
|
769
|
+
@Generated("string") // random string (default length)
|
|
770
|
+
@Field("string")
|
|
771
|
+
token!: string;
|
|
772
|
+
|
|
773
|
+
@Generated("string", { length: 12 }) // random string with custom length
|
|
774
|
+
@Field("string")
|
|
775
|
+
shortCode!: string;
|
|
776
|
+
|
|
777
|
+
@Generated("integer", { min: 1, max: 1000 })
|
|
778
|
+
@Field("integer")
|
|
779
|
+
sequenceNumber!: number;
|
|
780
|
+
|
|
781
|
+
@Generated("float", { min: 0.0, max: 1.0 })
|
|
782
|
+
@Field("float")
|
|
783
|
+
weight!: number;
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
**Arguments:** `(strategy: MetaGeneratedStrategy, options?: GeneratedDecoratorOptions)`.
|
|
787
|
+
|
|
788
|
+
**Strategies:**
|
|
789
|
+
|
|
790
|
+
| Strategy | Description |
|
|
791
|
+
| ----------- | ---------------------------------------- |
|
|
792
|
+
| `"uuid"` | Generate UUID v4 |
|
|
793
|
+
| `"date"` | Current timestamp |
|
|
794
|
+
| `"string"` | Random string with configurable `length` |
|
|
795
|
+
| `"integer"` | Random integer in `[min, max]` range |
|
|
796
|
+
| `"float"` | Random float in `[min, max]` range |
|
|
797
|
+
|
|
798
|
+
**Options:** `{ length?: number, min?: number, max?: number }` — all optional, all default to `null`.
|
|
799
|
+
|
|
800
|
+
#### `@Header`
|
|
801
|
+
|
|
802
|
+
Promotes a field value to a message header. Headers are transported as key-value metadata alongside the payload, accessible without deserialising the full message body.
|
|
803
|
+
|
|
804
|
+
```typescript
|
|
805
|
+
@Header() // header name = property name ("source")
|
|
806
|
+
@Field("string")
|
|
807
|
+
source!: string;
|
|
808
|
+
|
|
809
|
+
@Header("x-trace-id") // explicit header name
|
|
810
|
+
@Field("uuid")
|
|
811
|
+
traceId!: string;
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
**Argument:** `string?` — custom header name. Defaults to the property name. Throws `IrisMetadataError` if the resolved name is empty or whitespace-only.
|
|
815
|
+
|
|
816
|
+
#### `@Enum`
|
|
817
|
+
|
|
818
|
+
Restricts a field to a fixed set of allowed values. Pass a TypeScript enum or a plain `Record<string, string | number>`. Enforced during Zod validation.
|
|
819
|
+
|
|
820
|
+
```typescript
|
|
821
|
+
enum OrderStatus {
|
|
822
|
+
Pending = "pending",
|
|
823
|
+
Shipped = "shipped",
|
|
824
|
+
Delivered = "delivered",
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
@Enum(OrderStatus)
|
|
828
|
+
@Field("enum")
|
|
829
|
+
status!: OrderStatus;
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
**Argument:** `Record<string, string | number>` — the enum object or value map.
|
|
833
|
+
|
|
834
|
+
#### `@Min` / `@Max`
|
|
835
|
+
|
|
836
|
+
Set minimum/maximum bounds for numeric fields or minimum/maximum length for string fields. Enforced during Zod validation.
|
|
837
|
+
|
|
838
|
+
```typescript
|
|
839
|
+
@Min(0)
|
|
840
|
+
@Max(100)
|
|
841
|
+
@Field("integer")
|
|
842
|
+
score!: number;
|
|
843
|
+
|
|
844
|
+
@Min(1)
|
|
845
|
+
@Max(255)
|
|
846
|
+
@Field("string")
|
|
847
|
+
name!: string;
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
**Argument:** `number`.
|
|
851
|
+
|
|
852
|
+
#### `@Schema`
|
|
853
|
+
|
|
854
|
+
Attaches a Zod schema for fine-grained field validation. The schema is evaluated during message validation.
|
|
855
|
+
|
|
856
|
+
```typescript
|
|
857
|
+
import { z } from "zod";
|
|
858
|
+
|
|
859
|
+
@Schema(z.string().email())
|
|
860
|
+
@Field("email")
|
|
861
|
+
email!: string;
|
|
862
|
+
|
|
863
|
+
@Schema(z.number().int().min(13).max(150))
|
|
864
|
+
@Field("integer")
|
|
865
|
+
age!: number;
|
|
866
|
+
|
|
867
|
+
@Schema(z.string().regex(/^[A-Z]{2,3}$/))
|
|
868
|
+
@Field("string")
|
|
869
|
+
countryCode!: string;
|
|
870
|
+
```
|
|
871
|
+
|
|
872
|
+
**Argument:** `z.ZodType` — any Zod schema.
|
|
873
|
+
|
|
874
|
+
#### `@Transform`
|
|
875
|
+
|
|
876
|
+
Applies a bidirectional transform to a field value. `to` runs during serialisation (message -> transport), `from` runs during deserialisation (transport -> message).
|
|
877
|
+
|
|
878
|
+
```typescript
|
|
879
|
+
@Transform({
|
|
880
|
+
to: (value: string[]) => value.join(","),
|
|
881
|
+
from: (raw: string) => raw.split(","),
|
|
882
|
+
})
|
|
883
|
+
@Field("string")
|
|
884
|
+
tags!: string[];
|
|
885
|
+
|
|
886
|
+
@Transform<Date, number>({
|
|
887
|
+
to: (date) => date.getTime(),
|
|
888
|
+
from: (ms) => new Date(ms),
|
|
889
|
+
})
|
|
890
|
+
@Field("bigint")
|
|
891
|
+
timestamp!: Date;
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
**Options:** `{ to: (value: TFrom) => TTo, from: (raw: TTo) => TFrom }`.
|
|
895
|
+
|
|
896
|
+
This is a standalone decorator that uses a separate staging path. For inline transforms, use the `transform` option on `@Field` instead.
|
|
897
|
+
|
|
898
|
+
---
|
|
899
|
+
|
|
900
|
+
### Lifecycle Hook Decorators
|
|
196
901
|
|
|
197
|
-
|
|
198
|
-
console.log(`${res.price} ${res.currency}`); // 29.99 USD
|
|
902
|
+
Lifecycle hooks are **class decorators** that register callbacks at specific points in the message lifecycle. All hooks receive `(message, context?)` as arguments, where `context` is the source's context value.
|
|
199
903
|
|
|
200
|
-
|
|
201
|
-
const res2 = await client.request(req, { timeout: 5000 });
|
|
904
|
+
Hooks may be async (`void | Promise<void>`) unless otherwise noted.
|
|
202
905
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
906
|
+
#### `@OnCreate`
|
|
907
|
+
|
|
908
|
+
Fires when a message instance is created via `create()`. Useful for setting computed defaults or derived fields.
|
|
909
|
+
|
|
910
|
+
```typescript
|
|
911
|
+
@OnCreate((msg) => {
|
|
912
|
+
msg.slug = msg.name.toLowerCase().replace(/\s+/g, "-");
|
|
913
|
+
})
|
|
914
|
+
@Message()
|
|
915
|
+
class OrderPlaced {
|
|
916
|
+
@Field("string") name!: string;
|
|
917
|
+
@Field("string") slug!: string;
|
|
918
|
+
}
|
|
206
919
|
```
|
|
207
920
|
|
|
208
|
-
|
|
921
|
+
**Argument:** `(message: M, context?: C) => void | Promise<void>`.
|
|
209
922
|
|
|
210
|
-
|
|
923
|
+
#### `@OnHydrate`
|
|
924
|
+
|
|
925
|
+
Fires when a message is rehydrated from raw transport data, after all fields are populated but before the message is returned to the consume callback.
|
|
211
926
|
|
|
212
927
|
```typescript
|
|
928
|
+
@OnHydrate((msg) => {
|
|
929
|
+
msg.displayName = `${msg.firstName} ${msg.lastName}`;
|
|
930
|
+
})
|
|
213
931
|
@Message()
|
|
214
|
-
class
|
|
215
|
-
@Field("string")
|
|
216
|
-
@Field("
|
|
932
|
+
class UserEvent {
|
|
933
|
+
@Field("string") firstName!: string;
|
|
934
|
+
@Field("string") lastName!: string;
|
|
935
|
+
@Field("string") displayName!: string;
|
|
217
936
|
}
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
**Argument:** `(message: M, context?: C) => void | Promise<void>`.
|
|
940
|
+
|
|
941
|
+
#### `@OnValidate`
|
|
218
942
|
|
|
943
|
+
Fires during message validation, after the built-in Zod schema check. Throw to reject the message.
|
|
944
|
+
|
|
945
|
+
```typescript
|
|
946
|
+
@OnValidate((msg) => {
|
|
947
|
+
if (msg.startDate >= msg.endDate) {
|
|
948
|
+
throw new Error("startDate must be before endDate");
|
|
949
|
+
}
|
|
950
|
+
})
|
|
219
951
|
@Message()
|
|
220
|
-
class
|
|
221
|
-
@Field("
|
|
222
|
-
@Field("
|
|
952
|
+
class BookingRequest {
|
|
953
|
+
@Field("date") startDate!: Date;
|
|
954
|
+
@Field("date") endDate!: Date;
|
|
223
955
|
}
|
|
956
|
+
```
|
|
224
957
|
|
|
225
|
-
|
|
226
|
-
.stream()
|
|
227
|
-
.from(RawEvent)
|
|
228
|
-
.filter((msg) => msg.value > 0)
|
|
229
|
-
.map((msg) => {
|
|
230
|
-
const out = new AggregatedEvent();
|
|
231
|
-
out.sum = msg.value;
|
|
232
|
-
out.count = 1;
|
|
233
|
-
return out;
|
|
234
|
-
})
|
|
235
|
-
.to(AggregatedEvent);
|
|
958
|
+
**Argument:** `(message: M, context?: C) => void | Promise<void>`.
|
|
236
959
|
|
|
237
|
-
|
|
238
|
-
// pipeline.isRunning() === true
|
|
960
|
+
#### `@BeforePublish` / `@AfterPublish`
|
|
239
961
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
962
|
+
Fire around publish operations. `@BeforePublish` runs before the message is handed to the transport. `@AfterPublish` runs after the transport confirms delivery.
|
|
963
|
+
|
|
964
|
+
```typescript
|
|
965
|
+
@BeforePublish(async (msg) => {
|
|
966
|
+
await validateExternalId(msg.externalId);
|
|
967
|
+
})
|
|
968
|
+
@AfterPublish(async (msg) => {
|
|
969
|
+
metrics.increment("messages.published");
|
|
970
|
+
})
|
|
971
|
+
@Message()
|
|
972
|
+
class OrderPlaced {
|
|
973
|
+
@Field("string") externalId!: string;
|
|
974
|
+
}
|
|
243
975
|
```
|
|
244
976
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
### Class-Level
|
|
248
|
-
|
|
249
|
-
| Decorator | Description |
|
|
250
|
-
| -------------------- | ------------------------------------------------------ |
|
|
251
|
-
| `@Message(opts?)` | Mark class as a message type |
|
|
252
|
-
| `@AbstractMessage()` | Mark as abstract (non-concrete base) |
|
|
253
|
-
| `@Namespace(ns)` | Set message namespace |
|
|
254
|
-
| `@Version(n)` | Set message version (positive integer) |
|
|
255
|
-
| `@Topic(fn)` | Dynamic topic resolution callback |
|
|
256
|
-
| `@Broadcast()` | Deliver to all subscribers (not just one per queue) |
|
|
257
|
-
| `@Persistent()` | Mark message as persistent/durable |
|
|
258
|
-
| `@Priority(n)` | Set priority (integer 0-10) |
|
|
259
|
-
| `@Delay(ms)` | Default delivery delay in milliseconds |
|
|
260
|
-
| `@Expiry(ms)` | Message expiration in milliseconds |
|
|
261
|
-
| `@Encrypted(pred?)` | Enable payload encryption via `@lindorm/amphora` |
|
|
262
|
-
| `@Compressed(alg?)` | Enable compression (`"gzip"`, `"deflate"`, `"brotli"`) |
|
|
263
|
-
| `@Retry(opts?)` | Configure retry behaviour on consume failure |
|
|
264
|
-
| `@DeadLetter()` | Route failed messages to dead letter store |
|
|
265
|
-
|
|
266
|
-
### Field-Level
|
|
267
|
-
|
|
268
|
-
| Decorator | Description |
|
|
269
|
-
| ---------------------- | ---------------------------------------------------------------------------- |
|
|
270
|
-
| `@Field(type, opts?)` | Declare field with type and options |
|
|
271
|
-
| `@IdentifierField()` | Auto-generated UUID field |
|
|
272
|
-
| `@CorrelationField()` | Auto-generated UUID for correlation tracking |
|
|
273
|
-
| `@TimestampField()` | Auto-generated Date field |
|
|
274
|
-
| `@MandatoryField()` | Boolean field, defaults to `false` |
|
|
275
|
-
| `@PersistentField()` | Boolean persistence flag, defaults to `false` |
|
|
276
|
-
| `@Generated(strategy)` | Auto-generate value (`"uuid"`, `"date"`, `"string"`, `"integer"`, `"float"`) |
|
|
277
|
-
| `@Header(name?)` | Promote field to message header |
|
|
278
|
-
| `@Enum(values)` | Restrict to enum values |
|
|
279
|
-
| `@Min(n)` | Minimum value constraint |
|
|
280
|
-
| `@Max(n)` | Maximum value constraint |
|
|
281
|
-
| `@Schema(zodType)` | Zod schema validation |
|
|
282
|
-
| `@Transform(opts)` | Custom serialisation/deserialisation transform |
|
|
283
|
-
|
|
284
|
-
### Lifecycle Hooks
|
|
285
|
-
|
|
286
|
-
| Decorator | Description |
|
|
287
|
-
| --------------------- | ----------------------------------------------- |
|
|
288
|
-
| `@OnCreate(fn)` | Called when message instance is created |
|
|
289
|
-
| `@OnHydrate(fn)` | Called when message is rehydrated from raw data |
|
|
290
|
-
| `@OnValidate(fn)` | Called when message is validated |
|
|
291
|
-
| `@BeforePublish(fn)` | Called before publishing |
|
|
292
|
-
| `@AfterPublish(fn)` | Called after publishing |
|
|
293
|
-
| `@BeforeConsume(fn)` | Called before consume callback |
|
|
294
|
-
| `@AfterConsume(fn)` | Called after successful consume |
|
|
295
|
-
| `@OnConsumeError(fn)` | Called when consume callback throws |
|
|
977
|
+
**Argument:** `(message: M, context?: C) => void | Promise<void>`.
|
|
296
978
|
|
|
297
|
-
|
|
979
|
+
#### `@BeforeConsume` / `@AfterConsume`
|
|
298
980
|
|
|
299
|
-
|
|
981
|
+
Fire around consume callback execution. `@BeforeConsume` runs after deserialisation but before the consume callback. `@AfterConsume` runs after the callback completes successfully.
|
|
982
|
+
|
|
983
|
+
```typescript
|
|
984
|
+
@BeforeConsume(async (msg, ctx) => {
|
|
985
|
+
audit.log("consuming", msg);
|
|
986
|
+
})
|
|
987
|
+
@AfterConsume(async (msg) => {
|
|
988
|
+
metrics.increment("messages.consumed");
|
|
989
|
+
})
|
|
990
|
+
@Message()
|
|
991
|
+
class OrderPlaced {
|
|
992
|
+
/* ... */
|
|
993
|
+
}
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
**Argument:** `(message: M, context?: C) => void | Promise<void>`.
|
|
300
997
|
|
|
301
|
-
|
|
998
|
+
#### `@OnConsumeError`
|
|
999
|
+
|
|
1000
|
+
Fires when a consume callback throws an error. Receives the error as the **first** argument, followed by the message and context.
|
|
302
1001
|
|
|
303
1002
|
```typescript
|
|
1003
|
+
@OnConsumeError(async (error, msg) => {
|
|
1004
|
+
errorTracker.capture(error, { messageId: msg.id });
|
|
1005
|
+
})
|
|
304
1006
|
@Message()
|
|
305
|
-
class
|
|
1007
|
+
class PaymentCharge {
|
|
306
1008
|
@IdentifierField() id!: string;
|
|
307
|
-
@
|
|
308
|
-
|
|
1009
|
+
@Field("string") chargeId!: string;
|
|
1010
|
+
}
|
|
1011
|
+
```
|
|
309
1012
|
|
|
310
|
-
|
|
311
|
-
@Field("integer") count!: number;
|
|
312
|
-
@Field("float") price!: number;
|
|
313
|
-
@Field("boolean") active!: boolean;
|
|
314
|
-
@Field("date") expiresAt!: Date;
|
|
315
|
-
@Field("uuid") referenceId!: string;
|
|
316
|
-
@Field("email") contactEmail!: string;
|
|
317
|
-
@Field("url") callbackUrl!: string;
|
|
318
|
-
@Field("array") tags!: Array<string>;
|
|
319
|
-
@Field("object") metadata!: Record<string, unknown>;
|
|
1013
|
+
**Argument:** `(error: Error, message: M, context?: C) => void | Promise<void>`.
|
|
320
1014
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
1015
|
+
Note the different signature: `error` is the first parameter, unlike all other hooks where the message comes first.
|
|
1016
|
+
|
|
1017
|
+
---
|
|
1018
|
+
|
|
1019
|
+
### Hook Execution Order
|
|
1020
|
+
|
|
1021
|
+
| Phase | Hooks (in order) |
|
|
1022
|
+
| ---------- | ------------------------------------------------------------------------------------------------------- |
|
|
1023
|
+
| Creation | `@OnCreate` |
|
|
1024
|
+
| Validation | `@OnValidate` |
|
|
1025
|
+
| Publish | `@BeforePublish` -> subscriber.beforePublish -> transport -> `@AfterPublish` -> subscriber.afterPublish |
|
|
1026
|
+
| Consume | `@BeforeConsume` -> subscriber.beforeConsume -> callback -> `@AfterConsume` -> subscriber.afterConsume |
|
|
1027
|
+
| Hydration | `@OnHydrate` |
|
|
1028
|
+
| Error | `@OnConsumeError` -> subscriber.onConsumeError (replaces AfterConsume steps) |
|
|
1029
|
+
|
|
1030
|
+
Full publish + consume lifecycle:
|
|
1031
|
+
|
|
1032
|
+
```
|
|
1033
|
+
1. @BeforePublish hook
|
|
1034
|
+
2. subscriber.beforePublish
|
|
1035
|
+
3. (transport publishes)
|
|
1036
|
+
4. (transport delivers to consumer)
|
|
1037
|
+
5. @BeforeConsume hook
|
|
1038
|
+
6. subscriber.beforeConsume
|
|
1039
|
+
7. callback executes
|
|
1040
|
+
8. @AfterConsume hook
|
|
1041
|
+
9. subscriber.afterConsume
|
|
1042
|
+
10. @AfterPublish hook
|
|
1043
|
+
11. subscriber.afterPublish
|
|
326
1044
|
```
|
|
327
1045
|
|
|
1046
|
+
On error at step 7, steps 8-11 are replaced by `@OnConsumeError` and `subscriber.onConsumeError`.
|
|
1047
|
+
|
|
328
1048
|
## Retry and Dead Letter
|
|
329
1049
|
|
|
330
1050
|
Configure automatic retry with backoff strategies and dead letter routing for permanently failed messages.
|
|
@@ -346,14 +1066,6 @@ class PaymentCharge {
|
|
|
346
1066
|
}
|
|
347
1067
|
```
|
|
348
1068
|
|
|
349
|
-
**Retry strategies:**
|
|
350
|
-
|
|
351
|
-
| Strategy | Delay pattern (base=1000, multiplier=2) |
|
|
352
|
-
| --------------- | ------------------------------------------- |
|
|
353
|
-
| `"constant"` | 1000, 1000, 1000, ... |
|
|
354
|
-
| `"linear"` | 1000, 2000, 3000, ... |
|
|
355
|
-
| `"exponential"` | 1000, 2000, 4000, 8000, ... (capped at max) |
|
|
356
|
-
|
|
357
1069
|
## Dynamic Topics
|
|
358
1070
|
|
|
359
1071
|
Route messages to different topics based on their content:
|
|
@@ -420,24 +1132,6 @@ source.addSubscriber(auditSubscriber);
|
|
|
420
1132
|
source.removeSubscriber(auditSubscriber);
|
|
421
1133
|
```
|
|
422
1134
|
|
|
423
|
-
**Hook execution order on publish + consume:**
|
|
424
|
-
|
|
425
|
-
```
|
|
426
|
-
1. @BeforePublish hook
|
|
427
|
-
2. subscriber.beforePublish
|
|
428
|
-
3. (transport publishes)
|
|
429
|
-
4. (transport delivers to consumer)
|
|
430
|
-
5. @BeforeConsume hook
|
|
431
|
-
6. subscriber.beforeConsume
|
|
432
|
-
7. callback executes
|
|
433
|
-
8. @AfterConsume hook
|
|
434
|
-
9. subscriber.afterConsume
|
|
435
|
-
10. @AfterPublish hook
|
|
436
|
-
11. subscriber.afterPublish
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
On error at step 7, steps 8-11 are replaced by `@OnConsumeError` and `subscriber.onConsumeError`.
|
|
440
|
-
|
|
441
1135
|
## Consume Envelope
|
|
442
1136
|
|
|
443
1137
|
Every subscribe/consume callback receives the message and an envelope with routing metadata:
|
|
@@ -460,6 +1154,65 @@ await bus.subscribe({
|
|
|
460
1154
|
});
|
|
461
1155
|
```
|
|
462
1156
|
|
|
1157
|
+
## Message Manipulation
|
|
1158
|
+
|
|
1159
|
+
Every publisher, message bus, and worker queue provides utilities for working with message instances:
|
|
1160
|
+
|
|
1161
|
+
```typescript
|
|
1162
|
+
const bus = source.messageBus(OrderPlaced);
|
|
1163
|
+
|
|
1164
|
+
// Create: new instance with auto-generated fields and defaults
|
|
1165
|
+
const msg = bus.create({ orderId: "abc-123", total: 59.99 });
|
|
1166
|
+
|
|
1167
|
+
// Hydrate: reconstruct from raw data (no auto-generation)
|
|
1168
|
+
const hydrated = bus.hydrate({ orderId: "abc-123", total: 59.99, id: "existing-uuid" });
|
|
1169
|
+
|
|
1170
|
+
// Copy: deep clone with a fresh identifier
|
|
1171
|
+
const copied = bus.copy(msg);
|
|
1172
|
+
// copied.orderId === msg.orderId, but copied.id !== msg.id
|
|
1173
|
+
|
|
1174
|
+
// Validate: throws IrisValidationError if invalid
|
|
1175
|
+
bus.validate(msg);
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
## Publish Options
|
|
1179
|
+
|
|
1180
|
+
Override message-level defaults per publish call:
|
|
1181
|
+
|
|
1182
|
+
```typescript
|
|
1183
|
+
await bus.publish(msg, {
|
|
1184
|
+
delay: 5000, // delay delivery by 5 seconds
|
|
1185
|
+
priority: 8, // override @Priority
|
|
1186
|
+
expiry: 60000, // override @Expiry (TTL in ms)
|
|
1187
|
+
key: "partition-key", // routing/partition key
|
|
1188
|
+
headers: { "x-source": "api" }, // additional headers
|
|
1189
|
+
});
|
|
1190
|
+
```
|
|
1191
|
+
|
|
1192
|
+
## Zod Validation
|
|
1193
|
+
|
|
1194
|
+
Use `@Schema()` with Zod for fine-grained field validation:
|
|
1195
|
+
|
|
1196
|
+
```typescript
|
|
1197
|
+
import { z } from "zod";
|
|
1198
|
+
import { Schema, Field, Message } from "@lindorm/iris";
|
|
1199
|
+
|
|
1200
|
+
@Message()
|
|
1201
|
+
class UserCreated {
|
|
1202
|
+
@Schema(z.string().email())
|
|
1203
|
+
@Field("email")
|
|
1204
|
+
email!: string;
|
|
1205
|
+
|
|
1206
|
+
@Schema(z.number().int().min(13).max(150))
|
|
1207
|
+
@Field("integer")
|
|
1208
|
+
age!: number;
|
|
1209
|
+
|
|
1210
|
+
@Schema(z.string().regex(/^[A-Z]{2,3}$/))
|
|
1211
|
+
@Field("string")
|
|
1212
|
+
countryCode!: string;
|
|
1213
|
+
}
|
|
1214
|
+
```
|
|
1215
|
+
|
|
463
1216
|
## Cloning
|
|
464
1217
|
|
|
465
1218
|
Create independent source instances that share the underlying driver connection:
|
|
@@ -697,65 +1450,6 @@ source.onConnectionStateChange((state) => {
|
|
|
697
1450
|
const healthy = await source.ping();
|
|
698
1451
|
```
|
|
699
1452
|
|
|
700
|
-
## Message Manipulation
|
|
701
|
-
|
|
702
|
-
Every publisher, message bus, and worker queue provides utilities for working with message instances:
|
|
703
|
-
|
|
704
|
-
```typescript
|
|
705
|
-
const bus = source.messageBus(OrderPlaced);
|
|
706
|
-
|
|
707
|
-
// Create: new instance with auto-generated fields and defaults
|
|
708
|
-
const msg = bus.create({ orderId: "abc-123", total: 59.99 });
|
|
709
|
-
|
|
710
|
-
// Hydrate: reconstruct from raw data (no auto-generation)
|
|
711
|
-
const hydrated = bus.hydrate({ orderId: "abc-123", total: 59.99, id: "existing-uuid" });
|
|
712
|
-
|
|
713
|
-
// Copy: deep clone with a fresh identifier
|
|
714
|
-
const copied = bus.copy(msg);
|
|
715
|
-
// copied.orderId === msg.orderId, but copied.id !== msg.id
|
|
716
|
-
|
|
717
|
-
// Validate: throws IrisValidationError if invalid
|
|
718
|
-
bus.validate(msg);
|
|
719
|
-
```
|
|
720
|
-
|
|
721
|
-
## Publish Options
|
|
722
|
-
|
|
723
|
-
Override message-level defaults per publish call:
|
|
724
|
-
|
|
725
|
-
```typescript
|
|
726
|
-
await bus.publish(msg, {
|
|
727
|
-
delay: 5000, // delay delivery by 5 seconds
|
|
728
|
-
priority: 8, // override @Priority
|
|
729
|
-
expiry: 60000, // override @Expiry (TTL in ms)
|
|
730
|
-
key: "partition-key", // routing/partition key
|
|
731
|
-
headers: { "x-source": "api" }, // additional headers
|
|
732
|
-
});
|
|
733
|
-
```
|
|
734
|
-
|
|
735
|
-
## Zod Validation
|
|
736
|
-
|
|
737
|
-
Use `@Schema()` with Zod for fine-grained field validation:
|
|
738
|
-
|
|
739
|
-
```typescript
|
|
740
|
-
import { z } from "zod";
|
|
741
|
-
import { Schema, Field, Message } from "@lindorm/iris";
|
|
742
|
-
|
|
743
|
-
@Message()
|
|
744
|
-
class UserCreated {
|
|
745
|
-
@Schema(z.string().email())
|
|
746
|
-
@Field("email")
|
|
747
|
-
email!: string;
|
|
748
|
-
|
|
749
|
-
@Schema(z.number().int().min(13).max(150))
|
|
750
|
-
@Field("integer")
|
|
751
|
-
age!: number;
|
|
752
|
-
|
|
753
|
-
@Schema(z.string().regex(/^[A-Z]{2,3}$/))
|
|
754
|
-
@Field("string")
|
|
755
|
-
countryCode!: string;
|
|
756
|
-
}
|
|
757
|
-
```
|
|
758
|
-
|
|
759
1453
|
## Testing with Mocks
|
|
760
1454
|
|
|
761
1455
|
All mocks are available via the `@lindorm/iris/mocks` subpath:
|