import ts from "typescript" ; import * as Utils from "../../utils" ; import type { Factory } from "../TsGenerator" ; export interface FormatConversion { /**
     * Search parameters for the Schema to be converted
     */ selector : { see https format : string ; } ; /**
     * Output format
     */ output : { /**
         * The type after conversion. The input string will be output as a typedef.
         */ type : string [ ] ; /**
         * How to handle typedefs when there is more than one.
         *
         * Default oneOf
         */ multiType ? : "allOf" | "oneOf" ; } ; } /**
 * ユーザーが利用できる各種変換オプション
 */ export interface Options { formatConversions ? : FormatConversion [ ] ; } export interface Types { /**
     * operationIdに対するescape
     */ escapeOperationIdText : ( operationId : string ) => string ; /**
     * interface/namespace/typeAliasのnameをescapeする
     * import/exportなどの予約語も裁く
     */ escapeDeclarationText : ( text : string ) => string ; /**
     * Schemas.A.B.Cに対するEscape
     */ escapeReferenceDeclarationText : ( text : string ) => string ; /**
     * 非破壊: PropertySignatureのname用のescape
     */ escapePropertySignatureName : ( text : string ) => string ; /**
     * 破壊: TypeReferenceのname用のescape
     */ escapeTypeReferenceNodeName : ( text : string ) => string ; generateResponseName : ( operationId : string , statusCode : string ) => string ; generateArgumentParamsTypeDeclaration : ( operationId : string ) => string ; generateRequestContentTypeName : ( operationId : string ) => string ; generateResponseContentTypeName : ( operationId : string ) => string ; generateParameterName : ( operationId : string ) => string ; generateRequestBodyName : ( operationId : string ) => string ; generateFunctionName : ( operationId : string ) => string ; convertFormatTypeNode : ( schema : { format ? : string ; } ) => undefined | ts . TypeNode ; } const createFormatSchemaToTypeNode = ( factory , target ) : ts . TypeNode => { const typeNodes = target . output . type . map ( value => { return factory . TypeReferenceNode . create ( { name : value , } ) ; } ) ; if ( target . output . multiType === "allOf" ) { return factory . IntersectionTypeNode . create ( { typeNodes : typeNodes , } ) ; } return factory . UnionTypeNode . create ( { typeNodes : typeNodes , } ) ; } ; /**
 * ユーザーが利用できる各種変換オプション
 */ export const create = ( factory , options ? ) : Types => { const convertReservedWord = ( word ) : string => { if ( [ "import" , "export" ] . includes ( word ) ) { return word + "_" ; } return word ; } ; const convertString = ( text ) : string => { if ( Utils . isAvailableVariableName ( text ) ) { return text ; } return text . replace ( /-/g , "$" ) . replace ( /\//g , "$" ) ; } ; return { escapeOperationIdText : ( operationId ) : string => { return convertString ( operationId ) ; } , escapeDeclarationText : ( text ) => { return convertReservedWord ( convertString ( text ) . replace ( /\./g , "$" ) ) ; } , escapeReferenceDeclarationText : ( text ) => { return convertReservedWord ( convertString ( text ) ) ; } , escapePropertySignatureName : ( text ) => { return Utils . escapeText ( text ) ; } , escapeTypeReferenceNodeName : ( text ) => { return convertString ( text ) ; } , generateResponseName : ( operationId , statusCode ) : string => { return Utils . responseName ( convertString ( operationId ) , statusCode ) ; } , generateArgumentParamsTypeDeclaration : ( operationId ) => { return Utils . argumentParamsTypeDeclaration ( convertString ( operationId ) ) ; } , generateRequestContentTypeName : ( operationId ) => { return Utils . requestContentType ( convertString ( operationId ) ) ; } , generateResponseContentTypeName : ( operationId ) => { return Utils . responseContentType ( convertString ( operationId ) ) ; } , generateParameterName : ( operationId ) => { return Utils . parameterName ( convertString ( operationId ) ) ; } , generateRequestBodyName : ( operationId ) => { return Utils . requestBodyName ( convertString ( operationId ) ) ; } , generateFunctionName : ( operationId ) => { return convertString ( operationId ) ; } , convertFormatTypeNode : schema => { const formatConversions = options ?. formatConversions ; if ( ! formatConversions || formatConversions . length === 0 ) { return ; } if ( typeof schema === "boolean" ) { return ; } if ( ! schema . format ) { return ; } const target = formatConversions . find ( formatConvertion => formatConvertion . selector . format === schema . format ) ; if ( ! target ) { return ; } return createFormatSchemaToTypeNode ( factory , target ) ; } , } ; } ;