Skip to content

feat(breaking change): update apiClient arguments interface #106

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

Merged
merged 6 commits into from
Apr 1, 2023
Merged
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
8 changes: 4 additions & 4 deletions jest.snapshot.config.js
Original file line number Diff line number Diff line change
@@ -12,10 +12,10 @@ module.exports = {
"@swc/jest",
{
jsc: {
parser :{
syntax: "typescript"
}
}
parser: {
syntax: "typescript",
},
},
},
],
},
72 changes: 45 additions & 27 deletions src/code-templates/_shared/ApiClientInterface.ts
Original file line number Diff line number Diff line change
@@ -117,33 +117,10 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
],
});

const httpMethod = factory.ParameterDeclaration.create({
name: "httpMethod",
const requestArgs = factory.ParameterDeclaration.create({
name: "requestArgs",
type: factory.TypeReferenceNode.create({
name: "HttpMethod",
}),
});
const url = factory.ParameterDeclaration.create({
name: "url",
type: factory.TypeNode.create({ type: "string" }),
});
const headers = factory.ParameterDeclaration.create({
name: "headers",
type: objectLikeOrAnyType,
});
const requestBody = factory.ParameterDeclaration.create({
name: "requestBody",
type: objectLikeOrAnyType,
});
const queryParameters = factory.ParameterDeclaration.create({
name: "queryParameters",
type: factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
name: "QueryParameters",
}),
factory.TypeNode.create({ type: "undefined" }),
],
name: "RequestArgs",
}),
});
const options = factory.ParameterDeclaration.create({
@@ -186,7 +163,7 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
}),
}),
],
parameters: [httpMethod, url, headers, requestBody, queryParameters, options],
parameters: [requestArgs, options],
type: returnType,
});

@@ -196,12 +173,53 @@ export const create = (factory: TsGenerator.Factory.Type, list: CodeGenerator.Pa
type: functionType,
});

const requestArgsInterfaceDeclaration = factory.InterfaceDeclaration.create({
export: true,
name: "RequestArgs",
members: [
factory.PropertySignature.create({
name: `httpMethod`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "HttpMethod" }),
}),
factory.PropertySignature.create({
name: `url`,
optional: false,
type: factory.TypeReferenceNode.create({ name: "string" }),
}),
factory.PropertySignature.create({
name: `headers`,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `requestBody`,
optional: false,
type: objectLikeOrAnyType,
}),
factory.PropertySignature.create({
name: `queryParameters`,
optional: false,
type: factory.UnionTypeNode.create({
typeNodes: [
factory.TypeReferenceNode.create({
name: "QueryParameters",
}),
factory.TypeNode.create({ type: "undefined" }),
],
}),
}),
],
typeParameters: [],
});

