UNPKG

@himenon/openapi-typescript-code-generator

Version:
411 lines (404 loc) 14.2 kB
import ts from 'typescript'; import { JSONSchema7, JSONSchema7TypeName, JSONSchema7Type } from 'json-schema'; type JSONSchemaTypeName = JSONSchema7TypeName; interface JSONSchema extends JSONSchema7 { nullable?: boolean; } type JSONSchemaDefinition = JSONSchema | boolean; /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#serverVariableObject */ interface ServerVariable { enum: string[]; default?: string; description?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#serverObject */ interface Server { url: string; description?: string; variables?: Record<string, ServerVariable>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#referenceObject */ interface Reference { $ref: string; summary?: string; description?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#licenseObject */ interface License { name: string; identifier?: string; url?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#contactObject */ interface Contact { name?: string; url?: string; email?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#externalDocumentationObject */ interface ExternalDocumentation { url: string; description?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#exampleObject */ interface Example { summary?: string; description?: string; value?: any; externalValue?: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#parameterObject */ interface Parameter { name: string; in: "path" | "query" | "header" | "cookie"; description?: string; required: boolean; deprecated?: boolean; allowEmptyValue?: boolean; style?: "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject"; explode?: boolean; allowReserved?: boolean; schema?: Schema; example?: any; examples?: Record<string, Example | Reference>; content?: Record<string, MediaType>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#headerObject */ type Header = Omit<Parameter, "name" | "in">; /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#encodingObject */ interface Encoding { contentType?: string; headers?: Record<string, Header | Reference>; style?: string; explode?: boolean; allowReserved?: boolean; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#mediaTypeObject */ interface MediaType { schema?: Schema; example?: any; examples?: Record<string, Example | Reference>; encoding?: Record<string, Encoding>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#requestBodyObject */ interface RequestBody { description?: string; content: Record<string, MediaType>; required: boolean; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responseObject */ interface Response { description: string; headers?: Record<string, Header | Reference>; content?: Record<string, MediaType>; links?: Record<string, Link | Reference>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#responsesObject */ interface Responses { default: Response | Reference; [statusCode: string]: Response | Reference; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#callbackObject */ interface Callback { [expression: string]: PathItem | Reference; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#oauthFlowObject */ interface OauthFlow { authorizationUrl: string; tokenUrl: string; refreshUrl?: string; scopes: Record<string, string>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#oauthFlowsObject */ interface OAuthFlows { implicit?: OauthFlow; password?: OauthFlow; clientCredentials?: OauthFlow; authorizationCode?: OauthFlow; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#securitySchemeObject */ interface SecuritySchema { type: "apiKey" | "http" | "mutualTLS" | "oauth2" | "openIdConnect"; description?: string; name: string; in: "query" | "header" | "cookie"; scheme: string; bearerFormat?: string; flows: OAuthFlows; openIdConnectUrl: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#securityRequirementObject */ interface SecurityRequirement { [name: string]: string[]; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#discriminatorObject */ interface Discriminator { propertyName: string; mapping: Record<string, string>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#discriminatorObject */ interface XML { name?: string; namespace?: string; prefix?: string; attribute?: boolean; wrapped?: boolean; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schemaObject */ interface Schema extends JSONSchema { discriminator?: Discriminator; xml?: XML; externalDocs?: ExternalDocumentation; example?: any; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#linkObject */ interface Link { operationRef?: string; operationId?: string; parameters?: Record<string, any | string>; requestBody?: Record<string, any | string>; description: string; server?: Server; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#operationObject */ interface Operation { tags?: string[]; summary?: string; description?: string; externalDocs?: ExternalDocumentation; operationId?: string; parameters?: (Parameter | Reference)[]; requestBody?: RequestBody | Reference; responses?: Responses; callbacks?: Record<string, Callback | Reference>; deprecated?: boolean; security?: SecurityRequirement[]; servers?: Server[]; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#pathItemObject */ interface PathItem { $ref?: string; summary?: string; description?: string; get?: Operation; put?: Operation; post?: Operation; delete?: Operation; options?: Operation; head?: Operation; patch?: Operation; trace?: Operation; servers?: Server[]; parameters?: (Parameter | Reference)[]; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#infoObject */ interface Info { title: string; summary: string; description: string; termsOfService: string; contact?: Contact; license?: License; version: string; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#pathsObject */ interface Paths { [path: string]: PathItem; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#componentsObject */ interface Components { schemas?: Record<string, Schema | Reference>; responses?: Record<string, Response | Reference>; parameters?: Record<string, Parameter | Reference>; examples?: Record<string, Example | Reference>; requestBodies?: Record<string, RequestBody | Reference>; headers?: Record<string, Header | Reference>; securitySchemes?: Record<string, SecuritySchema | Reference>; links?: Record<string, Link | Reference>; callbacks?: Record<string, Callback>; pathItems?: Record<string, PathItem>; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#tagObject */ interface Tag { name: string; description?: string; externalDocs?: ExternalDocumentation; } /** * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#openapi-object */ interface Document { openapi: string; info: Info; servers?: Server[]; paths?: Paths; webhooks?: Record<string, PathItem | Reference>; components?: Components; security?: SecurityRequirement; tags?: Tag[]; externalDocs?: ExternalDocumentation; } type OpenApi_Callback = Callback; type OpenApi_Components = Components; type OpenApi_Contact = Contact; type OpenApi_Discriminator = Discriminator; type OpenApi_Document = Document; type OpenApi_Encoding = Encoding; type OpenApi_Example = Example; type OpenApi_ExternalDocumentation = ExternalDocumentation; type OpenApi_Header = Header; type OpenApi_Info = Info; type OpenApi_JSONSchema = JSONSchema; type OpenApi_JSONSchemaDefinition = JSONSchemaDefinition; type OpenApi_JSONSchemaTypeName = JSONSchemaTypeName; type OpenApi_License = License; type OpenApi_Link = Link; type OpenApi_MediaType = MediaType; type OpenApi_OAuthFlows = OAuthFlows; type OpenApi_OauthFlow = OauthFlow; type OpenApi_Operation = Operation; type OpenApi_Parameter = Parameter; type OpenApi_PathItem = PathItem; type OpenApi_Paths = Paths; type OpenApi_Reference = Reference; type OpenApi_RequestBody = RequestBody; type OpenApi_Response = Response; type OpenApi_Responses = Responses; type OpenApi_Schema = Schema; type OpenApi_SecurityRequirement = SecurityRequirement; type OpenApi_SecuritySchema = SecuritySchema; type OpenApi_Server = Server; type OpenApi_ServerVariable = ServerVariable; type OpenApi_Tag = Tag; type OpenApi_XML = XML; declare namespace OpenApi { export { type OpenApi_Callback as Callback, type OpenApi_Components as Components, type OpenApi_Contact as Contact, type OpenApi_Discriminator as Discriminator, type OpenApi_Document as Document, type OpenApi_Encoding as Encoding, type OpenApi_Example as Example, type OpenApi_ExternalDocumentation as ExternalDocumentation, type OpenApi_Header as Header, type OpenApi_Info as Info, type OpenApi_JSONSchema as JSONSchema, type OpenApi_JSONSchemaDefinition as JSONSchemaDefinition, JSONSchema7Type as JSONSchemaType, type OpenApi_JSONSchemaTypeName as JSONSchemaTypeName, type OpenApi_License as License, type OpenApi_Link as Link, type OpenApi_MediaType as MediaType, type OpenApi_OAuthFlows as OAuthFlows, type OpenApi_OauthFlow as OauthFlow, type OpenApi_Operation as Operation, type OpenApi_Parameter as Parameter, type OpenApi_PathItem as PathItem, type OpenApi_Paths as Paths, type OpenApi_Reference as Reference, type OpenApi_RequestBody as RequestBody, type OpenApi_Response as Response, type OpenApi_Responses as Responses, type OpenApi_Schema as Schema, type OpenApi_SecurityRequirement as SecurityRequirement, type OpenApi_SecuritySchema as SecuritySchema, type OpenApi_Server as Server, type OpenApi_ServerVariable as ServerVariable, type OpenApi_Tag as Tag, type OpenApi_XML as XML }; } type PickedParameter = Pick<Parameter, "name" | "in" | "required" | "style" | "explode">; interface OpenApiResponses { [statusCode: string]: Response; } interface OpenApiOperation { httpMethod: string; requestUri: string; comment: string | undefined; deprecated: boolean; requestBody?: RequestBody; parameters?: Parameter[]; responses: OpenApiResponses; } interface ConvertedParams { escapedOperationId: string; argumentParamsTypeDeclaration: string; functionName: string; requestContentTypeName: string; responseContentTypeName: string; parameterName: string; pickedParameters: PickedParameter[]; requestBodyName: string; requestContentTypes: string[]; requestFirstContentType: string | undefined; has2OrMoreRequestContentTypes: boolean; responseErrorNames: string[]; responseSuccessNames: string[]; responseFirstSuccessName: string | undefined; has2OrMoreSuccessNames: boolean; successResponseContentTypes: string[]; successResponseFirstContentType: string | undefined; has2OrMoreSuccessResponseContentTypes: boolean; hasAdditionalHeaders: boolean; hasQueryParameters: boolean; hasParameter: boolean; hasRequestBody: boolean; } interface Params { operationId: string; convertedParams: ConvertedParams; operationParams: OpenApiOperation; } /** * Used to further transform the code created by the specified Generator Template. */ type IntermediateCode = string | ts.Statement; type GenerateFunction<Option = {}> = (payload: Params[], option?: Option) => IntermediateCode[]; interface OutputConfiguration { /** * */ transform?: (params: IntermediateCode) => IntermediateCode[]; } interface CustomGenerator<T> { generator: GenerateFunction<T>; option?: T; } type CodeGenerator_ConvertedParams = ConvertedParams; type CodeGenerator_CustomGenerator<T> = CustomGenerator<T>; type CodeGenerator_GenerateFunction<Option = {}> = GenerateFunction<Option>; type CodeGenerator_IntermediateCode = IntermediateCode; type CodeGenerator_OpenApiOperation = OpenApiOperation; type CodeGenerator_OpenApiResponses = OpenApiResponses; type CodeGenerator_OutputConfiguration = OutputConfiguration; type CodeGenerator_Params = Params; type CodeGenerator_PickedParameter = PickedParameter; declare namespace CodeGenerator { export type { CodeGenerator_ConvertedParams as ConvertedParams, CodeGenerator_CustomGenerator as CustomGenerator, CodeGenerator_GenerateFunction as GenerateFunction, CodeGenerator_IntermediateCode as IntermediateCode, CodeGenerator_OpenApiOperation as OpenApiOperation, CodeGenerator_OpenApiResponses as OpenApiResponses, CodeGenerator_OutputConfiguration as OutputConfiguration, CodeGenerator_Params as Params, CodeGenerator_PickedParameter as PickedParameter }; } export { type CustomGenerator as C, type Document as D, type GenerateFunction as G, type IntermediateCode as I, OpenApi as O, type Params as P, CodeGenerator as a };