|
| 1 | +--- |
| 2 | +title: Nuxt |
| 3 | +--- |
| 4 | + |
| 5 | +# Nuxt |
| 6 | + |
| 7 | +This guide will walk through setting up your first workflow in a Nuxt app. Along the way, you'll learn more about the concepts that are fundamental to using the development kit in your own projects. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +<Steps> |
| 12 | + |
| 13 | +<Step> |
| 14 | +## Create Your Nuxt Project |
| 15 | + |
| 16 | +Start by creating a new Nuxt project. This command will create a new directory named `nuxt-app` and setup a Nuxt project inside it. |
| 17 | + |
| 18 | +```bash |
| 19 | +npm create nuxt@latest nuxt-app |
| 20 | +``` |
| 21 | + |
| 22 | +Enter the newly made directory: |
| 23 | + |
| 24 | +```bash |
| 25 | +cd nuxt-app |
| 26 | +``` |
| 27 | + |
| 28 | +### Install `workflow` |
| 29 | + |
| 30 | +<Tabs items={['npm', 'pnpm', 'yarn', 'bun']}> |
| 31 | + <Tab value="npm"> |
| 32 | + <CodeBlock>npm i workflow</CodeBlock> |
| 33 | + </Tab> |
| 34 | + <Tab value="pnpm"> |
| 35 | + <CodeBlock>pnpm i workflow</CodeBlock> |
| 36 | + </Tab> |
| 37 | + <Tab value="yarn"> |
| 38 | + <CodeBlock>yarn add workflow</CodeBlock> |
| 39 | + </Tab> |
| 40 | + <Tab value="bun"> |
| 41 | + <CodeBlock>bun add workflow</CodeBlock> |
| 42 | + </Tab> |
| 43 | +</Tabs> |
| 44 | + |
| 45 | +### Configure Nuxt |
| 46 | + |
| 47 | +Add `workflow` to your `nuxt.config.ts`. This automatically configures the Nitro integration and enables usage of the `"use workflow"` and `"use step"` directives. |
| 48 | + |
| 49 | +```typescript title="nuxt.config.ts" lineNumbers |
| 50 | +import { defineNuxtConfig } from "nuxt/config"; |
| 51 | + |
| 52 | +export default defineNuxtConfig({ |
| 53 | + modules: ["workflow/nuxt"], // [!code highlight] |
| 54 | + compatibilityDate: "latest", |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +This will also automatically enable the TypeScript plugin, which provides helpful IntelliSense hints in your IDE for workflow and step functions. |
| 59 | + |
| 60 | +<Accordion type="single" collapsible> |
| 61 | + <AccordionItem value="typescript-intellisense" className="[&_h3]:my-0"> |
| 62 | + <AccordionTrigger className="[&_p]:my-0 text-lg [&_p]:text-foreground"> |
| 63 | + Disable TypeScript Plugin (Optional) |
| 64 | + </AccordionTrigger> |
| 65 | + <AccordionContent className="[&_p]:my-2"> |
| 66 | +The TypeScript plugin is enabled by default. If you need to disable it, you can configure it in your `nuxt.config.ts`: |
| 67 | + |
| 68 | +```typescript title="nuxt.config.ts" lineNumbers |
| 69 | +export default defineNuxtConfig({ |
| 70 | + modules: ["workflow/nuxt"], |
| 71 | + workflow: { |
| 72 | + typescriptPlugin: false, // [!code highlight] |
| 73 | + }, |
| 74 | + compatibilityDate: "latest", |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | + </AccordionContent> |
| 79 | + |
| 80 | + </AccordionItem> |
| 81 | +</Accordion> |
| 82 | + |
| 83 | +</Step> |
| 84 | + |
| 85 | +<Step> |
| 86 | + |
| 87 | +## Create Your First Workflow |
| 88 | + |
| 89 | +Create a new file for our first workflow: |
| 90 | + |
| 91 | +```typescript title="server/workflows/user-signup.ts" lineNumbers |
| 92 | +import { sleep } from "workflow"; |
| 93 | + |
| 94 | +export async function handleUserSignup(email: string) { |
| 95 | + "use workflow"; // [!code highlight] |
| 96 | + |
| 97 | + const user = await createUser(email); |
| 98 | + await sendWelcomeEmail(user); |
| 99 | + |
| 100 | + await sleep("5s"); // Pause for 5s - doesn't consume any resources |
| 101 | + await sendOnboardingEmail(user); |
| 102 | + |
| 103 | + return { userId: user.id, status: "onboarded" }; |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +We'll fill in those functions next, but let's take a look at this code: |
| 108 | + |
| 109 | +- We define a **workflow** function with the directive `"use workflow"`. Think of the workflow function as the _orchestrator_ of individual **steps**. |
| 110 | +- The Workflow DevKit's `sleep` function allows us to suspend execution of the workflow without using up any resources. A sleep can be a few seconds, hours, days, or even months long. |
| 111 | + |
| 112 | +## Create Your Workflow Steps |
| 113 | + |
| 114 | +Let's now define those missing functions. |
| 115 | + |
| 116 | +```typescript title="server/workflows/user-signup.ts" lineNumbers |
| 117 | +import { FatalError } from "workflow"; |
| 118 | + |
| 119 | +// Our workflow function defined earlier |
| 120 | + |
| 121 | +async function createUser(email: string) { |
| 122 | + "use step"; // [!code highlight] |
| 123 | + |
| 124 | + console.log(`Creating user with email: ${email}`); |
| 125 | + |
| 126 | + // Full Node.js access - database calls, APIs, etc. |
| 127 | + return { id: crypto.randomUUID(), email }; |
| 128 | +} |
| 129 | + |
| 130 | +async function sendWelcomeEmail(user: { id: string; email: string }) { |
| 131 | + "use step"; // [!code highlight] |
| 132 | + |
| 133 | + console.log(`Sending welcome email to user: ${user.id}`); |
| 134 | + |
| 135 | + if (Math.random() < 0.3) { |
| 136 | + // By default, steps will be retried for unhandled errors |
| 137 | + throw new Error("Retryable!"); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +async function sendOnboardingEmail(user: { id: string; email: string }) { |
| 142 | + "use step"; // [!code highlight] |
| 143 | + |
| 144 | + if (!user.email.includes("@")) { |
| 145 | + // To skip retrying, throw a FatalError instead |
| 146 | + throw new FatalError("Invalid Email"); |
| 147 | + } |
| 148 | + |
| 149 | + console.log(`Sending onboarding email to user: ${user.id}`); |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +Taking a look at this code: |
| 154 | + |
| 155 | +- Business logic lives inside **steps**. When a step is invoked inside a **workflow**, it gets enqueued to run on a separate request while the workflow is suspended, just like `sleep`. |
| 156 | +- If a step throws an error, like in `sendWelcomeEmail`, the step will automatically be retried until it succeeds (or hits the step's max retry count). |
| 157 | +- Steps can throw a `FatalError` if an error is intentional and should not be retried. |
| 158 | + |
| 159 | +<Callout> |
| 160 | + We'll dive deeper into workflows, steps, and other ways to suspend or handle |
| 161 | + events in [Foundations](/docs/foundations). |
| 162 | +</Callout> |
| 163 | + |
| 164 | +</Step> |
| 165 | + |
| 166 | +<Step> |
| 167 | + |
| 168 | +## Create Your API Route |
| 169 | + |
| 170 | +To invoke your new workflow, we'll create a new API route handler at `server/api/signup.post.ts` with the following code: |
| 171 | + |
| 172 | +```typescript title="server/api/signup.post.ts" |
| 173 | +import { start } from 'workflow/api'; |
| 174 | +import { defineEventHandler, readBody } from 'h3'; |
| 175 | +import { handleUserSignup } from "../workflows/user-signup"; |
| 176 | + |
| 177 | +export default defineEventHandler(async (event) => { |
| 178 | + const { email } = await readBody(event); |
| 179 | + |
| 180 | + // Executes asynchronously and doesn't block your app |
| 181 | + await start(handleUserSignup, [email]); |
| 182 | + |
| 183 | + return { |
| 184 | + message: "User signup workflow started", |
| 185 | + }; |
| 186 | +}); |
| 187 | +``` |
| 188 | + |
| 189 | +This API route creates a `POST` request endpoint at `/api/signup` that will trigger your workflow. |
| 190 | + |
| 191 | +<Callout> |
| 192 | + Workflows can be triggered from API routes or any server-side |
| 193 | + code. |
| 194 | +</Callout> |
| 195 | + |
| 196 | +</Step> |
| 197 | + |
| 198 | +<Step> |
| 199 | + |
| 200 | +## Run in development |
| 201 | + |
| 202 | +To start your development server, run the following command in your terminal in the Nuxt root directory: |
| 203 | + |
| 204 | +```bash |
| 205 | +npm run dev |
| 206 | +``` |
| 207 | + |
| 208 | +Once your development server is running, you can trigger your workflow by running this command in the terminal: |
| 209 | + |
| 210 | +```bash |
| 211 | +curl -X POST --json '{"email":"hello@example.com"}' http://localhost:3000/api/signup |
| 212 | +``` |
| 213 | + |
| 214 | +Check the Nuxt development server logs to see your workflow execute as well as the steps that are being processed. |
| 215 | + |
| 216 | +Additionally, you can use the [Workflow DevKit CLI or Web UI](/docs/observability) to inspect your workflow runs and steps in detail. |
| 217 | + |
| 218 | +```bash |
| 219 | +npx workflow inspect runs # add '--web' for an interactive Web based UI |
| 220 | +``` |
| 221 | + |
| 222 | +<img src="/o11y-ui.png" alt="Workflow DevKit Web UI" /> |
| 223 | + |
| 224 | +</Step> |
| 225 | + |
| 226 | +</Steps> |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## Deploying to production |
| 231 | + |
| 232 | +Workflow DevKit apps currently work best when deployed to [Vercel](https://vercel.com/home) and needs no special configuration. |
| 233 | + |
| 234 | +Check the [Deploying](/docs/deploying) section to learn how your workflows can be deployed elsewhere. |
| 235 | + |
| 236 | +## Next Steps |
| 237 | + |
| 238 | +- Learn more about the [Foundations](/docs/foundations). |
| 239 | +- Check [Errors](/docs/errors) if you encounter issues. |
| 240 | +- Explore the [API Reference](/docs/api-reference). |
| 241 | + |
0 commit comments