Skip to content

Commit

Permalink
chore: use configurable axios
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberalien committed Nov 26, 2023
1 parent 5da9a8a commit 8dc70ab
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
6 changes: 6 additions & 0 deletions @iconify/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions @iconify/tools/src/download/api/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AxiosRequestConfig } from 'axios';

/**
* Axios config, customisable
*/
export const axiosConfig: AxiosRequestConfig = {
// Empty by default. Add properties
};
11 changes: 8 additions & 3 deletions @iconify/tools/src/download/api/download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';
import { writeFile } from 'fs/promises';
import type { APIQueryParams } from './types';
import { axiosConfig } from './config';

/**
* Download file
Expand All @@ -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));
}
14 changes: 11 additions & 3 deletions @iconify/tools/src/download/api/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,14 +37,20 @@ async function sendQuery(query: APIQueryParams): Promise<number | string> {
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;
}
Expand Down
1 change: 1 addition & 0 deletions @iconify/tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
64 changes: 64 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8dc70ab

Please sign in to comment.