@trigora/sdk 0.1.3 → 0.3.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 CHANGED
@@ -1,8 +1,8 @@
1
1
  # @trigora/sdk
2
2
 
3
- Define flows for Trigora.
3
+ Flow authoring for Trigora.
4
4
 
5
- ---
5
+ `@trigora/sdk` is the package most Trigora users write against directly. It provides `defineFlow` plus the shared runtime types needed to author flows with good TypeScript ergonomics.
6
6
 
7
7
  ## Install
8
8
 
@@ -10,7 +10,11 @@ Define flows for Trigora.
10
10
  npm install @trigora/sdk
11
11
  ```
12
12
 
13
- ---
13
+ Most projects will install the CLI alongside it:
14
+
15
+ ```bash
16
+ npm install trigora @trigora/sdk
17
+ ```
14
18
 
15
19
  ## Basic Usage
16
20
 
@@ -21,51 +25,208 @@ export default defineFlow({
21
25
  id: 'hello',
22
26
  trigger: { type: 'manual' },
23
27
  async run(event, ctx) {
24
- await ctx.log.info('Hello');
28
+ await ctx.log.info('Hello from Trigora', event.payload);
29
+ },
30
+ });
31
+ ```
32
+
33
+ ## What `defineFlow` Gives You
34
+
35
+ `defineFlow` helps with:
36
+
37
+ - typed flow definitions
38
+ - typed trigger variants
39
+ - typed event payload access
40
+ - typed runtime context
41
+ - trigger-aware return types
42
+ - compile-time validation of trigger shape
43
+
44
+ The trigger typing is strict. If you mix properties from different trigger kinds, TypeScript will flag it.
45
+
46
+ For example, this is invalid:
47
+
48
+ ```ts
49
+ import { defineFlow } from '@trigora/sdk';
50
+
51
+ export default defineFlow({
52
+ id: 'broken',
53
+ trigger: {
54
+ type: 'webhook',
55
+ cron: '* * * * *',
56
+ },
57
+ async run() {
58
+ return 'nope';
25
59
  },
26
60
  });
27
61
  ```
28
62
 
29
- ---
63
+ ## Trigger Types
64
+
65
+ ### Manual
30
66
 
31
- ## Flow Structure
67
+ Use manual triggers for local invocation and testing.
32
68
 
33
69
  ```ts
34
- defineFlow({
35
- id: string,
70
+ import { defineFlow } from '@trigora/sdk';
71
+
72
+ export default defineFlow({
73
+ id: 'hello',
36
74
  trigger: { type: 'manual' },
37
- run: async (event, ctx) => {}
38
- })
75
+ async run(event, ctx) {
76
+ await ctx.log.info('Triggered manually', event.payload);
77
+ },
78
+ });
79
+ ```
80
+
81
+ ### Webhook
82
+
83
+ Use webhook triggers for hosted HTTP entrypoints.
84
+
85
+ ```ts
86
+ import { defineFlow } from '@trigora/sdk';
87
+
88
+ export default defineFlow({
89
+ id: 'ping',
90
+ trigger: { type: 'webhook' },
91
+ async run() {
92
+ return 'pong';
93
+ },
94
+ });
95
+ ```
96
+
97
+ You can also include an optional event name:
98
+
99
+ ```ts
100
+ trigger: { type: 'webhook', event: 'orders.created' }
101
+ ```
102
+
103
+ ### Cron
104
+
105
+ Use cron triggers for scheduled work.
106
+
107
+ ```ts
108
+ import { defineFlow } from '@trigora/sdk';
109
+
110
+ export default defineFlow({
111
+ id: 'nightly-sync',
112
+ trigger: { type: 'cron', cron: '0 2 * * *' },
113
+ async run(event, ctx) {
114
+ await ctx.log.info('Nightly sync started', event.payload);
115
+ },
116
+ });
117
+ ```
118
+
119
+ ## Return Types
120
+
121
+ Return values are trigger-aware.
122
+
123
+ ### Webhook flows
124
+
125
+ Webhook flows can return HTTP-friendly values directly from `run`.
126
+
127
+ Supported return values:
128
+
129
+ - `Response`
130
+ - plain objects, arrays, numbers, and booleans
131
+ - `string`
132
+ - `null`
133
+ - `undefined`
134
+
135
+ Example:
136
+
137
+ ```ts
138
+ import { defineFlow } from '@trigora/sdk';
139
+
140
+ export default defineFlow({
141
+ id: 'status',
142
+ trigger: { type: 'webhook' },
143
+ async run() {
144
+ return {
145
+ ok: true,
146
+ service: 'trigora',
147
+ };
148
+ },
149
+ });
39
150
  ```
40
151
 
41
- ---
152
+ ### Manual and cron flows
42
153
 
43
- ## Event
154
+ Manual and cron flows do not use return values. Their `run` functions are typed as `void`.
155
+
156
+ This matches how Trigora uses them:
157
+
158
+ - manual flows are invoked locally for testing and development
159
+ - cron flows are background-style scheduled jobs
160
+ - webhook flows are request-response flows
161
+
162
+ ## Event Shape
163
+
164
+ Flows receive an event object:
44
165
 
