UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

179 lines (178 loc) 5.92 kB
/** * PZG Pro - API Docs Generator (DMMF-based) * * Generate OpenAPI v3 documentation and mock servers from DMMF data */ import { ProFeatureBase } from '../../core/ProFeatureBase'; import { ProGeneratorContext } from '../../core/ProGeneratorContext'; export interface APIDocsConfig { outputPath?: string; title?: string; version?: string; description?: string; serverUrl?: string; mockPort?: number; enableMockServer?: boolean; startMockServer?: boolean; generateExamples?: boolean; includeDeprecated?: boolean; authScheme?: 'bearer' | 'apikey' | 'oauth2' | 'none'; responseFormats?: string[]; mockServer?: boolean; openApiVersion?: string; includeExamples?: boolean; includeChangelog?: boolean; /** * Route pluralisation. `'literal'` (default) appends a bare `s`, matching every * previously generated document; `'english'` applies the usual -ies/-es rules. */ pluralization?: 'literal' | 'english'; } export interface OpenAPISchema { openapi: string; info: { title: string; version: string; description?: string; }; servers: Array<{ url: string; description?: string; }>; paths: Record<string, Record<string, OpenAPIOperation>>; components: { schemas: Record<string, OpenAPISchemaObject>; securitySchemes?: Record<string, OpenAPISecurityScheme>; }; security?: Array<Record<string, string[]>>; } export interface OpenAPIOperation { summary: string; description?: string; operationId: string; tags: string[]; parameters?: OpenAPIParameter[]; requestBody?: OpenAPIRequestBody; responses: Record<string, OpenAPIResponse>; security?: Array<Record<string, string[]>>; } export interface OpenAPISchemaObject { type?: string; properties?: Record<string, OpenAPISchemaObject>; required?: string[]; items?: OpenAPISchemaObject; example?: unknown; description?: string; format?: string; enum?: unknown[]; allOf?: OpenAPISchemaObject[]; anyOf?: OpenAPISchemaObject[]; oneOf?: OpenAPISchemaObject[]; $ref?: string; maxLength?: number; minLength?: number; minimum?: number; maximum?: number; additionalProperties?: boolean | OpenAPISchemaObject; deprecated?: boolean; } export interface OpenAPIParameter { name: string; in: 'query' | 'path' | 'header' | 'cookie'; required?: boolean; schema: OpenAPISchemaObject; description?: string; example?: unknown; } export interface OpenAPIRequestBody { description?: string; required?: boolean; content: Record<string, { schema: OpenAPISchemaObject; example?: unknown; }>; } export interface OpenAPIResponse { description: string; content?: Record<string, { schema: OpenAPISchemaObject; example?: unknown; }>; headers?: Record<string, OpenAPISchemaObject>; } export interface OpenAPISecurityScheme { type: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect'; scheme?: string; bearerFormat?: string; name?: string; in?: 'query' | 'header' | 'cookie'; flows?: Record<string, unknown>; } export declare class ApiDocsGenerator extends ProFeatureBase { private config; constructor(context: ProGeneratorContext, config?: APIDocsConfig); protected getFeatureName(): string; protected generateFeature(): Promise<void>; /** * Offer every configured media type wherever the document describes a body. * * The builders emit `application/json` throughout, so rather than thread the * option through a dozen of them, each `content` map is expanded once here. * `responseFormats` was previously declared and never read, so a caller asking * for XML or CSV silently received JSON only. */ private applyResponseFormats; /** * The spec version to declare. * * Only the two versions the emitted document is actually valid against are * accepted; anything else falls back to 3.0.3 with a warning rather than * labelling the document as something it is not. */ private resolveOpenApiVersion; private generateOpenAPISpec; private generateModelSchema; private generateFieldSchema; private generateScalarFieldSchema; private generateCreateInputSchema; private generateUpdateInputSchema; private generateModelPaths; private generateIdParameters; private generateSecuritySchemes; private generateSecurity; private generateFieldExample; private generateScalarExample; private convertToYaml; private generateDocsPortal; private generateMockServer; private generateMockDataFunction; private generateMockRoutes; /** * How the emitted client attaches its credential, honouring `authScheme`. * * A bearer Authorization header was hardcoded, so the pack's own authScheme * option never reached the client — the SDK pack's equivalent was fixed in 2.6.0 * and the two packs disagreed until now. */ private authHeaderAssignment; private generateTypeScriptSDK; private generateSDKMethods; private generateExamples; private generateStaticDocs; private generateExampleData; /** * Route segment for a model. * * The spec's paths appended a bare `s` while this helper — used for the examples * and README — applied English rules, so the emitted artefacts disagreed: the * docs advertised `/categories` and the spec served `/categorys`. One rule now * governs all of them. * * `literal` is the default because it is what existing consumers are built * against; correcting the routes unconditionally would break them. * `pluralization: 'english'` opts in. */ private pluralize; private getExampleValueForField; private generateUsageREADME; }