@amqp-contract/contract 0.12.0 → 0.14.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.mts CHANGED
@@ -798,7 +798,51 @@ type ConsumerDefinition<TMessage extends MessageDefinition = MessageDefinition>
798
798
  message: TMessage;
799
799
  };
800
800
  /**
801
- * Complete AMQP contract definition.
801
+ * Base type for event publisher configuration.
802
+ *
803
+ * This is a simplified type used in ContractDefinition. The full generic type
804
+ * is defined in the builder module.
805
+ *
806
+ * @see defineEventPublisher for creating event publishers
807
+ */
808
+ type EventPublisherConfigBase = {
809
+ __brand: "EventPublisherConfig";
810
+ exchange: ExchangeDefinition;
811
+ message: MessageDefinition;
812
+ routingKey: string | undefined;
813
+ arguments?: Record<string, unknown>;
814
+ };
815
+ /**
816
+ * Base type for command consumer configuration.
817
+ *
818
+ * This is a simplified type used in ContractDefinition. The full generic type
819
+ * is defined in the builder module.
820
+ *
821
+ * @see defineCommandConsumer for creating command consumers
822
+ */
823
+ type CommandConsumerConfigBase = {
824
+ __brand: "CommandConsumerConfig";
825
+ consumer: ConsumerDefinition;
826
+ binding: QueueBindingDefinition;
827
+ exchange: ExchangeDefinition;
828
+ message: MessageDefinition;
829
+ routingKey: string | undefined;
830
+ };
831
+ /**
832
+ * Base type for event consumer result.
833
+ *
834
+ * This is a simplified type used in ContractDefinitionInput. The full generic type
835
+ * is defined in the builder module.
836
+ *
837
+ * @see defineEventConsumer for creating event consumers
838
+ */
839
+ type EventConsumerResultBase = {
840
+ __brand: "EventConsumerResult";
841
+ consumer: ConsumerDefinition;
842
+ binding: QueueBindingDefinition;
843
+ };
844
+ /**
845
+ * Complete AMQP contract definition (output type).
802
846
  *
803
847
  * A contract brings together all AMQP resources into a single, type-safe definition.
804
848
  * It defines the complete messaging topology including exchanges, queues, bindings,
@@ -862,6 +906,69 @@ type ContractDefinition = {
862
906
  */
863
907
  consumers?: Record<string, ConsumerDefinition>;
864
908
  };
909
+ /**
910
+ * Publisher entry that can be passed to defineContract's publishers section.
911
+ *
912
+ * Can be either:
913
+ * - A plain PublisherDefinition from definePublisher
914
+ * - An EventPublisherConfig from defineEventPublisher (auto-extracted to publisher)
915
+ */
916
+ type PublisherEntry = PublisherDefinition | EventPublisherConfigBase;
917
+ /**
918
+ * Consumer entry that can be passed to defineContract's consumers section.
919
+ *
920
+ * Can be either:
921
+ * - A plain ConsumerDefinition from defineConsumer
922
+ * - An EventConsumerResult from defineEventConsumer (binding auto-extracted)
923
+ * - A CommandConsumerConfig from defineCommandConsumer (binding auto-extracted)
924
+ */
925
+ type ConsumerEntry = ConsumerDefinition | EventConsumerResultBase | CommandConsumerConfigBase;
926
+ /**
927
+ * Contract definition input type with automatic extraction of event/command patterns.
928
+ *
929
+ * This type allows passing event and command configs directly to the publishers
930
+ * and consumers sections. `defineContract` will automatically extract the appropriate
931
+ * definitions and generate bindings.
932
+ *
933
+ * @example
934
+ * ```typescript
935
+ * const contract = defineContract({
936
+ * exchanges: { orders: ordersExchange },
937
+ * queues: { processing: processingQueue },
938
+ * publishers: {
939
+ * // EventPublisherConfig → auto-extracted to publisher
940
+ * orderCreated: defineEventPublisher(ordersExchange, orderMessage, { routingKey: "order.created" }),
941
+ * },
942
+ * consumers: {
943
+ * // CommandConsumerConfig → auto-extracted to consumer + binding
944
+ * processOrder: defineCommandConsumer(orderQueue, ordersExchange, orderMessage, { routingKey: "order.process" }),
945
+ * // EventConsumerResult → auto-extracted to consumer + binding
946
+ * notify: defineEventConsumer(orderCreatedEvent, notificationQueue),
947
+ * },
948
+ * });
949
+ * ```
950
+ *
951
+ * @see defineContract - Processes this input and returns a ContractDefinition
952
+ */
953
+ type ContractDefinitionInput = Omit<ContractDefinition, "publishers" | "consumers"> & {
954
+ /**
955
+ * Named publisher definitions.
956
+ *
957
+ * Can accept:
958
+ * - PublisherDefinition from definePublisher
959
+ * - EventPublisherConfig from defineEventPublisher (auto-extracted to publisher)
960
+ */
961
+ publishers?: Record<string, PublisherEntry>;
962
+ /**
963
+ * Named consumer definitions.
964
+ *
965
+ * Can accept:
966
+ * - ConsumerDefinition from defineConsumer
967
+ * - EventConsumerResult from defineEventConsumer (binding auto-extracted)
968
+ * - CommandConsumerConfig from defineCommandConsumer (binding auto-extracted)
969
+ */
970
+ consumers?: Record<string, ConsumerEntry>;
971
+ };
865
972
  /**
866
973
  * Extract publisher names from a contract.
867
974
  *
@@ -877,7 +984,7 @@ type ContractDefinition = {
877
984
  * // Result: 'orderCreated' | 'orderUpdated' | 'orderCancelled'
878
985
  * ```
879
986
  */
880
- type InferPublisherNames<TContract extends ContractDefinition> = TContract["publishers"] extends Record<string, unknown> ? keyof TContract["publishers"] : never;
987
+ type InferPublisherNames<TContract extends ContractDefinitionInput> = TContract["publishers"] extends Record<string, unknown> ? keyof TContract["publishers"] : never;
881
988
  /**
882
989
  * Extract consumer names from a contract.
883
990
  *
@@ -893,7 +1000,7 @@ type InferPublisherNames<TContract extends ContractDefinition> = TContract["publ
893
1000
  * // Result: 'processOrder' | 'sendNotification' | 'updateInventory'
894
1001
  * ```
895
1002
  */
896
- type InferConsumerNames<TContract extends ContractDefinition> = TContract["consumers"] extends Record<string, unknown> ? keyof TContract["consumers"] : never;
1003
+ type InferConsumerNames<TContract extends ContractDefinitionInput> = TContract["consumers"] extends Record<string, unknown> ? keyof TContract["consumers"] : never;
897
1004
  //#endregion
898
1005
  //#region src/builder/exchange.d.ts
