Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-zod-router

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-zod-router

Inspired by koa-joi-router, this package aims to provide a similar feature-set while leveraging Zod and Typescript to create a fantastic dev experience.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-65.22%
Maintainers
1
Weekly downloads
 
Created
Source

⚡ koa-zod-router ⚡

Inspired by koa-joi-router, this package aims to provide a similar feature-set while leveraging Zod and Typescript to create a fantastic dev experience.

npm release Coverage Status downloads

🔥 Features:

  • Input/output validation and typesafety using zod
  • Body parsing using koa-bodyparser
  • Multipart parsing using formidable
  • Wraps @koa/router, providing the same API but with typesafety and validation.
  • CJS and ESM support

🚀 Install

npm install koa-zod-router

🚦 Quickstart

index.ts:

import Koa from 'koa';
import zodRouter from 'koa-zod-router';
import { z } from 'zod';

const app = new Koa();

const router = zodRouter();

router.register({
  name: 'example',
  method: 'post',
  path: '/post/:id',
  handler: async (ctx, next) => {
    const { foo } = ctx.request.body;
    ctx.body = { hello: 'world' };

    await next();
  },
  validate: {
    params: z.object({ id: z.coerce.number() }),
    body: z.object({ foo: z.number() }),
    response: z.object({ hello: z.string() }),
  },
});

app.use(router.routes());

app.listen(3000, () => {
  console.log('app listening on http://localhost:3000');
});

🛠️ Options

ParamTypeDescription
bodyParserObjectkoa-bodyparser options
formidableObjectformidable options
koaRouterObject@koa/router options
zodRouterObjectkoa-zod-router options
⚙️ zodRouter options
ParamTypeDescription
enableMultipartBooleanEnable Multipart parser middleware, used for file uploads
exposeRequestErrorsBooleanSend ZodErrors caused by client in response body
exposeResponseErrorsBooleanSend ZodErrors caused by the server in response body

Import/Exporting routes

Most likely you'll want to seperate your routes into seperate files, and register them somewhere else. To do this you can use the helper function createRouteSpec and specify the route's properties.

get-user.ts:

import { createRouteSpec } from '../src/util';
import { z } from 'zod';

export const getUserRoute = createRouteSpec({
  method: 'get',
  path: '/user/:id',
  handler: (ctx) => {
    ctx.body = {
      /* payload here */
    };
  },
  validate: {
    params: z.object({ id: z.coerce.number() }),
    response: z.object({
      /* validation here */
    }),
  },
});

index.ts:

import Koa from 'koa';
import zodRouter from 'koa-zod-router';
import { z } from 'zod';
import { getUserRoute } from './get-user.ts';

const app = new Koa();

const router = zodRouter();

router.register(getUserRoute);

app.use(router.routes());

app.listen(3000, () => {
  console.log('app listening on http://localhost:3000');
});

Keywords

FAQs

Package last updated on 17 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc