openapi-zod-typed-express
Version:
Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types
98 lines (97 loc) • 3.56 kB
TypeScript
import type { NextFunction, Request, Response } from 'express';
import { z } from 'zod';
import { DeepPartial } from './utils';
export declare const __openapiZodTypedHackKey__ = "__openapiZodTypedHackKey__";
export declare const __openapiZodTypedHack__: unique symbol;
type Config = {
headers?: z.ZodTypeAny;
params?: Record<string, z.ZodTypeAny>;
query?: Record<string, z.ZodTypeAny>;
body?: z.ZodTypeAny;
returns?: z.ZodTypeAny;
};
type Present<T> = Exclude<T, undefined>;
type HeadersType<C extends Config> = [Present<C['headers']>] extends [never] ? {} : {
headers: z.output<Present<C['headers']>>;
};
type ParamsType<C extends Config> = [Present<C['params']>] extends [never] ? Record<string, never> : z.output<z.ZodObject<Present<C['params']>>>;
type QueryType<C extends Config> = [Present<C['query']>] extends [never] ? Record<string, never> : z.output<z.ZodObject<Present<C['query']>>>;
type BodyType<C extends Config> = [Present<C['body']>] extends [never] ? unknown : z.output<Present<C['body']>>;
type ReturnsType<C extends Config> = [Present<C['returns']>] extends [never] ? unknown : z.input<Present<C['returns']>>;
type ReturnsTransformType<C extends Config> = [Present<C['returns']>] extends [never] ? unknown : z.output<Present<C['returns']>>;
type TypedHandleDual<C extends Config> = (req: Omit<Request<ParamsType<C>, any, BodyType<C>, QueryType<C>>, 'headers'> & HeadersType<C>, res: Omit<Response, 'send'> & {
send: (data: ReturnsType<C>) => void;
transformSend: (data: ReturnsTransformType<C>) => void;
}, next: NextFunction) => void;
export declare const getApiDocInstance: ({ errorFormatter, }?: {
errorFormatter?: ((errors: {
errors: {
headers?: any;
params?: any;
query?: any;
body?: any;
returns?: any;
};
}) => any) | undefined;
}) => <C extends Config>(docs: C) => (handle: TypedHandleDual<C>) => any;
export declare const apiDoc: <C extends Config>(docs: C) => (handle: TypedHandleDual<C>) => any;
type ExpressRouterInternalStruct = {
name: 'router';
regexp: RegExp;
keys: {
name: string;
optional: boolean;
offset: number;
}[];
__handle: ExpressRouteInternalStruct;
handle: ExpressRouteInternalStruct;
route: {
stack: {
handle: (...any: any[]) => any;
method: string;
}[];
path: string;
};
};
type ExpressRouteHandlerInternalStruct = {
name: 'handle' | 'bound dispatch';
route: {
stack: {
handle: (a?: symbol) => {
apiRouteSchema: {
paramsSchema: any;
querySchema: any;
bodySchema: any;
returnsSchema: any;
};
handle: (...args: any[]) => any;
};
method: string;
_openapiZodTypedExpress__route_cache?: any;
}[];
path: string;
};
};
type ExpressRouteInternalStruct = {
stack: (ExpressRouteHandlerInternalStruct | ExpressRouterInternalStruct)[];
};
type OpenAPIShape = DeepPartial<{
openapi: '3.0.0';
info: {
description: string;
version: string;
title: string;
termsOfService: string;
contact: {
email: string;
};
};
servers: {
url: string;
}[];
paths: any;
}>;
export declare const initApiDocs: (expressApp: {
_router: ExpressRouteInternalStruct;
}, customOpenAPIType?: OpenAPIShape) => any;
export {};