Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(functional): make the functional api client a pure function. #103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/code-templates/_shared/MethodBody/PathParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -18,25 +18,23 @@ const generateUrlVariableStatement = (
urlTemplate: Utils.Params$TemplateExpression,
methodType: MethodType,
): ts.VariableStatement => {
const left: Record<MethodType, ts.Expression> = {
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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't understand the reason but this code isn't worked.

The class part works fine but function part will be wrong template string.

};
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each functions are now accept the apiClient argument and RequestOption type argument.


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",
}),
],
}),
}),
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the apiClient argument was added here.


const hasParamsArguments =
convertedParams.hasParameter ||
convertedParams.hasRequestBody ||
convertedParams.has2OrMoreSuccessResponseContentTypes ||
convertedParams.has2OrMoreRequestContentTypes;

if (hasParamsArguments) {
methodArguments.push(generateParams(factory, params));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,89 +1,23 @@
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 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),
}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the functional api client will be list of ArrowFunctions now.

}),
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",
}),
});
});
};
12 changes: 4 additions & 8 deletions src/code-templates/functional-api-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -35,11 +33,9 @@ export const generator: CodeGenerator.GenerateFunction<Option> = (
statements.push(statement);
});

const apiClientStatement = FunctionalApiClient.create(factory, codeGeneratorParamsList, option || {});
statements.push(apiClientStatement);
ClientTypeDefinition.create(factory).forEach(statement => {
FunctionalApiClient.create(factory, codeGeneratorParamsList, option || {}).forEach(statement => {
statements.push(statement);
})
});

return statements;
};
2 changes: 1 addition & 1 deletion src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const Name = "@himenon/openapi-typescript-code-generator";
export const Version = "0.22.2";
export const Version = "0.22.3";