Skip to content

Commit 2171e2b

Browse files
committed
Add zod module
1 parent 267a23b commit 2171e2b

File tree

12 files changed

+263
-3
lines changed

12 files changed

+263
-3
lines changed

package-lock.json

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"packages/restate-sdk",
2020
"packages/restate-sdk-cloudflare-workers",
2121
"packages/restate-sdk-clients",
22+
"packages/restate-sdk-zod",
2223
"packages/restate-sdk-examples",
2324
"packages/restate-sdk-testcontainers",
2425
"packages/restate-e2e-services"
@@ -31,7 +32,7 @@
3132
"format": "npm run format -ws --if-present",
3233
"format-check": "npm run format-check -ws --if-present",
3334
"verify": "npm run verify -ws --if-present",
34-
"clean": "rm -rf packages/restate-sdk/dist && rm -rf packages/restate-sdk-cloudflare-workers/dist && rm -rf packages/restate-sdk-examples/dist && rm -rf packages/restate-sdk-ingress/dist && rm -rf packages/restate-e2e-services/dist && rm -rf packages/restate-sdk-core/dist"
35+
"clean": "rm -rf packages/restate-sdk-zod/dist && rm -rf packages/restate-sdk/dist && rm -rf packages/restate-sdk-cloudflare-workers/dist && rm -rf packages/restate-sdk-examples/dist && rm -rf packages/restate-sdk-ingress/dist && rm -rf packages/restate-e2e-services/dist && rm -rf packages/restate-sdk-core/dist"
3536
},
3637
"devDependencies": {
3738
"@arethetypeswrong/cli": "^0.15.3",

packages/restate-sdk-examples/tsconfig.eslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"include": ["src/**/*.ts", "test/**/*.ts"],
44
"references": [
55
{ "path": "../restate-sdk-core" },
6+
{ "path": "../restate-sdk-zod" },
67
{ "path": "../restate-sdk-testcontainers" }
78
]
89
}

