Skip to content

Instantly share code, notes, and snippets.

@barakplasma
Last active January 7, 2021 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barakplasma/e0a8d32174915b280fa955ca18048d11 to your computer and use it in GitHub Desktop.
Save barakplasma/e0a8d32174915b280fa955ca18048d11 to your computer and use it in GitHub Desktop.
const axios = require('axios').default;
let callCounter = {};
const displayCalls = () => {
console.log(callCounter);
};
function makeInterceptor(keyName, returns) {
callCounter[keyName] = 0;
return (r) => {
callCounter[keyName]++;
switch (returns) {
case 'response':
return r;
case 'reject':
return Promise.reject();
default:
break;
}
};
}
const returnsResponse = axios.create();
returnsResponse
.interceptors.response.use(makeInterceptor('returnsResponse first', 'response'), null);
returnsResponse
.interceptors.response.use(makeInterceptor('returnsResponse second', 'response'), null);
const noReturn = axios.create();
noReturn
.interceptors.response.use(makeInterceptor('noReturn first', 'no response'), null);
noReturn
.interceptors.response.use(makeInterceptor('noReturn second', 'no response'), null);
const errorInterceptorReturnsRejectedPromise = axios.create();
errorInterceptorReturnsRejectedPromise
.interceptors.response.use(null, makeInterceptor('errorInterceptorReturnsRejectedPromise first', 'reject'));
errorInterceptorReturnsRejectedPromise
.interceptors.response.use(null, makeInterceptor('errorInterceptorReturnsRejectedPromise second', 'reject'));
const errorInterceptorReturnsResponse = axios.create();
errorInterceptorReturnsResponse
.interceptors.response.use(null, makeInterceptor('errorInterceptorReturnsResponse first', 'response'));
errorInterceptorReturnsResponse
.interceptors.response.use(null, makeInterceptor('errorInterceptorReturnsResponse second', 'response'));
const errorInterceptorDoesntReturn = axios.create();
errorInterceptorDoesntReturn
.interceptors.response.use(null, makeInterceptor('errorInterceptorDoesntReturn first', 'no response'));
errorInterceptorDoesntReturn
.interceptors.response.use(null, makeInterceptor('errorInterceptorDoesntReturn second', 'no response'));
Promise.allSettled([
returnsResponse.get('http://example.com'),
noReturn.get('http://example.com'),
errorInterceptorReturnsRejectedPromise.get('http://ajfalfjdalfjalfjalsdjf.com'),
errorInterceptorDoesntReturn.get('http://ajfalfjdalfjalfjalsdjf.com'),
])
.then(displayCalls)
.catch(displayCalls);
/**
* Results
*{
'returnsResponse first': 1,
'returnsResponse second': 1,
'noReturn first': 1,
'noReturn second': 1,
'errorInterceptorReturnsRejectedPromise first': 1,
'errorInterceptorReturnsRejectedPromise second': 1,
'errorInterceptorReturnsResponse first': 0,
'errorInterceptorReturnsResponse second': 0,
'errorInterceptorDoesntReturn first': 1,
'errorInterceptorDoesntReturn second': 0
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment