Skip to content

Commit

Permalink
fix: comment for functional api (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Mar 20, 2023
1 parent 8572d59 commit 24f4a55
Show file tree
Hide file tree
Showing 18 changed files with 16,157 additions and 15,086 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
};
Original file line number Diff line number Diff line change
@@ -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<typeof createClient>`,
})
})
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
});
};
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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,
}),
});
Expand Down
2 changes: 2 additions & 0 deletions src/code-templates/functional-api-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

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

const apiClientStatement = FunctionalApiClient.create(factory, codeGeneratorParamsList, option || {});
statements.push(apiClientStatement);
statements.push(ClientTypeDefinition.create(factory));
return statements;
};
1 change: 1 addition & 0 deletions src/internal/TsGenerator/factory/ArrowFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const create =
params.equalsGreaterThanToken,
params.body,
);

return node;
};

Expand Down
7 changes: 7 additions & 0 deletions src/internal/TsGenerator/factory/PropertyAssignment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import ts from "typescript";
import { generateComment } from "./utils";

export interface Params {
name: string;
initializer: ts.Expression;
comment?: string;
deprecated?: boolean;
}

export interface Factory {
Expand All @@ -13,6 +16,10 @@ export const create =
({ factory }: Pick<ts.TransformationContext, "factory">): Factory["create"] =>
(params: Params): ts.PropertyAssignment => {
const node = factory.createPropertyAssignment(params.name, params.initializer);
if (params.comment) {
const comment = generateComment(params.comment, params.deprecated);
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, comment.value, comment.hasTrailingNewLine);
}
return node;
};

Expand Down
1 change: 1 addition & 0 deletions src/internal/TsGenerator/factory/VariableDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts from "typescript";
import { generateComment } from "./utils";

export interface Params {
name: string | ts.BindingName;
Expand Down
2 changes: 2 additions & 0 deletions src/internal/TsGenerator/factory/VariableDeclarationList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ts from "typescript";
import { generateComment } from "./utils";

const flags = {
const: ts.NodeFlags.Const,
Expand All @@ -7,6 +8,7 @@ const flags = {
export interface Params {
declarations: readonly ts.VariableDeclaration[];
flag: keyof typeof flags;

}

export interface Factory {
Expand Down
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.21.0";
export const Version = "0.22.0";
182 changes: 92 additions & 90 deletions test/__tests__/functional/__snapshots__/argo-rollout-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3830,97 +3830,99 @@ export interface ApiClient<RequestOption> {
request: <T = SuccessResponses>(httpMethod: HttpMethod, url: string, headers: ObjectLike | any, requestBody: ObjectLike | any, queryParameters: QueryParameters | undefined, options?: RequestOption) => Promise<T>;
}
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
const RolloutService_GetNamespace = (option?: RequestOption): Promise<Response$RolloutService_GetNamespace$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/namespace\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
};
const RolloutService_ListRolloutInfos = (params: Params$RolloutService_ListRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_ListRolloutInfos$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
};
const RolloutService_WatchRolloutInfos = (params: Params$RolloutService_WatchRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfos$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info/watch\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
};
const RolloutService_AbortRollout = (params: Params$RolloutService_AbortRollout, option?: RequestOption): Promise<Response$RolloutService_AbortRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/abort\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_GetRolloutInfo = (params: Params$RolloutService_GetRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_GetRolloutInfo$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
};
const RolloutService_WatchRolloutInfo = (params: Params$RolloutService_WatchRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfo$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info/watch\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
};
const RolloutService_PromoteRollout = (params: Params$RolloutService_PromoteRollout, option?: RequestOption): Promise<Response$RolloutService_PromoteRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/promote\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_RestartRollout = (params: Params$RolloutService_RestartRollout, option?: RequestOption): Promise<Response$RolloutService_RestartRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/restart\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_RetryRollout = (params: Params$RolloutService_RetryRollout, option?: RequestOption): Promise<Response$RolloutService_RetryRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/retry\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_SetRolloutImage = (params: Params$RolloutService_SetRolloutImage, option?: RequestOption): Promise<Response$RolloutService_SetRolloutImage$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/set/\${params.parameter.container}/\${params.parameter.image}/\${params.parameter.tag}\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_UndoRollout = (params: Params$RolloutService_UndoRollout, option?: RequestOption): Promise<Response$RolloutService_UndoRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/undo/\${params.parameter.revision}\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
};
const RolloutService_Version = (option?: RequestOption): Promise<Response$RolloutService_Version$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/version\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
return {
RolloutService_GetNamespace: (option?: RequestOption): Promise<Response$RolloutService_GetNamespace$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/namespace\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
},
RolloutService_ListRolloutInfos: (params: Params$RolloutService_ListRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_ListRolloutInfos$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
},
RolloutService_WatchRolloutInfos: (params: Params$RolloutService_WatchRolloutInfos, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfos$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/info/watch\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
},
RolloutService_AbortRollout: (params: Params$RolloutService_AbortRollout, option?: RequestOption): Promise<Response$RolloutService_AbortRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/abort\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_GetRolloutInfo: (params: Params$RolloutService_GetRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_GetRolloutInfo$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
},
RolloutService_WatchRolloutInfo: (params: Params$RolloutService_WatchRolloutInfo, option?: RequestOption): Promise<Response$RolloutService_WatchRolloutInfo$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/info/watch\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
},
RolloutService_PromoteRollout: (params: Params$RolloutService_PromoteRollout, option?: RequestOption): Promise<Response$RolloutService_PromoteRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/promote\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_RestartRollout: (params: Params$RolloutService_RestartRollout, option?: RequestOption): Promise<Response$RolloutService_RestartRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/restart\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_RetryRollout: (params: Params$RolloutService_RetryRollout, option?: RequestOption): Promise<Response$RolloutService_RetryRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.name}/retry\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_SetRolloutImage: (params: Params$RolloutService_SetRolloutImage, option?: RequestOption): Promise<Response$RolloutService_SetRolloutImage$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/set/\${params.parameter.container}/\${params.parameter.image}/\${params.parameter.tag}\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_UndoRollout: (params: Params$RolloutService_UndoRollout, option?: RequestOption): Promise<Response$RolloutService_UndoRollout$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/rollouts/\${params.parameter.namespace}/\${params.parameter.rollout}/undo/\${params.parameter.revision}\`;
const headers = {
"Content-Type": "application/json",
Accept: "application/json"
};
return apiClient.request("PUT", url, headers, params.requestBody, undefined, option);
},
RolloutService_Version: (option?: RequestOption): Promise<Response$RolloutService_Version$Status$200["application/json"]> => {
const url = baseUrl + \`/api/v1/version\`;
const headers = {
Accept: "application/json"
};
return apiClient.request("GET", url, headers, undefined, undefined, option);
}
};
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 };
};
export type Client = ReturnType<typeof createClient>;
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ export interface ApiClient<RequestOption> {
export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>, baseUrl: string) => {
return {};
};
export type Client = ReturnType<typeof createClient>;
"
`;
Loading

0 comments on commit 24f4a55

Please sign in to comment.