diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/Method.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts similarity index 87% rename from src/code-templates/functional-api-client/FunctionalApiClient/Method.ts rename to src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts index ace52ef1..cdc53a7d 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/Method.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts @@ -117,11 +117,9 @@ const methodTypeParameters = (factory: TsGenerator.Factory.Type, { convertedPara }; /** - * const {functionName} = async (params: {argumentParamsTypeDeclaration}<{RequestContentType}>): Promise<{requestBodyName}[ResponseContentType]> => { - * - * } + * async (params: {argumentParamsTypeDeclaration}<{RequestContentType}>): Promise<{requestBodyName}[ResponseContentType]> => {} */ -export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.VariableStatement => { +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 methodArguments: ts.ParameterDeclaration[] = []; @@ -153,7 +151,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator. }), ); - const arrowFunction = factory.ArrowFunction.create({ + return factory.ArrowFunction.create({ typeParameters: typeParameters, parameters: methodArguments, type: returnType, @@ -162,18 +160,4 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator. multiLine: true, }), }); - - const variableDeclarationList = factory.VariableDeclarationList.create({ - declarations: [ - factory.VariableDeclaration.create({ - name: convertedParams.functionName, - initializer: arrowFunction, - }), - ], - flag: "const", - }); - - return factory.VariableStatement.create({ - declarationList: variableDeclarationList, - }); }; diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts new file mode 100644 index 00000000..b80ff55c --- /dev/null +++ b/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts @@ -0,0 +1,11 @@ +import type { TsGenerator } from "../../../api"; + +export const create = (factory: TsGenerator.Factory.Type) => { + return factory.TypeAliasDeclaration.create({ + export: true, + name: "Client", + type: factory.TypeReferenceNode.create({ + name: `ReturnType`, + }) + }) +}; diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/ReturnStatement.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ReturnStatement.ts index 49663c83..e5a77398 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/ReturnStatement.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/ReturnStatement.ts @@ -13,6 +13,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa return factory.ReturnStatement.create({ expression: factory.ObjectLiteralExpression.create({ properties, + multiLine: true, }), }); }; diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/index.ts b/src/code-templates/functional-api-client/FunctionalApiClient/index.ts index 8d622aa5..5dfe55dc 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/index.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/index.ts @@ -1,15 +1,29 @@ +import { EOL } from "os"; import ts from "typescript"; import type { TsGenerator } from "../../../api"; import type { CodeGenerator } from "../../../types"; import type { Option } from "../../_shared/types"; -import * as Method from "./Method"; -import * as ReturnStatement from "./ReturnStatement"; -export { Method }; +import * as ArrowFunction from "./ArrowFunction"; export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement => { - const variableStatements = list.map(params => { - return Method.create(factory, params, option); + 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({ @@ -36,7 +50,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa }), ], body: factory.Block.create({ - statements: [...variableStatements, ReturnStatement.create(factory, list)], + statements: [returnValue], multiLine: true, }), }); diff --git a/src/code-templates/functional-api-client/index.ts b/src/code-templates/functional-api-client/index.ts index 9a69887d..a45ad3bf 100644 --- a/src/code-templates/functional-api-client/index.ts +++ b/src/code-templates/functional-api-client/index.ts @@ -5,6 +5,7 @@ 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"; @@ -36,5 +37,6 @@ export const generator: CodeGenerator.GenerateFunction