diff --git a/src/code-templates/_shared/MethodBody/PathParameter.ts b/src/code-templates/_shared/MethodBody/PathParameter.ts index 74841828..5a9a5c81 100644 --- a/src/code-templates/_shared/MethodBody/PathParameter.ts +++ b/src/code-templates/_shared/MethodBody/PathParameter.ts @@ -2,8 +2,8 @@ import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; -import * as Utils from "../../class-api-client/utils"; import { escapeText2 as escapeText } from "../../../utils"; +import * as Utils from "../../class-api-client/utils"; import type { MethodType } from "./types"; export const isPathParameter = (params: any): params is CodeGenerator.PickedParameter => { @@ -18,25 +18,23 @@ const generateUrlVariableStatement = ( urlTemplate: Utils.Params$TemplateExpression, methodType: MethodType, ): ts.VariableStatement => { - const left: Record = { - class: factory.PropertyAccessExpression.create({ - name: "baseUrl", - expression: "this", - }), - function: factory.Identifier.create({ - name: "_baseUrl", + const expression = { + class: factory.BinaryExpression.create({ + left: factory.PropertyAccessExpression.create({ + name: "baseUrl", + expression: "this", + }), + operator: "+", + right: Utils.generateTemplateExpression(factory, urlTemplate), }), + function: Utils.generateTemplateExpression(factory, urlTemplate), }; return factory.VariableStatement.create({ declarationList: factory.VariableDeclarationList.create({ declarations: [ factory.VariableDeclaration.create({ name: "url", - initializer: factory.BinaryExpression.create({ - left: left[methodType], - operator: "+", - right: Utils.generateTemplateExpression(factory, urlTemplate), - }), + initializer: expression[methodType], }), ], flag: "const", diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts index cdc53a7d..84ffcc65 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts @@ -2,8 +2,8 @@ import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; -import type { Option } from "../../_shared/types"; import * as MethodBody from "../../_shared/MethodBody"; +import type { Option } from "../../_shared/types"; export { MethodBody }; @@ -121,14 +121,35 @@ const methodTypeParameters = (factory: TsGenerator.Factory.Type, { convertedPara */ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.ArrowFunction => { const { convertedParams } = params; - const typeParameters: ts.TypeParameterDeclaration[] = methodTypeParameters(factory, params); + const typeParameters: ts.TypeParameterDeclaration[] = []; + typeParameters.push( + factory.TypeParameterDeclaration.create({ + name: "RequestOption", + }), + ); + typeParameters.push(...methodTypeParameters(factory, params)); + const methodArguments: ts.ParameterDeclaration[] = []; + methodArguments.push( + factory.ParameterDeclaration.create({ + name: "apiClient", + modifiers: undefined, + type: factory.TypeReferenceNode.create({ + name: "ApiClient", + typeArguments: [ + factory.TypeReferenceNode.create({ + name: "RequestOption", + }), + ], + }), + }), + ); + const hasParamsArguments = convertedParams.hasParameter || convertedParams.hasRequestBody || convertedParams.has2OrMoreSuccessResponseContentTypes || convertedParams.has2OrMoreRequestContentTypes; - if (hasParamsArguments) { methodArguments.push(generateParams(factory, params)); } diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts deleted file mode 100644 index d202f8a5..00000000 --- a/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts +++ /dev/null @@ -1,20 +0,0 @@ -import ts from "typescript"; -import type { TsGenerator } from "../../../api"; - -export const create = (factory: TsGenerator.Factory.Type): ts.TypeAliasDeclaration[] => { - return [ - factory.TypeAliasDeclaration.create({ - name: "ClientFunction", - type: factory.TypeReferenceNode.create({ - name: `typeof createClient`, - }) - }), - factory.TypeAliasDeclaration.create({ - export: true, - name: "Client", - type: factory.TypeReferenceNode.create({ - name: `ReturnType>`, - }) - }) - ] -}; diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/index.ts b/src/code-templates/functional-api-client/FunctionalApiClient/index.ts index fa883000..1876f2e4 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/index.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/index.ts @@ -1,4 +1,3 @@ -import { EOL } from "os"; import ts from "typescript"; import type { TsGenerator } from "../../../api"; @@ -6,84 +5,19 @@ import type { CodeGenerator } from "../../../types"; import type { Option } from "../../_shared/types"; import * as ArrowFunction from "./ArrowFunction"; -export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement => { - const properties = list.map(params => { - return factory.PropertyAssignment.create({ - name: params.convertedParams.functionName, - initializer: ArrowFunction.create(factory, params, option), - comment: option.additionalMethodComment - ? [params.operationParams.comment, `operationId: ${params.operationId}`, `Request URI: ${params.operationParams.requestUri}`] - .filter(t => !!t) - .join(EOL) - : params.operationParams.comment, - }); - }); - - const returnValue = factory.ReturnStatement.create({ - expression: factory.ObjectLiteralExpression.create({ - properties, - multiLine: true, - }), - }); - - const arrowFunction = factory.ArrowFunction.create({ - typeParameters: [ - factory.TypeParameterDeclaration.create({ - name: "RequestOption", - }), - ], - parameters: [ - factory.ParameterDeclaration.create({ - name: "apiClient", - type: factory.TypeReferenceNode.create({ - name: "ApiClient", - typeArguments: [ - factory.TypeReferenceNode.create({ - name: "RequestOption", - }), - ], - }), - }), - factory.ParameterDeclaration.create({ - name: "baseUrl", - type: ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), - }), - ], - body: factory.Block.create({ - statements: [ - factory.VariableStatement.create({ - declarationList: factory.VariableDeclarationList.create({ - flag: "const", - declarations: [ - factory.VariableDeclaration.create({ - name: "_baseUrl", - initializer: factory.CallExpression.create({ - expression: factory.PropertyAccessExpression.create({ - expression: "baseUrl", - name: "replace", - }), - argumentsArray: [factory.RegularExpressionLiteral.create({ text: "/\\/$/" }), factory.StringLiteral.create({ text: "" })], - }), - }), - ], +export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement[] => { + return list.map(params => { + return factory.VariableStatement.create({ + modifiers: [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], + declarationList: factory.VariableDeclarationList.create({ + declarations: [ + factory.VariableDeclaration.create({ + name: params.convertedParams.functionName, + initializer: ArrowFunction.create(factory, params, option), }), - }), - returnValue, - ], - multiLine: true, - }), - }); - - return factory.VariableStatement.create({ - modifiers: [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], - declarationList: factory.VariableDeclarationList.create({ - declarations: [ - factory.VariableDeclaration.create({ - name: "createClient", - initializer: arrowFunction, - }), - ], - flag: "const", - }), + ], + flag: "const", + }), + }); }); }; diff --git a/src/code-templates/functional-api-client/index.ts b/src/code-templates/functional-api-client/index.ts index 0164141e..5e541574 100644 --- a/src/code-templates/functional-api-client/index.ts +++ b/src/code-templates/functional-api-client/index.ts @@ -3,11 +3,9 @@ import ts from "typescript"; import { TsGenerator } from "../../api"; import type { CodeGenerator } from "../../types"; import * as ApiClientArgument from "../_shared/ApiClientArgument"; -import * as FunctionalApiClient from "./FunctionalApiClient"; import * as ApiClientInterface from "../_shared/ApiClientInterface"; -import * as ClientTypeDefinition from "./FunctionalApiClient/ClientTypeDefinition"; - import type { Option } from "../_shared/types"; +import * as FunctionalApiClient from "./FunctionalApiClient"; export { Option }; @@ -35,11 +33,9 @@ export const generator: CodeGenerator.GenerateFunction