899
1006
  /**
@@ -1010,19 +1117,87 @@ declare function defineMessage<TPayload extends MessageDefinition["payload"], TH
1010
1117
  }): MessageDefinition<TPayload, THeaders>;
1011
1118
  //#endregion
1012
1119
  //#region src/builder/queue.d.ts
1120
+ /**
1121
+ * Type guard to check if a queue entry is a QueueWithTtlBackoffInfrastructure.
1122
+ *
1123
+ * When you configure a queue with TTL-backoff retry and a dead letter exchange,
1124
+ * `defineQueue` returns a `QueueWithTtlBackoffInfrastructure` instead of a plain
1125
+ * `QueueDefinition`. This type guard helps you distinguish between the two.
1126
+ *
1127
+ * **When to use:**
1128
+ * - When you need to check the type of a queue entry at runtime
1129
+ * - When writing generic code that handles both plain queues and infrastructure wrappers
1130
+ *
1131
+ * **Related functions:**
1132
+ * - `extractQueue()` - Use this to get the underlying queue definition from either type
1133
+ *
1134
+ * @param entry - The queue entry to check
1135
+ * @returns True if the entry is a QueueWithTtlBackoffInfrastructure, false otherwise
1136
+ *
1137
+ * @example
1138
+ * ```typescript
1139
+ * const queue = defineQueue('orders', {
1140
+ * deadLetter: { exchange: dlx },
1141
+ * retry: { mode: 'ttl-backoff' },
1142
+ * });
1143
+ *
1144
+ * if (isQueueWithTtlBackoffInfrastructure(queue)) {
1145
+ * // queue has .queue, .waitQueue, .waitQueueBinding, .mainQueueRetryBinding
1146
+ * console.log('Wait queue:', queue.waitQueue.name);
1147
+ * } else {
1148
+ * // queue is a plain QueueDefinition
1149
+ * console.log('Queue:', queue.name);
1150
+ * }
1151
+ * ```
1152
+ */
1153
+ declare function isQueueWithTtlBackoffInfrastructure(entry: QueueEntry): entry is QueueWithTtlBackoffInfrastructure;
1013
1154
  /**
1014
1155
  * Extract the plain QueueDefinition from a QueueEntry.
1015
- * If the entry is a QueueWithTtlBackoffInfrastructure, returns the inner queue.
1016
- * Otherwise, returns the entry as-is.
1156
+ *
1157
+ * **Why this function exists:**
1158
+ * When you configure a queue with TTL-backoff retry and a dead letter exchange,
1159
+ * `defineQueue` (or `defineTtlBackoffQueue`) returns a wrapper object that includes
1160
+ * the main queue, wait queue, and bindings. This function extracts the underlying
1161
+ * queue definition so you can access properties like `name`, `type`, etc.
1162
+ *
1163
+ * **When to use:**
1164
+ * - When you need to access queue properties (name, type, deadLetter, etc.)
1165
+ * - When passing a queue to functions that expect a plain QueueDefinition
1166
+ * - Works safely on both plain queues and infrastructure wrappers
1167
+ *
1168
+ * **How it works:**
1169
+ * - If the entry is a `QueueWithTtlBackoffInfrastructure`, returns `entry.queue`
1170
+ * - Otherwise, returns the entry as-is (it's already a plain QueueDefinition)
1017
1171
  *
1018
1172
  * @param entry - The queue entry (either plain QueueDefinition or QueueWithTtlBackoffInfrastructure)
1019
1173
  * @returns The plain QueueDefinition
1020
1174
  *
1021
1175
  * @example
1022
1176
  * ```typescript
1023
- * const queue = defineQueue('orders', { retry: { mode: 'ttl-backoff' }, deadLetter: { exchange: dlx } });
1024
- * const plainQueue = extractQueue(queue); // Returns the inner QueueDefinition
1177
+ * import { defineQueue, defineTtlBackoffQueue, extractQueue } from '@amqp-contract/contract';
1178
+ *
1179
+ * // TTL-backoff queue returns a wrapper
1180
+ * const orderQueue = defineTtlBackoffQueue('orders', {
1181
+ * deadLetterExchange: dlx,
1182
+ * maxRetries: 3,
1183
+ * });
1184
+ *
1185
+ * // Use extractQueue to access the queue name
1186
+ * const queueName = extractQueue(orderQueue).name; // 'orders'
1187
+ *
1188
+ * // Also works safely on plain queues
1189
+ * const plainQueue = defineQueue('simple', { type: 'quorum', retry: { mode: 'quorum-native' } });
1190
+ * const plainName = extractQueue(plainQueue).name; // 'simple'
1191
+ *
1192
+ * // Access other properties
1193
+ * const queueDef = extractQueue(orderQueue);
1194
+ * console.log(queueDef.name); // 'orders'
1195
+ * console.log(queueDef.type); // 'quorum'
1196
+ * console.log(queueDef.deadLetter); // { exchange: dlx, ... }
1025
1197
  * ```
1198
+ *
1199
+ * @see isQueueWithTtlBackoffInfrastructure - Type guard to check if extraction is needed
1200
+ * @see defineTtlBackoffQueue - Creates queues with TTL-backoff infrastructure
1026
1201
  */
1027
1202
  declare function extractQueue(entry: QueueEntry): QueueDefinition;
1028
1203
  /**
@@ -1088,6 +1263,176 @@ declare function extractQueue(entry: QueueEntry): QueueDefinition;
1088
1263
  * ```
1089
1264
  */
1090
1265
  declare function defineQueue(name: string, options?: DefineQueueOptions): QueueDefinition | QueueWithTtlBackoffInfrastructure;
1266
+ /**
1267
+ * Options for creating a quorum queue with quorum-native retry.
1268
+ *
1269
+ * This simplified helper enforces the required configuration for quorum-native retry:
1270
+ * - Dead letter exchange is required (for failed messages)
1271
+ * - Delivery limit is required (for retry count)
1272
+ */
1273
+ type DefineQuorumQueueOptions = {
1274
+ /**
1275
+ * Dead letter configuration - required for retry support.
1276
+ * Failed messages will be sent to this exchange.
1277
+ */
1278
+ deadLetterExchange: ExchangeDefinition;
1279
+ /**
1280
+ * Optional routing key for dead-lettered messages.
1281
+ */
1282
+ deadLetterRoutingKey?: string;
1283
+ /**
1284
+ * Maximum number of delivery attempts before dead-lettering.
1285
+ * @minimum 1
1286
+ */
1287
+ deliveryLimit: number;
1288
+ /**
1289
+ * If true, the queue is deleted when the last consumer unsubscribes.
1290
+ * @default false
1291
+ */
1292
+ autoDelete?: boolean;
1293
+ /**
1294
+ * Additional AMQP arguments for advanced configuration.
1295
+ */
1296
+ arguments?: Record<string, unknown>;
1297
+ };
1298
+ /**
1299
+ * Create a quorum queue with quorum-native retry.
1300
+ *
1301
+ * This is a simplified helper that enforces best practices:
1302
+ * - Uses quorum queues (recommended for most use cases)
1303
+ * - Requires dead letter exchange for failed message handling
1304
+ * - Uses quorum-native retry mode (simpler than TTL-backoff)
1305
+ *
1306
+ * **When to use:**
1307
+ * - You want simple, immediate retries without exponential backoff
1308
+ * - You don't need configurable delays between retries
1309
+ * - You want the simplest retry configuration
1310
+ *
1311
+ * @param name - The queue name
1312
+ * @param options - Configuration options
1313
+ * @returns A quorum queue definition with quorum-native retry
1314
+ *
1315
+ * @example
1316
+ * ```typescript
1317
+ * const dlx = defineExchange('orders-dlx', 'direct', { durable: true });
1318
+ *
1319
+ * const orderQueue = defineQuorumQueue('order-processing', {
1320
+ * deadLetterExchange: dlx,
1321
+ * deliveryLimit: 3, // Retry up to 3 times
1322
+ * });
1323
+ *
1324
+ * const contract = defineContract({
1325
+ * exchanges: { dlx },
1326
+ * queues: { orderProcessing: orderQueue },
1327
+ * // ...
1328
+ * });
1329
+ * ```
1330
+ *
1331
+ * @see defineQueue - For full queue configuration options
1332
+ * @see defineTtlBackoffQueue - For queues with exponential backoff retry
1333
+ */
1334
+ declare function defineQuorumQueue(name: string, options: DefineQuorumQueueOptions): QuorumQueueDefinition;
1335
+ /**
1336
+ * Options for creating a queue with TTL-backoff retry.
1337
+ *
1338
+ * This simplified helper enforces the required configuration for TTL-backoff retry:
1339
+ * - Dead letter exchange is required (used for retry routing)
1340
+ * - Returns infrastructure that includes wait queue and bindings
1341
+ */
1342
+ type DefineTtlBackoffQueueOptions = {
1343
+ /**
1344
+ * Dead letter exchange - required for TTL-backoff retry.
1345
+ * Used for routing messages to the wait queue and back.
1346
+ */
1347
+ deadLetterExchange: ExchangeDefinition;
1348
+ /**
1349
+ * Optional routing key for dead-lettered messages.
1350
+ */
1351
+ deadLetterRoutingKey?: string;
1352
+ /**
1353
+ * Maximum retry attempts before sending to DLQ.
1354
+ * @default 3
1355
+ */
1356
+ maxRetries?: number;
1357
+ /**
1358
+ * Initial delay in ms before first retry.
1359
+ * @default 1000
1360
+ */
1361
+ initialDelayMs?: number;
1362
+ /**
1363
+ * Maximum delay in ms between retries.
1364
+ * @default 30000
1365
+ */
1366
+ maxDelayMs?: number;
1367
+ /**
1368
+ * Exponential backoff multiplier.
1369
+ * @default 2
1370
+ */
1371
+ backoffMultiplier?: number;
1372
+ /**
1373
+ * Add jitter to prevent thundering herd.
1374
+ * @default true
1375
+ */
1376
+ jitter?: boolean;
1377
+ /**
1378
+ * If true, the queue is deleted when the last consumer unsubscribes.
1379
+ * @default false
1380
+ */
1381
+ autoDelete?: boolean;
1382
+ /**
1383
+ * Additional AMQP arguments for advanced configuration.
1384
+ */
1385
+ arguments?: Record<string, unknown>;
1386
+ };
1387
+ /**
1388
+ * Create a queue with TTL-backoff retry (exponential backoff).
1389
+ *
1390
+ * This is a simplified helper that enforces best practices:
1391
+ * - Uses quorum queues (recommended for most use cases)
1392
+ * - Requires dead letter exchange for retry routing
1393
+ * - Uses TTL-backoff retry mode with configurable delays
1394
+ * - Automatically generates wait queue and bindings
1395
+ *
1396
+ * **When to use:**
1397
+ * - You need exponential backoff between retries
1398
+ * - You want configurable delays (initial delay, max delay, jitter)
1399
+ * - You're processing messages that may need time before retry
1400
+ *
1401
+ * **Returns:** A `QueueWithTtlBackoffInfrastructure` object that includes the
1402
+ * main queue, wait queue, and bindings. Pass this directly to `defineContract`
1403
+ * and it will be expanded automatically.
1404
+ *
1405
+ * @param name - The queue name
1406
+ * @param options - Configuration options
1407
+ * @returns A queue with TTL-backoff infrastructure
1408
+ *
1409
+ * @example
1410
+ * ```typescript
1411
+ * const dlx = defineExchange('orders-dlx', 'direct', { durable: true });
1412
+ *
1413
+ * const orderQueue = defineTtlBackoffQueue('order-processing', {
1414
+ * deadLetterExchange: dlx,
1415
+ * maxRetries: 5,
1416
+ * initialDelayMs: 1000, // Start with 1s delay
1417
+ * maxDelayMs: 30000, // Cap at 30s
1418
+ * });
1419
+ *
1420
+ * const contract = defineContract({
1421
+ * exchanges: { dlx },
1422
+ * queues: { orderProcessing: orderQueue }, // Wait queue auto-added
1423
+ * // ... bindings auto-generated
1424
+ * });
1425
+ *
1426
+ * // To access the underlying queue definition (e.g., for the queue name):
1427
+ * import { extractQueue } from '@amqp-contract/contract';
1428
+ * const queueName = extractQueue(orderQueue).name;
1429
+ * ```
1430
+ *
1431
+ * @see defineQueue - For full queue configuration options
1432
+ * @see defineQuorumQueue - For queues with quorum-native retry (simpler, immediate retries)
1433
+ * @see extractQueue - To access the underlying queue definition
1434
+ */
1435
+ declare function defineTtlBackoffQueue(name: string, options: DefineTtlBackoffQueueOptions): QueueWithTtlBackoffInfrastructure;
1091
1436
  //#endregion
