-
Notifications
You must be signed in to change notification settings - Fork 10.4k
workflows: update type params docs #18416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa63aac
workflows: update type params docs
elithrar 6333d9b
workflows: add type validation partial
elithrar b6cd4bb
workflows: doc type params for create
elithrar 150a839
Apply suggestions from code review
elithrar 0921123
Update events-and-parameters.mdx
elithrar 4b6f3fd
Update workers-api.mdx
elithrar 3469622
Update workers-api.mdx
elithrar 29c1448
Update events-and-parameters.mdx
elithrar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,13 +171,16 @@ Create (trigger) a new instance of the given Workflow. | |
|
|
||
| * <code>create(options?: WorkflowInstanceCreateOptions): Promise<WorkflowInstance></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(), | ||
|
|
@@ -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. | ||
|
|
||
14 changes: 14 additions & 0 deletions
14
src/content/partials/workflows/workflows-type-parameters.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.