Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
koa-zod-router
Advanced tools
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.
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 install koa-zod-router
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');
});
Param | Type | Description |
---|---|---|
bodyParser | Object | koa-bodyparser options |
formidable | Object | formidable options |
koaRouter | Object | @koa/router options |
zodRouter | Object | koa-zod-router options |
Param | Type | Description |
---|---|---|
enableMultipart | Boolean | Enable Multipart parser middleware, used for file uploads |
exposeRequestErrors | Boolean | Send ZodErrors caused by client in response body |
exposeResponseErrors | Boolean | Send ZodErrors caused by the server in response body |
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');
});
1.0.0
FAQs
Inspired by koa-joi-router, this package aims to provide a similar feature-set while leveraging Zod and Typescript to create typesafe routes and middlewares with built in I/O validation.
The npm package koa-zod-router receives a total of 783 weekly downloads. As such, koa-zod-router popularity was classified as not popular.
We found that koa-zod-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.