Skip to content

Commit 7f43916

Browse files
committed
add examples
Signed-off-by: Nik Nasr <nik@restate.dev>
1 parent 709bf1d commit 7f43916

File tree

9 files changed

+436
-15
lines changed

9 files changed

+436
-15
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ dist
33
*.tsbuildinfo
44
.turbo
55
.DS_Store
6+
.next
7+
.swc
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
import "./.next/types/routes.d.ts";
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { withWorkflow } from "workflow/next";
2+
import type { NextConfig } from "next";
3+
4+
const nextConfig: NextConfig = {
5+
/* config options here */
6+
};
7+
8+
export default withWorkflow(nextConfig);

packages/examples/workflow/package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@
1919
"test": "turbo run _test --filter={.}...",
2020
"_test": "vitest run",
2121
"build": "turbo run _build --filter={.}...",
22-
"_build": "tsc --project tsconfig.build.json",
23-
"dev": "tsx watch --tsconfig ./tsconfig.json ./src/index.ts",
22+
"_build": "next build",
23+
"dev": "next dev",
2424
"start": "turbo run _start --filter={.}...",
25-
"_start": "node ./dist/index.js",
25+
"_start": "next start --port 9080",
2626
"clean": "rm -rf dist *.tsbuildinfo .turbo",
2727
"check:types": "turbo run _check:types --filter={.}...",
2828
"_check:types": "tsc --noEmit --project tsconfig.build.json",
2929
"lint": "eslint ."
3030
},
3131
"dependencies": {
3232
"@restatedev/world": "workspace:*",
33-
"workflow": "catalog:"
33+
"workflow": "catalog:",
34+
"next": "16.0.0"
35+
},
36+
"devDependencies": {
37+
"typescript": "^5",
38+
"@types/node": "^20",
39+
"@types/react": "^19"
3440
},
35-
"devDependencies": {},
3641
"peerDependencies": {
3742
"workflow": "catalog:"
3843
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { start } from "workflow/api";
2+
import { handleUserSignup } from "../../../workflows/user-signup.js";
3+
import { NextResponse } from "next/server.js";
4+
5+
export async function POST(request: Request) {
6+
const { email } = (await request.json()) as { email: string };
7+
8+
// Executes asynchronously and doesn't block your app
9+
await start(handleUserSignup, [email]);
10+
11+
return NextResponse.json({
12+
message: "User signup workflow started",
13+
});
14+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable @typescript-eslint/require-await */
2+
import { sleep } from "workflow";
3+
import { FatalError } from "workflow";
4+
5+
export async function handleUserSignup(email: string) {
6+
"use workflow";
7+
8+
const user = await createUser(email);
9+
await sendWelcomeEmail(user);
10+
11+
await sleep("5s"); // Pause for 5s - doesn't consume any resources
12+
await sendOnboardingEmail(user);
13+
14+
return { userId: user.id, status: "onboarded" };
15+
}
16+
17+
// Our workflow function defined earlier
18+
19+
async function createUser(email: string) {
20+
"use step";
21+
22+
console.log(`Creating user with email: ${email}`);
23+
24+
// Full Node.js access - database calls, APIs, etc.
25+
return { id: crypto.randomUUID(), email };
26+
}
27+
28+
async function sendWelcomeEmail(user: { id: string; email: string }) {
29+
"use step";
30+
31+
console.log(`Sending welcome email to user: ${user.id}`);
32+
33+
if (Math.random() < 0.3) {
34+
// By default, steps will be retried for unhandled errors
35+
throw new Error("Retryable!");
36+
}
37+
}
38+
39+
async function sendOnboardingEmail(user: { id: string; email: string }) {
40+
"use step";
41+
42+
if (!user.email.includes("@")) {
43+
// To skip retrying, throw a FatalError instead
44+
throw new FatalError("Invalid Email");
45+
}
46+
47+
console.log(`Sending onboarding email to user: ${user.id}`);
48+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
22
"extends": "../../../tsconfig.json",
33
"compilerOptions": {
4-
"composite": false
4+
"composite": false,
5+
"plugins": [
6+
{
7+
"name": "next"
8+
}
9+
]
510
},
6-
"include": ["src/**/*"]
11+
"include": [
12+
"src/**/*",
13+
".next/types/**/*.ts",
14+
"next-env.d.ts",
15+
".next/dev/types/**/*.ts"
16+
]
717
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://openapi.vercel.sh/vercel.json",
3+
"framework": "nextjs"
4+
}

0 commit comments

Comments
 (0)