From bcdc1d193af8ef45d24a9c360b7a7fab384625af Mon Sep 17 00:00:00 2001 From: nekitguminskiy Date: Wed, 20 Mar 2024 13:27:52 +0200 Subject: [PATCH 1/2] fix(codegen): axios query bug --- src/templates/core/axios/request.hbs | 20 +++++++++++++++++--- src/templates/core/axios/sendRequest.hbs | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index 6612f1614..94d04a2cc 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -35,9 +35,6 @@ import type { OpenAPIConfig } from './OpenAPI'; {{>functions/getQueryString}} -{{>functions/getUrl}} - - {{>functions/getFormData}} @@ -61,6 +58,23 @@ import type { OpenAPIConfig } from './OpenAPI'; {{>functions/catchErrorCodes}} +const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => { + const encoder = config.ENCODE_PATH || encodeURI; + + const path = options.url + .replace('{api-version}', config.VERSION) + .replace(/{(.*?)}/g, (substring: string, group: string) => { + if (options.path?.hasOwnProperty(group)) { + return encoder(String(options.path[group])); + } + return substring; + }); + + const url = `${config.BASE}${path}`; + + return url; +}; + /** * Request method diff --git a/src/templates/core/axios/sendRequest.hbs b/src/templates/core/axios/sendRequest.hbs index 66b5dcb1d..6f06e06ec 100644 --- a/src/templates/core/axios/sendRequest.hbs +++ b/src/templates/core/axios/sendRequest.hbs @@ -15,6 +15,7 @@ export const sendRequest = async ( headers, data: body ?? formData, method: options.method, + params: options.query, withCredentials: config.WITH_CREDENTIALS, withXSRFToken: config.CREDENTIALS === 'include' ? config.WITH_CREDENTIALS : false, cancelToken: source.token, From 386497fad60d820de55b8cfec2a82711f1024be1 Mon Sep 17 00:00:00 2001 From: nekitguminskiy Date: Wed, 20 Mar 2024 18:26:25 +0200 Subject: [PATCH 2/2] fix(codegen): axios query --- src/templates/core/axios/request.hbs | 19 ++----------------- .../core/functions/getQueryString.hbs | 4 ++-- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/templates/core/axios/request.hbs b/src/templates/core/axios/request.hbs index 94d04a2cc..85c511ff3 100644 --- a/src/templates/core/axios/request.hbs +++ b/src/templates/core/axios/request.hbs @@ -58,22 +58,7 @@ import type { OpenAPIConfig } from './OpenAPI'; {{>functions/catchErrorCodes}} -const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => { - const encoder = config.ENCODE_PATH || encodeURI; - - const path = options.url - .replace('{api-version}', config.VERSION) - .replace(/{(.*?)}/g, (substring: string, group: string) => { - if (options.path?.hasOwnProperty(group)) { - return encoder(String(options.path[group])); - } - return substring; - }); - - const url = `${config.BASE}${path}`; - - return url; -}; +{{>functions/getUrl}} /** @@ -87,7 +72,7 @@ const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => { export const request = (config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise => { return new CancelablePromise(async (resolve, reject, onCancel) => { try { - const url = getUrl(config, options); + const url = getUrl(config, { ...options, query: undefined }); const formData = getFormData(options); const body = getRequestBody(options); const headers = await getHeaders(config, options, formData); diff --git a/src/templates/core/functions/getQueryString.hbs b/src/templates/core/functions/getQueryString.hbs index 7946851dd..57fd8bb33 100644 --- a/src/templates/core/functions/getQueryString.hbs +++ b/src/templates/core/functions/getQueryString.hbs @@ -8,8 +8,8 @@ export const getQueryString = (params: Record): string => { const process = (key: string, value: any) => { if (isDefined(value)) { if (Array.isArray(value)) { - value.forEach(v => { - process(key, v); + value.forEach((v, i) => { + process(`${key}[${i}]`, v); }); } else if (typeof value === 'object') { Object.entries(value).forEach(([k, v]) => {