diff --git a/jest.snapshot.config.js b/jest.snapshot.config.js index 89cd28d5..4c377d93 100644 --- a/jest.snapshot.config.js +++ b/jest.snapshot.config.js @@ -12,10 +12,10 @@ module.exports = { "@swc/jest", { jsc: { - parser :{ - syntax: "typescript" - } - } + parser: { + syntax: "typescript", + }, + }, }, ], }, diff --git a/src/code-templates/_shared/ApiClientInterface.ts b/src/code-templates/_shared/ApiClientInterface.ts index 0a5c71ab..c9def245 100644 --- a/src/code-templates/_shared/ApiClientInterface.ts +++ b/src/code-templates/_shared/ApiClientInterface.ts @@ -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", diff --git a/src/code-templates/_shared/MethodBody/CallRequest.ts b/src/code-templates/_shared/MethodBody/CallRequest.ts index 7a03b19e..2868aed0 100644 --- a/src/code-templates/_shared/MethodBody/CallRequest.ts +++ b/src/code-templates/_shared/MethodBody/CallRequest.ts @@ -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, diff --git a/src/code-templates/_shared/MethodBody/HeaderParameter.ts b/src/code-templates/_shared/MethodBody/HeaderParameter.ts index 0134d75a..f42fb711 100644 --- a/src/code-templates/_shared/MethodBody/HeaderParameter.ts +++ b/src/code-templates/_shared/MethodBody/HeaderParameter.ts @@ -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; diff --git a/src/code-templates/_shared/MethodBody/PathParameter.ts b/src/code-templates/_shared/MethodBody/PathParameter.ts index 74841828..28a7ec12 100644 --- a/src/code-templates/_shared/MethodBody/PathParameter.ts +++ b/src/code-templates/_shared/MethodBody/PathParameter.ts @@ -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"; diff --git a/src/code-templates/_shared/MethodBody/QueryParameter.ts b/src/code-templates/_shared/MethodBody/QueryParameter.ts index f5c52ca1..d8d51b98 100644 --- a/src/code-templates/_shared/MethodBody/QueryParameter.ts +++ b/src/code-templates/_shared/MethodBody/QueryParameter.ts @@ -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"; diff --git a/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts b/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts index 9aacdd82..d313a7d0 100644 --- a/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts +++ b/src/code-templates/_shared/MethodBody/__tests__/PathParameter-test.ts @@ -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 = diff --git a/src/code-templates/_shared/MethodBody/index.ts b/src/code-templates/_shared/MethodBody/index.ts index 164e3f98..5a39284b 100644 --- a/src/code-templates/_shared/MethodBody/index.ts +++ b/src/code-templates/_shared/MethodBody/index.ts @@ -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 })); diff --git a/src/code-templates/class-api-client/__tests__/utils.test.ts b/src/code-templates/_shared/__tests__/utils.test.ts similarity index 100% rename from src/code-templates/class-api-client/__tests__/utils.test.ts rename to src/code-templates/_shared/__tests__/utils.test.ts diff --git a/src/code-templates/class-api-client/utils.ts b/src/code-templates/_shared/utils.ts similarity index 100% rename from src/code-templates/class-api-client/utils.ts rename to src/code-templates/_shared/utils.ts diff --git a/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts b/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts index d202f8a5..ad7b3553 100644 --- a/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts +++ b/src/code-templates/functional-api-client/FunctionalApiClient/ClientTypeDefinition.ts @@ -7,14 +7,14 @@ export const create = (factory: TsGenerator.Factory.Type): ts.TypeAliasDeclarati name: "ClientFunction", type: factory.TypeReferenceNode.create({ name: `typeof createClient`, - }) + }), }), factory.TypeAliasDeclaration.create({ export: true, name: "Client", type: factory.TypeReferenceNode.create({ name: `ReturnType>`, - }) - }) - ] + }), + }), + ]; }; diff --git a/src/code-templates/functional-api-client/index.ts b/src/code-templates/functional-api-client/index.ts index 0164141e..003116cb 100644 --- a/src/code-templates/functional-api-client/index.ts +++ b/src/code-templates/functional-api-client/index.ts @@ -39,7 +39,7 @@ export const generator: CodeGenerator.GenerateFunction