1092
1437
  //#region src/builder/binding.d.ts
1093
1438
  /**
@@ -1219,6 +1564,19 @@ declare function defineExchangeBinding(destination: ExchangeDefinition, source:
1219
1564
  *
1220
1565
  * The message schema is validated when publishing to ensure type safety.
1221
1566
  *
1567
+ * **Which pattern to use:**
1568
+ *
1569
+ * | Pattern | Best for | Description |
1570
+ * |---------|----------|-------------|
1571
+ * | `definePublisher` + `defineConsumer` | Independent definition | Define publishers and consumers separately with manual schema consistency |
1572
+ * | `defineEventPublisher` + `defineEventConsumer` | Event broadcasting | Define event publisher first, create consumers that subscribe to it |
1573
+ * | `defineCommandConsumer` + `defineCommandPublisher` | Task queues | Define command consumer first, create publishers that send commands to it |
1574
+ *
1575
+ * Use `defineEventPublisher` when:
1576
+ * - One publisher feeds multiple consumers
1577
+ * - You want automatic schema consistency between publisher and consumers
1578
+ * - You're building event-driven architectures
1579
+ *
1222
1580
  * @param exchange - The fanout exchange definition to publish to
1223
1581
  * @param message - The message definition with payload schema
1224
1582
  * @param options - Optional publisher configuration
@@ -1239,6 +1597,9 @@ declare function defineExchangeBinding(destination: ExchangeDefinition, source:
1239
1597
  *
1240
1598
  * const logPublisher = definePublisher(logsExchange, logMessage);
1241
1599
  * ```
1600
+ *
1601
+ * @see defineEventPublisher - For event-driven patterns with automatic schema consistency
1602
+ * @see defineCommandConsumer - For task queue patterns with automatic schema consistency
1242
1603
  */
