@amqp-contract/contract 0.11.0 → 0.13.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 +28 -14
- package/dist/index.cjs +550 -238
- package/dist/index.d.cts +787 -405
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +787 -405
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +540 -236
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +738 -216
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,21 +18,30 @@ pnpm add @amqp-contract/contract
|
|
|
18
18
|
|
|
19
19
|
## Quick Start
|
|
20
20
|
|
|
21
|
-
### Recommended:
|
|
21
|
+
### Recommended: Event / Command Patterns
|
|
22
22
|
|
|
23
|
-
For robust contract definitions with guaranteed consistency, use
|
|
23
|
+
For robust contract definitions with guaranteed consistency, use Event or Command patterns:
|
|
24
|
+
|
|
25
|
+
| Pattern | Use Case | Flow |
|
|
26
|
+
| ----------- | ------------------------------------------ | -------------------------------------------------- |
|
|
27
|
+
| **Event** | One publisher, many consumers (broadcast) | `defineEventPublisher` → `defineEventConsumer` |
|
|
28
|
+
| **Command** | Many publishers, one consumer (task queue) | `defineCommandConsumer` → `defineCommandPublisher` |
|
|
24
29
|
|
|
25
30
|
```typescript
|
|
26
31
|
import {
|
|
27
|
-
|
|
32
|
+
defineEventPublisher,
|
|
33
|
+
defineEventConsumer,
|
|
34
|
+
defineCommandConsumer,
|
|
35
|
+
defineCommandPublisher,
|
|
28
36
|
defineContract,
|
|
29
37
|
defineExchange,
|
|
30
38
|
defineQueue,
|
|
39
|
+
definePublisher,
|
|
31
40
|
defineMessage,
|
|
32
41
|
} from "@amqp-contract/contract";
|
|
33
42
|
import { z } from "zod";
|
|
34
43
|
|
|
35
|
-
// Event
|
|
44
|
+
// Event pattern: publisher broadcasts, consumers subscribe
|
|
36
45
|
const ordersExchange = defineExchange("orders", "topic", { durable: true });
|
|
37
46
|
const orderMessage = defineMessage(
|
|
38
47
|
z.object({
|
|
@@ -41,25 +50,30 @@ const orderMessage = defineMessage(
|
|
|
41
50
|
}),
|
|
42
51
|
);
|
|
43
52
|
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
// Define event publisher
|
|
54
|
+
const orderCreatedEvent = defineEventPublisher(ordersExchange, orderMessage, {
|
|
55
|
+
routingKey: "order.created",
|
|
56
|
+
});
|
|
46
57
|
|
|
47
58
|
// Multiple queues can consume the same event
|
|
48
59
|
const orderQueue = defineQueue("order-processing", { durable: true });
|
|
49
|
-
const { consumer, binding } =
|
|
60
|
+
const { consumer, binding } = defineEventConsumer(orderCreatedEvent, orderQueue);
|
|
50
61
|
|
|
51
62
|
// For topic exchanges, consumers can override with their own pattern
|
|
52
63
|
const analyticsQueue = defineQueue("analytics", { durable: true });
|
|
53
|
-
const { consumer: analyticsConsumer, binding: analyticsBinding } =
|
|
64
|
+
const { consumer: analyticsConsumer, binding: analyticsBinding } = defineEventConsumer(
|
|
65
|
+
orderCreatedEvent,
|
|
54
66
|
analyticsQueue,
|
|
55
|
-
"order.*",
|
|
56
|
-
);
|
|
67
|
+
{ routingKey: "order.*" }, // Subscribe to all order events
|
|
68
|
+
);
|
|
57
69
|
|
|
58
70
|
const contract = defineContract({
|
|
59
71
|
exchanges: { orders: ordersExchange },
|
|
60
72
|
queues: { orderQueue, analyticsQueue },
|
|
61
73
|
bindings: { orderBinding: binding, analyticsBinding },
|
|
62
|
-
publishers: {
|
|
74
|
+
publishers: {
|
|
75
|
+
orderCreated: definePublisher(ordersExchange, orderMessage, { routingKey: "order.created" }),
|
|
76
|
+
},
|
|
63
77
|
consumers: {
|
|
64
78
|
processOrder: consumer,
|
|
65
79
|
trackOrders: analyticsConsumer,
|
|
@@ -72,7 +86,7 @@ const contract = defineContract({
|
|
|
72
86
|
- ✅ Guaranteed message schema consistency between publishers and consumers
|
|
73
87
|
- ✅ Routing key validation and type safety
|
|
74
88
|
- ✅ Full type safety with TypeScript inference
|
|
75
|
-
- ✅ Event-oriented
|
|
89
|
+
- ✅ Event-oriented and command-oriented patterns
|
|
76
90
|
- ✅ Flexible routing key patterns for topic exchanges
|
|
77
91
|
|
|
78
92
|
## Documentation
|
|
@@ -80,8 +94,8 @@ const contract = defineContract({
|
|
|
80
94
|
📖 **[Read the full documentation →](https://btravers.github.io/amqp-contract)**
|
|
81
95
|
|
|
82
96
|
- [Getting Started Guide](https://btravers.github.io/amqp-contract/guide/defining-contracts)
|
|
83
|
-
- [
|
|
84
|
-
- [
|
|
97
|
+
- [Event Pattern](https://btravers.github.io/amqp-contract/guide/defining-contracts#event-pattern)
|
|
98
|
+
- [Command Pattern](https://btravers.github.io/amqp-contract/guide/defining-contracts#command-pattern)
|
|
85
99
|
- [Complete API Reference](https://btravers.github.io/amqp-contract/api/contract)
|
|
86
100
|
|
|
87
101
|
## License
|