@amqp-contract/contract 0.2.0 → 0.3.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 +11 -1
- package/dist/index.cjs +201 -8
- package/dist/index.d.cts +691 -72
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +691 -72
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +201 -8
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +766 -0
- package/package.json +8 -3
package/docs/index.md
ADDED
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
**@amqp-contract/contract**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @amqp-contract/contract
|
|
6
|
+
|
|
7
|
+
## Type Aliases
|
|
8
|
+
|
|
9
|
+
### AnySchema
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
type AnySchema = StandardSchemaV1;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Defined in: [types.ts:12](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L12)
|
|
16
|
+
|
|
17
|
+
Any schema that conforms to Standard Schema v1.
|
|
18
|
+
|
|
19
|
+
This library supports any validation library that implements the Standard Schema v1 specification,
|
|
20
|
+
including Zod, Valibot, and ArkType. This allows you to use your preferred validation library
|
|
21
|
+
while maintaining type safety.
|
|
22
|
+
|
|
23
|
+
#### See
|
|
24
|
+
|
|
25
|
+
https://github.com/standard-schema/standard-schema
|
|
26
|
+
|
|
27
|
+
***
|
|
28
|
+
|
|
29
|
+
### BaseExchangeDefinition
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
type BaseExchangeDefinition = object;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Defined in: [types.ts:20](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L20)
|
|
36
|
+
|
|
37
|
+
Base definition of an AMQP exchange.
|
|
38
|
+
|
|
39
|
+
An exchange receives messages from publishers and routes them to queues based on the exchange
|
|
40
|
+
type and routing rules. This type contains properties common to all exchange types.
|
|
41
|
+
|
|
42
|
+
#### Properties
|
|
43
|
+
|
|
44
|
+
| Property | Type | Description | Defined in |
|
|
45
|
+
| ------ | ------ | ------ | ------ |
|
|
46
|
+
| <a id="arguments"></a> `arguments?` | `Record`\<`string`, `unknown`\> | Additional AMQP arguments for advanced configuration. Common arguments include alternate-exchange for handling unroutable messages. | [types.ts:49](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L49) |
|
|
47
|
+
| <a id="autodelete"></a> `autoDelete?` | `boolean` | If true, the exchange is deleted when all queues have finished using it. **Default** `false` | [types.ts:36](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L36) |
|
|
48
|
+
| <a id="durable"></a> `durable?` | `boolean` | If true, the exchange survives broker restarts. Durable exchanges are persisted to disk. **Default** `false` | [types.ts:30](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L30) |
|
|
49
|
+
| <a id="internal"></a> `internal?` | `boolean` | If true, the exchange cannot be directly published to by clients. It can only receive messages from other exchanges via exchange-to-exchange bindings. **Default** `false` | [types.ts:43](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L43) |
|
|
50
|
+
| <a id="name"></a> `name` | `string` | The name of the exchange. Must be unique within the RabbitMQ virtual host. | [types.ts:24](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L24) |
|
|
51
|
+
|
|
52
|
+
***
|
|
53
|
+
|
|
54
|
+
### BindingDefinition
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
type BindingDefinition =
|
|
58
|
+
| QueueBindingDefinition
|
|
59
|
+
| ExchangeBindingDefinition;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Defined in: [types.ts:298](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L298)
|
|
63
|
+
|
|
64
|
+
Union type of all binding definitions.
|
|
65
|
+
|
|
66
|
+
A binding can be either:
|
|
67
|
+
- Queue-to-exchange binding: Routes messages from an exchange to a queue
|
|
68
|
+
- Exchange-to-exchange binding: Forwards messages from one exchange to another
|
|
69
|
+
|
|
70
|
+
***
|
|
71
|
+
|
|
72
|
+
### ConsumerDefinition
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
type ConsumerDefinition<TMessage> = object;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Defined in: [types.ts:354](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L354)
|
|
79
|
+
|
|
80
|
+
Definition of a message consumer.
|
|
81
|
+
|
|
82
|
+
A consumer receives and processes messages from a queue with automatic schema validation.
|
|
83
|
+
The message payload is validated against the schema before being passed to your handler.
|
|
84
|
+
|
|
85
|
+
#### Example
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const consumer: ConsumerDefinition = {
|
|
89
|
+
queue: orderProcessingQueue,
|
|
90
|
+
message: orderMessage
|
|
91
|
+
};
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Type Parameters
|
|
95
|
+
|
|
96
|
+
| Type Parameter | Default type | Description |
|
|
97
|
+
| ------ | ------ | ------ |
|
|
98
|
+
| `TMessage` *extends* [`MessageDefinition`](#messagedefinition) | [`MessageDefinition`](#messagedefinition) | The message definition with payload schema |
|
|
99
|
+
|
|
100
|
+
#### Properties
|
|
101
|
+
|
|
102
|
+
| Property | Type | Description | Defined in |
|
|
103
|
+
| ------ | ------ | ------ | ------ |
|
|
104
|
+
| <a id="message"></a> `message` | `TMessage` | The message definition including the payload schema | [types.ts:359](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L359) |
|
|
105
|
+
| <a id="queue"></a> `queue` | [`QueueDefinition`](#queuedefinition) | The queue to consume messages from | [types.ts:356](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L356) |
|
|
106
|
+
|
|
107
|
+
***
|
|
108
|
+
|
|
109
|
+
### ContractDefinition
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
type ContractDefinition = object;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Defined in: [types.ts:395](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L395)
|
|
116
|
+
|
|
117
|
+
Complete AMQP contract definition.
|
|
118
|
+
|
|
119
|
+
A contract brings together all AMQP resources into a single, type-safe definition.
|
|
120
|
+
It defines the complete messaging topology including exchanges, queues, bindings,
|
|
121
|
+
publishers, and consumers.
|
|
122
|
+
|
|
123
|
+
The contract is used by:
|
|
124
|
+
- Clients (TypedAmqpClient) for type-safe message publishing
|
|
125
|
+
- Workers (TypedAmqpWorker) for type-safe message consumption
|
|
126
|
+
- AsyncAPI generator for documentation
|
|
127
|
+
|
|
128
|
+
#### Example
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const contract: ContractDefinition = {
|
|
132
|
+
exchanges: {
|
|
133
|
+
orders: ordersExchange,
|
|
134
|
+
},
|
|
135
|
+
queues: {
|
|
136
|
+
orderProcessing: orderProcessingQueue,
|
|
137
|
+
},
|
|
138
|
+
bindings: {
|
|
139
|
+
orderBinding: orderQueueBinding,
|
|
140
|
+
},
|
|
141
|
+
publishers: {
|
|
142
|
+
orderCreated: orderCreatedPublisher,
|
|
143
|
+
},
|
|
144
|
+
consumers: {
|
|
145
|
+
processOrder: processOrderConsumer,
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Properties
|
|
151
|
+
|
|
152
|
+
| Property | Type | Description | Defined in |
|
|
153
|
+
| ------ | ------ | ------ | ------ |
|
|
154
|
+
| <a id="bindings"></a> `bindings?` | `Record`\<`string`, [`BindingDefinition`](#bindingdefinition)\> | Named binding definitions. Bindings can be queue-to-exchange or exchange-to-exchange. | [types.ts:412](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L412) |
|
|
155
|
+
| <a id="consumers"></a> `consumers?` | `Record`\<`string`, [`ConsumerDefinition`](#consumerdefinition)\> | Named consumer definitions. Each key requires a corresponding handler in the TypedAmqpWorker. The handler will be fully typed based on the message schema. | [types.ts:426](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L426) |
|
|
156
|
+
| <a id="exchanges"></a> `exchanges?` | `Record`\<`string`, [`ExchangeDefinition`](#exchangedefinition)\> | Named exchange definitions. Each key becomes available as a named resource in the contract. | [types.ts:400](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L400) |
|
|
157
|
+
| <a id="publishers"></a> `publishers?` | `Record`\<`string`, [`PublisherDefinition`](#publisherdefinition)\> | Named publisher definitions. Each key becomes a method on the TypedAmqpClient for publishing messages. The method will be fully typed based on the message schema. | [types.ts:419](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L419) |
|
|
158
|
+
| <a id="queues"></a> `queues?` | `Record`\<`string`, [`QueueDefinition`](#queuedefinition)\> | Named queue definitions. Each key becomes available as a named resource in the contract. | [types.ts:406](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L406) |
|
|
159
|
+
|
|
160
|
+
***
|
|
161
|
+
|
|
162
|
+
### DirectExchangeDefinition
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
type DirectExchangeDefinition = BaseExchangeDefinition & object;
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Defined in: [types.ts:82](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L82)
|
|
169
|
+
|
|
170
|
+
A direct exchange definition.
|
|
171
|
+
|
|
172
|
+
Direct exchanges route messages to queues based on exact routing key matches.
|
|
173
|
+
This is ideal for point-to-point messaging where each message should go to specific queues.
|
|
174
|
+
|
|
175
|
+
#### Type declaration
|
|
176
|
+
|
|
177
|
+
| Name | Type | Defined in |
|
|
178
|
+
| ------ | ------ | ------ |
|
|
179
|
+
| `type` | `"direct"` | [types.ts:83](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L83) |
|
|
180
|
+
|
|
181
|
+
#### Example
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
const tasksExchange: DirectExchangeDefinition = defineExchange('tasks', 'direct', {
|
|
185
|
+
durable: true
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
***
|
|
190
|
+
|
|
191
|
+
### ExchangeBindingDefinition
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
type ExchangeBindingDefinition = object &
|
|
195
|
+
| {
|
|
196
|
+
routingKey: string;
|
|
197
|
+
source: | DirectExchangeDefinition
|
|
198
|
+
| TopicExchangeDefinition;
|
|
199
|
+
}
|
|
200
|
+
| {
|
|
201
|
+
routingKey?: never;
|
|
202
|
+
source: FanoutExchangeDefinition;
|
|
203
|
+
};
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Defined in: [types.ts:262](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L262)
|
|
207
|
+
|
|
208
|
+
Binding between two exchanges (exchange-to-exchange routing).
|
|
209
|
+
|
|
210
|
+
Defines how messages should be forwarded from a source exchange to a destination exchange.
|
|
211
|
+
This allows for more complex routing topologies.
|
|
212
|
+
|
|
213
|
+
#### Type declaration
|
|
214
|
+
|
|
215
|
+
| Name | Type | Description | Defined in |
|
|
216
|
+
| ------ | ------ | ------ | ------ |
|
|
217
|
+
| `arguments?` | `Record`\<`string`, `unknown`\> | Additional AMQP arguments for the binding. | [types.ts:272](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L272) |
|
|
218
|
+
| `destination` | [`ExchangeDefinition`](#exchangedefinition) | The destination exchange that will receive forwarded messages | [types.ts:267](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L267) |
|
|
219
|
+
| `type` | `"exchange"` | Discriminator indicating this is an exchange-to-exchange binding | [types.ts:264](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L264) |
|
|
220
|
+
|
|
221
|
+
#### Example
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// Forward high-priority orders to a special processing exchange
|
|
225
|
+
const binding: ExchangeBindingDefinition = {
|
|
226
|
+
type: 'exchange',
|
|
227
|
+
source: ordersExchange,
|
|
228
|
+
destination: highPriorityExchange,
|
|
229
|
+
routingKey: 'order.high-priority.*'
|
|
230
|
+
};
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
***
|
|
234
|
+
|
|
235
|
+
### ExchangeDefinition
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
type ExchangeDefinition =
|
|
239
|
+
| FanoutExchangeDefinition
|
|
240
|
+
| DirectExchangeDefinition
|
|
241
|
+
| TopicExchangeDefinition;
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Defined in: [types.ts:112](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L112)
|
|
245
|
+
|
|
246
|
+
Union type of all exchange definitions.
|
|
247
|
+
|
|
248
|
+
Represents any type of AMQP exchange: fanout, direct, or topic.
|
|
249
|
+
|
|
250
|
+
***
|
|
251
|
+
|
|
252
|
+
### FanoutExchangeDefinition
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
type FanoutExchangeDefinition = BaseExchangeDefinition & object;
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Defined in: [types.ts:65](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L65)
|
|
259
|
+
|
|
260
|
+
A fanout exchange definition.
|
|
261
|
+
|
|
262
|
+
Fanout exchanges broadcast all messages to all bound queues, ignoring routing keys.
|
|
263
|
+
This is the simplest exchange type for pub/sub messaging patterns.
|
|
264
|
+
|
|
265
|
+
#### Type declaration
|
|
266
|
+
|
|
267
|
+
| Name | Type | Defined in |
|
|
268
|
+
| ------ | ------ | ------ |
|
|
269
|
+
| `type` | `"fanout"` | [types.ts:66](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L66) |
|
|
270
|
+
|
|
271
|
+
#### Example
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
const logsExchange: FanoutExchangeDefinition = defineExchange('logs', 'fanout', {
|
|
275
|
+
durable: true
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
***
|
|
280
|
+
|
|
281
|
+
### InferConsumerNames
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
type InferConsumerNames<TContract> = TContract["consumers"] extends Record<string, unknown> ? keyof TContract["consumers"] : never;
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Defined in: [types.ts:462](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L462)
|
|
288
|
+
|
|
289
|
+
Extract consumer names from a contract.
|
|
290
|
+
|
|
291
|
+
This utility type extracts the keys of all consumers defined in a contract.
|
|
292
|
+
It's used internally for type inference in the TypedAmqpWorker.
|
|
293
|
+
|
|
294
|
+
#### Type Parameters
|
|
295
|
+
|
|
296
|
+
| Type Parameter | Description |
|
|
297
|
+
| ------ | ------ |
|
|
298
|
+
| `TContract` *extends* [`ContractDefinition`](#contractdefinition) | The contract definition |
|
|
299
|
+
|
|
300
|
+
#### Returns
|
|
301
|
+
|
|
302
|
+
Union of consumer names, or never if no consumers defined
|
|
303
|
+
|
|
304
|
+
#### Example
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
type ConsumerNames = InferConsumerNames<typeof myContract>;
|
|
308
|
+
// Result: 'processOrder' | 'sendNotification' | 'updateInventory'
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
***
|
|
312
|
+
|
|
313
|
+
### InferPublisherNames
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
type InferPublisherNames<TContract> = TContract["publishers"] extends Record<string, unknown> ? keyof TContract["publishers"] : never;
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Defined in: [types.ts:444](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L444)
|
|
320
|
+
|
|
321
|
+
Extract publisher names from a contract.
|
|
322
|
+
|
|
323
|
+
This utility type extracts the keys of all publishers defined in a contract.
|
|
324
|
+
It's used internally for type inference in the TypedAmqpClient.
|
|
325
|
+
|
|
326
|
+
#### Type Parameters
|
|
327
|
+
|
|
328
|
+
| Type Parameter | Description |
|
|
329
|
+
| ------ | ------ |
|
|
330
|
+
| `TContract` *extends* [`ContractDefinition`](#contractdefinition) | The contract definition |
|
|
331
|
+
|
|
332
|
+
#### Returns
|
|
333
|
+
|
|
334
|
+
Union of publisher names, or never if no publishers defined
|
|
335
|
+
|
|
336
|
+
#### Example
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
type PublisherNames = InferPublisherNames<typeof myContract>;
|
|
340
|
+
// Result: 'orderCreated' | 'orderUpdated' | 'orderCancelled'
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
***
|
|
344
|
+
|
|
345
|
+
### MessageDefinition
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
type MessageDefinition<TPayload, THeaders> = object;
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
Defined in: [types.ts:178](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L178)
|
|
352
|
+
|
|
353
|
+
Definition of a message with typed payload and optional headers.
|
|
354
|
+
|
|
355
|
+
#### Type Parameters
|
|
356
|
+
|
|
357
|
+
| Type Parameter | Default type | Description |
|
|
358
|
+
| ------ | ------ | ------ |
|
|
359
|
+
| `TPayload` *extends* [`AnySchema`](#anyschema) | [`AnySchema`](#anyschema) | The Standard Schema v1 compatible schema for the message payload |
|
|
360
|
+
| `THeaders` *extends* `StandardSchemaV1`\<`Record`\<`string`, `unknown`\>\> \| `undefined` | `undefined` | The Standard Schema v1 compatible schema for the message headers (optional) |
|
|
361
|
+
|
|
362
|
+
#### Properties
|
|
363
|
+
|
|
364
|
+
| Property | Type | Description | Defined in |
|
|
365
|
+
| ------ | ------ | ------ | ------ |
|
|
366
|
+
| <a id="description"></a> `description?` | `string` | Detailed description of the message for documentation purposes. Used in AsyncAPI specification generation. | [types.ts:204](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L204) |
|
|
367
|
+
| <a id="headers"></a> `headers?` | `THeaders` | Optional headers schema for validating message metadata. Must be a Standard Schema v1 compatible schema. | [types.ts:192](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L192) |
|
|
368
|
+
| <a id="payload"></a> `payload` | `TPayload` | The payload schema for validating message content. Must be a Standard Schema v1 compatible schema (Zod, Valibot, ArkType, etc.). | [types.ts:186](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L186) |
|
|
369
|
+
| <a id="summary"></a> `summary?` | `string` | Brief description of the message for documentation purposes. Used in AsyncAPI specification generation. | [types.ts:198](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L198) |
|
|
370
|
+
|
|
371
|
+
***
|
|
372
|
+
|
|
373
|
+
### PublisherDefinition
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
type PublisherDefinition<TMessage> = object &
|
|
377
|
+
| {
|
|
378
|
+
exchange: | DirectExchangeDefinition
|
|
379
|
+
| TopicExchangeDefinition;
|
|
380
|
+
routingKey: string;
|
|
381
|
+
}
|
|
382
|
+
| {
|
|
383
|
+
exchange: FanoutExchangeDefinition;
|
|
384
|
+
routingKey?: never;
|
|
385
|
+
};
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
Defined in: [types.ts:317](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L317)
|
|
389
|
+
|
|
390
|
+
Definition of a message publisher.
|
|
391
|
+
|
|
392
|
+
A publisher sends messages to an exchange with automatic schema validation.
|
|
393
|
+
The message payload is validated against the schema before being sent to RabbitMQ.
|
|
394
|
+
|
|
395
|
+
#### Type declaration
|
|
396
|
+
|
|
397
|
+
| Name | Type | Description | Defined in |
|
|
398
|
+
| ------ | ------ | ------ | ------ |
|
|
399
|
+
| `message` | `TMessage` | The message definition including the payload schema | [types.ts:319](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L319) |
|
|
400
|
+
|
|
401
|
+
#### Type Parameters
|
|
402
|
+
|
|
403
|
+
| Type Parameter | Default type | Description |
|
|
404
|
+
| ------ | ------ | ------ |
|
|
405
|
+
| `TMessage` *extends* [`MessageDefinition`](#messagedefinition) | [`MessageDefinition`](#messagedefinition) | The message definition with payload schema |
|
|
406
|
+
|
|
407
|
+
#### Example
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
const publisher: PublisherDefinition = {
|
|
411
|
+
exchange: ordersExchange,
|
|
412
|
+
message: orderMessage,
|
|
413
|
+
routingKey: 'order.created'
|
|
414
|
+
};
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
***
|
|
418
|
+
|
|
419
|
+
### QueueBindingDefinition
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
type QueueBindingDefinition = object &
|
|
423
|
+
| {
|
|
424
|
+
exchange: | DirectExchangeDefinition
|
|
425
|
+
| TopicExchangeDefinition;
|
|
426
|
+
routingKey: string;
|
|
427
|
+
}
|
|
428
|
+
| {
|
|
429
|
+
exchange: FanoutExchangeDefinition;
|
|
430
|
+
routingKey?: never;
|
|
431
|
+
};
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
Defined in: [types.ts:214](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L214)
|
|
435
|
+
|
|
436
|
+
Binding between a queue and an exchange.
|
|
437
|
+
|
|
438
|
+
Defines how messages from an exchange should be routed to a queue.
|
|
439
|
+
For direct and topic exchanges, a routing key is required.
|
|
440
|
+
For fanout exchanges, no routing key is needed as all messages are broadcast.
|
|
441
|
+
|
|
442
|
+
#### Type declaration
|
|
443
|
+
|
|
444
|
+
| Name | Type | Description | Defined in |
|
|
445
|
+
| ------ | ------ | ------ | ------ |
|
|
446
|
+
| `arguments?` | `Record`\<`string`, `unknown`\> | Additional AMQP arguments for the binding. Can be used for advanced routing scenarios with the headers exchange type. | [types.ts:225](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L225) |
|
|
447
|
+
| `queue` | [`QueueDefinition`](#queuedefinition) | The queue that will receive messages | [types.ts:219](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L219) |
|
|
448
|
+
| `type` | `"queue"` | Discriminator indicating this is a queue-to-exchange binding | [types.ts:216](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L216) |
|
|
449
|
+
|
|
450
|
+
***
|
|
451
|
+
|
|
452
|
+
### QueueDefinition
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
type QueueDefinition = object;
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
Defined in: [types.ts:123](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L123)
|
|
459
|
+
|
|
460
|
+
Definition of an AMQP queue.
|
|
461
|
+
|
|
462
|
+
A queue stores messages until they are consumed by workers. Queues are bound to exchanges
|
|
463
|
+
to receive messages based on routing rules.
|
|
464
|
+
|
|
465
|
+
#### Properties
|
|
466
|
+
|
|
467
|
+
| Property | Type | Description | Defined in |
|
|
468
|
+
| ------ | ------ | ------ | ------ |
|
|
469
|
+
| <a id="arguments-1"></a> `arguments?` | `Record`\<`string`, `unknown`\> | Additional AMQP arguments for advanced configuration. Common arguments include: - `x-message-ttl`: Message time-to-live in milliseconds - `x-expires`: Queue expiration time in milliseconds - `x-max-length`: Maximum number of messages in the queue - `x-max-length-bytes`: Maximum size of the queue in bytes - `x-dead-letter-exchange`: Exchange for dead-lettered messages - `x-dead-letter-routing-key`: Routing key for dead-lettered messages - `x-max-priority`: Maximum priority level for priority queues **Example** `{ 'x-message-ttl': 86400000, // 24 hours 'x-dead-letter-exchange': 'dlx', 'x-max-priority': 10 }` | [types.ts:169](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L169) |
|
|
470
|
+
| <a id="autodelete-1"></a> `autoDelete?` | `boolean` | If true, the queue is deleted when the last consumer unsubscribes. **Default** `false` | [types.ts:146](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L146) |
|
|
471
|
+
| <a id="durable-1"></a> `durable?` | `boolean` | If true, the queue survives broker restarts. Durable queues are persisted to disk. **Default** `false` | [types.ts:133](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L133) |
|
|
472
|
+
| <a id="exclusive"></a> `exclusive?` | `boolean` | If true, the queue can only be used by the declaring connection and is deleted when that connection closes. Exclusive queues are private to the connection. **Default** `false` | [types.ts:140](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L140) |
|
|
473
|
+
| <a id="name-1"></a> `name` | `string` | The name of the queue. Must be unique within the RabbitMQ virtual host. | [types.ts:127](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L127) |
|
|
474
|
+
|
|
475
|
+
***
|
|
476
|
+
|
|
477
|
+
### TopicExchangeDefinition
|
|
478
|
+
|
|
479
|
+
```ts
|
|
480
|
+
type TopicExchangeDefinition = BaseExchangeDefinition & object;
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
Defined in: [types.ts:103](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L103)
|
|
484
|
+
|
|
485
|
+
A topic exchange definition.
|
|
486
|
+
|
|
487
|
+
Topic exchanges route messages to queues based on routing key patterns with wildcards:
|
|
488
|
+
- `*` (star) matches exactly one word
|
|
489
|
+
- `#` (hash) matches zero or more words
|
|
490
|
+
|
|
491
|
+
Words are separated by dots (e.g., `order.created.high-value`).
|
|
492
|
+
|
|
493
|
+
#### Type declaration
|
|
494
|
+
|
|
495
|
+
| Name | Type | Defined in |
|
|
496
|
+
| ------ | ------ | ------ |
|
|
497
|
+
| `type` | `"topic"` | [types.ts:104](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/types.ts#L104) |
|
|
498
|
+
|
|
499
|
+
#### Example
|
|
500
|
+
|
|
501
|
+
```typescript
|
|
502
|
+
const ordersExchange: TopicExchangeDefinition = defineExchange('orders', 'topic', {
|
|
503
|
+
durable: true
|
|
504
|
+
});
|
|
505
|
+
// Can be bound with patterns like 'order.*' or 'order.#'
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
## Functions
|
|
509
|
+
|
|
510
|
+
### defineConsumer()
|
|
511
|
+
|
|
512
|
+
```ts
|
|
513
|
+
function defineConsumer<TMessage>(
|
|
514
|
+
queue,
|
|
515
|
+
message,
|
|
516
|
+
options?): ConsumerDefinition<TMessage>;
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
Defined in: [builder.ts:595](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/builder.ts#L595)
|
|
520
|
+
|
|
521
|
+
Define a message consumer.
|
|
522
|
+
|
|
523
|
+
A consumer receives and processes messages from a queue. The message schema is validated
|
|
524
|
+
automatically when messages are consumed, ensuring type safety for your handlers.
|
|
525
|
+
|
|
526
|
+
Consumers are associated with a specific queue and message type. When you create a worker
|
|
527
|
+
with this consumer, it will process messages from the queue according to the schema.
|
|
528
|
+
|
|
529
|
+
#### Type Parameters
|
|
530
|
+
|
|
531
|
+
| Type Parameter |
|
|
532
|
+
| ------ |
|
|
533
|
+
| `TMessage` *extends* [`MessageDefinition`](#messagedefinition) |
|
|
534
|
+
|
|
535
|
+
#### Parameters
|
|
536
|
+
|
|
537
|
+
| Parameter | Type | Description |
|
|
538
|
+
| ------ | ------ | ------ |
|
|
539
|
+
| `queue` | [`QueueDefinition`](#queuedefinition) | The queue definition to consume from |
|
|
540
|
+
| `message` | `TMessage` | The message definition with payload schema |
|
|
541
|
+
| `options?` | `Omit`\<[`ConsumerDefinition`](#consumerdefinition)\<`TMessage`\>, `"queue"` \| `"message"`\> | Optional consumer configuration |
|
|
542
|
+
|
|
543
|
+
#### Returns
|
|
544
|
+
|
|
545
|
+
[`ConsumerDefinition`](#consumerdefinition)\<`TMessage`\>
|
|
546
|
+
|
|
547
|
+
A consumer definition with inferred message types
|
|
548
|
+
|
|
549
|
+
#### Example
|
|
550
|
+
|
|
551
|
+
```typescript
|
|
552
|
+
import { z } from 'zod';
|
|
553
|
+
|
|
554
|
+
const orderQueue = defineQueue('order-processing', { durable: true });
|
|
555
|
+
const orderMessage = defineMessage(
|
|
556
|
+
z.object({
|
|
557
|
+
orderId: z.string().uuid(),
|
|
558
|
+
customerId: z.string().uuid(),
|
|
559
|
+
amount: z.number().positive(),
|
|
560
|
+
})
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
const processOrderConsumer = defineConsumer(orderQueue, orderMessage);
|
|
564
|
+
|
|
565
|
+
// Later, when creating a worker, you'll provide a handler for this consumer:
|
|
566
|
+
// const worker = await TypedAmqpWorker.create({
|
|
567
|
+
// contract,
|
|
568
|
+
// handlers: {
|
|
569
|
+
// processOrder: async (message) => {
|
|
570
|
+
// // message is automatically typed based on the schema
|
|
571
|
+
// console.log(message.orderId); // string
|
|
572
|
+
// }
|
|
573
|
+
// },
|
|
574
|
+
// connection
|
|
575
|
+
// });
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
***
|
|
579
|
+
|
|
580
|
+
### defineContract()
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
function defineContract<TContract>(definition): TContract;
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
Defined in: [builder.ts:676](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/builder.ts#L676)
|
|
587
|
+
|
|
588
|
+
Define an AMQP contract.
|
|
589
|
+
|
|
590
|
+
A contract is the central definition of your AMQP messaging topology. It brings together
|
|
591
|
+
all exchanges, queues, bindings, publishers, and consumers in a single, type-safe definition.
|
|
592
|
+
|
|
593
|
+
The contract is used by both clients (for publishing) and workers (for consuming) to ensure
|
|
594
|
+
type safety throughout your messaging infrastructure. TypeScript will infer all message types
|
|
595
|
+
and publisher/consumer names from the contract.
|
|
596
|
+
|
|
597
|
+
#### Type Parameters
|
|
598
|
+
|
|
599
|
+
| Type Parameter |
|
|
600
|
+
| ------ |
|
|
601
|
+
| `TContract` *extends* [`ContractDefinition`](#contractdefinition) |
|
|
602
|
+
|
|
603
|
+
#### Parameters
|
|
604
|
+
|
|
605
|
+
| Parameter | Type | Description |
|
|
606
|
+
| ------ | ------ | ------ |
|
|
607
|
+
| `definition` | `TContract` | The contract definition containing all AMQP resources |
|
|
608
|
+
|
|
609
|
+
#### Returns
|
|
610
|
+
|
|
611
|
+
`TContract`
|
|
612
|
+
|
|
613
|
+
The same contract definition with full type inference
|
|
614
|
+
|
|
615
|
+
#### Example
|
|
616
|
+
|
|
617
|
+
```typescript
|
|
618
|
+
import {
|
|
619
|
+
defineContract,
|
|
620
|
+
defineExchange,
|
|
621
|
+
defineQueue,
|
|
622
|
+
defineQueueBinding,
|
|
623
|
+
definePublisher,
|
|
624
|
+
defineConsumer,
|
|
625
|
+
defineMessage,
|
|
626
|
+
} from '@amqp-contract/contract';
|
|
627
|
+
import { z } from 'zod';
|
|
628
|
+
|
|
629
|
+
// Define resources
|
|
630
|
+
const ordersExchange = defineExchange('orders', 'topic', { durable: true });
|
|
631
|
+
const orderQueue = defineQueue('order-processing', { durable: true });
|
|
632
|
+
const orderMessage = defineMessage(
|
|
633
|
+
z.object({
|
|
634
|
+
orderId: z.string(),
|
|
635
|
+
amount: z.number(),
|
|
636
|
+
})
|
|
637
|
+
);
|
|
638
|
+
|
|
639
|
+
// Compose contract
|
|
640
|
+
export const contract = defineContract({
|
|
641
|
+
exchanges: {
|
|
642
|
+
orders: ordersExchange,
|
|
643
|
+
},
|
|
644
|
+
queues: {
|
|
645
|
+
orderProcessing: orderQueue,
|
|
646
|
+
},
|
|
647
|
+
bindings: {
|
|
648
|
+
orderBinding: defineQueueBinding(orderQueue, ordersExchange, {
|
|
649
|
+
routingKey: 'order.created',
|
|
650
|
+
}),
|
|
651
|
+
},
|
|
652
|
+
publishers: {
|
|
653
|
+
orderCreated: definePublisher(ordersExchange, orderMessage, {
|
|
654
|
+
routingKey: 'order.created',
|
|
655
|
+
}),
|
|
656
|
+
},
|
|
657
|
+
consumers: {
|
|
658
|
+
processOrder: defineConsumer(orderQueue, orderMessage),
|
|
659
|
+
},
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
// TypeScript now knows:
|
|
663
|
+
// - client.publish('orderCreated', { orderId: string, amount: number })
|
|
664
|
+
// - handler: async (message: { orderId: string, amount: number }) => void
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
***
|
|
668
|
+
|
|
669
|
+
### defineMessage()
|
|
670
|
+
|
|
671
|
+
```ts
|
|
672
|
+
function defineMessage<TPayload, THeaders>(payload, options?): MessageDefinition<TPayload, THeaders>;
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
Defined in: [builder.ts:197](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/builder.ts#L197)
|
|
676
|
+
|
|
677
|
+
Define a message definition with payload and optional headers/metadata.
|
|
678
|
+
|
|
679
|
+
A message definition specifies the schema for message payloads and headers using
|
|
680
|
+
Standard Schema v1 compatible libraries (Zod, Valibot, ArkType, etc.).
|
|
681
|
+
The schemas are used for automatic validation when publishing or consuming messages.
|
|
682
|
+
|
|
683
|
+
#### Type Parameters
|
|
684
|
+
|
|
685
|
+
| Type Parameter | Default type |
|
|
686
|
+
| ------ | ------ |
|
|
687
|
+
| `TPayload` *extends* [`AnySchema`](#anyschema) | - |
|
|
688
|
+
| `THeaders` *extends* \| `undefined` \| `StandardSchemaV1`\<`Record`\<`string`, `unknown`\>, `Record`\<`string`, `unknown`\>\> | `undefined` |
|
|
689
|
+
|
|
690
|
+
#### Parameters
|
|
691
|
+
|
|
692
|
+
| Parameter | Type | Description |
|
|
693
|
+
| ------ | ------ | ------ |
|
|
694
|
+
| `payload` | `TPayload` | The payload schema (must be Standard Schema v1 compatible) |
|
|
695
|
+
| `options?` | \{ `description?`: `string`; `headers?`: `THeaders`; `summary?`: `string`; \} | Optional message metadata |
|
|
696
|
+
| `options.description?` | `string` | Detailed description for documentation (used in AsyncAPI generation) |
|
|
697
|
+
| `options.headers?` | `THeaders` | Optional header schema for message headers |
|
|
698
|
+
| `options.summary?` | `string` | Brief description for documentation (used in AsyncAPI generation) |
|
|
699
|
+
|
|
700
|
+
#### Returns
|
|
701
|
+
|
|
702
|
+
[`MessageDefinition`](#messagedefinition)\<`TPayload`, `THeaders`\>
|
|
703
|
+
|
|
704
|
+
A message definition with inferred types
|
|
705
|
+
|
|
706
|
+
#### Example
|
|
707
|
+
|
|
708
|
+
```typescript
|
|
709
|
+
import { z } from 'zod';
|
|
710
|
+
|
|
711
|
+
const orderMessage = defineMessage(
|
|
712
|
+
z.object({
|
|
713
|
+
orderId: z.string().uuid(),
|
|
714
|
+
customerId: z.string().uuid(),
|
|
715
|
+
amount: z.number().positive(),
|
|
716
|
+
items: z.array(z.object({
|
|
717
|
+
productId: z.string(),
|
|
718
|
+
quantity: z.number().int().positive(),
|
|
719
|
+
})),
|
|
720
|
+
}),
|
|
721
|
+
{
|
|
722
|
+
summary: 'Order created event',
|
|
723
|
+
description: 'Emitted when a new order is created in the system'
|
|
724
|
+
}
|
|
725
|
+
);
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
***
|
|
729
|
+
|
|
730
|
+
### defineQueue()
|
|
731
|
+
|
|
732
|
+
```ts
|
|
733
|
+
function defineQueue(name, options?): QueueDefinition;
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
Defined in: [builder.ts:152](https://github.com/btravers/amqp-contract/blob/63bb54252f8a9109544152686427ef223964c15c/packages/contract/src/builder.ts#L152)
|
|
737
|
+
|
|
738
|
+
Define an AMQP queue.
|
|
739
|
+
|
|
740
|
+
A queue stores messages until they are consumed by workers. Queues can be bound to exchanges
|
|
741
|
+
to receive messages based on routing rules.
|
|
742
|
+
|
|
743
|
+
#### Parameters
|
|
744
|
+
|
|
745
|
+
| Parameter | Type | Description |
|
|
746
|
+
| ------ | ------ | ------ |
|
|
747
|
+
| `name` | `string` | The name of the queue |
|
|
748
|
+
| `options?` | `Omit`\<[`QueueDefinition`](#queuedefinition), `"name"`\> | Optional queue configuration |
|
|
749
|
+
|
|
750
|
+
#### Returns
|
|
751
|
+
|
|
752
|
+
[`QueueDefinition`](#queuedefinition)
|
|
753
|
+
|
|
754
|
+
A queue definition
|
|
755
|
+
|
|
756
|
+
#### Example
|
|
757
|
+
|
|
758
|
+
```typescript
|
|
759
|
+
const orderProcessingQueue = defineQueue('order-processing', {
|
|
760
|
+
durable: true,
|
|
761
|
+
arguments: {
|
|
762
|
+
'x-message-ttl': 86400000, // 24 hours
|
|
763
|
+
'x-dead-letter-exchange': 'orders-dlx'
|
|
764
|
+
}
|
|
765
|
+
});
|
|
766
|
+
```
|