@crossdelta/platform-sdk 0.3.35 → 0.3.36
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.
|
@@ -62,16 +62,19 @@ Bun.serve({ port, fetch: app.fetch })
|
|
|
62
62
|
### Publishing Events
|
|
63
63
|
|
|
64
64
|
```ts
|
|
65
|
-
import {
|
|
66
|
-
|
|
67
|
-
await
|
|
68
|
-
'orders
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
{ source: '{{projectName}}://orders-service', subject: orderId }
|
|
72
|
-
)
|
|
65
|
+
import { publish } from '@crossdelta/cloudevents'
|
|
66
|
+
|
|
67
|
+
await publish('orders.created', { orderId, customerId, total }, {
|
|
68
|
+
source: '{{projectName}}://orders-service',
|
|
69
|
+
subject: orderId
|
|
70
|
+
})
|
|
73
71
|
```
|
|
74
72
|
|
|
73
|
+
The `publish` function automatically:
|
|
74
|
+
- Constructs the CloudEvent type from the subject (e.g., `orders.created` → `{{projectName}}.orders.created`)
|
|
75
|
+
- Adds required CloudEvent metadata (id, time, specversion, datacontenttype)
|
|
76
|
+
- Publishes to NATS JetStream
|
|
77
|
+
|
|
75
78
|
### Consuming Events (Auto-Discovery)
|
|
76
79
|
|
|
77
80
|
Create handler files in `src/handlers/*.event.ts`:
|
|
@@ -80,13 +83,29 @@ Create handler files in `src/handlers/*.event.ts`:
|
|
|
80
83
|
// services/notifications/src/handlers/order-created.event.ts
|
|
81
84
|
import { handleEvent } from '@crossdelta/cloudevents'
|
|
82
85
|
import { z } from 'zod'
|
|
86
|
+
import { sendNotification } from '../use-cases/send-notification.use-case'
|
|
87
|
+
|
|
88
|
+
const OrderCreatedSchema = z.object({
|
|
89
|
+
type: z.literal('orders.created'),
|
|
90
|
+
orderId: z.string(),
|
|
91
|
+
customerId: z.string(),
|
|
92
|
+
total: z.number(),
|
|
93
|
+
items: z.array(
|
|
94
|
+
z.object({
|
|
95
|
+
productId: z.string(),
|
|
96
|
+
quantity: z.number(),
|
|
97
|
+
price: z.number(),
|
|
98
|
+
}),
|
|
99
|
+
).optional(),
|
|
100
|
+
})
|
|
83
101
|
|
|
84
|
-
export default handleEvent(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
)
|
|
102
|
+
export default handleEvent(OrderCreatedSchema, async (data) => {
|
|
103
|
+
await sendNotification(data)
|
|
104
|
+
})
|
|
88
105
|
```
|
|
89
106
|
|
|
107
|
+
**Schema naming:** Use PascalCase for schema constants (e.g., `OrderCreatedSchema`, `UserUpdatedSchema`)
|
|
108
|
+
|
|
90
109
|
### Service Folder Structure
|
|
91
110
|
|
|
92
111
|
Organize service code with this structure:
|