45
166
  ```ts
46
- {
167
+ type FlowEvent<TPayload = JsonValue> = {
47
168
  id: string;
48
169
  type: string;
49
170
  timestamp: string;
50
- payload: unknown;
51
- }
171
+ payload: TPayload;
172
+ request?: {
173
+ headers: Record<string, string>;
174
+ method: string;
175
+ url: string;
176
+ rawBody: string;
177
+ };
178
+ };
52
179
  ```
53
180
 
54
- ---
181
+ In local manual runs, the payload comes from your JSON payload file when one is provided. In local webhook dev and deployed webhook flows, the payload is the parsed JSON request body.
182
+
183
+ If you do not provide your own payload type, `event.payload` defaults to `JsonValue`.
184
+
185
+ For webhook flows, request metadata may also be available on `event.request`, including headers, method, URL, and the raw request body. `event.request.rawBody` is useful when you want to verify webhook signatures yourself.
55
186
 
56
187
  ## Context
57
188
 
189
+ Flows receive a context object with logging and environment access:
190
+
58
191
  ```ts
59
- ctx.log.info()
60
- ctx.log.warn()
61
- ctx.log.error()
192
+ ctx.log.info('message')
193
+ ctx.log.warn('message')
194
+ ctx.log.error('message')
62
195
 
63
196
  ctx.env
64
197
  ```
65
198
 
66
- ---
199
+ The exact exported type is `FlowContext<TEnv>`.
200
+
201
+ ## Exported Types
202
+
203
+ `@trigora/sdk` re-exports the main flow authoring types from `@trigora/contracts`:
204
+
205
+ - `FlowContext`
206
+ - `FlowDefinition`
207
+ - `FlowEvent`
208
+ - `FlowRunFn`
209
+ - `JsonValue`
210
+ - `Trigger`
211
+ - `WebhookFlowResult`
212
+
213
+ That means most flow authors can stay entirely within `@trigora/sdk`.
214
+
215
+ ## Typical Workflow
216
+
217
+ 1. Define a flow with `defineFlow`
218
+ 2. Run it locally with:
219
+ - `trigora trigger hello --payload payload.json` for one-off runs
220
+ - `trigora dev hello --payload payload.json` for manual or payload watch mode
221
+ - `trigora dev stripe-checkout` for a local webhook server
222
+ 3. Deploy webhook flows with `trigora deploy`
223
+ 4. Manage hosted flows with `trigora flows`
224
+
225
+ ## Related Packages
226
+
227
+ - `trigora` - CLI for local development and hosted deploys
228
+ - `@trigora/contracts` - shared public types used by the SDK, CLI, and API consumers
67
229
 
68
- ## Notes
230
+ ## License
69
231
 
70
- - flows are plain TypeScript modules
71
- - no framework required
232
+ MIT
@@ -1,2 +1,34 @@
1
- import type { FlowDefinition } from '@trigora/contracts';
2
- export declare function defineFlow<TPayload = unknown, TEnv extends Record<string, string> = Record<string, string>>(flow: FlowDefinition<TPayload, TEnv>): FlowDefinition<TPayload, TEnv>;
1
+ import type { CronFlowDefinition, JsonValue, ManualFlowDefinition, WebhookFlowDefinition } from '@trigora/contracts';
2
+ /**
3
+ * Define a Trigora flow.
4
+ *
5
+ * Flows are plain TypeScript modules with three core parts:
6
+ * - `id`: the source identifier for the flow in your project
7
+ * - `trigger`: how the flow is invoked
8
+ * - `run`: the function that executes when the flow is triggered
9
+ *
10
+ * `run` receives the incoming `event` and a `ctx` object with logging and environment access.
11
+ *
12
+ * Webhook flows can return HTTP-friendly values:
13
+ * - `Response`
14
+ * - plain objects / arrays / numbers / booleans
15
+ * - `string`
16
+ * - `null` or `undefined`
17
+ *
18
+ * Other trigger types generally do not need to return anything meaningful.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * export default defineFlow({
23
+ * id: 'hello',
24
+ * trigger: { type: 'webhook' },
25
+ * async run(event, ctx) {
26
+ * await ctx.log.info('Received event', event.payload);
27
+ * return { ok: true };
28
+ * },
29
+ * });
30
+ * ```
31
+ */
32
+ export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: ManualFlowDefinition<TPayload, TEnv>): ManualFlowDefinition<TPayload, TEnv>;
33
+ export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: WebhookFlowDefinition<TPayload, TEnv>): WebhookFlowDefinition<TPayload, TEnv>;
34
+ export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: CronFlowDefinition<TPayload, TEnv>): CronFlowDefinition<TPayload, TEnv>;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { defineFlow } from './defineFlow';
2
- export type { FlowContext, FlowDefinition, FlowEvent, FlowRunFn, Trigger, } from '@trigora/contracts';
2
+ export type { FlowContext, FlowDefinition, FlowEvent, FlowRunFn, JsonValue, Trigger, WebhookFlowResult, } from '@trigora/contracts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trigora/sdk",
3
- "version": "0.1.3",
3
+ "version": "0.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/trigora-dev/trigora"
@@ -37,7 +37,7 @@
37
37
  "access": "public"
38
38
  },
39
39
  "dependencies": {
40
- "@trigora/contracts": "0.1.0"
40
+ "@trigora/contracts": "0.3.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "tsup": "^8.5.1"