一覧へ戻る
KindaSloth/zod-express-endpoint(新しいタブで開きます)
主要言語: TypeScript
Create fully inferred typesafe express endpoints using zod schemas
- star 数
- 9
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2023/02/16
主要言語: TypeScript
Create fully inferred typesafe express endpoints using zod schemas
$ yarn add zod-express-endpoint
# or
$ npm i zod-express-endpoint
import express, { json } from "express"; import { z } from "zod"; import { TypeSafeEndpoint } from "zod-express-endpoint"; const server = express(); server.use(json()); const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); server.post( "/api", endpoint.create( { bodySchema: z.object({ message: z.string() }), responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { const { message } = req.body; return send(200, { message }); } ) );
const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always", customUnexpectedError: "Oh no", });
| Syntax | Type | Required | Description |
|---|---|---|---|
| zodErrorMode | ZodErrorMode | true | Endpoint will trigger zod errors in production, development or both |
| customUnexpectedError | String | false | Error Message when some unexpected error happens |
type ZodErrorMode = "production" | "development" | "always";
import { TypeSafeEndpoint } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { bodySchema: z.object({ message: z.string() }), responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { const { message } = req.body; return send(200, { message }); } );
import { TypeSafeEndpoint } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { paramsSchema: z.object({ message: z.string() }), responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { const { message } = req.params; return send(200, { message }); } );
import { TypeSafeEndpoint } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { querySchema: z.object({ message: z.string() }), responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { const { message } = req.query; return send(200, { message }); } );
import { TypeSafeEndpoint } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { return send(200, { message: "Hello there" }); } );
import { TypeSafeEndpoint, EndpointError } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { return send(400, { error: 'Error' }); } ); # or import { TypeSafeEndpoint, EndpointError } from "zod-express-endpoint"; const endpoint = new TypeSafeEndpoint({ zodErrorMode: "always" }); const exampleEndpoint = endpoint.create( { responseSchema: z.object({ message: z.string() }), }, async (req, _res, send) => { throw new EndpointError("Error"); } );