1243
1604
  declare function definePublisher<TMessage extends MessageDefinition>(exchange: FanoutExchangeDefinition, message: TMessage, options?: Omit<Extract<PublisherDefinition<TMessage>, {
1244
1605
  exchange: FanoutExchangeDefinition;
@@ -1253,6 +1614,19 @@ declare function definePublisher<TMessage extends MessageDefinition>(exchange: F
1253
1614
  *
1254
1615
  * The message schema is validated when publishing to ensure type safety.
1255
1616
  *
1617
+ * **Which pattern to use:**
1618
+ *
1619
+ * | Pattern | Best for | Description |
1620
+ * |---------|----------|-------------|
1621
+ * | `definePublisher` + `defineConsumer` | Independent definition | Define publishers and consumers separately with manual schema consistency |
1622
+ * | `defineEventPublisher` + `defineEventConsumer` | Event broadcasting | Define event publisher first, create consumers that subscribe to it |
1623
+ * | `defineCommandConsumer` + `defineCommandPublisher` | Task queues | Define command consumer first, create publishers that send commands to it |
1624
+ *
1625
+ * Use `defineEventPublisher` when:
1626
+ * - One publisher feeds multiple consumers
1627
+ * - You want automatic schema consistency between publisher and consumers
1628
+ * - You're building event-driven architectures
1629
+ *
1256
1630
  * @param exchange - The direct or topic exchange definition to publish to
1257
1631
  * @param message - The message definition with payload schema
1258
1632
  * @param options - Publisher configuration (routingKey is required)
@@ -1279,6 +1653,9 @@ declare function definePublisher<TMessage extends MessageDefinition>(exchange: F
1279
1653
  * routingKey: 'order.created'
1280
1654
  * });
1281
1655
  * ```
1656
+ *
1657
+ * @see defineEventPublisher - For event-driven patterns with automatic schema consistency
1658
+ * @see defineCommandConsumer - For task queue patterns with automatic schema consistency
1282
1659
  */
1283
1660
  declare function definePublisher<TMessage extends MessageDefinition>(exchange: DirectExchangeDefinition | TopicExchangeDefinition, message: TMessage, options: Omit<Extract<PublisherDefinition<TMessage>, {
1284
1661
  exchange: DirectExchangeDefinition | TopicExchangeDefinition;
@@ -1287,6 +1664,37 @@ declare function definePublisher<TMessage extends MessageDefinition>(exchange: D
1287
1664
  }>;
1288
1665
  //#endregion
1289
1666
  //#region src/builder/consumer.d.ts
1667
+ /**
1668
+ * Extract the ConsumerDefinition from any ConsumerEntry type.
1669
+ *
1670
+ * Handles the following entry types:
1671
+ * - ConsumerDefinition: returned as-is
1672
+ * - EventConsumerResult: returns the nested `.consumer` property
1673
+ * - CommandConsumerConfig: returns the nested `.consumer` property
1674
+ *
1675
+ * Use this function when you need to access the underlying ConsumerDefinition
1676
+ * from a consumer entry that may have been created with defineEventConsumer
1677
+ * or defineCommandConsumer.
1678
+ *
1679
+ * @param entry - The consumer entry to extract from
1680
+ * @returns The underlying ConsumerDefinition
1681
+ *
1682
+ * @example
1683
+ * ```typescript
1684
+ * // Works with plain ConsumerDefinition
1685
+ * const consumer1 = defineConsumer(queue, message);
1686
+ * extractConsumer(consumer1).queue.name; // "my-queue"
1687
+ *
1688
+ * // Works with EventConsumerResult
1689
+ * const consumer2 = defineEventConsumer(eventPublisher, queue);
1690
+ * extractConsumer(consumer2).queue.name; // "my-queue"
1691
+ *
1692
+ * // Works with CommandConsumerConfig
1693
+ * const consumer3 = defineCommandConsumer(queue, exchange, message, { routingKey: "cmd" });
1694
+ * extractConsumer(consumer3).queue.name; // "my-queue"
1695
+ * ```
1696
+ */
1697
+ declare function extractConsumer(entry: ConsumerEntry): ConsumerDefinition;
1290
1698
  /**
1291
1699
  * Define a message consumer.
1292
1700
  *
@@ -1296,6 +1704,19 @@ declare function definePublisher<TMessage extends MessageDefinition>(exchange: D
1296
1704
  * Consumers are associated with a specific queue and message type. When you create a worker
1297
1705
  * with this consumer, it will process messages from the queue according to the schema.
1298
1706
  *
1707
+ * **Which pattern to use:**
1708
+ *
1709
+ * | Pattern | Best for | Description |
1710
+ * |---------|----------|-------------|
1711
+ * | `definePublisher` + `defineConsumer` | Independent definition | Define publishers and consumers separately with manual schema consistency |
1712
+ * | `defineEventPublisher` + `defineEventConsumer` | Event broadcasting | Define event publisher first, create consumers that subscribe to it |
1713
+ * | `defineCommandConsumer` + `defineCommandPublisher` | Task queues | Define command consumer first, create publishers that send commands to it |
1714
+ *
1715
+ * Use `defineCommandConsumer` when:
1716
+ * - One consumer receives from multiple publishers
1717
+ * - You want automatic schema consistency between consumer and publishers
1718
+ * - You're building task queue or command patterns
1719
+ *
1299
1720
  * @param queue - The queue definition to consume from
1300
1721
  * @param message - The message definition with payload schema
1301
1722
  * @param options - Optional consumer configuration
@@ -1328,10 +1749,20 @@ declare function definePublisher<TMessage extends MessageDefinition>(exchange: D
1328
1749
  * // connection
1329
1750
  * // });
1330
1751
  * ```
1752
+ *
1753
+ * @see defineCommandConsumer - For task queue patterns with automatic schema consistency
1754
+ * @see defineEventPublisher - For event-driven patterns with automatic schema consistency
1331
1755
  */
1332
1756
  declare function defineConsumer<TMessage extends MessageDefinition>(queue: QueueEntry, message: TMessage, options?: Omit<ConsumerDefinition<TMessage>, "queue" | "message">): ConsumerDefinition<TMessage>;
1333
1757
  //#endregion
1334
1758
  //#region src/builder/contract.d.ts
1759
+ /**
1760
+ * Type utility to produce the output contract type from the input.
1761
+ * The output preserves the exact input type structure to maintain all specific
1762
+ * type information (routing keys, message types, etc.) while the runtime
1763
+ * transforms the values correctly.
1764
+ */
1765
+ type ContractOutput<TContract extends ContractDefinitionInput> = TContract;
1335
1766
  /**
1336
1767
  * Define an AMQP contract.
1337
1768
  *
@@ -1401,7 +1832,7 @@ declare function defineConsumer<TMessage extends MessageDefinition>(queue: Queue
1401
1832
  * // - handler: async (message: { orderId: string, amount: number }) => void
1402
1833
  * ```
1403
1834
  */
1404
- declare function defineContract<TContract extends ContractDefinition>(definition: TContract): TContract;
1835
+ declare function defineContract<TContract extends ContractDefinitionInput>(definition: TContract): ContractOutput<TContract>;
1405
1836
  //#endregion
1406
1837
  //#region src/builder/routing-types.d.ts
1407
1838
  /**
@@ -1481,460 +1912,396 @@ type MatchesPattern<Key extends string, Pattern extends string> = Pattern extend
1481
1912
  */
1482
1913
  type MatchingRoutingKey<Pattern extends string, Key extends string> = RoutingKey<Key> extends never ? never : BindingPattern<Pattern> extends never ? never : MatchesPattern<Key, Pattern> extends true ? Key : never;
1483
1914
  //#endregion
1484
- //#region src/builder/publisher-first.d.ts
1915
+ //#region src/builder/event.d.ts
1485
1916
  /**
1486
- * Publisher-first builder result for fanout and direct exchanges.
1917
+ * Configuration for an event publisher.
1487
1918
  *
1488
- * This type represents a publisher and provides a method to create
1489
- * a consumer that uses the same message schema with a binding to the exchange.
1490
- *
1491
- * This pattern is suitable for event-oriented messaging where publishers
1492
- * emit events without knowing which queues will consume them.
1919
+ * Events are published without knowing who consumes them. Multiple consumers
1920
+ * can subscribe to the same event. This follows the pub/sub pattern where
1921
+ * publishers broadcast events and consumers subscribe to receive them.
1493
1922
  *
1494
1923
  * @template TMessage - The message definition
1495
- * @template TPublisher - The publisher definition
1924
+ * @template TExchange - The exchange definition
1925
+ * @template TRoutingKey - The routing key type (undefined for fanout)
1496
1926
  */
1497
- type PublisherFirstResult<TMessage extends MessageDefinition, TPublisher extends PublisherDefinition<TMessage>> = {
1498
- /** The publisher definition */
1499
- publisher: TPublisher;
1500
- /**
1501
- * Create a consumer that receives messages from this publisher.
1502
- * The consumer will automatically use the same message schema and
1503
- * a binding will be created with the same routing key.
1504
- *
1505
- * @param queue - The queue (or queue with infrastructure) that will consume the messages
1506
- * @returns An object with the consumer definition and binding
1507
- */
1508
- createConsumer: (queue: QueueEntry) => {
1509
- consumer: ConsumerDefinition<TMessage>;
1510
- binding: QueueBindingDefinition;
1511
- };
1927
+ type EventPublisherConfig<TMessage extends MessageDefinition, TExchange extends ExchangeDefinition, TRoutingKey extends string | undefined = undefined> = {
1928
+ /** Discriminator to identify this as an event publisher config */
1929
+ __brand: "EventPublisherConfig";
1930
+ /** The exchange to publish to */
1931
+ exchange: TExchange;
1932
+ /** The message definition */
1933
+ message: TMessage;
1934
+ /** The routing key for direct/topic exchanges */
1935
+ routingKey: TRoutingKey;
1936
+ /** Additional AMQP arguments */
1937
+ arguments?: Record<string, unknown>;
1512
1938
  };
1513
1939
  /**
1514
- * Publisher-first builder result for topic exchanges.
1940
+ * Result from defineEventConsumer.
1515
1941
  *
1516
- * This type represents a publisher with a concrete routing key and provides a method
1517
- * to create consumers that can use routing key patterns matching the publisher's key.
1942
+ * Contains the consumer definition and binding needed to subscribe to an event.
1943
+ * Can be used directly in defineContract's consumers section - the binding
1944
+ * will be automatically extracted.
1518
1945
  *
1519
1946
  * @template TMessage - The message definition
1520
- * @template TPublisher - The publisher definition
1521
- * @template TRoutingKey - The literal routing key type from the publisher (for documentation purposes)
1522
1947
  */
1523
- type PublisherFirstResultWithRoutingKey<TMessage extends MessageDefinition, TPublisher extends PublisherDefinition<TMessage>, TRoutingKey extends string> = {
1524
- /** The publisher definition */
1525
- publisher: TPublisher;
1526
- /**
1527
- * Create a consumer that receives messages from this publisher.
1528
- * For topic exchanges, the routing key pattern can be specified for the binding.
1529
- *
1530
- * @param queue - The queue (or queue with infrastructure) that will consume the messages
1531
- * @param routingKey - Optional routing key pattern for the binding (defaults to publisher's routing key)
1532
- * @returns An object with the consumer definition and binding
1533
- */
1534
- createConsumer: <TConsumerRoutingKey extends string = TRoutingKey>(queue: QueueEntry, routingKey?: BindingPattern<TConsumerRoutingKey>) => {
1535
- consumer: ConsumerDefinition<TMessage>;
1536
- binding: QueueBindingDefinition;
1537
- };
1948
+ type EventConsumerResult<TMessage extends MessageDefinition> = {
1949
+ /** Discriminator to identify this as an event consumer result */
1950
+ __brand: "EventConsumerResult";
1951
+ /** The consumer definition for processing messages */
1952
+ consumer: ConsumerDefinition<TMessage>;
1953
+ /** The binding connecting the queue to the exchange */
1954
+ binding: QueueBindingDefinition;
1538
1955
  };
1539
1956
  /**
1540
- * Define a publisher-first relationship for event-oriented messaging.
1541
- *
1542
- * This builder enforces consistency by:
1543
- * 1. Ensuring the publisher and consumer use the same message schema
1544
- * 2. Linking the routing key from the publisher to the binding
1957
+ * Define an event publisher for broadcasting messages via fanout exchange.
1545
1958
  *
1546
- * Use this pattern for events where publishers don't need to know about queues.
1547
- * Multiple consumers can be created for different queues, all using the same message schema.
1959
+ * Events are published without knowing who consumes them. Multiple consumers
1960
+ * can subscribe to the same event using `defineEventConsumer`.
1548
1961
  *
1549
- * @param exchange - The exchange to publish to (fanout type)
1962
+ * @param exchange - The fanout exchange to publish to
1550
1963
  * @param message - The message definition (schema and metadata)
1551
- * @param options - Optional binding configuration
1552
- * @returns A publisher-first result with publisher and consumer factory
1964
+ * @returns An event publisher configuration
1553
1965
  *
1554
1966
  * @example
1555
1967
  * ```typescript
1556
- * import { z } from 'zod';
1557
- *
1558
1968
  * const logsExchange = defineExchange('logs', 'fanout', { durable: true });
1559
- * const logMessage = defineMessage(
1560
- * z.object({
1561
- * level: z.enum(['info', 'warn', 'error']),
1562
- * message: z.string(),
1563
- * })
1564
- * );
1565
- *
1566
- * // Create publisher-first relationship (event pattern)
1567
- * const { publisher: publishLog, createConsumer: createLogConsumer } = definePublisherFirst(logsExchange, logMessage);
1568
- *
1569
- * // Multiple queues can consume the same event
1570
- * const logsQueue1 = defineQueue('logs-queue-1', { durable: true });
1571
- * const logsQueue2 = defineQueue('logs-queue-2', { durable: true });
1572
- *
1573
- * // Use in contract
1574
- * const { consumer: consumer1, binding: binding1 } = createLogConsumer(logsQueue1);
1575
- * const { consumer: consumer2, binding: binding2 } = createLogConsumer(logsQueue2);
1576
- *
1577
- * const contract = defineContract({
1578
- * exchanges: { logs: logsExchange },
1579
- * queues: { logsQueue1, logsQueue2 },
1580
- * bindings: {
1581
- * logBinding1: binding1,
1582
- * logBinding2: binding2,
1583
- * },
1584
- * publishers: { publishLog },
1585
- * consumers: {
1586
- * consumeLog1: consumer1,
1587
- * consumeLog2: consumer2,
1588
- * },
1589
- * });
1969
+ * const logMessage = defineMessage(z.object({
1970
+ * level: z.enum(['info', 'warn', 'error']),
1971
+ * message: z.string(),
1972
+ * }));
1973
+ *
1974
+ * // Create event publisher
1975
+ * const logEvent = defineEventPublisher(logsExchange, logMessage);
1976
+ *
1977
+ * // Multiple consumers can subscribe
1978
+ * const { consumer: fileConsumer, binding: fileBinding } =
1979
+ * defineEventConsumer(logEvent, fileLogsQueue);
1980
+ * const { consumer: alertConsumer, binding: alertBinding } =
1981
+ * defineEventConsumer(logEvent, alertsQueue);
1590
1982
  * ```
1591
1983
  */
1592
- declare function definePublisherFirst<TMessage extends MessageDefinition>(exchange: FanoutExchangeDefinition, message: TMessage, options?: Omit<Extract<QueueBindingDefinition, {
1593
- exchange: FanoutExchangeDefinition;
1594
- }>, "type" | "queue" | "exchange" | "routingKey">): PublisherFirstResult<TMessage, Extract<PublisherDefinition<TMessage>, {
1595
- exchange: FanoutExchangeDefinition;
1596
- }>>;
1984
+ declare function defineEventPublisher<TMessage extends MessageDefinition>(exchange: FanoutExchangeDefinition, message: TMessage): EventPublisherConfig<TMessage, FanoutExchangeDefinition, undefined>;
1597
1985
  /**
1598
- * Define a publisher-first relationship for event-oriented messaging with direct exchange.
1986
+ * Define an event publisher for broadcasting messages via direct exchange.
1599
1987
  *
1600
- * This builder enforces consistency by:
1601
- * 1. Ensuring the publisher and consumer use the same message schema
1602
- * 2. Linking the routing key from the publisher to the binding
1988
+ * Events are published with a specific routing key. Consumers will receive
1989
+ * messages that match the routing key exactly.
1603
1990
  *
1604
- * Use this pattern for events where publishers don't need to know about queues.
1605
- * Multiple consumers can be created for different queues, all using the same message schema.
1606
- *
1607
- * @param exchange - The exchange to publish to (direct type)
1991
+ * @param exchange - The direct exchange to publish to
1608
1992
  * @param message - The message definition (schema and metadata)
1609
- * @param options - Binding configuration (routingKey is required)
1993
+ * @param options - Configuration with required routing key
1610
1994
  * @param options.routingKey - The routing key for message routing
1611
- * @returns A publisher-first result with publisher and consumer factory
1995
+ * @param options.arguments - Additional AMQP arguments
1996
+ * @returns An event publisher configuration
1612
1997
  *
1613
1998
  * @example
1614
1999
  * ```typescript
1615
- * import { z } from 'zod';
1616
- *
1617
2000
  * const tasksExchange = defineExchange('tasks', 'direct', { durable: true });
1618
- * const taskMessage = defineMessage(
1619
- * z.object({
1620
- * taskId: z.string(),
1621
- * payload: z.record(z.unknown()),
1622
- * })
1623
- * );
1624
- *
1625
- * // Create publisher-first relationship with routing key
1626
- * const { publisher: executeTaskPublisher, createConsumer: createTaskConsumer } = definePublisherFirst(
1627
- * tasksExchange,
1628
- * taskMessage,
1629
- * { routingKey: 'task.execute' }
1630
- * );
2001
+ * const taskMessage = defineMessage(z.object({ taskId: z.string() }));
1631
2002
  *
1632
- * // Use in contract - routing key is consistent across publisher and bindings
1633
- * const taskQueue = defineQueue('task-queue', { durable: true });
1634
- * const { consumer, binding } = createTaskConsumer(taskQueue);
1635
- *
1636
- * const contract = defineContract({
1637
- * exchanges: { tasks: tasksExchange },
1638
- * queues: { taskQueue },
1639
- * bindings: { taskBinding: binding },
1640
- * publishers: { executeTask: executeTaskPublisher },
1641
- * consumers: { processTask: consumer },
2003
+ * const taskEvent = defineEventPublisher(tasksExchange, taskMessage, {
2004
+ * routingKey: 'task.execute',
1642
2005
  * });
1643
2006
  * ```
1644
2007
  */
1645
- declare function definePublisherFirst<TMessage extends MessageDefinition, TRoutingKey extends string>(exchange: DirectExchangeDefinition, message: TMessage, options: {
2008
+ declare function defineEventPublisher<TMessage extends MessageDefinition, TRoutingKey extends string>(exchange: DirectExchangeDefinition, message: TMessage, options: {
1646
2009
  routingKey: RoutingKey<TRoutingKey>;
1647
2010
  arguments?: Record<string, unknown>;
1648
- }): PublisherFirstResult<TMessage, Extract<PublisherDefinition<TMessage>, {
1649
- exchange: DirectExchangeDefinition | TopicExchangeDefinition;
1650
- }>>;
2011
+ }): EventPublisherConfig<TMessage, DirectExchangeDefinition, TRoutingKey>;
1651
2012
  /**
1652
- * Define a publisher-first relationship for event-oriented messaging with topic exchange.
1653
- *
1654
- * This builder enforces consistency by:
1655
- * 1. Ensuring the publisher and consumer use the same message schema
1656
- * 2. The publisher uses a concrete routing key (e.g., 'order.created')
1657
- * 3. Consumers can optionally specify routing key patterns (e.g., 'order.*') or use the default
2013
+ * Define an event publisher for broadcasting messages via topic exchange.
1658
2014
  *
1659
- * Use this pattern for events where publishers emit with specific routing keys,
1660
- * and consumers can subscribe with patterns. This is less common than the consumer-first pattern.
2015
+ * Events are published with a concrete routing key. Consumers can subscribe
2016
+ * using patterns (with * and # wildcards) to receive matching messages.
1661
2017
  *
1662
- * @param exchange - The exchange to publish to (topic type)
2018
+ * @param exchange - The topic exchange to publish to
1663
2019
  * @param message - The message definition (schema and metadata)
1664
- * @param options - Binding configuration (routingKey is required)
1665
- * @param options.routingKey - The concrete routing key for the publisher
1666
- * @returns A publisher-first result with publisher and consumer factory that accepts optional routing key patterns
2020
+ * @param options - Configuration with required routing key
2021
+ * @param options.routingKey - The concrete routing key (no wildcards)
2022
+ * @param options.arguments - Additional AMQP arguments
2023
+ * @returns An event publisher configuration
1667
2024
  *
1668
2025
  * @example
1669
2026
  * ```typescript
1670
- * import { z } from 'zod';
1671
- *
1672
2027
  * const ordersExchange = defineExchange('orders', 'topic', { durable: true });
1673
- * const orderMessage = defineMessage(
1674
- * z.object({
1675
- * orderId: z.string(),
1676
- * amount: z.number(),
1677
- * })
1678
- * );
2028
+ * const orderMessage = defineMessage(z.object({
2029
+ * orderId: z.string(),
2030
+ * amount: z.number(),
2031
+ * }));
2032
+ *
2033
+ * // Publisher uses concrete routing key
2034
+ * const orderCreatedEvent = defineEventPublisher(ordersExchange, orderMessage, {
2035
+ * routingKey: 'order.created',
2036
+ * });
1679
2037
  *
1680
- * // Create publisher-first relationship with concrete routing key
1681
- * const { publisher: orderCreatedPublisher, createConsumer: createOrderCreatedConsumer } = definePublisherFirst(
1682
- * ordersExchange,
1683
- * orderMessage,
1684
- * { routingKey: 'order.created' } // Concrete key
2038
+ * // Consumer can use pattern
2039
+ * const { consumer, binding } = defineEventConsumer(
2040
+ * orderCreatedEvent,
2041
+ * allOrdersQueue,
2042
+ * { routingKey: 'order.*' },
1685
2043
  * );
2044
+ * ```
2045
+ */
2046
+ declare function defineEventPublisher<TMessage extends MessageDefinition, TRoutingKey extends string>(exchange: TopicExchangeDefinition, message: TMessage, options: {
2047
+ routingKey: RoutingKey<TRoutingKey>;
2048
+ arguments?: Record<string, unknown>;
2049
+ }): EventPublisherConfig<TMessage, TopicExchangeDefinition, TRoutingKey>;
2050
+ /**
2051
+ * Create a consumer that subscribes to an event from a fanout exchange.
1686
2052
  *
1687
- * // Consumers can use patterns or specific keys
1688
- * const orderQueue = defineQueue('order-processing', { durable: true });
1689
- * const allOrdersQueue = defineQueue('all-orders', { durable: true });
2053
+ * @param eventPublisher - The event publisher configuration
2054
+ * @param queue - The queue that will receive messages
2055
+ * @param options - Optional binding configuration
2056
+ * @returns An object with the consumer definition and binding
2057
+ *
2058
+ * @example
2059
+ * ```typescript
2060
+ * const logEvent = defineEventPublisher(logsExchange, logMessage);
2061
+ * const { consumer, binding } = defineEventConsumer(logEvent, logsQueue);
2062
+ * ```
2063
+ */
2064
+ declare function defineEventConsumer<TMessage extends MessageDefinition>(eventPublisher: EventPublisherConfig<TMessage, FanoutExchangeDefinition, undefined>, queue: QueueEntry, options?: {
2065
+ arguments?: Record<string, unknown>;
2066
+ }): EventConsumerResult<TMessage>;
2067
+ /**
2068
+ * Create a consumer that subscribes to an event from a direct exchange.
1690
2069
  *
1691
- * // Use in contract
1692
- * const { consumer: processConsumer, binding: processBinding } =
1693
- * createOrderCreatedConsumer(orderQueue); // Uses 'order.created'
1694
- * const { consumer: allOrdersConsumer, binding: allOrdersBinding } =
1695
- * createOrderCreatedConsumer(allOrdersQueue, 'order.*'); // Uses pattern
2070
+ * @param eventPublisher - The event publisher configuration
2071
+ * @param queue - The queue that will receive messages
2072
+ * @param options - Optional binding configuration
2073
+ * @returns An object with the consumer definition and binding
2074
+ */
2075
+ declare function defineEventConsumer<TMessage extends MessageDefinition, TRoutingKey extends string>(eventPublisher: EventPublisherConfig<TMessage, DirectExchangeDefinition, TRoutingKey>, queue: QueueEntry, options?: {
2076
+ arguments?: Record<string, unknown>;
2077
+ }): EventConsumerResult<TMessage>;
2078
+ /**
2079
+ * Create a consumer that subscribes to an event from a topic exchange.
1696
2080
  *
1697
- * const contract = defineContract({
1698
- * exchanges: { orders: ordersExchange },
1699
- * queues: { orderQueue, allOrdersQueue },
1700
- * bindings: {
1701
- * orderBinding: processBinding,
1702
- * allOrdersBinding,
1703
- * },
1704
- * publishers: { orderCreated: orderCreatedPublisher },
1705
- * consumers: {
1706
- * processOrder: processConsumer,
1707
- * trackAllOrders: allOrdersConsumer,
1708
- * },
2081
+ * For topic exchanges, the consumer can optionally override the routing key
2082
+ * with a pattern to subscribe to multiple events.
2083
+ *
2084
+ * @param eventPublisher - The event publisher configuration
2085
+ * @param queue - The queue that will receive messages
2086
+ * @param options - Optional binding configuration
2087
+ * @param options.routingKey - Override routing key with pattern (defaults to publisher's key)
2088
+ * @returns An object with the consumer definition and binding
2089
+ *
2090
+ * @example
2091
+ * ```typescript
2092
+ * const orderCreatedEvent = defineEventPublisher(ordersExchange, orderMessage, {
2093
+ * routingKey: 'order.created',
2094
+ * });
2095
+ *
2096
+ * // Use exact routing key from publisher
2097
+ * const { consumer: exactConsumer } = defineEventConsumer(orderCreatedEvent, exactQueue);
2098
+ *
2099
+ * // Override with pattern to receive all order events
2100
+ * const { consumer: allConsumer } = defineEventConsumer(orderCreatedEvent, allQueue, {
2101
+ * routingKey: 'order.*',
1709
2102
  * });
1710
2103
  * ```
1711
2104
  */
1712
- declare function definePublisherFirst<TMessage extends MessageDefinition, TRoutingKey extends string>(exchange: TopicExchangeDefinition, message: TMessage, options: {
1713
- routingKey: RoutingKey<TRoutingKey>;
2105
+ declare function defineEventConsumer<TMessage extends MessageDefinition, TRoutingKey extends string, TConsumerRoutingKey extends string = TRoutingKey>(eventPublisher: EventPublisherConfig<TMessage, TopicExchangeDefinition, TRoutingKey>, queue: QueueEntry, options?: {
2106
+ routingKey?: BindingPattern<TConsumerRoutingKey>;
1714
2107
  arguments?: Record<string, unknown>;
1715
- }): PublisherFirstResultWithRoutingKey<TMessage, Extract<PublisherDefinition<TMessage>, {
1716
- exchange: DirectExchangeDefinition | TopicExchangeDefinition;
1717
- }>, TRoutingKey>;
1718
- //#endregion
1719
- //#region src/builder/consumer-first.d.ts
2108
+ }): EventConsumerResult<TMessage>;
1720
2109
  /**
1721
- * Consumer-first builder result for fanout and direct exchanges.
2110
+ * Type guard to check if a value is an EventPublisherConfig.
1722
2111
  *
1723
- * This type represents a consumer with its binding and provides a method to create
1724
- * a publisher that uses the same message schema and routing key.
2112
+ * @param value - The value to check
2113
+ * @returns True if the value is an EventPublisherConfig
2114
+ */
2115
+ declare function isEventPublisherConfig(value: unknown): value is EventPublisherConfig<MessageDefinition, ExchangeDefinition, string | undefined>;
2116
+ /**
2117
+ * Type guard to check if a value is an EventConsumerResult.
1725
2118
  *
1726
- * @template TMessage - The message definition
1727
- * @template TConsumer - The consumer definition
1728
- * @template TBinding - The queue binding definition
2119
+ * @param value - The value to check
2120
+ * @returns True if the value is an EventConsumerResult
1729
2121
  */
1730
- type ConsumerFirstResult<TMessage extends MessageDefinition, TConsumer extends ConsumerDefinition<TMessage>, TBinding extends QueueBindingDefinition> = {
1731
- /** The consumer definition */
1732
- consumer: TConsumer;
1733
- /** The binding definition connecting the exchange to the queue */
1734
- binding: TBinding;
1735
- /**
1736
- * Create a publisher that sends messages to this consumer.
1737
- * The publisher will automatically use the same message schema and routing key.
1738
- *
1739
- * @returns A publisher definition with the same message type and routing key
1740
- */
1741
- createPublisher: () => TBinding["exchange"] extends FanoutExchangeDefinition ? Extract<PublisherDefinition<TMessage>, {
1742
- exchange: FanoutExchangeDefinition;
1743
- }> : Extract<PublisherDefinition<TMessage>, {
1744
- exchange: DirectExchangeDefinition | TopicExchangeDefinition;
1745
- }>;
1746
- };
2122
+ declare function isEventConsumerResult(value: unknown): value is EventConsumerResult<MessageDefinition>;
2123
+ //#endregion
2124
+ //#region src/builder/command.d.ts
1747
2125
  /**
1748
- * Consumer-first builder result for topic exchanges.
2126
+ * Configuration for a command consumer.
1749
2127
  *
1750
- * This type represents a consumer with its binding (which may use a pattern) and provides
1751
- * a method to create a publisher with a concrete routing key that matches the pattern.
2128
+ * Commands are sent by one or more publishers to a single consumer (task queue pattern).
2129
+ * The consumer "owns" the queue, and publishers send commands to it.
1752
2130
  *
1753
2131
  * @template TMessage - The message definition
1754
- * @template TConsumer - The consumer definition
1755
- * @template TBinding - The queue binding definition
2132
+ * @template TExchange - The exchange definition
2133
+ * @template TRoutingKey - The routing key type (undefined for fanout)
1756
2134
  */
1757
- type ConsumerFirstResultWithRoutingKey<TMessage extends MessageDefinition, TConsumer extends ConsumerDefinition<TMessage>, TBinding extends QueueBindingDefinition> = {
1758
- /** The consumer definition */
1759
- consumer: TConsumer;
1760
- /** The binding definition connecting the exchange to the queue */
1761
- binding: TBinding;
1762
- /**
1763
- * Create a publisher that sends messages to this consumer.
1764
- * For topic exchanges, the routing key can be specified to match the binding pattern.
1765
- *
1766
- * @param routingKey - The concrete routing key that matches the binding pattern
1767
- * @returns A publisher definition with the specified routing key
1768
- */
1769
- createPublisher: <TPublisherRoutingKey extends string>(routingKey: RoutingKey<TPublisherRoutingKey>) => Extract<PublisherDefinition<TMessage>, {
1770
- exchange: DirectExchangeDefinition | TopicExchangeDefinition;
1771
- }>;
2135
+ type CommandConsumerConfig<TMessage extends MessageDefinition, TExchange extends ExchangeDefinition, TRoutingKey extends string | undefined = undefined> = {
2136
+ /** Discriminator to identify this as a command consumer config */
2137
+ __brand: "CommandConsumerConfig";
2138
+ /** The consumer definition for processing commands */
2139
+ consumer: ConsumerDefinition<TMessage>;
2140
+ /** The binding connecting the queue to the exchange */
2141
+ binding: QueueBindingDefinition;
2142
+ /** The exchange that receives commands */
2143
+ exchange: TExchange;
2144
+ /** The message definition */
2145
+ message: TMessage;
2146
+ /** The routing key pattern for the binding */
2147
+ routingKey: TRoutingKey;
1772
2148
  };
1773
2149
  /**
1774
- * Define a consumer-first relationship between a consumer and publisher.
1775
- *
1776
- * This builder enforces consistency by:
1777
- * 1. Ensuring the consumer and publisher use the same message schema
1778
- * 2. Linking the routing key from the binding to the publisher
1779
- * 3. Creating a binding that connects the exchange to the queue
2150
+ * Define a command consumer for receiving commands via fanout exchange.
1780
2151
  *
1781
- * Use this when you want to start with a consumer and ensure publishers
1782
- * send messages of the correct type.
2152
+ * Commands are sent by publishers to a specific queue. The consumer "owns" the
2153
+ * queue and defines what commands it accepts.
1783
2154
  *
1784
- * @param queue - The queue to consume from
1785
- * @param exchange - The exchange that routes to the queue (fanout type)
2155
+ * @param queue - The queue that will receive commands
2156
+ * @param exchange - The fanout exchange that routes commands
1786
2157
  * @param message - The message definition (schema and metadata)
1787
- * @param options - Optional binding configuration
1788
- * @returns A consumer-first result with consumer, binding, and publisher factory
2158
+ * @returns A command consumer configuration
1789
2159
  *
1790
2160
  * @example
1791
2161
  * ```typescript
1792
- * import { z } from 'zod';
2162
+ * const tasksExchange = defineExchange('tasks', 'fanout', { durable: true });
2163
+ * const taskMessage = defineMessage(z.object({ taskId: z.string() }));
1793
2164
  *
1794
- * const notificationsQueue = defineQueue('notifications', { durable: true });
1795
- * const notificationsExchange = defineExchange('notifications', 'fanout', { durable: true });
1796
- * const notificationMessage = defineMessage(
1797
- * z.object({
1798
- * userId: z.string(),
1799
- * message: z.string(),
1800
- * })
1801
- * );
2165
+ * // Consumer owns the queue
2166
+ * const executeTask = defineCommandConsumer(taskQueue, tasksExchange, taskMessage);
1802
2167
  *
1803
- * // Create consumer-first relationship
1804
- * const { consumer: processNotificationConsumer, binding: notificationBinding, createPublisher: createNotificationPublisher } = defineConsumerFirst(
1805
- * notificationsQueue,
1806
- * notificationsExchange,
1807
- * notificationMessage
1808
- * );
1809
- *
1810
- * // Use in contract
1811
- * const contract = defineContract({
1812
- * exchanges: { notifications: notificationsExchange },
1813
- * queues: { notificationsQueue },
1814
- * bindings: { notificationBinding },
1815
- * publishers: { sendNotification: createNotificationPublisher() },
1816
- * consumers: { processNotification: processNotificationConsumer },
1817
- * });
2168
+ * // Publishers send commands to it
2169
+ * const sendTask = defineCommandPublisher(executeTask);
1818
2170
  * ```
1819
2171
  */
1820
- declare function defineConsumerFirst<TMessage extends MessageDefinition>(queue: QueueEntry, exchange: FanoutExchangeDefinition, message: TMessage, options?: Omit<Extract<QueueBindingDefinition, {
1821
- exchange: FanoutExchangeDefinition;
1822
- }>, "type" | "queue" | "exchange" | "routingKey">): ConsumerFirstResult<TMessage, ConsumerDefinition<TMessage>, Extract<QueueBindingDefinition, {
1823
- exchange: FanoutExchangeDefinition;
1824
- }>>;
2172
+ declare function defineCommandConsumer<TMessage extends MessageDefinition>(queue: QueueEntry, exchange: FanoutExchangeDefinition, message: TMessage): CommandConsumerConfig<TMessage, FanoutExchangeDefinition, undefined>;
1825
2173
  /**
1826
- * Define a consumer-first relationship between a consumer and publisher.
2174
+ * Define a command consumer for receiving commands via direct exchange.
1827
2175
  *
1828
- * This builder enforces consistency by:
1829
- * 1. Ensuring the consumer and publisher use the same message schema
1830
- * 2. Linking the routing key from the binding to the publisher
1831
- * 3. Creating a binding that connects the exchange to the queue
2176
+ * Commands are sent by publishers with a specific routing key that matches
2177
+ * the binding pattern.
1832
2178
  *
1833
- * Use this when you want to start with a consumer and ensure publishers
1834
- * send messages with the correct type and routing key.
1835
- *
1836
- * @param queue - The queue to consume from
1837
- * @param exchange - The exchange that routes to the queue (direct type)
2179
+ * @param queue - The queue that will receive commands
2180
+ * @param exchange - The direct exchange that routes commands
1838
2181
  * @param message - The message definition (schema and metadata)
1839
- * @param options - Binding configuration (routingKey is required)
1840
- * @param options.routingKey - The routing key for message routing
1841
- * @returns A consumer-first result with consumer, binding, and publisher factory
2182
+ * @param options - Configuration with required routing key
2183
+ * @param options.routingKey - The routing key for the binding
2184
+ * @param options.arguments - Additional AMQP arguments
2185
+ * @returns A command consumer configuration
1842
2186
  *
1843
2187
  * @example
1844
2188
  * ```typescript
1845
- * import { z } from 'zod';
1846
- *
1847
- * const taskQueue = defineQueue('tasks', { durable: true });
1848
2189
  * const tasksExchange = defineExchange('tasks', 'direct', { durable: true });
1849
- * const taskMessage = defineMessage(
1850
- * z.object({
1851
- * taskId: z.string(),
1852
- * payload: z.record(z.unknown()),
1853
- * })
1854
- * );
2190
+ * const taskMessage = defineMessage(z.object({ taskId: z.string() }));
1855
2191
  *
1856
- * // Create consumer-first relationship with routing key
1857
- * const { consumer: processTaskConsumer, binding: taskBinding, createPublisher: createTaskPublisher } = defineConsumerFirst(
1858
- * taskQueue,
1859
- * tasksExchange,
1860
- * taskMessage,
1861
- * { routingKey: 'task.execute' }
1862
- * );
1863
- *
1864
- * // Use in contract - routing key is consistent across consumer and publisher
1865
- * const contract = defineContract({
1866
- * exchanges: { tasks: tasksExchange },
1867
- * queues: { taskQueue },
1868
- * bindings: { taskBinding },
1869
- * publishers: { executeTask: createTaskPublisher() },
1870
- * consumers: { processTask: processTaskConsumer },
2192
+ * const executeTask = defineCommandConsumer(taskQueue, tasksExchange, taskMessage, {
2193
+ * routingKey: 'task.execute',
1871
2194
  * });
2195
+ *
2196
+ * const sendTask = defineCommandPublisher(executeTask);
1872
2197
  * ```
1873
2198
  */
1874
- declare function defineConsumerFirst<TMessage extends MessageDefinition, TRoutingKey extends string>(queue: QueueEntry, exchange: DirectExchangeDefinition, message: TMessage, options: {
2199
+ declare function defineCommandConsumer<TMessage extends MessageDefinition, TRoutingKey extends string>(queue: QueueEntry, exchange: DirectExchangeDefinition, message: TMessage, options: {
1875
2200
  routingKey: RoutingKey<TRoutingKey>;
1876
2201
  arguments?: Record<string, unknown>;
1877
- }): ConsumerFirstResult<TMessage, ConsumerDefinition<TMessage>, Extract<QueueBindingDefinition, {
1878
- exchange: DirectExchangeDefinition;
1879
- }>>;
2202
+ }): CommandConsumerConfig<TMessage, DirectExchangeDefinition, TRoutingKey>;
1880
2203
  /**
1881
- * Define a consumer-first relationship between a consumer and publisher with topic exchange.
1882
- *
1883
- * This builder enforces consistency by:
1884
- * 1. Ensuring the consumer and publisher use the same message schema
1885
- * 2. The binding uses a routing key pattern (e.g., 'order.*')
1886
- * 3. The publisher factory accepts a concrete routing key that matches the pattern (e.g., 'order.created')
2204
+ * Define a command consumer for receiving commands via topic exchange.
1887
2205
  *
1888
- * Use this when you want to start with a consumer that uses a routing key pattern,
1889
- * and allow publishers to specify concrete routing keys that match that pattern.
2206
+ * The consumer binds with a routing key pattern (can use * and # wildcards).
2207
+ * Publishers then send commands with concrete routing keys that match the pattern.
1890
2208
  *
1891
- * @param queue - The queue to consume from
1892
- * @param exchange - The exchange that routes to the queue (topic type)
2209
+ * @param queue - The queue that will receive commands
2210
+ * @param exchange - The topic exchange that routes commands
1893
2211
  * @param message - The message definition (schema and metadata)
1894
- * @param options - Binding configuration (routingKey is required)
1895
- * @param options.routingKey - The routing key pattern for the binding (can use wildcards)
1896
- * @returns A consumer-first result with consumer, binding, and publisher factory that accepts a routing key
2212
+ * @param options - Configuration with required routing key pattern
2213
+ * @param options.routingKey - The routing key pattern for the binding
2214
+ * @param options.arguments - Additional AMQP arguments
2215
+ * @returns A command consumer configuration
1897
2216
  *
1898
2217
  * @example
1899
2218
  * ```typescript
1900
- * import { z } from 'zod';
1901
- *
1902
- * const orderQueue = defineQueue('order-processing', { durable: true });
1903
2219
  * const ordersExchange = defineExchange('orders', 'topic', { durable: true });
1904
- * const orderMessage = defineMessage(
1905
- * z.object({
1906
- * orderId: z.string(),
1907
- * amount: z.number(),
1908
- * })
1909
- * );
2220
+ * const orderMessage = defineMessage(z.object({ orderId: z.string() }));
1910
2221
  *
1911
- * // Create consumer-first relationship with pattern
1912
- * const { consumer: processOrderConsumer, binding: orderBinding, createPublisher: createOrderPublisher } = defineConsumerFirst(
1913
- * orderQueue,
1914
- * ordersExchange,
1915
- * orderMessage,
1916
- * { routingKey: 'order.*' } // Pattern in binding
1917
- * );
2222
+ * // Consumer uses pattern to receive multiple command types
2223
+ * const processOrder = defineCommandConsumer(orderQueue, ordersExchange, orderMessage, {
2224
+ * routingKey: 'order.*',
2225
+ * });
1918
2226
  *
1919
- * // Use in contract - publisher can specify concrete routing key
1920
- * const contract = defineContract({
1921
- * exchanges: { orders: ordersExchange },
1922
- * queues: { orderQueue },
1923
- * bindings: { orderBinding },
1924
- * publishers: {
1925
- * orderCreated: createOrderPublisher('order.created'), // Concrete key
1926
- * orderUpdated: createOrderPublisher('order.updated'), // Concrete key
1927
- * },
1928
- * consumers: { processOrder: processOrderConsumer },
2227
+ * // Publishers send with concrete keys
2228
+ * const createOrder = defineCommandPublisher(processOrder, {
2229
+ * routingKey: 'order.create',
2230
+ * });
2231
+ * const updateOrder = defineCommandPublisher(processOrder, {
2232
+ * routingKey: 'order.update',
1929
2233
  * });
1930
2234
  * ```
1931
2235
  */
1932
- declare function defineConsumerFirst<TMessage extends MessageDefinition, TRoutingKey extends string>(queue: QueueEntry, exchange: TopicExchangeDefinition, message: TMessage, options: {
2236
+ declare function defineCommandConsumer<TMessage extends MessageDefinition, TRoutingKey extends string>(queue: QueueEntry, exchange: TopicExchangeDefinition, message: TMessage, options: {
1933
2237
  routingKey: BindingPattern<TRoutingKey>;
1934
2238
  arguments?: Record<string, unknown>;
1935
- }): ConsumerFirstResultWithRoutingKey<TMessage, ConsumerDefinition<TMessage>, Extract<QueueBindingDefinition, {
2239
+ }): CommandConsumerConfig<TMessage, TopicExchangeDefinition, TRoutingKey>;
2240
+ /**
2241
+ * Create a publisher that sends commands to a fanout exchange consumer.
2242
+ *
2243
+ * @param commandConsumer - The command consumer configuration
2244
+ * @returns A publisher definition
2245
+ *
2246
+ * @example
2247
+ * ```typescript
2248
+ * const executeTask = defineCommandConsumer(taskQueue, fanoutExchange, taskMessage);
2249
+ * const sendTask = defineCommandPublisher(executeTask);
2250
+ * ```
2251
+ */
2252
+ declare function defineCommandPublisher<TMessage extends MessageDefinition>(commandConsumer: CommandConsumerConfig<TMessage, FanoutExchangeDefinition, undefined>): {
2253
+ message: TMessage;
2254
+ exchange: FanoutExchangeDefinition;
2255
+ };
2256
+ /**
2257
+ * Create a publisher that sends commands to a direct exchange consumer.
2258
+ *
2259
+ * @param commandConsumer - The command consumer configuration
2260
+ * @returns A publisher definition
2261
+ */
2262
+ declare function defineCommandPublisher<TMessage extends MessageDefinition, TRoutingKey extends string>(commandConsumer: CommandConsumerConfig<TMessage, DirectExchangeDefinition, TRoutingKey>): {
2263
+ message: TMessage;
2264
+ exchange: DirectExchangeDefinition;
2265
+ routingKey: string;
2266
+ };
2267
+ /**
2268
+ * Create a publisher that sends commands to a topic exchange consumer.
2269
+ *
2270
+ * For topic exchanges where the consumer uses a pattern, the publisher can
2271
+ * optionally specify a concrete routing key that matches the pattern.
2272
+ *
2273
+ * @param commandConsumer - The command consumer configuration
2274
+ * @param options - Optional publisher configuration
2275
+ * @param options.routingKey - Override routing key (must match consumer's pattern)
2276
+ * @returns A publisher definition
2277
+ *
2278
+ * @example
2279
+ * ```typescript
2280
+ * // Consumer binds with pattern
2281
+ * const processOrder = defineCommandConsumer(orderQueue, topicExchange, orderMessage, {
2282
+ * routingKey: 'order.*',
2283
+ * });
2284
+ *
2285
+ * // Publisher uses concrete key matching the pattern
2286
+ * const createOrder = defineCommandPublisher(processOrder, {
2287
+ * routingKey: 'order.create',
2288
+ * });
2289
+ * ```
2290
+ */
2291
+ declare function defineCommandPublisher<TMessage extends MessageDefinition, TRoutingKey extends string, TPublisherRoutingKey extends string = TRoutingKey>(commandConsumer: CommandConsumerConfig<TMessage, TopicExchangeDefinition, TRoutingKey>, options?: {
2292
+ routingKey?: RoutingKey<TPublisherRoutingKey>;
2293
+ }): {
2294
+ message: TMessage;
1936
2295
  exchange: TopicExchangeDefinition;
1937
- }>>;
2296
+ routingKey: string;
2297
+ };
2298
+ /**
2299
+ * Type guard to check if a value is a CommandConsumerConfig.
2300
+ *
2301
+ * @param value - The value to check
2302
+ * @returns True if the value is a CommandConsumerConfig
2303
+ */
2304
+ declare function isCommandConsumerConfig(value: unknown): value is CommandConsumerConfig<MessageDefinition, ExchangeDefinition, string | undefined>;
1938
2305
  //#endregion
1939
2306
  //#region src/builder/ttl-backoff.d.ts
1940
2307
  /**
@@ -2011,5 +2378,5 @@ declare function defineTtlBackoffRetryInfrastructure(queueEntry: QueueEntry, opt
2011
2378
  waitQueueDurable?: boolean;
2012
2379
  }): TtlBackoffRetryInfrastructure;
2013
2380
  //#endregion
2014
- export { type AnySchema, type BaseExchangeDefinition, type BindingDefinition, type BindingPattern, type ClassicQueueDefinition, type ClassicQueueOptions, type CompressionAlgorithm, type ConsumerDefinition, type ConsumerFirstResult, type ConsumerFirstResultWithRoutingKey, type ContractDefinition, type DeadLetterConfig, type DefineQueueOptions, type DirectExchangeDefinition, type ExchangeBindingDefinition, type ExchangeDefinition, type FanoutExchangeDefinition, type InferConsumerNames, type InferPublisherNames, type MatchingRoutingKey, type MessageDefinition, type PublisherDefinition, type PublisherFirstResult, type PublisherFirstResultWithRoutingKey, type QueueBindingDefinition, type QueueDefinition, type QueueEntry, type QueueType, type QueueWithTtlBackoffInfrastructure, type QuorumNativeRetryOptions, type QuorumQueueDefinition, type QuorumQueueOptions, type ResolvedRetryOptions, type ResolvedTtlBackoffRetryOptions, type RoutingKey, type TopicExchangeDefinition, type TtlBackoffRetryInfrastructure, type TtlBackoffRetryOptions, defineConsumer, defineConsumerFirst, defineContract, defineExchange, defineExchangeBinding, defineMessage, definePublisher, definePublisherFirst, defineQueue, defineQueueBinding, defineTtlBackoffRetryInfrastructure, extractQueue };
2381
+ export { type AnySchema, type BaseExchangeDefinition, type BindingDefinition, type BindingPattern, type ClassicQueueDefinition, type ClassicQueueOptions, type CommandConsumerConfig, type CommandConsumerConfigBase, type CompressionAlgorithm, type ConsumerDefinition, type ConsumerEntry, type ContractDefinition, type ContractDefinitionInput, type DeadLetterConfig, type DefineQueueOptions, type DefineQuorumQueueOptions, type DefineTtlBackoffQueueOptions, type DirectExchangeDefinition, type EventConsumerResult, type EventConsumerResultBase, type EventPublisherConfig, type EventPublisherConfigBase, type ExchangeBindingDefinition, type ExchangeDefinition, type FanoutExchangeDefinition, type InferConsumerNames, type InferPublisherNames, type MatchingRoutingKey, type MessageDefinition, type PublisherDefinition, type PublisherEntry, type QueueBindingDefinition, type QueueDefinition, type QueueEntry, type QueueType, type QueueWithTtlBackoffInfrastructure, type QuorumNativeRetryOptions, type QuorumQueueDefinition, type QuorumQueueOptions, type ResolvedRetryOptions, type ResolvedTtlBackoffRetryOptions, type RoutingKey, type TopicExchangeDefinition, type TtlBackoffRetryInfrastructure, type TtlBackoffRetryOptions, defineCommandConsumer, defineCommandPublisher, defineConsumer, defineContract, defineEventConsumer, defineEventPublisher, defineExchange, defineExchangeBinding, defineMessage, definePublisher, defineQueue, defineQueueBinding, defineQuorumQueue, defineTtlBackoffQueue, defineTtlBackoffRetryInfrastructure, extractConsumer, extractQueue, isCommandConsumerConfig, isEventConsumerResult, isEventPublisherConfig, isQueueWithTtlBackoffInfrastructure };
2015
2382
  //# sourceMappingURL=index.d.mts.map