another day, another correction/improvement/refactor commit

This commit is contained in:
2024-08-09 01:01:17 +05:30
parent b7d6f06ff2
commit 41fff1d406
11 changed files with 238 additions and 264 deletions

51
api/axios.js Normal file
View File

@@ -0,0 +1,51 @@
const axios = require('axios');
const { baseAPIURL, accountsAPIURL } = require("../constants");
const logger = require('../utils/logger')(module);
const authInstance = axios.default.create({
baseURL: accountsAPIURL,
timeout: 20000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (Buffer.from(process.env.CLIENT_ID + ':' + process.env.CLIENT_SECRET).toString('base64'))
},
});
const axiosInstance = axios.default.create({
baseURL: baseAPIURL,
timeout: 20000,
headers: {
'Content-Type': 'application/json'
},
});
axiosInstance.interceptors.request.use(config => {
logger.http("API call", {
url: config.url,
method: config.method,
params: config.params ?? {},
headers: Object.keys(config.headers),
});
return config;
});
axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
logger.warn("AxiosError", {
error: {
name: error.name,
code: error.code,
message: error.message,
},
req: error.config,
});
return Promise.reject(error);
}
);
module.exports = {
authInstance,
axiosInstance
};