UNPKG

express-zod-api

Version:

A Typescript framework to help you get an API server up and running with I/O schema validation and custom middlewares in minutes.

96 lines (95 loc) 3.69 kB
import { d as FlatObject, g as CommonConfig, l as EmptyObject, t as Routing } from "./routing-xQnwfH2D.js"; import { z } from "zod"; import ts, { default as ts$1 } from "typescript"; interface NextHandlerInc<U> { next: (schema: z.core.$ZodType) => U; } type SchemaHandler<U, Context extends FlatObject = EmptyObject, Variant extends "regular" | "last" = "regular"> = ( schema: any, // eslint-disable-line @typescript-eslint/no-explicit-any -- for assignment compatibility ctx: Context & (Variant extends "regular" ? NextHandlerInc<U> : Context), ) => U; type HandlingRules<U, Context extends FlatObject = EmptyObject, K extends string | symbol = string | symbol> = Partial< Record<K, SchemaHandler<U, Context>> >; interface ZTSContext extends FlatObject { isResponse: boolean; makeAlias: (key: object, produce: () => ts.TypeNode) => ts.TypeNode; } type Producer = SchemaHandler<ts.TypeNode, ZTSContext>; declare abstract class IntegrationBase { protected readonly serverUrl: string; protected constructor(serverUrl: string); protected makeOmit: (base: string, props: Iterable<string>, reason?: string) => string; } interface IntegrationParams { routing: Routing; config: CommonConfig; /** * @desc What should be generated * @example "types" — types of your endpoint requests and responses (for a DIY solution) * @example "client" — an entity for performing typed requests and receiving typed responses * @default "client" * */ variant?: "types" | "client"; /** @default Client */ clientClassName?: string; /** @default Subscription */ subscriptionClassName?: string; /** * @desc The API URL to use in the generated code * @default https://example.com * */ serverUrl?: string; /** * @desc The schema to use for responses without body such as 204 * @default z.undefined() * */ noBodySchema?: z.ZodType; /** * @desc Depict the HEAD method for each Endpoint supporting the GET method (feature of Express) * @default true * */ hasHeadMethod?: boolean; /** * @desc Handling rules for your own schemas branded with `x-brand` metadata. * @desc Keys: brands (recommended to use unique symbols). * @desc Values: functions having schema as first argument that you should assign type to, second one is a context. * @example { MyBrand: (schema: typeof myBrandSchema, { next }) => createKeywordTypeNode(SyntaxKind.AnyKeyword) * @link https://www.npmjs.com/package/@express-zod-api/zod-plugin */ brandHandling?: HandlingRules<ts$1.TypeNode, ZTSContext>; /** * @desc Whether the server supports credentials in cross-origin requests. * @desc It sets `credentials: "include"` in Client default Implementation and `withCredentials` in Subscription. * @desc Requires the CORS configuration that includes: * @desc `Access-Control-Allow-Credentials: true` and a specific `Access-Control-Allow-Origin` headers. * @default false * */ hasCredentials?: boolean; } interface FormattedPrintingOptions { /** @desc Typescript printer options */ printerOptions?: ts$1.PrinterOptions; /** * @desc Typescript code formatter * @default prettier.format * */ format?: (program: string) => Promise<string>; } declare class Integration extends IntegrationBase { constructor({ routing, config, brandHandling, variant, clientClassName, subscriptionClassName, serverUrl, noBodySchema, hasHeadMethod, hasCredentials, }: IntegrationParams); print(printerOptions?: ts$1.PrinterOptions): string; printFormatted({ printerOptions, format: userDefined }?: FormattedPrintingOptions): Promise<string>; } export { Integration, type Producer };