Skip to content

Commit b5c8e0d

Browse files
elithrarSkye-31
authored andcommitted
workflows: update type params docs (#18416)
* workflows: update type params docs * workflows: add type validation partial * workflows: doc type params for create * Apply suggestions from code review Co-authored-by: Skye <[email protected]> * Update events-and-parameters.mdx * Update workers-api.mdx * Update workers-api.mdx * Update events-and-parameters.mdx --------- Co-authored-by: Skye <[email protected]>
1 parent d74df08 commit b5c8e0d

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

src/content/docs/workflows/build/events-and-parameters.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sidebar:
66

77
---
88

9+
import { MetaInfo, Render, Type } from "~/components";
10+
911
When a Workflow is triggered, it can receive an optional event. This event can include data that your Workflow can act on, including request details, user data fetched from your database (such as D1 or KV) or from a webhook, or messages from a Queue consumer.
1012

1113
Events are a powerful part of a Workflow, as you often want a Workflow to act on data. Because a given Workflow instance executes durably, events are a useful way to provide a Workflow with data that should be immutable (not changing) and/or represents data the Workflow needs to operate on at that point in time.
@@ -104,4 +106,4 @@ export class MyWorkflow extends WorkflowEntrypoint {
104106
}
105107
```
106108
107-
Note that type parameters do not _validate_ that the incoming event matches your type definition. Properties (fields) that do not exist or conform to the type you provided will be dropped. If you need to validate incoming events, we recommend a library such as [zod](https://zod.dev/) or your own validator logic.
109+
<Render file="workflows-type-parameters"/>

src/content/docs/workflows/build/workers-api.mdx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66

77
---
88

9-
import { MetaInfo, Type } from "~/components";
9+
import { MetaInfo, Render, Type } from "~/components";
1010

1111
This guide details the Workflows API within Cloudflare Workers, including methods, types, and usage examples.
1212

@@ -183,13 +183,16 @@ Create (trigger) a new instance of the given Workflow.
183183

184184
* <code>create(options?: WorkflowInstanceCreateOptions): Promise&lt;WorkflowInstance&gt;</code>
185185

186-
* `options` - optional properties to pass when creating an instance.
186+
* `options` - optional properties to pass when creating an instance, includng a user-provided ID and payload parameters.
187187

188-
An ID is automatically generated, but a user-provided ID can be specified (up to 64 characters [^1]). This can be useful when mapping Workflows to users, merchants or other identifiers in your system.
188+
An ID is automatically generated, but a user-provided ID can be specified (up to 64 characters [^1]). This can be useful when mapping Workflows to users, merchants or other identifiers in your system. You can also provide a JSON object as the `params` property, allowing you to pass data for the Workflow instance to act on as its [`WorkflowEvent`](/workflows/build/events-and-parameters/).
189189

190190
```ts
191-
// Create a new Workflow instance with your own ID:
192-
let instance = await env.MY_WORKFLOW.create({ id: myIdDefinedFromOtherSystem })
191+
// Create a new Workflow instance with your own ID and pass params to the Workflow instance
192+
let instance = await env.MY_WORKFLOW.create({
193+
id: myIdDefinedFromOtherSystem,
194+
params: { "hello": "world" }
195+
})
193196
return Response.json({
194197
id: instance.id,
195198
details: await instance.status(),
@@ -198,6 +201,42 @@ return Response.json({
198201

199202
Returns a `WorkflowInstance`.
200203

204+
<Render file="workflows-type-parameters"/>
205+
206+
To provide an optional type parameter to the `Workflow`, pass a type argument with your type when defining your Workflow bindings:
207+
208+
```ts
209+
interface User {
210+
email: string;
211+
createdTimestamp: number;
212+
}
213+
214+
interface Env {
215+
// Pass our User type as the type parameter to the Workflow definition
216+
MY_WORKFLOW: Workflow<User>;
217+
}
218+
219+
export default {
220+
async fetch(request, env, ctx) {
221+
// More likely to come from your database or via the request body!
222+
const user: User = {
223+
email: user@example.com,
224+
createdTimestamp: Date.now()
225+
}
226+
227+
let instance = await env.MY_WORKFLOW.create({
228+
// params expects the type User
229+
params: user
230+
})
231+
232+
return Response.json({
233+
id: instance.id,
234+
details: await instance.status(),
235+
});
236+
}
237+
}
238+
```
239+
201240
### get
202241

203242
Get a specific Workflow instance by ID.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
{}
3+
4+
---
5+
6+
import { Markdown } from "~/components"
7+
8+
:::caution
9+
10+
Providing a type parameter does _not_ validate that the incoming event matches your type definition. In TypeScript, properties (fields) that do not exist or conform to the type you provided will be dropped. If you need to validate incoming events, we recommend a library such as [zod](https://zod.dev/) or your own validator logic.
11+
12+
:::
13+
14+
You can also provide a type parameter to the `Workflows` type when creating (triggering) a Workflow instance using the `create` method of the [Workers API](/workflows/build/workers-api/#workflow). Note that this does _not_ propagate type information into the Workflow itself, as TypeScript types are a build-time construct. To provide the type of an incoming `WorkflowEvent`, refer to the [TypeScript and type parameters](/workflows/build/events-and-parameters/#typescript-and-type-parameters) section of the Workflows documentation.

0 commit comments

Comments
 (0)