koa-zod-router
Advanced tools
Comparing version 0.9.11 to 0.9.12
@@ -1,3 +0,215 @@ | ||
import zodRouter from './zod-router'; | ||
export type { RouterOpts, ZodRouter } from './types'; | ||
export default zodRouter; | ||
import * as koa from 'koa'; | ||
import { Middleware, DefaultState } from 'koa'; | ||
import KoaRouter, { RouterOptions, LayerOptions } from '@koa/router'; | ||
import formidable from 'formidable'; | ||
import bodyParser from 'koa-bodyparser'; | ||
import { ZodSchema, z } from 'zod'; | ||
import * as formidable_PersistentFile from 'formidable/PersistentFile'; | ||
type Method = 'acl' | 'bind' | 'checkout' | 'connect' | 'copy' | 'delete' | 'get' | 'head' | 'link' | 'lock' | 'm-search' | 'merge' | 'mkactivity' | 'mkcalendar' | 'mkcol' | 'move' | 'notify' | 'options' | 'patch' | 'post' | 'propfind' | 'proppatch' | 'purge' | 'put' | 'rebind' | 'report' | 'search' | 'source' | 'subscribe' | 'trace' | 'unbind' | 'unlink' | 'unlock' | 'unsubscribe'; | ||
interface ZodContext<Headers, Params, Query, Body, Files> { | ||
request: { | ||
body: Body; | ||
headers: Headers; | ||
params: Params; | ||
query: Query; | ||
files: Files; | ||
}; | ||
} | ||
type ValidationOptions<Headers, Params, Query, Body, Files, Response> = { | ||
headers?: ZodSchema<Headers>; | ||
body?: ZodSchema<Body>; | ||
params?: ZodSchema<Params>; | ||
query?: ZodSchema<Query>; | ||
files?: ZodSchema<Files>; | ||
response?: ZodSchema<Response>; | ||
}; | ||
type ZodMiddleware<H, P, Q, B, F, R> = Middleware<DefaultState, ZodContext<H, P, Q, B, F>, R> | Middleware<DefaultState, ZodContext<H, P, Q, B, F>, R>[]; | ||
type Spec<H, P, Q, B, F, R> = { | ||
name?: string; | ||
path: string; | ||
handlers: ZodMiddleware<H, P, Q, B, F, R>; | ||
pre?: ZodMiddleware<H, P, Q, B, F, R>; | ||
validate?: ValidationOptions<H, P, Q, B, F, R>; | ||
}; | ||
type RegisterSpec<H, P, Q, B, F, R> = { | ||
method: Method | Method[]; | ||
opts?: LayerOptions; | ||
} & Spec<H, P, Q, B, F, R>; | ||
type ZodRouter = ReturnType<typeof zodRouter>; | ||
interface RouterOpts { | ||
bodyParser?: bodyParser.Options; | ||
formidable?: formidable.Options; | ||
koaRouter?: RouterOptions; | ||
zodRouter?: { | ||
enableMultipart?: boolean; | ||
exposeRequestErrors?: boolean; | ||
exposeResponseErrors?: boolean; | ||
}; | ||
} | ||
declare const zodRouter: (opts?: RouterOpts) => { | ||
readonly router: KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
readonly register: <H = unknown, P = unknown, Q = unknown, B = unknown, F = unknown, R = unknown>(spec: RegisterSpec<H, P, Q, B, F, R>) => KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
readonly all: { | ||
<T = {}, U = {}, B_1 = unknown>(name: string, path: string | RegExp, ...middleware: KoaRouter.Middleware<koa.DefaultState & T, koa.DefaultContext & U, B_1>[]): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<T_1 = {}, U_1 = {}, B_2 = unknown>(path: string | RegExp | (string | RegExp)[], ...middleware: KoaRouter.Middleware<koa.DefaultState & T_1, koa.DefaultContext & U_1, B_2>[]): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly allowedMethods: (options?: KoaRouter.RouterAllowedMethodsOptions | undefined) => KoaRouter.Middleware<koa.DefaultState, koa.DefaultContext, unknown>; | ||
readonly match: (path: string, method: string) => KoaRouter.RoutesMatch; | ||
readonly methods: string[]; | ||
readonly middleware: () => KoaRouter.Middleware<koa.DefaultState, koa.DefaultContext, unknown>; | ||
readonly opts: KoaRouter.RouterOptions; | ||
readonly param: <BodyT = unknown>(param: string, middleware: KoaRouter.ParamMiddleware<koa.DefaultState, koa.DefaultContext, BodyT>) => KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
readonly params: object; | ||
readonly prefix: (prefix: string) => KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
readonly redirect: (source: string, destination: string, code?: number | undefined) => KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
readonly route: (name: string) => boolean | KoaRouter.Layer; | ||
readonly routes: () => KoaRouter.Middleware<koa.DefaultState, koa.DefaultContext, unknown>; | ||
readonly stack: KoaRouter.Layer[]; | ||
readonly use: { | ||
(...middleware: KoaRouter.Middleware<koa.DefaultState, koa.DefaultContext, unknown>[]): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
(path: string | RegExp | string[], ...middleware: KoaRouter.Middleware<koa.DefaultState, koa.DefaultContext, unknown>[]): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly url: (name: string, params?: any, options?: KoaRouter.UrlOptionsQuery | undefined) => string | Error; | ||
readonly acl: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly bind: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly checkout: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly connect: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly copy: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly delete: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly get: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly head: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly link: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly lock: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly "m-search": { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly merge: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly mkactivity: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly mkcalendar: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly mkcol: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly move: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly notify: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly options: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly patch: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly post: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly propfind: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly proppatch: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly purge: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly put: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly rebind: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly report: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly search: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly source: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly subscribe: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly trace: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly unbind: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly unlink: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly unlock: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
readonly unsubscribe: { | ||
<H_1, P_1, Q_1, B_3, F_1, R_1>(path: string, handlers: ZodMiddleware<H_1, P_1, Q_1, B_3, F_1, R_1>, validationOptions?: ValidationOptions<H_1, P_1, Q_1, B_3, F_1, R_1> | undefined): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
<H_2, P_2, Q_2, B_4, F_2, R_2>(spec: Spec<H_2, P_2, Q_2, B_4, F_2, R_2>): KoaRouter<koa.DefaultState, koa.DefaultContext>; | ||
}; | ||
}; | ||
declare const zFile: () => z.ZodUnion<[z.ZodType<formidable_PersistentFile, z.ZodTypeDef, formidable_PersistentFile>, z.ZodType<formidable_PersistentFile, z.ZodTypeDef, formidable_PersistentFile>]>; | ||
export { RouterOpts, ZodRouter, zodRouter as default, zFile }; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const zod_router_1 = __importDefault(require("./zod-router")); | ||
exports.default = zod_router_1.default; | ||
//# sourceMappingURL=index.js.map | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
default: () => src_default, | ||
zFile: () => zFile | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/zod-router.ts | ||
var import_router = __toESM(require("@koa/router")); | ||
// src/util.ts | ||
var import_formidable = require("formidable"); | ||
var import_zod = require("zod"); | ||
var { FormidableError } = import_formidable.errors; | ||
function flatten(middlewares) { | ||
const flattened = middlewares.reduce((acc, curr) => { | ||
if (!curr) { | ||
return acc; | ||
} | ||
if (Array.isArray(curr)) { | ||
return acc.concat(flatten(curr)); | ||
} | ||
return acc.concat(curr); | ||
}, []); | ||
return flattened; | ||
} | ||
var prepareMiddleware = (input) => { | ||
return flatten(input); | ||
}; | ||
async function noopMiddleware(ctx, next) { | ||
return await next(); | ||
} | ||
var assertValidation = (val) => { | ||
const props = ["headers", "body", "query", "params", "files", "response"]; | ||
if (typeof val === "object") { | ||
for (const prop of props) { | ||
if (val[prop]) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
var assertHandlers = (val) => { | ||
if (Array.isArray(val)) { | ||
for (const fn of val) { | ||
if (typeof fn !== "function") { | ||
return false; | ||
} | ||
} | ||
} else if (typeof val !== "function") { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
var zFile = () => { | ||
return import_zod.z.instanceof(import_formidable.PersistentFile).or(import_zod.z.instanceof(import_formidable.VolatileFile)); | ||
}; | ||
var assertFormidableError = (val) => { | ||
if (val instanceof FormidableError) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var methods = [ | ||
"acl", | ||
"bind", | ||
"checkout", | ||
"connect", | ||
"copy", | ||
"delete", | ||
"get", | ||
"head", | ||
"link", | ||
"lock", | ||
"m-search", | ||
"merge", | ||
"mkactivity", | ||
"mkcalendar", | ||
"mkcol", | ||
"move", | ||
"notify", | ||
"options", | ||
"patch", | ||
"post", | ||
"propfind", | ||
"proppatch", | ||
"purge", | ||
"put", | ||
"rebind", | ||
"report", | ||
"search", | ||
"source", | ||
"subscribe", | ||
"trace", | ||
"unbind", | ||
"unlink", | ||
"unlock", | ||
"unsubscribe" | ||
]; | ||
// src/zod-router.ts | ||
var import_koa_bodyparser = __toESM(require("koa-bodyparser")); | ||
// src/validation-middleware.ts | ||
var import_zod2 = require("zod"); | ||
var ValidationError = class extends Error { | ||
constructor(error) { | ||
super("VALIDATION_ERROR", { cause: error }); | ||
} | ||
}; | ||
var parsedSuccessful = (parsed) => { | ||
return parsed.success; | ||
}; | ||
var validate = async (data, schema) => { | ||
if (!schema) { | ||
return void 0; | ||
} | ||
const parsed = await schema.safeParseAsync(data); | ||
if (!parsedSuccessful(parsed)) { | ||
return parsed.error; | ||
} | ||
return parsed.data; | ||
}; | ||
var validationMiddleware = (validation, opts) => { | ||
if (!assertValidation(validation)) { | ||
return noopMiddleware; | ||
} | ||
return async (ctx, next) => { | ||
let inputErrors = []; | ||
const [headers, params, query, body, files] = await Promise.all([ | ||
validate(ctx.request.headers, validation.headers), | ||
validate(ctx.request.params, validation.params), | ||
validate(ctx.request.query, validation.query), | ||
validate(ctx.request.body, validation.body), | ||
validate(ctx.request.files, validation.files) | ||
]); | ||
if (headers) { | ||
if (headers instanceof import_zod2.ZodError) { | ||
inputErrors.push(headers); | ||
} else { | ||
Object.keys(headers).forEach((header) => { | ||
ctx.request.headers[header] = headers[header]; | ||
}); | ||
} | ||
} | ||
if (params) { | ||
if (params instanceof import_zod2.ZodError) { | ||
inputErrors.push(params); | ||
} else { | ||
ctx.request.params = params; | ||
} | ||
} | ||
if (query) { | ||
if (query instanceof import_zod2.ZodError) { | ||
inputErrors.push(query); | ||
} else { | ||
ctx.request.query = query; | ||
} | ||
} | ||
if (body) { | ||
if (body instanceof import_zod2.ZodError) { | ||
inputErrors.push(body); | ||
} else { | ||
ctx.request.body = body; | ||
} | ||
} | ||
if (files) { | ||
if (files instanceof import_zod2.ZodError) { | ||
inputErrors.push(files); | ||
} else { | ||
ctx.request.files = files; | ||
} | ||
} | ||
if (inputErrors.length && opts?.exposeRequestErrors) { | ||
ctx.status = 400; | ||
ctx.type = "json"; | ||
ctx.body = { error: inputErrors }; | ||
ctx.app.emit("error", new ValidationError({ inputErrors }), ctx); | ||
return; | ||
} | ||
if (inputErrors.length) { | ||
ctx.throw(400, "VALIDATION_ERROR"); | ||
} | ||
await next(); | ||
const output = await validate(ctx.body, validation.response); | ||
if (!output) { | ||
return; | ||
} | ||
if (output instanceof import_zod2.ZodError) { | ||
if (opts?.exposeResponseErrors) { | ||
ctx.status = 500; | ||
ctx.type = "json"; | ||
ctx.body = { error: output }; | ||
ctx.app.emit("error", new ValidationError({ output }), ctx); | ||
return; | ||
} | ||
ctx.throw(500); | ||
} else { | ||
ctx.body = output; | ||
ctx.response.body = output; | ||
} | ||
}; | ||
}; | ||
// src/multipart-parser-middleware.ts | ||
var import_formidable2 = __toESM(require("formidable")); | ||
var multipartParserMiddleware = (options) => { | ||
return async (ctx, next) => { | ||
if (!ctx.is("multipart")) { | ||
return next(); | ||
} | ||
const form = (0, import_formidable2.default)({ multiples: true, ...options }); | ||
try { | ||
await new Promise((resolve, reject) => { | ||
form.parse(ctx.req, (err, fields, files) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
ctx.request.body = fields; | ||
ctx.request.files = files; | ||
resolve(); | ||
}); | ||
}); | ||
await next(); | ||
} catch (err) { | ||
if (assertFormidableError(err)) { | ||
if (err.httpCode && err.httpCode >= 400 && err.httpCode < 500) { | ||
ctx.status = err.httpCode; | ||
ctx.app.emit("error", err, ctx); | ||
return; | ||
} | ||
} | ||
ctx.throw(500, err); | ||
} | ||
}; | ||
}; | ||
// src/zod-router.ts | ||
var zodRouter = (opts) => { | ||
const _router = new import_router.default(opts?.koaRouter); | ||
_router.use((0, import_koa_bodyparser.default)(opts?.bodyParser)); | ||
if (opts?.zodRouter?.enableMultipart) { | ||
_router.use(multipartParserMiddleware(opts?.formidable)); | ||
} | ||
function all() { | ||
return _router.all(...arguments); | ||
} | ||
function allowedMethods(options) { | ||
return _router.allowedMethods(options); | ||
} | ||
function match(path, method) { | ||
return _router.match(path, method); | ||
} | ||
function middleware() { | ||
return _router.middleware(); | ||
} | ||
function param(path, middleware2) { | ||
return _router.param(path, middleware2); | ||
} | ||
function prefix(path) { | ||
return _router.prefix(path); | ||
} | ||
function redirect(source, destination, code) { | ||
return _router.redirect(source, destination, code); | ||
} | ||
function route(name) { | ||
return _router.route(name); | ||
} | ||
function routes() { | ||
return _router.routes(); | ||
} | ||
function use() { | ||
return _router.use(...arguments); | ||
} | ||
function url(name, params, options) { | ||
return _router.url(name, params, options); | ||
} | ||
function register(spec) { | ||
const methodsParam = Array.isArray(spec.method) ? spec.method : [spec.method]; | ||
const name = spec.name ? spec.name : null; | ||
_router.register( | ||
spec.path, | ||
methodsParam, | ||
prepareMiddleware([spec.pre, validationMiddleware(spec.validate, opts?.zodRouter), spec.handlers]), | ||
{ name } | ||
); | ||
return _router; | ||
} | ||
const makeRouteMethods = () => methods.reduce((acc, method) => { | ||
acc[method] = (pathOrSpec, handlers, validationOptions) => { | ||
if (typeof pathOrSpec === "string" && assertHandlers(handlers)) { | ||
register({ | ||
method, | ||
path: pathOrSpec, | ||
handlers, | ||
validate: validationOptions | ||
}); | ||
return _router; | ||
} | ||
if (typeof pathOrSpec === "object") { | ||
register({ | ||
...pathOrSpec, | ||
method | ||
}); | ||
return _router; | ||
} | ||
throw new Error("Invalid route arguments"); | ||
}; | ||
return acc; | ||
}, {}); | ||
return { | ||
...makeRouteMethods(), | ||
get router() { | ||
return _router; | ||
}, | ||
register, | ||
all, | ||
allowedMethods, | ||
match, | ||
methods: _router.methods, | ||
middleware, | ||
opts: _router.opts, | ||
param, | ||
params: _router.params, | ||
prefix, | ||
redirect, | ||
route, | ||
routes, | ||
stack: _router.stack, | ||
use, | ||
url | ||
}; | ||
}; | ||
var zod_router_default = zodRouter; | ||
// src/index.ts | ||
var src_default = zod_router_default; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
zFile | ||
}); |
{ | ||
"name": "koa-zod-router", | ||
"version": "0.9.11", | ||
"version": "0.9.12", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"type": "commonjs", | ||
"scripts": { | ||
"build": "tsc --build tsconfig.build.json", | ||
"example": "ts-node-dev examples/index.ts", | ||
"build": "rm -rf dist && tsup src/index.ts --format cjs,esm --dts", | ||
"example": "tsup examples/index.ts --watch", | ||
"test": "mocha", | ||
"lint": "eslint \"src/**/*.ts\" --cache && prettier --check \"src/**/*.ts\"" | ||
"test:coverage": "nyc --reporter=text mocha", | ||
"lint": "tsc && eslint \"src/**/*.ts\" --cache && prettier --check \"src/**/*.ts\"", | ||
"release": "pnpm build && npm publish" | ||
}, | ||
@@ -26,2 +29,3 @@ "author": { | ||
"devDependencies": { | ||
"@changesets/cli": "^2.26.0", | ||
"@types/formidable": "^2.0.5", | ||
@@ -32,2 +36,3 @@ "@types/koa": "^2.13.5", | ||
"@types/node": "^18.11.18", | ||
"@types/sinon": "^10.0.13", | ||
"@types/supertest": "^2.0.12", | ||
@@ -43,2 +48,3 @@ "@typescript-eslint/eslint-plugin": "^5.48.1", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.8.2", | ||
@@ -48,7 +54,8 @@ "sinon": "^15.0.1", | ||
"ts-node": "^10.9.1", | ||
"ts-node-dev": "^2.0.0", | ||
"tsup": "^6.5.0", | ||
"turbo": "^1.7.0", | ||
"typescript": "^4.9.4" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3.20.2" | ||
"zod": ">=3.20.2 <4.x" | ||
}, | ||
@@ -55,0 +62,0 @@ "keywords": [ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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 README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
925
1
9
39512
25
9
1