@crossdelta/cloudevents 0.5.6 → 0.5.7
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 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,18 +40,18 @@ import { handleEvent } from '@crossdelta/cloudevents'
|
|
|
40
40
|
import { z } from 'zod'
|
|
41
41
|
|
|
42
42
|
// Export schema for mock generation
|
|
43
|
-
export const
|
|
43
|
+
export const OrderCreatedSchema = z.object({
|
|
44
44
|
orderId: z.string(),
|
|
45
45
|
total: z.number(),
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
// Export type for use in use-cases
|
|
49
|
-
export type
|
|
49
|
+
export type OrderCreatedEvent = z.infer<typeof OrderCreatedSchema>
|
|
50
50
|
|
|
51
51
|
export default handleEvent(
|
|
52
52
|
{
|
|
53
|
-
schema:
|
|
54
|
-
type: '
|
|
53
|
+
schema: OrderCreatedSchema,
|
|
54
|
+
type: 'order.created',
|
|
55
55
|
},
|
|
56
56
|
async (data) => {
|
|
57
57
|
console.log(`New order: ${data.orderId}, total: ${data.total}`)
|
|
@@ -76,7 +76,7 @@ consumeJetStreams({
|
|
|
76
76
|
```typescript
|
|
77
77
|
import { publish } from '@crossdelta/cloudevents'
|
|
78
78
|
|
|
79
|
-
await publish('
|
|
79
|
+
await publish('order.created', { orderId: 'ord_123', total: 99.99 })
|
|
80
80
|
```
|
|
81
81
|
|
|
82
82
|
That's it. Handlers are auto-discovered, validated with Zod, and messages persist in JetStream.
|
|
@@ -104,7 +104,7 @@ That's it. Handlers are auto-discovered, validated with Zod, and messages persis
|
|
|
104
104
|
|
|
105
105
|
**Important distinction:**
|
|
106
106
|
|
|
107
|
-
- **Event Type** (`
|
|
107
|
+
- **Event Type** (`order.created`): Lives in the CloudEvent **envelope** (`ce.type`). Used for routing and handler matching.
|
|
108
108
|
- **Event Data** (`{ orderId, total }`): The actual payload. Does **not** include the type.
|
|
109
109
|
|
|
110
110
|
```typescript
|
|
@@ -115,7 +115,7 @@ const Schema = z.object({
|
|
|
115
115
|
export default handleEvent(
|
|
116
116
|
{
|
|
117
117
|
schema: Schema,
|
|
118
|
-
type: '
|
|
118
|
+
type: 'order.created',
|
|
119
119
|
},
|
|
120
120
|
async (data) => { ... }
|
|
121
121
|
)
|
|
@@ -140,7 +140,7 @@ export type UserSignupEvent = z.infer<typeof UserSignupSchema>
|
|
|
140
140
|
export default handleEvent(
|
|
141
141
|
{
|
|
142
142
|
schema: UserSignupSchema,
|
|
143
|
-
type: '
|
|
143
|
+
type: 'user.signup',
|
|
144
144
|
},
|
|
145
145
|
async (data) => {
|
|
146
146
|
await sendWelcomeEmail(data.email)
|
|
@@ -151,7 +151,7 @@ export default handleEvent(
|
|
|
151
151
|
### Publishing
|
|
152
152
|
|
|
153
153
|
```typescript
|
|
154
|
-
await publish('
|
|
154
|
+
await publish('order.created', orderData)
|
|
155
155
|
```
|
|
156
156
|
|
|
157
157
|
### Stream Setup
|
|
@@ -244,7 +244,7 @@ consumeJetStreams({
|
|
|
244
244
|
|
|
245
245
|
// Optional
|
|
246
246
|
servers: 'nats://localhost:4222',
|
|
247
|
-
filterSubjects: ['
|
|
247
|
+
filterSubjects: ['order.created'], // Filter specific subjects
|
|
248
248
|
maxDeliver: 5, // Retry attempts
|
|
249
249
|
ackWait: 30_000, // Timeout per attempt (ms)
|
|
250
250
|
quarantineTopic: 'dlq', // For poison messages
|
|
@@ -269,10 +269,10 @@ import { createContract } from '@crossdelta/cloudevents'
|
|
|
269
269
|
import { z } from 'zod'
|
|
270
270
|
|
|
271
271
|
export const OrdersCreatedContract = createContract({
|
|
272
|
-
type: '
|
|
272
|
+
type: 'order.created',
|
|
273
273
|
channel: {
|
|
274
274
|
stream: 'ORDERS',
|
|
275
|
-
// subject defaults to '
|
|
275
|
+
// subject defaults to 'order.created' if omitted
|
|
276
276
|
},
|
|
277
277
|
schema: z.object({
|
|
278
278
|
orderId: z.string(),
|
|
@@ -300,7 +300,7 @@ interface ChannelConfig {
|
|
|
300
300
|
// Auto-pluralized subject
|
|
301
301
|
createContract({
|
|
302
302
|
type: 'order.created', // Event type: singular
|
|
303
|
-
channel: { stream: 'ORDERS' }, // Subject:
|
|
303
|
+
channel: { stream: 'ORDERS' }, // Subject: order.created (plural)
|
|
304
304
|
})
|
|
305
305
|
|
|
306
306
|
// Custom subject (no auto-pluralization)
|
|
@@ -316,31 +316,31 @@ createContract({
|
|
|
316
316
|
### Multiple Events, Same Stream
|
|
317
317
|
|
|
318
318
|
```typescript
|
|
319
|
-
//
|
|
319
|
+
// order.created → ORDERS stream
|
|
320
320
|
export const OrdersCreatedContract = createContract({
|
|
321
|
-
type: '
|
|
321
|
+
type: 'order.created',
|
|
322
322
|
channel: { stream: 'ORDERS' },
|
|
323
323
|
schema: OrderCreatedSchema,
|
|
324
324
|
})
|
|
325
325
|
|
|
326
|
-
//
|
|
326
|
+
// order.updated → ORDERS stream (same stream!)
|
|
327
327
|
export const OrdersUpdatedContract = createContract({
|
|
328
|
-
type: '
|
|
328
|
+
type: 'order.updated',
|
|
329
329
|
channel: { stream: 'ORDERS' },
|
|
330
330
|
schema: OrderUpdatedSchema,
|
|
331
331
|
})
|
|
332
332
|
```
|
|
333
333
|
|
|
334
|
-
**Result:** Both events share the `ORDERS` stream with subjects `
|
|
334
|
+
**Result:** Both events share the `ORDERS` stream with subjects `order.created` and `order.updated`.
|
|
335
335
|
|
|
336
336
|
### Infrastructure Materialization
|
|
337
337
|
|
|
338
338
|
Channel metadata is **conceptual** - it defines routing, not infrastructure policy.
|
|
339
339
|
|
|
340
340
|
**Contracts define:**
|
|
341
|
-
- Event type (`
|
|
341
|
+
- Event type (`order.created`)
|
|
342
342
|
- Stream routing (`ORDERS`)
|
|
343
|
-
- Subject mapping (`
|
|
343
|
+
- Subject mapping (`order.created`)
|
|
344
344
|
|
|
345
345
|
**Infrastructure defines:**
|
|
346
346
|
- Retention (7 days, 14 days, etc.)
|
|
@@ -356,7 +356,7 @@ import { ensureJetStreams } from '@crossdelta/cloudevents'
|
|
|
356
356
|
|
|
357
357
|
// Collect from contracts
|
|
358
358
|
const streams = collectStreamDefinitions()
|
|
359
|
-
// [{ stream: 'ORDERS', subjects: ['
|
|
359
|
+
// [{ stream: 'ORDERS', subjects: ['order.created', 'order.updated'] }]
|
|
360
360
|
|
|
361
361
|
// Materialize with policies
|
|
362
362
|
await ensureJetStreams({
|
|
@@ -406,16 +406,16 @@ When multiple services consume the same event, use `createContract` to create sh
|
|
|
406
406
|
import { createContract } from '@crossdelta/cloudevents'
|
|
407
407
|
import { z } from 'zod'
|
|
408
408
|
|
|
409
|
-
export const
|
|
409
|
+
export const OrderCreatedSchema = z.object({
|
|
410
410
|
orderId: z.string(),
|
|
411
411
|
total: z.number(),
|
|
412
412
|
})
|
|
413
413
|
|
|
414
|
-
export type
|
|
414
|
+
export type OrderCreatedData = z.infer<typeof OrderCreatedSchema>
|
|
415
415
|
|
|
416
416
|
export const OrdersCreatedContract = createContract({
|
|
417
|
-
type: '
|
|
418
|
-
schema:
|
|
417
|
+
type: 'order.created',
|
|
418
|
+
schema: OrderCreatedSchema,
|
|
419
419
|
})
|
|
420
420
|
```
|
|
421
421
|
|
|
@@ -428,7 +428,7 @@ import { OrdersCreatedContract } from '@my-org/contracts'
|
|
|
428
428
|
export default handleEvent(
|
|
429
429
|
OrdersCreatedContract,
|
|
430
430
|
async (data) => {
|
|
431
|
-
// data is typed as
|
|
431
|
+
// data is typed as OrderCreatedData
|
|
432
432
|
console.log(data.orderId)
|
|
433
433
|
},
|
|
434
434
|
)
|
|
@@ -448,7 +448,7 @@ Filter events by tenant:
|
|
|
448
448
|
|
|
449
449
|
```typescript
|
|
450
450
|
export default handleEvent({
|
|
451
|
-
type: '
|
|
451
|
+
type: 'order.created',
|
|
452
452
|
schema: OrderSchema,
|
|
453
453
|
tenantId: 'tenant-a', // Only process tenant-a events
|
|
454
454
|
}, async (data) => { ... })
|
|
@@ -463,7 +463,7 @@ Add custom filter logic:
|
|
|
463
463
|
|
|
464
464
|
```typescript
|
|
465
465
|
export default handleEvent({
|
|
466
|
-
type: '
|
|
466
|
+
type: 'order.created',
|
|
467
467
|
schema: OrderSchema,
|
|
468
468
|
match: (event) => event.data.total > 100, // Only high-value orders
|
|
469
469
|
}, async (data) => { ... })
|