From 8dc70abbcfb6f0600a47cf5b99549bd4178a9044 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 26 Nov 2023 19:06:58 +0200 Subject: [PATCH] chore: use configurable axios --- @iconify/tools/package.json | 6 ++ @iconify/tools/src/download/api/config.ts | 8 +++ @iconify/tools/src/download/api/download.ts | 11 +++- @iconify/tools/src/download/api/index.ts | 14 ++++- @iconify/tools/src/index.ts | 1 + pnpm-lock.yaml | 64 +++++++++++++++++++++ 6 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 @iconify/tools/src/download/api/config.ts diff --git a/@iconify/tools/package.json b/@iconify/tools/package.json index bdba06f..9e916f7 100644 --- a/@iconify/tools/package.json +++ b/@iconify/tools/package.json @@ -31,6 +31,7 @@ "@iconify/types": "^2.0.0", "@iconify/utils": "^2.1.12", "@types/tar": "^6.1.10", + "axios": "^1.6.2", "cheerio": "1.0.0-rc.12", "extract-zip": "^2.0.1", "local-pkg": "^0.4.3", @@ -126,6 +127,11 @@ "require": "./lib/download/api/cache.cjs", "import": "./lib/download/api/cache.mjs" }, + "./lib/download/api/config": { + "types": "./lib/download/api/config.d.ts", + "require": "./lib/download/api/config.cjs", + "import": "./lib/download/api/config.mjs" + }, "./lib/download/api/download": { "types": "./lib/download/api/download.d.ts", "require": "./lib/download/api/download.cjs", diff --git a/@iconify/tools/src/download/api/config.ts b/@iconify/tools/src/download/api/config.ts new file mode 100644 index 0000000..d3a8737 --- /dev/null +++ b/@iconify/tools/src/download/api/config.ts @@ -0,0 +1,8 @@ +import { AxiosRequestConfig } from 'axios'; + +/** + * Axios config, customisable + */ +export const axiosConfig: AxiosRequestConfig = { + // Empty by default. Add properties +}; diff --git a/@iconify/tools/src/download/api/download.ts b/@iconify/tools/src/download/api/download.ts index e8537a4..8ad1ed3 100644 --- a/@iconify/tools/src/download/api/download.ts +++ b/@iconify/tools/src/download/api/download.ts @@ -1,5 +1,7 @@ +import axios from 'axios'; import { writeFile } from 'fs/promises'; import type { APIQueryParams } from './types'; +import { axiosConfig } from './config'; /** * Download file @@ -12,13 +14,16 @@ export async function downloadFile( const url = query.uri + (params ? '?' + params : ''); const headers = query.headers; - const response = await fetch(url, { + const response = await axios.get(url, { + ...axiosConfig, headers, + responseType: 'arraybuffer', }); - if (!response.ok || !response.body) { + if (response.status !== 200) { throw new Error(`Error downloading ${url}: ${response.status}`); } - const data = await response.arrayBuffer(); + + const data = response.data as ArrayBuffer; await writeFile(target, Buffer.from(data)); } diff --git a/@iconify/tools/src/download/api/index.ts b/@iconify/tools/src/download/api/index.ts index 01c7dca..e847107 100644 --- a/@iconify/tools/src/download/api/index.ts +++ b/@iconify/tools/src/download/api/index.ts @@ -1,5 +1,7 @@ +import axios from 'axios'; import { apiCacheKey, getAPICache, storeAPICache } from './cache'; import type { APICacheOptions, APIQueryParams } from './types'; +import { axiosConfig } from './config'; /** * Send API query @@ -35,14 +37,20 @@ async function sendQuery(query: APIQueryParams): Promise { console.log('Fetch:', url); const headers = query.headers; try { - const response = await fetch(url, { + const response = await axios.get(url, { + ...axiosConfig, headers, + responseType: 'text', }); - if (response.status >= 400) { + + if (response.status !== 200) { return response.status; } + if (typeof response.data !== 'string') { + return 404; + } - return await response.text(); + return response.data; } catch (err) { return 404; } diff --git a/@iconify/tools/src/index.ts b/@iconify/tools/src/index.ts index 5924317..bce8bff 100644 --- a/@iconify/tools/src/index.ts +++ b/@iconify/tools/src/index.ts @@ -64,4 +64,5 @@ export { untar } from './download/helpers/untar'; export { execAsync } from './misc/exec'; export { cleanupIconKeyword } from './misc/keyword'; export { bumpVersion } from './misc/bump-version'; +export { axiosConfig } from './download/api/config'; export { sendAPIQuery } from './download/api/index'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9f032a4..5bead22 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -146,6 +146,9 @@ importers: '@types/tar': specifier: ^6.1.10 version: 6.1.10 + axios: + specifier: ^1.6.2 + version: 1.6.2 cheerio: specifier: 1.0.0-rc.12 version: 1.0.0-rc.12 @@ -2305,6 +2308,20 @@ packages: engines: {node: '>=8'} dev: true + /asynckit@0.4.0: + resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} + dev: false + + /axios@1.6.2: + resolution: {integrity: sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==} + dependencies: + follow-redirects: 1.15.3 + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + /babel-jest@29.7.0(@babel/core@7.21.4): resolution: {integrity: sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -2596,6 +2613,13 @@ packages: resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==} dev: true + /combined-stream@1.0.8: + resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} + engines: {node: '>= 0.8'} + dependencies: + delayed-stream: 1.0.0 + dev: false + /commander@7.2.0: resolution: {integrity: sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==} engines: {node: '>= 10'} @@ -2751,6 +2775,11 @@ packages: resolution: {integrity: sha512-+uO4+qr7msjNNWKYPHqN/3+Dx3NFkmIzayk2L1MyZQlvgZb/J1A0fo410dpKrN2SnqFjt8n4JL8fDJE0wIgjFQ==} dev: true + /delayed-stream@1.0.0: + resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} + engines: {node: '>=0.4.0'} + dev: false + /destr@1.2.2: resolution: {integrity: sha512-lrbCJwD9saUQrqUfXvl6qoM+QN3W7tLV5pAOs+OqOmopCCz/JkE05MHedJR1xfk4IAnZuJXPVuN5+7jNA2ZCiA==} dev: true @@ -3228,6 +3257,16 @@ packages: resolution: {integrity: sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==} dev: true + /follow-redirects@1.15.3: + resolution: {integrity: sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dev: false + /foreground-child@3.1.1: resolution: {integrity: sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==} engines: {node: '>=14'} @@ -3236,6 +3275,15 @@ packages: signal-exit: 4.0.2 dev: true + /form-data@4.0.0: + resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==} + engines: {node: '>= 6'} + dependencies: + asynckit: 0.4.0 + combined-stream: 1.0.8 + mime-types: 2.1.35 + dev: false + /fs-extra@11.1.1: resolution: {integrity: sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==} engines: {node: '>=14.14'} @@ -4258,6 +4306,18 @@ packages: picomatch: 2.3.1 dev: true + /mime-db@1.52.0: + resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} + engines: {node: '>= 0.6'} + dev: false + + /mime-types@2.1.35: + resolution: {integrity: sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==} + engines: {node: '>= 0.6'} + dependencies: + mime-db: 1.52.0 + dev: false + /mimic-fn@2.1.0: resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==} engines: {node: '>=6'} @@ -4667,6 +4727,10 @@ packages: sisteransi: 1.0.5 dev: true + /proxy-from-env@1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + dev: false + /pump@3.0.0: resolution: {integrity: sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==} dependencies: