feat: prefer noop over undefined when debugging

This commit is contained in:
Arthur Fiorette 2023-09-30 15:03:21 -03:00
parent 060e67d252
commit 16e51e788b
No known key found for this signature in database
GPG Key ID: 79FA8EC214FA0233
4 changed files with 29 additions and 28 deletions

4
src/cache/cache.ts vendored
View File

@ -330,10 +330,10 @@ export interface CacheInstance {
* Read the [Debugging](https://axios-cache-interceptor.js.org/guide/debugging) page for
* the complete guide.
*
* @default undefined
* @default noop function
* @see https://axios-cache-interceptor.js.org/#/pages/development-mode
*/
debug: undefined | ((msg: DebugObject) => void);
debug: (this: void, msg: DebugObject) => void;
}
/**

3
src/cache/create.ts vendored
View File

@ -54,7 +54,8 @@ export function setupCache(
axiosCache.responseInterceptor =
options.responseInterceptor || defaultResponseInterceptor(axiosCache);
axiosCache.debug = options.debug;
// eslint-disable-next-line @typescript-eslint/no-empty-function
axiosCache.debug = options.debug || function noop() {};
// CacheRequestConfig values
axiosCache.defaults.cache = {

View File

@ -20,7 +20,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
if (config.cache === false) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
msg: 'Ignoring cache because config.cache === false',
data: config
});
@ -44,7 +44,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
if (!isMethodIn(config.method, config.cache.methods)) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
msg: `Ignored because method (${config.method}) is not in cache.methods (${config.cache.methods})`
});
}
@ -81,7 +81,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
//@ts-expect-error read above
if (cache.state !== 'empty') {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Waiting list had an deferred for this key, waiting for it to finish'
});
@ -131,7 +131,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
updateStaleRequest(cache, config as ConfigWithCache<unknown>);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Updated stale request'
});
@ -141,7 +141,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
config.validateStatus = createValidateStatus(config.validateStatus);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Sending request, waiting for response',
data: {
@ -177,7 +177,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
}
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Detected concurrent request, waiting for it to finish'
});
@ -187,7 +187,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
cachedResponse = await deferred;
} catch (err) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Deferred rejected, requesting again',
data: err
@ -224,7 +224,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance) {
};
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: config.id,
msg: 'Returning cached response'
});

View File

@ -35,7 +35,7 @@ export function defaultResponseInterceptor(
// When response.config is not present, the response is indeed a error.
if (!response?.config) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
msg: 'Response interceptor received an unknown response.',
data: response
});
@ -56,7 +56,7 @@ export function defaultResponseInterceptor(
// Response is already cached
if (response.cached) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Returned cached response'
});
@ -69,7 +69,7 @@ export function defaultResponseInterceptor(
// config.cache should always exists, at least from global config merge.
if (!cacheConfig) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Response with config.cache falsy',
data: response
@ -87,7 +87,7 @@ export function defaultResponseInterceptor(
if (!isMethodIn(config.method, cacheConfig.methods)) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: `Ignored because method (${config.method}) is not in cache.methods (${cacheConfig.methods})`,
data: { config, cacheConfig }
@ -104,7 +104,7 @@ export function defaultResponseInterceptor(
cache.state !== 'loading'
) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: "Response not cached and storage isn't loading",
data: { cache, response }
@ -123,7 +123,7 @@ export function defaultResponseInterceptor(
await rejectResponse(response.id, config);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Cache predicate rejected this response'
});
@ -161,7 +161,7 @@ export function defaultResponseInterceptor(
await rejectResponse(response.id, config);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: `Cache header interpreted as 'dont cache'`,
data: { cache, response, expirationTime }
@ -192,7 +192,7 @@ export function defaultResponseInterceptor(
}
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Useful response configuration found',
data: { cacheConfig, cacheResponse: data }
@ -215,7 +215,7 @@ export function defaultResponseInterceptor(
delete axios.waiting[response.id];
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Found waiting deferred(s) and resolved them'
});
@ -226,7 +226,7 @@ export function defaultResponseInterceptor(
await axios.storage.set(response.id, newCache, config);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id: response.id,
msg: 'Response cached',
data: { cache: newCache, response }
@ -241,7 +241,7 @@ export function defaultResponseInterceptor(
// When response.config is not present, the response is indeed a error.
if (!error.isAxiosError) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
msg: 'Received an non axios error in the rejected response interceptor, ignoring.',
data: error
});
@ -261,7 +261,7 @@ export function defaultResponseInterceptor(
// config.cache should always exist, at least from global config merge.
if (!cacheConfig || !id) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
msg: 'Web request returned an error but cache handling is not enabled',
data: { error }
});
@ -272,7 +272,7 @@ export function defaultResponseInterceptor(
if (!isMethodIn(config.method, cacheConfig.methods)) {
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id,
msg: `Ignored because method (${config.method}) is not in cache.methods (${cacheConfig.methods})`,
data: { config, cacheConfig }
@ -292,7 +292,7 @@ export function defaultResponseInterceptor(
await rejectResponse(id, config);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id,
msg: 'Caught an error in the request interceptor',
data: { cache, error, config }
@ -314,7 +314,7 @@ export function defaultResponseInterceptor(
: cacheConfig.staleIfError;
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id,
msg: 'Found cache if stale config for rejected response',
data: { error, config, staleIfError }
@ -342,7 +342,7 @@ export function defaultResponseInterceptor(
);
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id,
msg: 'staleIfError resolved this response with cached data',
data: { error, config, cache }
@ -362,7 +362,7 @@ export function defaultResponseInterceptor(
}
if (__ACI_DEV__) {
axios.debug?.({
axios.debug({
id,
msg: 'Received an unknown error that could not be handled',
data: { error, config }