From 5c4a18499c5611dd439ebdc4f22bc7e84c5f133c Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 11 Nov 2023 01:11:13 +0300 Subject: [PATCH] chore: bump axios version Signed-off-by: mertmit --- packages/nocodb-sdk/package.json | 4 +- packages/nocodb-sdk/src/lib/Api.ts | 76 +++++++++++------ packages/nocodb/package.json | 2 +- pnpm-lock.yaml | 48 +++++------ .../http-clients/axios-http-client.eta | 85 ++++++++++--------- tests/playwright/package.json | 2 +- 6 files changed, 117 insertions(+), 100 deletions(-) diff --git a/packages/nocodb-sdk/package.json b/packages/nocodb-sdk/package.json index 9f931415d2..3919ff04a5 100644 --- a/packages/nocodb-sdk/package.json +++ b/packages/nocodb-sdk/package.json @@ -38,7 +38,7 @@ "preinstall": "npx only-allow pnpm" }, "dependencies": { - "axios": "^0.21.1", + "axios": "^1.6.1", "jsep": "^1.3.8" }, "devDependencies": { @@ -69,4 +69,4 @@ "prettier": { "singleQuote": true } -} \ No newline at end of file +} diff --git a/packages/nocodb-sdk/src/lib/Api.ts b/packages/nocodb-sdk/src/lib/Api.ts index 8ceba0f9b1..0a777d4326 100644 --- a/packages/nocodb-sdk/src/lib/Api.ts +++ b/packages/nocodb-sdk/src/lib/Api.ts @@ -2761,7 +2761,13 @@ export interface NotificationUpdateType { is_read?: boolean; } -import axios, { AxiosInstance, AxiosRequestConfig, ResponseType } from 'axios'; +import type { + AxiosInstance, + AxiosRequestConfig, + HeadersDefaults, + ResponseType, +} from 'axios'; +import axios from 'axios'; export type QueryParamsType = Record; @@ -2783,7 +2789,10 @@ export interface FullRequestParams body?: unknown; } -export type RequestParams = Omit; +export type RequestParams = Omit< + FullRequestParams, + 'body' | 'method' | 'query' | 'path' +>; export interface ApiConfig extends Omit { @@ -2798,6 +2807,7 @@ export enum ContentType { Json = 'application/json', FormData = 'multipart/form-data', UrlEncoded = 'application/x-www-form-urlencoded', + Text = 'text/plain', } export class HttpClient { @@ -2826,43 +2836,50 @@ export class HttpClient { this.securityData = data; }; - private mergeRequestParams( + protected mergeRequestParams( params1: AxiosRequestConfig, params2?: AxiosRequestConfig ): AxiosRequestConfig { + const method = params1.method || (params2 && params2.method); + return { ...this.instance.defaults, ...params1, ...(params2 || {}), headers: { - ...(this.instance.defaults.headers || {}), + ...((method && + this.instance.defaults.headers[ + method.toLowerCase() as keyof HeadersDefaults + ]) || + {}), ...(params1.headers || {}), ...((params2 && params2.headers) || {}), }, }; } - protected createFormData(input: Record): FormData { - if (input instanceof FormData) { - return input; + protected stringifyFormItem(formItem: unknown) { + if (typeof formItem === 'object' && formItem !== null) { + return JSON.stringify(formItem); + } else { + return `${formItem}`; } + } + + protected createFormData(input: Record): FormData { return Object.keys(input || {}).reduce((formData, key) => { const property = input[key]; - - if (property instanceof Blob) { - formData.append(key, property); - } else if (typeof property === 'object' && property !== null) { - if (Array.isArray(property)) { - // eslint-disable-next-line functional/no-loop-statement - for (const prop of property) { - formData.append(`${key}[]`, prop); - } - } else { - formData.append(key, JSON.stringify(property)); - } - } else { - formData.append(key, `${property}`); + const propertyContent: any[] = + property instanceof Array ? property : [property]; + + for (const formItem of propertyContent) { + const isFileType = formItem instanceof Blob || formItem instanceof File; + formData.append( + key, + isFileType ? formItem : this.stringifyFormItem(formItem) + ); } + return formData; }, new FormData()); } @@ -2883,7 +2900,7 @@ export class HttpClient { (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); - const responseFormat = (format && this.format) || void 0; + const responseFormat = format || this.format || undefined; if ( type === ContentType.FormData && @@ -2891,21 +2908,26 @@ export class HttpClient { body !== null && typeof body === 'object' ) { - requestParams.headers.common = { Accept: '*/*' }; - requestParams.headers.post = {}; - requestParams.headers.put = {}; - body = this.createFormData(body as Record); } + if ( + type === ContentType.Text && + body && + body !== null && + typeof body !== 'string' + ) { + body = JSON.stringify(body); + } + return this.instance .request({ ...requestParams, headers: { + ...(requestParams.headers || {}), ...(type && type !== ContentType.FormData ? { 'Content-Type': type } : {}), - ...(requestParams.headers || {}), }, params: query, responseType: responseFormat, diff --git a/packages/nocodb/package.json b/packages/nocodb/package.json index 421e9edc2c..414cea756b 100644 --- a/packages/nocodb/package.json +++ b/packages/nocodb/package.json @@ -76,7 +76,7 @@ "auto-bind": "^4.0.0", "aws-kcl": "^2.2.2", "aws-sdk": "^2.1455.0", - "axios": "^0.21.1", + "axios": "^1.6.1", "bcryptjs": "^2.4.3", "body-parser": "^1.20.2", "boxen": "^5.1.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9e29d49b9b..9536067e05 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -473,8 +473,8 @@ importers: specifier: ^2.1455.0 version: 2.1455.0 axios: - specifier: ^0.21.1 - version: 0.21.1(debug@4.3.4) + specifier: ^1.6.1 + version: 1.6.2(debug@4.3.4) bcryptjs: specifier: ^2.4.3 version: 2.4.3 @@ -876,8 +876,8 @@ importers: packages/nocodb-sdk: dependencies: axios: - specifier: ^0.21.1 - version: 0.21.1 + specifier: ^1.6.1 + version: 1.6.2 jsep: specifier: ^1.3.8 version: 1.3.8 @@ -927,6 +927,9 @@ importers: tests/playwright: dependencies: + axios: + specifier: ^1.6.1 + version: 1.6.2 body-parser: specifier: ^1.20.1 version: 1.20.1 @@ -958,9 +961,6 @@ importers: '@typescript-eslint/parser': specifier: ^6.1.0 version: 6.1.0(eslint@8.33.0)(typescript@5.2.2) - axios: - specifier: ^0.24.0 - version: 0.24.0 dotenv: specifier: ^16.0.3 version: 16.0.3 @@ -10538,14 +10538,6 @@ packages: is-retry-allowed: 2.2.0 dev: false - /axios@0.21.1: - resolution: {integrity: sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==} - dependencies: - follow-redirects: 1.15.2(debug@3.2.7) - transitivePeerDependencies: - - debug - dev: false - /axios@0.21.1(debug@4.3.4): resolution: {integrity: sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==} dependencies: @@ -10554,14 +10546,6 @@ packages: - debug dev: false - /axios@0.24.0: - resolution: {integrity: sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==} - dependencies: - follow-redirects: 1.15.2(debug@3.2.7) - transitivePeerDependencies: - - debug - dev: true - /axios@0.24.0(debug@4.3.4): resolution: {integrity: sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==} dependencies: @@ -10596,15 +10580,24 @@ packages: - debug dev: false - /axios@1.5.0: - resolution: {integrity: sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==} + /axios@1.6.2: + resolution: {integrity: sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==} dependencies: follow-redirects: 1.15.2(debug@3.2.7) form-data: 4.0.0 proxy-from-env: 1.1.0 transitivePeerDependencies: - debug - dev: true + + /axios@1.6.2(debug@4.3.4): + resolution: {integrity: sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==} + dependencies: + follow-redirects: 1.15.2(debug@4.3.4) + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false /b4a@1.6.4: resolution: {integrity: sha512-fpWrvyVHEKyeEvbKZTVOeZF3VSKKWtJxFIxX/jaVPf+cLbGUSitjb49pHLqPV2BUNNZ0LcoeEGfE/YCpyDYHIw==} @@ -19532,7 +19525,7 @@ packages: '@yarnpkg/lockfile': 1.1.0 '@yarnpkg/parsers': 3.0.0-rc.46 '@zkochan/js-yaml': 0.0.6 - axios: 1.5.0 + axios: 1.6.2 chalk: 4.1.2 cli-cursor: 3.1.0 cli-spinners: 2.6.1 @@ -20914,7 +20907,6 @@ packages: /proxy-from-env@1.1.0: resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} - dev: true /prr@1.0.1: resolution: {integrity: sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==} diff --git a/scripts/sdk/templates/http-clients/axios-http-client.eta b/scripts/sdk/templates/http-clients/axios-http-client.eta index 3bcd3c75f2..5019eed673 100644 --- a/scripts/sdk/templates/http-clients/axios-http-client.eta +++ b/scripts/sdk/templates/http-clients/axios-http-client.eta @@ -2,7 +2,8 @@ const { apiConfig, generateResponses, config } = it; %> -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, ResponseType } from "axios"; +import type { AxiosInstance, AxiosRequestConfig, HeadersDefaults, ResponseType, AxiosResponse } from "axios"; +import axios from "axios"; export type QueryParamsType = Record; @@ -23,7 +24,7 @@ export interface FullRequestParams extends Omit; +export type RequestParams = Omit; export interface ApiConfig extends Omit { securityWorker?: (securityData: SecurityDataType | null) => Promise | AxiosRequestConfig | void; @@ -35,6 +36,7 @@ export enum ContentType { Json = "application/json", FormData = "multipart/form-data", UrlEncoded = "application/x-www-form-urlencoded", + Text = "text/plain", } export class HttpClient { @@ -55,43 +57,44 @@ export class HttpClient { this.securityData = data } - private mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig { - return { - ...this.instance.defaults, - ...params1, - ...(params2 || {}), - headers: { - ...(this.instance.defaults.headers || {}), - ...(params1.headers || {}), - ...((params2 && params2.headers) || {}), - }, - }; + protected mergeRequestParams(params1: AxiosRequestConfig, params2?: AxiosRequestConfig): AxiosRequestConfig { + const method = params1.method || (params2 && params2.method) + + return { + ...this.instance.defaults, + ...params1, + ...(params2 || {}), + headers: { + ...((method && this.instance.defaults.headers[method.toLowerCase() as keyof HeadersDefaults]) || {}), + ...(params1.headers || {}), + ...((params2 && params2.headers) || {}), + }, + }; } + protected stringifyFormItem(formItem: unknown) { + if (typeof formItem === "object" && formItem !== null) { + return JSON.stringify(formItem); + } else { + return `${formItem}`; + } + } protected createFormData(input: Record): FormData { - if (input instanceof FormData) { - return input; + return Object.keys(input || {}).reduce((formData, key) => { + const property = input[key]; + const propertyContent: any[] = (property instanceof Array) ? property : [property] + + for (const formItem of propertyContent) { + const isFileType = formItem instanceof Blob || formItem instanceof File; + formData.append( + key, + isFileType ? formItem : this.stringifyFormItem(formItem) + ); } - return Object.keys(input || {}).reduce((formData, key) => { - const property = input[key]; - - if (property instanceof Blob) { - formData.append(key, property) - } else if (typeof property === 'object' && property !== null) { - if (Array.isArray(property)) { - // eslint-disable-next-line functional/no-loop-statement - for (const prop of property) { - formData.append(`${key}[]`, prop) - } - } else { - formData.append(key, JSON.stringify(property)) - } - } else { - formData.append(key, `${property}`) - } - return formData; - }, new FormData()); + + return formData; + }, new FormData()); } public request = async ({ @@ -110,21 +113,21 @@ export class HttpClient { <% } %> const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); - const responseFormat = (format && this.format) || void 0; + const responseFormat = (format || this.format) || undefined; if (type === ContentType.FormData && body && body !== null && typeof body === "object") { - requestParams.headers.common = { Accept: "*/*" }; - requestParams.headers.post = {}; - requestParams.headers.put = {}; - body = this.createFormData(body as Record); } + if (type === ContentType.Text && body && body !== null && typeof body !== "string") { + body = JSON.stringify(body); + } + return this.instance.request({ ...requestParams, headers: { - ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), ...(requestParams.headers || {}), + ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), }, params: query, responseType: responseFormat, @@ -139,4 +142,4 @@ export class HttpClient { }); <% } %> }; -} +} \ No newline at end of file diff --git a/tests/playwright/package.json b/tests/playwright/package.json index 3a2d733b79..0ba77b7f6c 100644 --- a/tests/playwright/package.json +++ b/tests/playwright/package.json @@ -40,6 +40,7 @@ "preinstall": "npx only-allow pnpm" }, "dependencies": { + "axios": "^1.6.1", "body-parser": "^1.20.1", "dayjs": "^1.11.9", "express": "^4.18.1", @@ -52,7 +53,6 @@ "@playwright/test": "1.38.0", "@typescript-eslint/eslint-plugin": "^6.1.0", "@typescript-eslint/parser": "^6.1.0", - "axios": "^0.24.0", "dotenv": "^16.0.3", "eslint": "^8.22.0", "eslint-config-prettier": "^6.15.0",