Skip to content

Commit

Permalink
chore: clean up callbacks for fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberalien committed Nov 27, 2023
1 parent 673ceb2 commit c82c640
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions @iconify/tools/src/download/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const axiosConfig: Omit<
// Empty by default. Add properties
};

interface AxiosLog {
interface AxiosCallbacks {
onStart?: (url: string, params: APIQueryParams) => void;
onSuccess?: (url: string, params: APIQueryParams) => void;
onError?: (url: string, params: APIQueryParams, errorCode?: number) => void;
}

/**
* Customisable console.log for fetching
* Customisable callbacks, used for logging
*/
export const axiosLog: AxiosLog = {
export const fetchCallbacks: AxiosCallbacks = {
onStart: (url) => console.log('Fetching:', url),
};
5 changes: 4 additions & 1 deletion @iconify/tools/src/download/api/download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { writeFile } from 'fs/promises';
import type { APIQueryParams } from './types';
import { axiosConfig } from './config';
import { axiosConfig, fetchCallbacks } from './config';

/**
* Download file
Expand All @@ -14,16 +14,19 @@ export async function downloadFile(
const url = query.uri + (params ? '?' + params : '');
const headers = query.headers;

fetchCallbacks.onStart?.(url, query);
const response = await axios.get(url, {
...axiosConfig,
headers,
responseType: 'arraybuffer',
});

if (response.status !== 200) {
fetchCallbacks.onError?.(url, query, response.status);
throw new Error(`Error downloading ${url}: ${response.status}`);
}

const data = response.data as ArrayBuffer;
fetchCallbacks.onSuccess?.(url, query);
await writeFile(target, Buffer.from(data));
}
8 changes: 4 additions & 4 deletions @iconify/tools/src/download/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { apiCacheKey, getAPICache, storeAPICache } from './cache';
import type { APICacheOptions, APIQueryParams } from './types';
import { axiosConfig, axiosLog } from './config';
import { axiosConfig, fetchCallbacks } from './config';

/**
* Send API query
Expand Down Expand Up @@ -36,10 +36,10 @@ async function sendQuery(query: APIQueryParams): Promise<number | string> {
const url = query.uri + (params ? '?' + params : '');
const headers = query.headers;

axiosLog.onStart?.(url, query);
fetchCallbacks.onStart?.(url, query);

function fail(value?: number) {
axiosLog.onError?.(url, query, value);
fetchCallbacks.onError?.(url, query, value);
return value ?? 404;
}

Expand All @@ -57,7 +57,7 @@ async function sendQuery(query: APIQueryParams): Promise<number | string> {
return fail();
}

axiosLog.onSuccess?.(url, query);
fetchCallbacks.onSuccess?.(url, query);

return response.data;
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions @iconify/tools/src/download/api/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ConcurrentQueriesCommonParams<T> {
//
// If callback is present, runConcurrentQueries() will not throw an error, it will call
// callback instead, which should throw an error if runConcurrentQueries() should be aborted
onFail?: (
onError?: (
index: number,
error: unknown,
params: ConcurrentQueriesParams<T>
Expand Down Expand Up @@ -128,10 +128,10 @@ export function runConcurrentQueries<T>(
}

// check for callback
if (allParams.onFail) {
if (allParams.onError) {
let retry: void | Promise<void>;
try {
retry = allParams.onFail(index, err, params);
retry = allParams.onError(index, err, params);
} catch (err2) {
// Callback threw error: use that error
err = err2;
Expand Down

0 comments on commit c82c640

Please sign in to comment.