Skip to content

Commit 24f4a55

Browse files
authored
fix: comment for functional api (#100)
1 parent 8572d59 commit 24f4a55

18 files changed

+16157
-15086
lines changed

src/code-templates/functional-api-client/FunctionalApiClient/Method.ts renamed to src/code-templates/functional-api-client/FunctionalApiClient/ArrowFunction.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ const methodTypeParameters = (factory: TsGenerator.Factory.Type, { convertedPara
117117
};
118118

119119
/**
120-
* const {functionName} = async (params: {argumentParamsTypeDeclaration}<{RequestContentType}>): Promise<{requestBodyName}[ResponseContentType]> => {
121-
*
122-
* }
120+
* async (params: {argumentParamsTypeDeclaration}<{RequestContentType}>): Promise<{requestBodyName}[ResponseContentType]> => {}
123121
*/
124-
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.VariableStatement => {
122+
export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.Params, option: Option): ts.ArrowFunction => {
125123
const { convertedParams } = params;
126124
const typeParameters: ts.TypeParameterDeclaration[] = methodTypeParameters(factory, params);
127125
const methodArguments: ts.ParameterDeclaration[] = [];
@@ -153,7 +151,7 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
153151
}),
154152
);
155153

156-
const arrowFunction = factory.ArrowFunction.create({
154+
return factory.ArrowFunction.create({
157155
typeParameters: typeParameters,
158156
parameters: methodArguments,
159157
type: returnType,
@@ -162,18 +160,4 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
162160
multiLine: true,
163161
}),
164162
});
165-
166-
const variableDeclarationList = factory.VariableDeclarationList.create({
167-
declarations: [
168-
factory.VariableDeclaration.create({
169-
name: convertedParams.functionName,
170-
initializer: arrowFunction,
171-
}),
172-
],
173-
flag: "const",
174-
});
175-
176-
return factory.VariableStatement.create({
177-
declarationList: variableDeclarationList,
178-
});
179163
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { TsGenerator } from "../../../api";
2+
3+
export const create = (factory: TsGenerator.Factory.Type) => {
4+
return factory.TypeAliasDeclaration.create({
5+
export: true,
6+
name: "Client",
7+
type: factory.TypeReferenceNode.create({
8+
name: `ReturnType<typeof createClient>`,
9+
})
10+
})
11+
};

src/code-templates/functional-api-client/FunctionalApiClient/ReturnStatement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
1313
return factory.ReturnStatement.create({
1414
expression: factory.ObjectLiteralExpression.create({
1515
properties,
16+
multiLine: true,
1617
}),
1718
});
1819
};

src/code-templates/functional-api-client/FunctionalApiClient/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
import { EOL } from "os";
12
import ts from "typescript";
23

34
import type { TsGenerator } from "../../../api";
45
import type { CodeGenerator } from "../../../types";
56
import type { Option } from "../../_shared/types";
6-
import * as Method from "./Method";
7-
import * as ReturnStatement from "./ReturnStatement";
8-
export { Method };
7+
import * as ArrowFunction from "./ArrowFunction";
98

109
export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Params[], option: Option): ts.VariableStatement => {
11-
const variableStatements = list.map(params => {
12-
return Method.create(factory, params, option);
10+
const properties = list.map(params => {
11+
return factory.PropertyAssignment.create({
12+
name: params.convertedParams.functionName,
13+
initializer: ArrowFunction.create(factory, params, option),
14+
comment: option.additionalMethodComment
15+
? [params.operationParams.comment, `operationId: ${params.operationId}`, `Request URI: ${params.operationParams.requestUri}`]
16+
.filter(t => !!t)
17+
.join(EOL)
18+
: params.operationParams.comment,
19+
});
20+
});
21+
22+
const returnValue = factory.ReturnStatement.create({
23+
expression: factory.ObjectLiteralExpression.create({
24+
properties,
25+
multiLine: true,
26+
}),
1327
});
1428

1529
const arrowFunction = factory.ArrowFunction.create({
@@ -36,7 +50,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
3650
}),
3751
],
3852
body: factory.Block.create({
39-
statements: [...variableStatements, ReturnStatement.create(factory, list)],
53+
statements: [returnValue],
4054
multiLine: true,
4155
}),
4256
});

src/code-templates/functional-api-client/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { CodeGenerator } from "../../types";
55
import * as ApiClientArgument from "../_shared/ApiClientArgument";
66
import * as FunctionalApiClient from "./FunctionalApiClient";
77
import * as ApiClientInterface from "../_shared/ApiClientInterface";
8+
import * as ClientTypeDefinition from "./FunctionalApiClient/ClientTypeDefinition";
89

910
import type { Option } from "../_shared/types";
1011

@@ -36,5 +37,6 @@ export const generator: CodeGenerator.GenerateFunction<Option> = (
3637

3738
const apiClientStatement = FunctionalApiClient.create(factory, codeGeneratorParamsList, option || {});
3839
statements.push(apiClientStatement);
40+
statements.push(ClientTypeDefinition.create(factory));
3941
return statements;
4042
};

src/internal/TsGenerator/factory/ArrowFunction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const create =
2323
params.equalsGreaterThanToken,
2424
params.body,
2525
);
26+
2627
return node;
2728
};
2829

src/internal/TsGenerator/factory/PropertyAssignment.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import ts from "typescript";
2+
import { generateComment } from "./utils";
23

34
export interface Params {
45
name: string;
56
initializer: ts.Expression;
7+
comment?: string;
8+
deprecated?: boolean;
69
}
710

