import { EOL } from "os" ; import ts from "typescript" ; import type { TsGenerator } from "../../../api" ; import type { CodeGenerator } from "../../../types" ; import type { Option } from "../types" ; import * as MethodBody from "./MethodBody" ; export { MethodBody } ; const generateParams = ( factory , { convertedParams } ) => { const typeArguments = [ ] ; if ( convertedParams . has2OrMoreRequestContentTypes ) { typeArguments . push ( factory . TypeReferenceNode . create ( { name : "RequestContentType" , } ) ) ; } if ( convertedParams . has2OrMoreSuccessResponseContentTypes ) { typeArguments . push ( factory . TypeReferenceNode . create ( { name : "ResponseContentType" , } ) ) ; } return factory . ParameterDeclaration . create ( { name : "params" , modifiers : undefined , type : factory . TypeReferenceNode . create ( { name : convertedParams . argumentParamsTypeDeclaration , typeArguments , } ) , } ) ; } ; const generateResponseReturnType = ( factory , successResponseNameList , successResponseContentTypeList , option ) => { let objectType = factory . TypeNode . create ( { type : "void" , } ) ; if ( successResponseNameList . length === 1 ) { objectType = factory . TypeReferenceNode . create ( { name : successResponseNameList [ 0 ] , } ) ; } else if ( successResponseNameList . length > 1 ) { objectType = factory . UnionTypeNode . create ( { typeNodes : successResponseNameList . map ( item => factory . TypeReferenceNode . create ( { name : item } ) ) , } ) ; } if ( successResponseNameList . length === 0 ) { if ( option . sync ) { return objectType ; } return factory . TypeReferenceNode . create ( { name : "Promise" , typeArguments : [ objectType ] , } ) ; } const isOnlyOneResponseContentType = successResponseContentTypeList . length === 1 ; let indexType = factory . TypeReferenceNode . create ( { name : "ResponseContentType" , } ) ; if ( isOnlyOneResponseContentType ) { indexType = factory . TypeReferenceNode . create ( { name : `"${ successResponseContentTypeList [ 0 ] }"` , } ) ; } if ( option . sync ) { return factory . IndexedAccessTypeNode . create ( { objectType , indexType , } ) ; } return factory . TypeReferenceNode . create ( { name : "Promise" , typeArguments : [ factory . IndexedAccessTypeNode . create ( { objectType , indexType , } ) , ] , } ) ; } ; const methodTypeParameters = ( factory , { convertedParams } ) : ts . TypeParameterDeclaration [ ] => { const typeParameters = [ ] ; if ( convertedParams . has2OrMoreRequestContentTypes ) { typeParameters . push ( factory . TypeParameterDeclaration . create ( { name : "RequestContentType" , constraint : factory . TypeReferenceNode . create ( { name : convertedParams . requestContentTypeName , } ) , } ) ) ; } if ( convertedParams . has2OrMoreSuccessResponseContentTypes ) { typeParameters . push ( factory . TypeParameterDeclaration . create ( { name : "ResponseContentType" , constraint : factory . TypeReferenceNode . create ( { name : convertedParams . responseContentTypeName , } ) , } ) ) ; } return typeParameters ; } ; /**
 *
 * public async {functionName}(params: {argumentParamsTypeDeclaration}<{RequestContentType}>): Promise<{requestBodyName}[ResponseContentType]> {
 *
 * }
 */ export const create = ( factory , params , option ) : ts . MethodDeclaration => { const { convertedParams } = params ; const typeParameters = methodTypeParameters ( factory , params ) ; const methodArguments = [ ] ; const hasParamsArguments = convertedParams . hasParameter || convertedParams . hasRequestBody || convertedParams . has2OrMoreSuccessResponseContentTypes || convertedParams . has2OrMoreRequestContentTypes ; if ( hasParamsArguments ) { methodArguments . push ( generateParams ( factory , params ) ) ; } const returnType = generateResponseReturnType ( factory , convertedParams . responseSuccessNames , convertedParams . successResponseContentTypes , option ) ; methodArguments . push ( factory . ParameterDeclaration . create ( { name : "option" , modifiers : undefined , optional : true , type : factory . TypeReferenceNode . create ( { name : "RequestOption" , } ) , } ) ) ; return factory . MethodDeclaration . create ( { name : convertedParams . functionName , async : ! option . sync , parameters : methodArguments , comment : option . additionalMethodComment ? [ params . operationParams . comment , `operationId: ${ params . operationId }` , `Request URI: ${ params . operationParams . requestUri }` ] . filter ( t => ! ! t ) . join ( EOL ) : params . operationParams . comment , deprecated : params . operationParams . deprecated , type : returnType , typeParameters : typeParameters , body : factory . Block . create ( { statements : MethodBody . create ( factory , params ) , multiLine : true , } ) , } ) ; } ;