import type { GlobalContext , OpenAPI2 , OpenAPI3 , SchemaObject , SwaggerToTSOptions } from "./types" ; import path from "path" ; import prettier from "prettier" ; import parserTypescript from "prettier/parser-typescript.js" ; import { URL } from "url" ; import load , { resolveSchema , VIRTUAL_JSON_URL } from "./load.js" ; import { swaggerVersion } from "./utils.js" ; import { transformAll } from "./transform/index.js" ; export * from "./types.js" ; export const WARNING_MESSAGE = `/**
* This file was auto-generated by openapi-typescript.
* Do not make direct changes to the file.
*/


` ; param { string } schema param { SwaggerToTSOptions < typeof schema > } options return { Promise < string > } async function openapiTS ( schema , options = { } as Partial < SwaggerToTSOptions > ) { const ctx = { additionalProperties : options . additionalProperties || false , auth : options . auth , defaultNonNullable : options . defaultNonNullable || false , formatter : options && typeof options . formatter === "function" ? options . formatter : undefined , immutableTypes : options . immutableTypes || false , rawSchema : options . rawSchema || false , version : options . version || 3 , } as any ; const isInlineSchema = typeof schema != "string" && schema instanceof URL == false ; let rootSchema = { } ; let external = { } ; const allSchemas = { } ; const schemaURL = typeof schema === "string" ? resolveSchema ( schema ) : ( schema as URL ) ; await load ( schemaURL , { ... ctx , schemas : allSchemas , rootURL : isInlineSchema ? new URL ( VIRTUAL_JSON_URL ) : schemaURL , httpHeaders : options . httpHeaders , httpMethod : options . httpMethod , } ) ; for ( const k of Object . keys ( allSchemas ) ) { const rootSchemaID = isInlineSchema ? VIRTUAL_JSON_URL : schemaURL . href ; if ( k === rootSchemaID ) { rootSchema = allSchemas [ k ] ; } else { external [ k ] = allSchemas [ k ] ; } } let output = WARNING_MESSAGE ; if ( ! options ?. version && ! ctx . rawSchema ) ctx . version = swaggerVersion ( rootSchema as any ) ; const rootTypes = transformAll ( rootSchema , { ... ctx } ) ; for ( const k of Object . keys ( rootTypes ) ) { if ( typeof rootTypes [ k ] === "string" ) { output += `export interface ${ k } {\n  ${ rootTypes [ k ] }\n}\n\n` ; } } output += `export interface external {\n` ; const externalKeys = Object . keys ( external ) ; externalKeys . sort ( ( a , b ) => a . localeCompare ( b , "en" , { numeric : true } ) ) ; for ( const subschemaURL of externalKeys ) { output += `  "${ subschemaURL }": {\n` ; const subschemaTypes = transformAll ( external [ subschemaURL ] , { ... ctx , namespace : subschemaURL } ) ; for ( const k of Object . keys ( subschemaTypes ) ) { output += `    "${ k }": {\n      ${ subschemaTypes [ k ] }\n    }\n` ; } output += `  }\n` ; } output += `}\n\n` ; let prettierOptions = { parser : "typescript" , plugins : [ parserTypescript ] , } ; if ( options && options . prettierConfig ) { try { const userOptions = await prettier . resolveConfig ( path . resolve ( process . cwd ( ) , options . prettierConfig ) ) ; prettierOptions = { ... ( userOptions || { } ) , ... prettierOptions , plugins : [ ... ( prettierOptions . plugins as prettier . Plugin [ ] ) , ... ( ( userOptions && userOptions . plugins ) || [ ] ) ] , } ; } catch ( err ) { console . error ( `❌ ${ err }` ) ; } } return prettier . format ( output , prettierOptions ) ; } export default openapiTS ;