Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ export class MyWorkflow extends WorkflowEntrypoint {
}
```

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.
<Render file="workflows-type-parameters">
47 changes: 43 additions & 4 deletions src/content/docs/workflows/build/workers-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,16 @@ Create (trigger) a new instance of the given Workflow.

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

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

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.
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/).

```ts
// Create a new Workflow instance with your own ID:
let instance = await env.MY_WORKFLOW.create({ id: myIdDefinedFromOtherSystem })
// Create a new Workflow instance with your own ID and pass params to the Workflow instance
let instance = await env.MY_WORKFLOW.create({
id: myIdDefinedFromOtherSystem,
params: { "hello": "world }
})
return Response.json({
id: instance.id,
details: await instance.status(),
Expand All @@ -186,6 +189,42 @@ return Response.json({

Returns a `WorkflowInstance`.

<Render file="workflows-type-parameters">

To provide an optional type parameter to the `Workflow`, pass a type argument with your type when defining your Workflow bindings:

```ts
interface User {
email: string;
createdTimestamp: number;
}

interface Env {
// Pass our User type as the type parameter to the Workflow definition
MY_WORKFLOW: Workflow<User>;
}

export default {
async fetch(request, env, ctx) {
// More likely to come from your database or via the request body!
const user: User = {
email: [email protected],
createdTimestamp: Date.now()
}

let instance = await env.MY_WORKFLOW.create({
// params expects the type User
params: user
})

return Response.json({
id: instance.id,
details: await instance.status(),
});
}
}
```

### get

Get a specific Workflow instance by ID.
Expand Down
14 changes: 14 additions & 0 deletions src/content/partials/workflows/workflows-type-parameters.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
{}

---

import { Markdown } from "~/components"

:::caution

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.

:::

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.
Loading