piral-axios
Version:
Plugin for the integration of axios in Piral.
43 lines • 1.38 kB
JavaScript
import Axios from 'axios';
/**
* Creates new Pilet API extensions for axios.
* @param config The custom axios configuration, if any.
*/
export function createAxiosApi(config = {}) {
return (context) => {
const axios = Axios.create(config);
axios.interceptors.request.use((config) => {
const headerPromises = [];
context.emit('before-fetch', {
headers: config.headers,
agent: config.httpAgent,
method: config.method,
target: config.url,
setHeaders(headers) {
if (headers) {
headerPromises.push(headers);
}
},
});
return Promise.all(headerPromises).then((newHeaders) => {
const headers = newHeaders.reduce((obj, header) => {
if (typeof header === 'object' && header) {
return {
...obj,
...header,
};
}
return obj;
}, config.headers);
return {
...config,
headers,
};
});
});
return {
axios,
};
};
}
//# sourceMappingURL=create.js.map