@crossdelta/cloudevents 0.1.1 → 0.1.3
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 +37 -67
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,30 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @crossdelta/cloudevents
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
CloudEvents middleware, publishers, and consumers that integrate cleanly with [Hono](https://hono.dev/). Ship handler discovery, DLQ-safe processing for Google Pub/Sub & Eventarc, and NATS utilities without wiring everything yourself.
|
|
3
|
+
CloudEvents toolkit for TypeScript. Handler discovery, DLQ-safe processing, NATS streaming, and optional Hono middleware.
|
|
6
4
|
|
|
7
5
|
## Features
|
|
8
6
|
|
|
9
|
-
- Automatic handler discovery for `*.event.(ts|js)` files
|
|
10
|
-
- CloudEvents parsing
|
|
11
|
-
- DLQ-safe mode
|
|
12
|
-
-
|
|
13
|
-
-
|
|
7
|
+
- Automatic handler discovery for `*.event.(ts|js)` files
|
|
8
|
+
- CloudEvents parsing (structured, binary, Pub/Sub push, raw)
|
|
9
|
+
- DLQ-safe mode with quarantine for invalid messages
|
|
10
|
+
- Zod validation with optional safe-parse fallback
|
|
11
|
+
- NATS publish & consume support
|
|
12
|
+
- Google Pub/Sub support (optional dependency)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
bun add @crossdelta/cloudevents
|
|
18
|
+
```
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
### Optional: Google Pub/Sub
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
For Pub/Sub publishing, install the optional dependency:
|
|
18
23
|
|
|
19
24
|
```bash
|
|
20
|
-
bun add @
|
|
25
|
+
bun add @google-cloud/pubsub
|
|
21
26
|
```
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### Define a handler
|
|
24
31
|
|
|
25
32
|
```typescript
|
|
26
33
|
import { z } from 'zod'
|
|
27
|
-
import { eventSchema, handleEvent } from '@
|
|
34
|
+
import { eventSchema, handleEvent } from '@crossdelta/cloudevents'
|
|
28
35
|
|
|
29
36
|
const CustomerCreatedSchema = eventSchema({
|
|
30
37
|
type: z.literal('customer.created'),
|
|
@@ -39,87 +46,50 @@ export const CustomerCreatedHandler = handleEvent(CustomerCreatedSchema, async (
|
|
|
39
46
|
})
|
|
40
47
|
```
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
### 3. Register the middleware in Hono
|
|
49
|
+
### Register middleware
|
|
45
50
|
|
|
46
51
|
```typescript
|
|
47
52
|
import { Hono } from 'hono'
|
|
48
|
-
import { cloudEvents } from '@
|
|
53
|
+
import { cloudEvents } from '@crossdelta/cloudevents'
|
|
49
54
|
|
|
50
55
|
const app = new Hono()
|
|
51
56
|
|
|
52
57
|
app.use('/events', cloudEvents({
|
|
53
58
|
discover: 'src/events/**/*.event.{ts,js}',
|
|
54
|
-
quarantineTopic: process.env.QUARANTINE_TOPIC,
|
|
55
|
-
errorTopic: process.env.ERROR_TOPIC,
|
|
56
|
-
projectId: process.env.GCP_PROJECT_ID,
|
|
57
|
-
log: process.env.NODE_ENV === 'production' ? 'structured' : 'pretty',
|
|
58
59
|
}))
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
## Publishing events
|
|
62
|
+
## Publishing
|
|
64
63
|
|
|
65
64
|
```typescript
|
|
66
|
-
import {
|
|
67
|
-
publishEvent,
|
|
68
|
-
publishRawEvent,
|
|
69
|
-
publishNatsEvent,
|
|
70
|
-
publishNatsRawEvent,
|
|
71
|
-
} from '@orderboss/cloudevents'
|
|
72
|
-
|
|
73
|
-
// Google Pub/Sub
|
|
74
|
-
await publishEvent('customer-events', CustomerCreatedSchema, {
|
|
75
|
-
payload: { id: 'cust_1', email: 'jane@example.com' },
|
|
76
|
-
})
|
|
65
|
+
import { publishNatsEvent } from '@crossdelta/cloudevents'
|
|
77
66
|
|
|
78
|
-
await publishRawEvent('customer-events', 'customer.deleted', { id: 'cust_1' }, {
|
|
79
|
-
source: 'orderboss://customers',
|
|
80
|
-
projectId: 'my-project',
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
// NATS Streaming (reuse the schema you defined for your handler)
|
|
84
67
|
await publishNatsEvent('orders.placed', OrderPlacedSchema, {
|
|
85
68
|
payload: { id: 'ord_42', total: 149.9 },
|
|
86
69
|
})
|
|
87
|
-
|
|
88
|
-
await publishNatsRawEvent('orders.canceled', 'order.canceled', { id: 'ord_42' }, {
|
|
89
|
-
servers: process.env.NATS_URL,
|
|
90
|
-
})
|
|
91
70
|
```
|
|
92
71
|
|
|
93
72
|
## Consuming from NATS
|
|
94
73
|
|
|
95
74
|
```typescript
|
|
96
|
-
import { consumeNatsEvents } from '@
|
|
75
|
+
import { consumeNatsEvents } from '@crossdelta/cloudevents/transports/nats'
|
|
97
76
|
|
|
98
77
|
await consumeNatsEvents({
|
|
99
78
|
servers: process.env.NATS_URL,
|
|
100
79
|
subject: 'orders.placed',
|
|
101
80
|
discover: 'dist/events/*.event.js',
|
|
102
81
|
consumerName: 'orders-api',
|
|
103
|
-
quarantineTopic: 'projects/my-app/topics/nats-quarantine',
|
|
104
|
-
errorTopic: 'projects/my-app/topics/nats-errors',
|
|
105
82
|
})
|
|
106
83
|
```
|
|
107
84
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
- `consumeNatsEvents` – subscribe to NATS and execute discovered handlers
|
|
120
|
-
|
|
121
|
-
## Further reading
|
|
122
|
-
|
|
123
|
-
- [Architecture notes](docs/architecture.md)
|
|
124
|
-
- Examples in `packages/cloudevents/test`
|
|
125
|
-
- Project changelog in the repository root
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
| Function | Description |
|
|
88
|
+
|----------|-------------|
|
|
89
|
+
| `cloudEvents(options)` | Hono middleware with discovery and DLQ-safe semantics |
|
|
90
|
+
| `eventSchema(schema)` | Wraps a Zod schema with `type` literal |
|
|
91
|
+
| `handleEvent(schema, handler)` | Creates discovery-ready handler |
|
|
92
|
+
| `publishNatsEvent` / `publishNatsRawEvent` | Publish to NATS |
|
|
93
|
+
| `publishEvent` / `publishRawEvent` | Publish to Google Pub/Sub (requires optional dep) |
|
|
94
|
+
| `consumeNatsEvents` | Subscribe to NATS and execute handlers |
|
|
95
|
+
| `clearHandlerCache()` | Reset discovery cache |
|