import * as path from "path" ; import ts from "typescript" ; import type { OpenApi } from "../../../types" ; import { UndefinedComponent } from "../../Exception" ; import { Factory } from "../../TsGenerator" ; import * as ConverterContext from "../ConverterContext" ; import * as Guard from "../Guard" ; import * as Name from "../Name" ; import * as ToTypeNode from "../toTypeNode" ; import type * as Walker from "../Walker" ; import * as MediaType from "./MediaType" ; import * as Reference from "./Reference" ; import * as Response from "./Response" ; export const generateNamespace = ( entryPoint , currentPoint , store , factory , responses , context , converterContext ) : void => { const basePath = "components/responses" ; store . addComponent ( "responses" , { kind : "namespace" , name : Name . Components . Responses , } ) ; Object . entries ( responses ) . forEach ( ( [ name , response ] ) => { if ( Guard . isReference ( response ) ) { const reference = Reference . generate < OpenApi . Response > ( entryPoint , currentPoint , response ) ; if ( reference . type === "local" ) { if ( ! store . hasStatement ( reference . path , [ "interface" ] ) ) { throw new UndefinedComponent ( `Reference "${ response . $ref }" did not found in ${ reference . path } by ${ reference . name }` ) ; } } else if ( reference . type === "remote" ) { Response . generateNamespace ( entryPoint , reference . referencePoint , store , factory , path . posix . dirname ( reference . path ) , reference . name , reference . data , context , converterContext ) ; } } else { Response . generateNamespace ( entryPoint , currentPoint , store , factory , basePath , name , response , context , converterContext ) ; } } ) ; } ; export const generateNamespaceWithStatusCode = ( entryPoint , currentPoint , store , factory , parentPath , responses , context , converterContext ) : void => { const basePath = `${ parentPath }/responses` ; store . addStatement ( basePath , { kind : "namespace" , name : Name . ComponentChild . Response , } ) ; Object . entries ( responses ) . forEach ( ( [ statusCode , response ] ) => { const nameWithStatusCode = `Status$${ statusCode }` ; if ( Guard . isReference ( response ) ) { const reference = Reference . generate < OpenApi . Response > ( entryPoint , currentPoint , response ) ; if ( reference . type === "local" ) { context . setReferenceHandler ( currentPoint , reference ) ; Response . generateReferenceNamespace ( entryPoint , currentPoint , store , factory , basePath , nameWithStatusCode , reference , context , converterContext ) ; } else if ( reference . componentName ) { Response . generateNamespace ( entryPoint , reference . referencePoint , store , factory , path . posix . dirname ( reference . path ) , reference . name , reference . data , context , converterContext ) ; Response . generateReferenceNamespace ( entryPoint , currentPoint , store , factory , basePath , nameWithStatusCode , reference , context , converterContext ) ; } } else { Response . generateNamespace ( entryPoint , currentPoint , store , factory , basePath , nameWithStatusCode , response , context , converterContext ) ; } } ) ; } ; export const generateInterfacesWithStatusCode = ( entryPoint , currentPoint , store , factory , operationId , responses , context , converterContext ) : ts . Statement [ ] => { const statements = [ ] ; Object . entries ( responses ) . forEach ( ( [ statusCode , response ] ) => { if ( ! response ) { return ; } if ( Guard . isReference ( response ) ) { const reference = Reference . generate < OpenApi . Response > ( entryPoint , currentPoint , response ) ; if ( reference . type === "local" ) { context . setReferenceHandler ( currentPoint , reference ) ; const { maybeResolvedName , unresolvedPaths } = context . resolveReferencePath ( currentPoint , `${ reference . path }/Content` ) ; if ( unresolvedPaths . length === 0 ) { statements . push ( factory . TypeAliasDeclaration . create ( { export : true , name : converterContext . generateResponseName ( operationId , statusCode ) , type : factory . TypeReferenceNode . create ( { name : maybeResolvedName , } ) , } ) ) ; } } else if ( reference . componentName ) { Response . generateNamespace ( entryPoint , reference . referencePoint , store , factory , path . posix . dirname ( reference . path ) , reference . name , reference . data , context , converterContext ) ; const content = reference . data . content ; if ( content ) { statements . push ( MediaType . generateInterface ( entryPoint , reference . referencePoint , factory , converterContext . generateResponseName ( operationId , statusCode ) , content , context , converterContext ) ) ; } } } else { if ( response . content ) { statements . push ( MediaType . generateInterface ( entryPoint , currentPoint , factory , converterContext . generateResponseName ( operationId , statusCode ) , response . content , context , converterContext ) ) ; } } } ) ; return statements ; } ;