Giter Site home page Giter Site logo

jakefenley / koa-zod-router Goto Github PK

View Code? Open in Web Editor NEW
53.0 53.0 2.0 425 KB

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

TypeScript 99.95% Shell 0.05%
api endpoint http koa koa-router middleware nodejs router schema schema-validation server typescript validation zod

koa-zod-router's People

Contributors

cloudrac3r avatar github-actions[bot] avatar jakefenley avatar jakefenley-mh avatar tomaskallup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

koa-zod-router's Issues

Customize error display

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?

Exported variable ... has or is using name 'ZodContext' from external module

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

Move types to dev dependencies?

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.

Extending the router context?

Is there any way to extend the router context? I noticed that RouterState is generic, but the context is hardcoded to DefaultContext.

OpenAPI Specification

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 :)

How to expose full validation error in response?

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?

ctx.invalid.{body,headers,params...} is an object despite the type being an array of ZodError

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.

How to apply middleware from a specFactory on a single route?

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?

Can't handle large schemas?

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?

Broken async middlewares

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.