koa-zod-router
Advanced tools
Comparing version 1.1.0-beta.1 to 1.1.0
# koa-zod-router | ||
## 1.1.0 | ||
### Minor Changes | ||
- a0c71c6: Add state helpers, fix esm bundling, more descriptive zod errors in response | ||
## 1.0.4 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "koa-zod-router", | ||
"version": "1.1.0-beta.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -9,3 +9,3 @@ "main": "dist/index.js", | ||
"exports": { | ||
"default": "./dist/index.js", | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs", | ||
@@ -12,0 +12,0 @@ "types": "./dist/index.d.ts" |
# ⚡ 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. | ||
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 validation. | ||
@@ -103,2 +103,83 @@ ![npm release](https://img.shields.io/npm/v/koa-zod-router?label=latest) | ||
### Adding state to routes | ||
zodRouter accepts a type parameter for adding types to `ctx.state`, as well as providing a helper function used creating routes and middlewares with state types. | ||
`route-state.ts:` | ||
```js | ||
import { routerSpecFactory } from 'koa-zod-router'; | ||
export type UserState = { | ||
user: { | ||
username: string; | ||
email: string; | ||
id: number; | ||
}; | ||
}; | ||
export const specFactory = routerSpecFactory<User>(); | ||
``` | ||
`auth-middleware.ts:` | ||
```js | ||
import { z } from 'zod'; | ||
import { specFactory } from './route-state'; | ||
export const authMiddleware = specFactory.createUseSpec({ | ||
handler: async (ctx, next) => { | ||
// ... validate the session token | ||
// setting state is now typesafe | ||
ctx.state.user = { | ||
username: 'johndoe', | ||
email: 'example@email.com', | ||
id: 1, | ||
}; | ||
await next(); | ||
}, | ||
validate: { | ||
// validation fails if `x-session-token` is not set in the HTTP request headers | ||
headers: z.object({ 'x-session-token': z.string() }), | ||
}, | ||
}); | ||
``` | ||
`get-user.ts:` | ||
```js | ||
import { z } from 'zod'; | ||
import { specFactory } from './route-state'; | ||
export const getUserRoute = specFactory.createRouteSpec({ | ||
method: 'get', | ||
path: '/user/:id', | ||
handler: (ctx) => { | ||
//.. our route has access to the ctx.state.user types now | ||
ctx.state.user | ||
}, | ||
validate: { | ||
/* validation here */ | ||
}, | ||
}, | ||
); | ||
``` | ||
`index.ts:` | ||
```js | ||
import zodRouter from 'koa-zod-router'; | ||
import { UserState } from './router-state'; | ||
import { authMiddleware } from './auth-middleware'; | ||
import { getUserRoute } from './get-user'; | ||
const router = zodRouter<UserState>(); | ||
router.use(authMiddleware); | ||
router.register(getUserRoute); | ||
``` | ||
### Exposing validation errors to the client | ||
@@ -161,3 +242,3 @@ | ||
koa-zod-router uses [formidable] for any requests received with the `Content-Type` header set to `multipart/*`. | ||
koa-zod-router uses [formidable] for any requests received with the `Content-Type` header set to `multipart/*`. | ||
@@ -194,3 +275,3 @@ This functionality is disabled by default, to enable this functionality create an instance of zodRouter and pass in `{ zodRouter: { enableMultipart: true } }` as your config. Then to validate files utilize the helper function `zFile`. | ||
Found a bug? | ||
Found a bug? | ||
Please let me know in [Issues section](https://github.com/JakeFenley/koa-zod-router/issues). | ||
@@ -202,2 +283,2 @@ | ||
Found a vulnerability or other security issue? | ||
Please refer to [Security policy](https://github.com/JakeFenley/koa-zod-router/blob/main/SECURITY.md). | ||
Please refer to [Security policy](https://github.com/JakeFenley/koa-zod-router/blob/main/SECURITY.md). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
53090
1
281