@synkro/nestjs 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/README.md +165 -0
  2. package/package.json +4 -2
package/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # @synkro/nestjs
2
+
3
+ NestJS integration module for [@synkro/core](https://www.npmjs.com/package/@synkro/core).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @synkro/nestjs @synkro/core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ### 1. Register the module
14
+
15
+ ```typescript
16
+ import { Module } from "@nestjs/common";
17
+ import { SynkroModule } from "@synkro/nestjs";
18
+
19
+ @Module({
20
+ imports: [
21
+ SynkroModule.forRoot({
22
+ transport: "in-memory",
23
+ }),
24
+ ],
25
+ })
26
+ export class AppModule {}
27
+ ```
28
+
29
+ For async configuration (e.g. using `ConfigService`):
30
+
31
+ ```typescript
32
+ SynkroModule.forRootAsync({
33
+ imports: [ConfigModule],
34
+ inject: [ConfigService],
35
+ useFactory: (config: ConfigService) => ({
36
+ transport: "redis",
37
+ redisUrl: config.get("REDIS_URL"),
38
+ }),
39
+ });
40
+ ```
41
+
42
+ ### 2. Define event handlers
43
+
44
+ Use the `@OnEvent` decorator to register standalone event handlers:
45
+
46
+ ```typescript
47
+ import { Injectable } from "@nestjs/common";
48
+ import { OnEvent } from "@synkro/nestjs";
49
+ import type { HandlerCtx } from "@synkro/core";
50
+
51
+ @Injectable()
52
+ export class NotificationHandler {
53
+ @OnEvent("UserSignedUp", { maxRetries: 3 })
54
+ async handleUserSignedUp(ctx: HandlerCtx) {
55
+ const { email } = ctx.payload as { email: string };
56
+ console.log(`Welcome email sent to ${email}`);
57
+ }
58
+ }
59
+ ```
60
+
61
+ ### 3. Define workflows
62
+
63
+ Define workflow structure in a config file:
64
+
65
+ ```typescript
66
+ import type { SynkroWorkflow } from "@synkro/core";
67
+
68
+ const noop = async () => {};
69
+
70
+ export const workflows: SynkroWorkflow[] = [
71
+ {
72
+ name: "ProcessOrder",
73
+ onSuccess: "StartShipment",
74
+ steps: [
75
+ { type: "ValidateStock", handler: noop },
76
+ {
77
+ type: "ProcessPayment",
78
+ handler: noop,
79
+ retry: { maxRetries: 3 },
80
+ onSuccess: "PaymentCompleted",
81
+ onFailure: "PaymentFailed",
82
+ },
83
+ { type: "PaymentCompleted", handler: noop },
84
+ { type: "PaymentFailed", handler: noop },
85
+ ],
86
+ },
87
+ ];
88
+ ```
89
+
90
+ Then bind handlers with `@OnWorkflowStep`:
91
+
92
+ ```typescript
93
+ import { Injectable } from "@nestjs/common";
94
+ import { OnWorkflowStep } from "@synkro/nestjs";
95
+ import type { HandlerCtx } from "@synkro/core";
96
+
97
+ @Injectable()
98
+ export class OrderWorkflowHandler {
99
+ @OnWorkflowStep("ProcessOrder", "ValidateStock")
100
+ async handleValidateStock(ctx: HandlerCtx) {
101
+ console.log("Validating stock...");
102
+ }
103
+
104
+ @OnWorkflowStep("ProcessOrder", "ProcessPayment")
105
+ async handlePayment(ctx: HandlerCtx) {
106
+ console.log("Processing payment...");
107
+ }
108
+ }
109
+ ```
110
+
111
+ Pass the workflow config when registering the module:
112
+
113
+ ```typescript
114
+ SynkroModule.forRoot({
115
+ transport: "in-memory",
116
+ workflows,
117
+ });
118
+ ```
119
+
120
+ ### 4. Publish events
121
+
122
+ Inject `SynkroService` to publish events or start workflows:
123
+
124
+ ```typescript
125
+ import { Controller, Post, Body } from "@nestjs/common";
126
+ import { SynkroService } from "@synkro/nestjs";
127
+
128
+ @Controller("orders")
129
+ export class OrderController {
130
+ constructor(private readonly synkro: SynkroService) {}
131
+
132
+ @Post()
133
+ async create(@Body() body: { productId: string }) {
134
+ await this.synkro.publish("ProcessOrder", body);
135
+ return { status: "processing" };
136
+ }
137
+ }
138
+ ```
139
+
140
+ ## API
141
+
142
+ ### `SynkroModule`
143
+
144
+ | Method | Description |
145
+ |---|---|
146
+ | `forRoot(options)` | Register with static configuration |
147
+ | `forRootAsync(options)` | Register with async configuration (useFactory) |
148
+
149
+ ### `SynkroService`
150
+
151
+ | Method | Description |
152
+ |---|---|
153
+ | `publish(event, payload?, requestId?)` | Publish an event or start a workflow |
154
+ | `on(eventType, handler, retry?)` | Register an event handler at runtime |
155
+
156
+ ### Decorators
157
+
158
+ | Decorator | Description |
159
+ |---|---|
160
+ | `@OnEvent(type, retry?)` | Register a method as a standalone event handler |
161
+ | `@OnWorkflowStep(workflow, step)` | Bind a method to a workflow step |
162
+
163
+ ## License
164
+
165
+ ISC
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synkro/nestjs",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "NestJS integration module for @synkro/core",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,9 @@
11
11
  "default": "./dist/index.js"
12
12
  }
13
13
  },
14
- "files": ["dist"],
14
+ "files": [
15
+ "dist"
16
+ ],
15
17
  "type": "module",
16
18
  "scripts": {
17
19
  "build": "tsc",