jakefenley / koa-zod-router Goto Github PK
View Code? Open in Web Editor NEWBuild typesafe routes for Koa with ease. Utilizes Typescript, Zod, and Koa-Router to provide an easy solution to I/O validation and type inference.
License: MIT License
Build typesafe routes for Koa with ease. Utilizes Typescript, Zod, and Koa-Router to provide an easy solution to I/O validation and type inference.
License: MIT License
It seems that we cannot customize error display (except via the exposeRequestErrors or exposeResponseErrors options).
I'd like to be able to customize the error format (for example in order to give more user friendly messages)
Is it something that is possible, do you intend to implement it, or would you be open for a PR in that sense?
Tried using the example for pulling in routes from a different file but get an error Exported variable 'routeMe' has or is using name 'ZodContext' from external module ".../node_modules/.pnpm/[email protected]_6qvceonjeialq36lgrl7dj2ju4/node_modules/koa-zod-router/dist/index" but cannot be named.
Sample code:
// routeMe.ts
export const routeMe= createRouteSpec({
name: 'route-me',
method: 'get',
path: '/route-me',
handler: handleRouteMe,
validate: {
body: RouteMeRequestBodySchema,
response: RouteMeResponseSchema,
},
})
// index.ts
router.register(routeMe)
When I use createRouteSpec
in my index.ts
file everything works, but I would much rather have my routes defined elsewhere.
Package Manager: pnpm
Using version 1.0.3
Project is ESM
All peer dependencies met
Currently, package.json has these dependencies:
"@types/formidable": "^2.0.6",
"@types/koa-bodyparser": "^4.3.11",
"@types/koa__router": "^12.0.2",
Could these be moved to dev dependencies? This would make the npm install size smaller. The koa-zod-router tests pass after making this change. But I don't know if it would break other projects that use koa-zod-router.
Is there any way to extend the router context? I noticed that RouterState
is generic, but the context is hardcoded to DefaultContext
.
Hey!
This looks like a really useful tool, and I'm going to be using it for the API I'm currently working on.
I'm also looking for something to create an OpenAPI document for the API, and it looks like there are a few libraries out there for that at the moment, but none of them allow you to define everything in the router.
Would it be possible to add functionality to be able to generate an entire OpenAPI document from a router object? That would be super useful!
If there's already some way that I can accomplish this, that would also be great!
Thanks :)
For example
router.post({
path: "/",
handler: async (ctx, next) => {
const { upload } = ctx?.request?.files;
....
....
await next();
},
validate: {
files: z.object({
upload: zFile(), // only allow 'image/jpeg'
}),
},
});
Love the library! Thank you!
I was wondering, when a validation error occurs I'm just getting back a 400 response with body VALIDATION_ERROR
. Is there any way to configure the router to respond with all ZodError
issues?
Hi,
Just started using this library and came across a problem where trying to restructure the errors provided by ctx.invalid
inside a custom validationErrorHandler
function, that is that the type shows the fields to be ZodError[]
but the actual type at runtime is an object and not an array.
I'm in no way a typescript expert so please let me know if this is something I am doing wrong. Other then this issue, I am really liking the library.
Hello,
I am trying to apply middleware to some routes, unfortunately because the middleware makes use of createUseSpec
it is not compatible with the .use
on a route register as such
userRouter.register(getAllUsers).use(isAdmin);
I didn't think it made sense for one route out of 11 to make a separate router to apply the middleware to just to apply to that one route and since the createRouteSpec
doesn't seem to have a way to pass middleware there either, I wasn't sure what the correct or expected way of doing it was?
I have been using koa-zod-router in my API for about 6 months. Until now, I've had no issues with it.
We are reworking a form that submits to a koa-zod-router endpoint. Previously, this form had a schema with around 30 keys on it.
Now, it has over 50. A lot of these new keys are optional/conditional followup questions that have been added in the new form design.
This schema triggers an infinitely deep error from Typescript. Too much type inference. But only when passed to koa-zod-router for validation.
In an attempt to deal with this, I dramatically simplified the schema itself. Enums became strings, etc. I was also initially building up the schema by merging in sub-sections of the form (sub schemas) and then using the built up schema for final validation. I now just pass in a hard-coded schema.
This solves the infinitely deep problem. Type inference works, but is now incredibly slow. Borderline unusably slow.
In addition - instead of returning an VALIDATION_ERROR, it now seems to allow the request to just go through, but tacks on an issues array to the request body.
What about passing in a schema to koa-zod-router would causes this behavior?
hello there, mate! thanks a lot for your effort making this library
there's an issue that is easy to reproduce:
the below code works (i.e. the response is test
, in 5 sec):
import Koa from 'koa'
import Router from '@koa/router'
const router = new Router()
const app = new Koa()
router.get('/foo', async ctx => {
await new Promise(resolve => setTimeout(resolve, 5000))
ctx.cookies.set('foo', 'bar')
ctx.body = 'test'
})
app.use(router.routes())
app.listen(3000)
however, this doesn't work (the response "not found" is sent immediately, and in 5 seconds ERR_HTTP_HEADERS_SENT
happens):
import Koa from 'koa'
import { zodRouter } from 'koa-zod-router'
const router = zodRouter()
const app = new Koa()
router.get('/foo', async ctx => {
await new Promise(resolve => setTimeout(resolve, 5000))
ctx.cookies.set('foo', 'bar')
ctx.body = 'test'
})
app.use(router.routes())
app.listen(3000)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.