packages/restate-sdk-examples/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"references": [
88
{ "path": "../restate-sdk-core" },
99
{ "path": "../restate-sdk" },
10+
{ "path": "../restate-sdk-zod" },
1011
{ "path": "../restate-sdk-clients" },
1112
{ "path": "../restate-sdk-testcontainers" }
1213
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
src/generated

packages/restate-sdk-zod/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![Documentation](https://img.shields.io/badge/doc-reference-blue)](https://docs.restate.dev)
2+
[![Examples](https://img.shields.io/badge/view-examples-blue)](https://github.com/restatedev/examples)
3+
[![Discord](https://img.shields.io/discord/1128210118216007792?logo=discord)](https://discord.gg/skW3AZ6uGd)
4+
[![Twitter](https://img.shields.io/twitter/follow/restatedev.svg?style=social&label=Follow)](https://twitter.com/intent/follow?screen_name=restatedev)
5+
6+
# Restate Typescript SDK
7+
8+
[Restate](https://restate.dev/) is a system for easily building resilient applications using *distributed durable async/await*. This repository contains the Restate SDK for writing services in **Node.js / Typescript**.
9+
10+
Restate applications are composed of *durably executed, stateful RPC handlers* that can run either
11+
as part of long-running processes, or as FaaS (AWS Lambda).
12+
13+
```typescript
14+
import * as restate from "@restatedev/restate-sdk";
15+
16+
const greeter = restate.service({
17+
name: "greeter",
18+
handlers: {
19+
greet: async (ctx: restate.Context, name: string) => {
20+
return `Hello ${name}!`;
21+
},
22+
},
23+
});
24+
25+
restate.endpoint()
26+
.bind(greeter)
27+
.listen(9080);
28+
```
29+
30+
## Community
31+
32+
* 🤗️ [Join our online community](https://discord.gg/skW3AZ6uGd) for help, sharing feedback and talking to the community.
33+
* 📖 [Check out our documentation](https://docs.restate.dev) to get quickly started!
34+
* 📣 [Follow us on Twitter](https://twitter.com/restatedev) for staying up to date.
35+
* 🙋 [Create a GitHub issue](https://github.com/restatedev/sdk-typescript/issues) for requesting a new feature or reporting a problem.
36+
* 🏠 [Visit our GitHub org](https://github.com/restatedev) for exploring other repositories.
37+
38+
## Using the SDK
39+
40+
To use this SDK, add the dependency to your project:
41+
```shell
42+
npm install @restatedev/restate-sdk
43+
```
44+
45+
For brand-new projects, we recommend using the [Restate Node Template](https://github.com/restatedev/node-template-generator):
46+
```shell
47+
npx -y @restatedev/create-app@latest
48+
```
49+
50+
## Versions
51+
52+
This library follows [Semantic Versioning](https://semver.org/).
53+
54+
The compatibility with Restate is described in the following table:
55+
56+
| Restate Server\sdk-typescript | 1.0/1.1/1.2/1.3 | 1.4 | 1.5 |
57+
|-------------------------------|------------------|-----|-----|
58+
| 1.0 ||||
59+
| 1.1 | ✅ <sup>(1)</sup> |||
60+
| 1.2 ||||
61+
62+
<sup>(1)</sup> **Only** when upgrading from 1.0 to 1.1 you MUST rediscover all the existing deployments using `restate dp register <address> --force`. You don't need to update the SDK, nor change the code.

packages/restate-sdk-zod/package.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@restatedev/restate-sdk-zod",
3+
"version": "1.4.0",
4+
"description": "Restate Typescript SDK Zod",
5+
"author": "Restate Developers",
6+
"license": "MIT",
7+
"email": "[email protected]",
8+
"homepage": "https://github.com/restatedev/sdk-typescript#readme",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/restatedev/sdk-typescript.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/restatedev/sdk-typescript/issues"
15+
},
16+
"type": "module",
17+
"sideEffects": false,
18+
"main": "./dist/cjs/src/public_api.js",
19+
"types": "./dist/cjs/src/public_api.d.ts",
20+
"module": "./dist/esm/src/public_api.js",
21+
"exports": {
22+
".": {
23+
"import": {
24+
"types": "./dist/esm/src/public_api.d.ts",
25+
"default": "./dist/esm/src/public_api.js"
26+
},
27+
"require": {
28+
"types": "./dist/cjs/src/public_api.d.ts",
29+
"default": "./dist/cjs/src/public_api.js"
30+
}
31+
}
32+
},
33+
"files": [
34+
"dist"
35+
],
36+
"devDependencies": {
37+
"@restatedev/restate-sdk-core": "^1.4.0"
38+
},
39+
"dependencies": {
40+
"zod": "^3.6.0",
41+
"zod-to-json-schema": "3.24.3"
42+
},
43+
"scripts": {
44+
"build": "npm run build:cjs && npm run build:esm",
45+
"build:cjs": "tsc --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
46+
"build:esm": "tsc --outDir ./dist/esm --declaration --declarationDir ./dist/esm",
47+
"lint": "eslint --ignore-path .eslintignore --max-warnings=0 --ext .ts .",
48+
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|ts|json)\"",
49+
"format-check": "prettier --ignore-path .eslintignore --check \"**/*.+(js|ts|json)\"",
50+
"attw": "attw --pack",
51+
"verify": "npm run format-check && npm run lint && npm run build && npm run attw",
52+
"release": "release-it"
53+
},
54+
"engines": {
55+
"node": ">= 18.13"
56+
},
57+
"directories": {
58+
"example": "examples",
59+
"test": "test"
60+
},
61+
"publishConfig": {
62+
"@restatedev:registry": "https://registry.npmjs.org"
63+
}
64+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
12+
export { serde } from "./serde_api.js";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3+
*
4+
* This file is part of the Restate SDK for Node.js/TypeScript,
5+
* which is released under the MIT license.
6+
*
7+
* You can find a copy of the license in file LICENSE in the root
8+
* directory of this repository or package, or at
9+
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10+
*/
11+
12+
/* eslint-disable @typescript-eslint/no-explicit-any */
13+
/* eslint-disable @typescript-eslint/no-namespace */
14+
/* eslint-disable @typescript-eslint/ban-types */
15+
/* eslint-disable @typescript-eslint/no-unsafe-return */
16+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
17+
18+
import type { Serde } from "@restatedev/restate-sdk-core";
19+
20+
import type { z } from "zod";
21+
import { zodToJsonSchema } from "zod-to-json-schema";
22+
23+
class ZodSerde<T extends z.ZodType> implements Serde<z.infer<T>> {
24+
contentType = "application/json";
25+
jsonSchema?: object | undefined;
26+
27+
constructor(private readonly schema: T, name: string) {
28+
this.jsonSchema = zodToJsonSchema(schema, name);
29+
}
30+
31+
serialize(value: T): Uint8Array {
32+
if (value === undefined) {
33+
return new Uint8Array(0);
34+
}
35+
return new TextEncoder().encode(JSON.stringify(value));
36+
}
37+
38+
deserialize(data: Uint8Array): T {
39+
if (data.length === 0) {
40+
return undefined as any;
41+
}
42+
const js = JSON.parse(new TextDecoder().decode(data));
43+
44+
const res = this.schema.safeParse(js);
45+
if (res.success) {
46+
return res.data;
47+
}
48+
throw res.error;
49+
}
50+
}
51+
52+
export namespace serde {
53+
/**
54+
* A Zod based serde.
55+
*
56+
* @param schema the zod type
57+
* @param name the name of the type for the json schema
58+
* @returns a serde that will validate the data with the zod schema
59+
*/
60+
export const zod = <T extends z.ZodType>(
61+
zodType: T,
62+
name: string
63+
): Serde<z.infer<T>> => {
64+
return new ZodSerde(zodType, name);
65+
};
66+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["src/**/*.ts", "test/**/*.ts"],
4+
"references": [{ "path": "../restate-sdk-core" }]
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/esm"
5+
},
6+
"include": ["src/**/*.ts"],
7+
"references": [{ "path": "../restate-sdk-core" }]
8+
}

tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"@restatedev/restate-sdk": ["./packages/restate-sdk/src/public_api.ts"],
2222
"@restatedev/restate-sdk-clients": [
2323
"./packages/restate-sdk-clients/src/public_api.ts"
24-
]
24+
],
25+
"@restatedev/restate-sdk-zod": ["./packages/restate-sdk-zod/src/public_api.ts"]
2526
}
2627
}
2728
}

0 commit comments

Comments
 (0)