811
export interface Factory {
@@ -13,6 +16,10 @@ export const create =
1316
({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] =>
1417
(params: Params): ts.PropertyAssignment => {
1518
const node = factory.createPropertyAssignment(params.name, params.initializer);
19+
if (params.comment) {
20+
const comment = generateComment(params.comment, params.deprecated);
21+
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, comment.value, comment.hasTrailingNewLine);
22+
}
1623
return node;
1724
};
1825

src/internal/TsGenerator/factory/VariableDeclaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ts from "typescript";
2+
import { generateComment } from "./utils";
23

34
export interface Params {
45
name: string | ts.BindingName;

src/internal/TsGenerator/factory/VariableDeclarationList.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ts from "typescript";
2+
import { generateComment } from "./utils";
23

34
const flags = {
45
const: ts.NodeFlags.Const,
@@ -7,6 +8,7 @@ const flags = {
78
export interface Params {
89
declarations: readonly ts.VariableDeclaration[];
910
flag: keyof typeof flags;
11+
1012
}
1113

1214
export interface Factory {

src/meta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export const Name = "@himenon/openapi-typescript-code-generator";
2-
export const Version = "0.21.0";
2+
export const Version = "0.22.0";

test/__tests__/functional/__snapshots__/argo-rollout-test.ts.snap

Lines changed: 92 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,97 +3830,99 @@ export interface ApiClient<RequestOption> {
38303830
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
38313831
}
38323832
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
3833-
const RolloutService_GetNamespace = (option?: RequestOption): Promise<Response$RolloutService_GetNamespace$Status$200["application/json"]> => {
3834-
const url = baseUrl + \`/api/v1/namespace\`;
3835-
const headers = {
3836-
Accept: "application/json"
3837-
};
3838-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3839-
};
3840-
const RolloutService_ListRolloutInfos = (params: Params$RolloutService_ListRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_ListRolloutInfos$Status$200["application/json"]> => {
3841-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info\`;
3842-
const headers = {
3843-
Accept: "application/json"
3844-
};
3845-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3846-
};
3847-
const RolloutService_WatchRolloutInfos = (params: Params$RolloutService_WatchRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfos$Status$200["application/json"]> => {
3848-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info/watch\`;
3849-
const headers = {
3850-
Accept: "application/json"
3851-
};
3852-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3853-
};
3854-
const RolloutService_AbortRollout = (params: Params$RolloutService_AbortRollout, option?: RequestOption): Promise<Response$RolloutService_AbortRollout$Status$200["application/json"]> => {
3855-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/abort\`;
3856-
const headers = {
3857-
"Content-Type": "application/json",
3858-
Accept: "application/json"
3859-
};
3860-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3861-
};
3862-
const RolloutService_GetRolloutInfo = (params: Params$RolloutService_GetRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_GetRolloutInfo$Status$200["application/json"]> => {
3863-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info\`;
3864-
const headers = {
3865-
Accept: "application/json"
3866-
};
3867-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3868-
};
3869-
const RolloutService_WatchRolloutInfo = (params: Params$RolloutService_WatchRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfo$Status$200["application/json"]> => {
3870-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info/watch\`;
3871-
const headers = {
3872-
Accept: "application/json"
3873-
};
3874-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3875-
};
3876-
const RolloutService_PromoteRollout = (params: Params$RolloutService_PromoteRollout, option?: RequestOption): Promise<Response$RolloutService_PromoteRollout$Status$200["application/json"]> => {
3877-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/promote\`;
3878-
const headers = {
3879-
"Content-Type": "application/json",
3880-
Accept: "application/json"
3881-
};
3882-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3883-
};
3884-
const RolloutService_RestartRollout = (params: Params$RolloutService_RestartRollout, option?: RequestOption): Promise<Response$RolloutService_RestartRollout$Status$200["application/json"]> => {
3885-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/restart\`;
3886-
const headers = {
3887-
"Content-Type": "application/json",
3888-
Accept: "application/json"
3889-
};
3890-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3891-
};
3892-
const RolloutService_RetryRollout = (params: Params$RolloutService_RetryRollout, option?: RequestOption): Promise<Response$RolloutService_RetryRollout$Status$200["application/json"]> => {
3893-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/retry\`;
3894-
const headers = {
3895-
"Content-Type": "application/json",
3896-
Accept: "application/json"
3897-
};
3898-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3899-
};
3900-
const RolloutService_SetRolloutImage = (params: Params$RolloutService_SetRolloutImage, option?: RequestOption): Promise<Response$RolloutService_SetRolloutImage$Status$200["application/json"]> => {
3901-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/set/\${params.parameter.container}/\${params.parameter.image}/\${params.parameter.tag}\`;
3902-
const headers = {
3903-
"Content-Type": "application/json",
3904-
Accept: "application/json"
3905-
};
3906-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3907-
};
3908-
const RolloutService_UndoRollout = (params: Params$RolloutService_UndoRollout, option?: RequestOption): Promise<Response$RolloutService_UndoRollout$Status$200["application/json"]> => {
3909-
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/undo/\${params.parameter.revision}\`;
3910-
const headers = {
3911-
"Content-Type": "application/json",
3912-
Accept: "application/json"
3913-
};
3914-
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3915-
};
3916-
const RolloutService_Version = (option?: RequestOption): Promise<Response$RolloutService_Version$Status$200["application/json"]> => {
3917-
const url = baseUrl + \`/api/v1/version\`;
3918-
const headers = {
3919-
Accept: "application/json"
3920-
};
3921-
return apiClient.request("GET", url, headers, undefined, undefined, option);
3833+
return {
3834+
RolloutService_GetNamespace: (option?: RequestOption): Promise<Response$RolloutService_GetNamespace$Status$200["application/json"]> => {
3835+
const url = baseUrl + \`/api/v1/namespace\`;
3836+
const headers = {
3837+
Accept: "application/json"
3838+
};
3839+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3840+
},
3841+
RolloutService_ListRolloutInfos: (params: Params$RolloutService_ListRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_ListRolloutInfos$Status$200["application/json"]> => {
3842+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info\`;
3843+
const headers = {
3844+
Accept: "application/json"
3845+
};
3846+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3847+
},
3848+
RolloutService_WatchRolloutInfos: (params: Params$RolloutService_WatchRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfos$Status$200["application/json"]> => {
3849+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info/watch\`;
3850+
const headers = {
3851+
Accept: "application/json"
3852+
};
3853+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3854+
},
3855+
RolloutService_AbortRollout: (params: Params$RolloutService_AbortRollout, option?: RequestOption): Promise<Response$RolloutService_AbortRollout$Status$200["application/json"]> => {
3856+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/abort\`;
3857+
const headers = {
3858+
"Content-Type": "application/json",
3859+
Accept: "application/json"
3860+
};
3861+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3862+
},
3863+
RolloutService_GetRolloutInfo: (params: Params$RolloutService_GetRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_GetRolloutInfo$Status$200["application/json"]> => {
3864+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info\`;
3865+
const headers = {
3866+
Accept: "application/json"
3867+
};
3868+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3869+
},
3870+
RolloutService_WatchRolloutInfo: (params: Params$RolloutService_WatchRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfo$Status$200["application/json"]> => {
3871+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info/watch\`;
3872+
const headers = {
3873+
Accept: "application/json"
3874+
};
3875+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3876+
},
3877+
RolloutService_PromoteRollout: (params: Params$RolloutService_PromoteRollout, option?: RequestOption): Promise<Response$RolloutService_PromoteRollout$Status$200["application/json"]> => {
3878+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/promote\`;
3879+
const headers = {
3880+
"Content-Type": "application/json",
3881+
Accept: "application/json"
3882+
};
3883+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3884+
},
3885+
RolloutService_RestartRollout: (params: Params$RolloutService_RestartRollout, option?: RequestOption): Promise<Response$RolloutService_RestartRollout$Status$200["application/json"]> => {
3886+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/restart\`;
3887+
const headers = {
3888+
"Content-Type": "application/json",
3889+
Accept: "application/json"
3890+
};
3891+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3892+
},
3893+
RolloutService_RetryRollout: (params: Params$RolloutService_RetryRollout, option?: RequestOption): Promise<Response$RolloutService_RetryRollout$Status$200["application/json"]> => {
3894+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/retry\`;
3895+
const headers = {
3896+
"Content-Type": "application/json",
3897+
Accept: "application/json"
3898+
};
3899+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3900+
},
3901+
RolloutService_SetRolloutImage: (params: Params$RolloutService_SetRolloutImage, option?: RequestOption): Promise<Response$RolloutService_SetRolloutImage$Status$200["application/json"]> => {
3902+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/set/\${params.parameter.container}/\${params.parameter.image}/\${params.parameter.tag}\`;
3903+
const headers = {
3904+
"Content-Type": "application/json",
3905+
Accept: "application/json"
3906+
};
3907+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3908+
},
3909+
RolloutService_UndoRollout: (params: Params$RolloutService_UndoRollout, option?: RequestOption): Promise<Response$RolloutService_UndoRollout$Status$200["application/json"]> => {
3910+
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/undo/\${params.parameter.revision}\`;
3911+
const headers = {
3912+
"Content-Type": "application/json",
3913+
Accept: "application/json"
3914+
};
3915+
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
3916+
},
3917+
RolloutService_Version: (option?: RequestOption): Promise<Response$RolloutService_Version$Status$200["application/json"]> => {
3918+
const url = baseUrl + \`/api/v1/version\`;
3919+
const headers = {
3920+
Accept: "application/json"
3921+
};
3922+
return apiClient.request("GET", url, headers, undefined, undefined, option);
3923+
}
39223924
};
3923-
return { RolloutService_GetNamespace, RolloutService_ListRolloutInfos, RolloutService_WatchRolloutInfos, RolloutService_AbortRollout, RolloutService_GetRolloutInfo, RolloutService_WatchRolloutInfo, RolloutService_PromoteRollout, RolloutService_RestartRollout, RolloutService_RetryRollout, RolloutService_SetRolloutImage, RolloutService_UndoRollout, RolloutService_Version };
39243925
};
3926+
export type Client = ReturnType<typeof createClient>;
39253927
"
39263928
`;

test/__tests__/functional/__snapshots__/format.domain.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ export interface ApiClient<RequestOption> {
4949
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
5050
return {};
5151
};
52+
export type Client = ReturnType<typeof createClient>;
5253
"
5354
`;

0 commit comments

Comments
 (0)