From c82c640dbbcdc7812ef5270dfb7776fcf5a5e72e Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 27 Nov 2023 17:12:27 +0200 Subject: [PATCH] chore: clean up callbacks for fetching --- @iconify/tools/src/download/api/config.ts | 6 +++--- @iconify/tools/src/download/api/download.ts | 5 ++++- @iconify/tools/src/download/api/index.ts | 8 ++++---- @iconify/tools/src/download/api/queue.ts | 6 +++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/@iconify/tools/src/download/api/config.ts b/@iconify/tools/src/download/api/config.ts index aaf94e0..2721446 100644 --- a/@iconify/tools/src/download/api/config.ts +++ b/@iconify/tools/src/download/api/config.ts @@ -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), }; diff --git a/@iconify/tools/src/download/api/download.ts b/@iconify/tools/src/download/api/download.ts index 8ad1ed3..ad21d9f 100644 --- a/@iconify/tools/src/download/api/download.ts +++ b/@iconify/tools/src/download/api/download.ts @@ -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 @@ -14,6 +14,7 @@ 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, @@ -21,9 +22,11 @@ export async function downloadFile( }); 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)); } diff --git a/@iconify/tools/src/download/api/index.ts b/@iconify/tools/src/download/api/index.ts index e94de2f..598ddd1 100644 --- a/@iconify/tools/src/download/api/index.ts +++ b/@iconify/tools/src/download/api/index.ts @@ -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 @@ -36,10 +36,10 @@ async function sendQuery(query: APIQueryParams): Promise { 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; } @@ -57,7 +57,7 @@ async function sendQuery(query: APIQueryParams): Promise { return fail(); } - axiosLog.onSuccess?.(url, query); + fetchCallbacks.onSuccess?.(url, query); return response.data; } catch (err) { diff --git a/@iconify/tools/src/download/api/queue.ts b/@iconify/tools/src/download/api/queue.ts index cc3e02e..e1029c9 100644 --- a/@iconify/tools/src/download/api/queue.ts +++ b/@iconify/tools/src/download/api/queue.ts @@ -27,7 +27,7 @@ export interface ConcurrentQueriesCommonParams { // // 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 @@ -128,10 +128,10 @@ export function runConcurrentQueries( } // check for callback - if (allParams.onFail) { + if (allParams.onError) { let retry: void | Promise; try { - retry = allParams.onFail(index, err, params); + retry = allParams.onError(index, err, params); } catch (err2) { // Callback threw error: use that error err = err2;