import { EOL } from "os" ; import * as Api from "./api" ; import type * as Types from "./types" ; export interface Option { allowOperationIds ? : string [ ] ; convertOption : Api . OpenApiTools . ConvertContext . Options ; } export class CodeGenerator { private rootSchema : Types . OpenApi . Document ; private resolvedReferenceDocument : Types . OpenApi . Document ; private parser : Api . OpenApiTools . Parser ; constructor ( private readonly entryPointOrDocument , private option ? ) { if ( typeof entryPointOrDocument === "string" ) { this . rootSchema = Api . FileSystem . loadJsonOrYaml ( entryPointOrDocument ) ; this . resolvedReferenceDocument = Api . ResolveReference . resolve ( entryPointOrDocument , entryPointOrDocument , JSON . parse ( JSON . stringify ( this . rootSchema ) ) ) ; } else { this . rootSchema = entryPointOrDocument ; this . resolvedReferenceDocument = Api . ResolveReference . resolve ( "." , "." , JSON . parse ( JSON . stringify ( this . rootSchema ) ) ) ; } this . parser = this . createParser ( ) ; } private createParser ( ) { const entryPoint = typeof this . entryPointOrDocument === "string" ? this . entryPointOrDocument : "." ; return new Api . OpenApiTools . Parser ( entryPoint , this . rootSchema , this . resolvedReferenceDocument , this . option ?. convertOption ) ; } /**
     * Validate the OpenAPI Schema
     */ public validateOpenApiSchema ( option ? ) { if ( ! option ) { Api . Validator . validate ( this . resolvedReferenceDocument ) ; } else { Api . Validator . validate ( this . resolvedReferenceDocument , option . logger ) ; } } param generatorTemplate returns public generateTypeDefinition ( generatorTemplates ? , allowOperationIds ? ) { const create = ( ) => { const statements = this . parser . getOpenApiTypeDefinitionStatements ( ) ; generatorTemplates ?. forEach ( generatorTemplate => { const payload = this . parser . getCodeGeneratorParamsArray ( allowOperationIds ) ; const extraStatements = Api . TsGenerator . Utils . convertIntermediateCodes ( generatorTemplate . generator ( payload , generatorTemplate . option ) ) ; statements . push ( ... extraStatements ) ; } ) ; return statements ; } ; return [ Api . OpenApiTools . Comment . generateLeading ( this . resolvedReferenceDocument ) , Api . TsGenerator . generate ( create ) ] . join ( EOL + EOL + EOL ) ; } param generatorTemplate returns public generateCode ( generatorTemplates , allowOperationIds ? ) { const payload = this . parser . getCodeGeneratorParamsArray ( allowOperationIds ) ; const create = ( ) => { return generatorTemplates . map ( generatorTemplate => { return Api . TsGenerator . Utils . convertIntermediateCodes ( generatorTemplate ?. generator ( payload , generatorTemplate . option ) ) ; } ) . flat ( ) ; } ; return [ Api . OpenApiTools . Comment . generateLeading ( this . resolvedReferenceDocument ) , Api . TsGenerator . generate ( create ) ] . join ( EOL + EOL + EOL ) ; } /**
     * Provides parameters extracted from OpenApi Schema
     */ public getCodeGeneratorParamsArray ( allowOperationIds ? ) { return this . parser . getCodeGeneratorParamsArray ( allowOperationIds ) ; } /**
     * Provides types for parameters for Templates.ApiClient.
     *
     * This API will be moved to Templates in the future.
     */ public getAdditionalTypeDefinitionCustomCodeGenerator ( ) { return { generator : ( ) => this . parser . getAdditionalTypeStatements ( ) , } ; } }