return [
createHttpMethod(factory),
createObjectLikeInterface(factory),
...createQueryParamsDeclarations(factory),
createSuccessResponseTypeAlias("SuccessResponses", factory, successResponseNames),
errorResponseNamespace,
requestArgsInterfaceDeclaration,
factory.InterfaceDeclaration.create({
export: true,
name: "ApiClient",
46 changes: 33 additions & 13 deletions src/code-templates/_shared/MethodBody/CallRequest.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import ts from "typescript";

import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import * as Utils from "../../class-api-client/utils";
import * as Utils from "../utils";
import type { MethodType } from "./types";

export interface Params {
@@ -20,18 +20,38 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
function: "apiClient.request",
};
const expression = Utils.generateVariableIdentifier(factory, apiClientVariableIdentifier[methodType]);
const argumentsArray = [
factory.StringLiteral.create({ text: params.operationParams.httpMethod.toUpperCase() }),
factory.Identifier.create({ name: "url" }),
factory.Identifier.create({ name: "headers" }),
convertedParams.hasRequestBody
? Utils.generateVariableIdentifier(factory, "params.requestBody")
: factory.Identifier.create({ name: "undefined" }),
convertedParams.hasQueryParameters
? factory.Identifier.create({ name: "queryParameters" })
: factory.Identifier.create({ name: "undefined" }),
factory.Identifier.create({ name: "option" }),
];

const requestArgs = factory.ObjectLiteralExpression.create({
properties: [
factory.PropertyAssignment.create({
name: "httpMethod",
initializer: factory.StringLiteral.create({ text: params.operationParams.httpMethod.toUpperCase() }),
}),
factory.PropertyAssignment.create({
name: "url",
initializer: factory.Identifier.create({ name: "url" }),
}),
factory.PropertyAssignment.create({
name: "headers",
initializer: factory.Identifier.create({ name: "headers" }),
}),
factory.PropertyAssignment.create({
name: "requestBody",
initializer: convertedParams.hasRequestBody
? Utils.generateVariableIdentifier(factory, "params.requestBody")
: factory.Identifier.create({ name: "undefined" }),
}),
factory.PropertyAssignment.create({
name: "queryParameters",
initializer: convertedParams.hasQueryParameters
? factory.Identifier.create({ name: "queryParameters" })
: factory.Identifier.create({ name: "undefined" }),
}),
],
multiLine: true,
});

const argumentsArray = [requestArgs, factory.Identifier.create({ name: "option" })];

return factory.CallExpression.create({
expression: expression,
2 changes: 1 addition & 1 deletion src/code-templates/_shared/MethodBody/HeaderParameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ts from "typescript";

import type { TsGenerator } from "../../../api";
import * as Utils from "../../class-api-client/utils";
import * as Utils from "../utils";

export interface Params {
variableName: string;
2 changes: 1 addition & 1 deletion src/code-templates/_shared/MethodBody/PathParameter.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import ts from "typescript";

import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import * as Utils from "../../class-api-client/utils";
import * as Utils from "../utils";
import { escapeText2 as escapeText } from "../../../utils";
import type { MethodType } from "./types";

2 changes: 1 addition & 1 deletion src/code-templates/_shared/MethodBody/QueryParameter.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import ts from "typescript";

import type { TsGenerator } from "../../../api";
import * as Utils from "../../../utils";
import * as UtilsExtra from "../../class-api-client/utils";
import * as UtilsExtra from "../utils";

export interface Item {
type: "string" | "variable";
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import ts from "typescript";

import { TsGenerator } from "../../../../api";
import type { CodeGenerator } from "../../../../types";
import * as Utils from "../../../class-api-client/utils";
import * as Utils from "../../utils";
import * as PathParameter from "../PathParameter";

const traverse =
6 changes: 3 additions & 3 deletions src/code-templates/_shared/MethodBody/index.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import ts from "typescript";
import type { TsGenerator } from "../../../api";
import type { CodeGenerator } from "../../../types";
import { escapeText2 as escapeText } from "../../../utils";
import * as Utils from "../../class-api-client/utils";
import * as Utils from "../utils";
import * as CallRequest from "./CallRequest";
import * as HeaderParameter from "./HeaderParameter";
import * as PathParameter from "./PathParameter";
@@ -64,10 +64,10 @@ export const create = (factory: TsGenerator.Factory.Type, params: CodeGenerator.
const queryParameter = pickedParameters.filter(item => item.in === "query");
const queryObject = Object.values(queryParameter).reduce<{ [key: string]: QueryParameter.Item }>((previous, current) => {
const { text, escaped } = escapeText(current.name);
const variableDeclaraText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
const variableDeclareText = escaped ? `params.parameter[${text}]` : `params.parameter.${text}`;
return {
...previous,
[current.name]: { type: "variable", value: variableDeclaraText, style: current.style, explode: !!current.explode },
[current.name]: { type: "variable", value: variableDeclareText, style: current.style, explode: !!current.explode },
};
}, {});
statements.push(QueryParameter.create(factory, { variableName: "queryParameters", object: queryObject }));
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -7,14 +7,14 @@ export const create = (factory: TsGenerator.Factory.Type): ts.TypeAliasDeclarati
name: "ClientFunction<RequestOption>",
type: factory.TypeReferenceNode.create({
name: `typeof createClient<RequestOption>`,
})
}),
}),
factory.TypeAliasDeclaration.create({
export: true,
name: "Client<RequestOption>",
type: factory.TypeReferenceNode.create({
name: `ReturnType<ClientFunction<RequestOption>>`,
})
})
]
}),
}),
];
};
4 changes: 2 additions & 2 deletions src/code-templates/functional-api-client/index.ts
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ export const generator: CodeGenerator.GenerateFunction<Option> = (
statements.push(apiClientStatement);
ClientTypeDefinition.create(factory).forEach(statement => {
statements.push(statement);
})
});

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

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

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

}

export interface Factory {
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";
Loading