@atrysglobal/babel-ripper
Version:
Interface and strict typing toolkit for accessing the BABEL unified translation service.
50 lines (49 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BabelProxyHttp = void 0;
const axios_1 = require("axios");
const enums_1 = require("../enums");
class BabelProxyHttp {
/**
* Receives basic communication values with babel service
* and builds axios admissible configuration object.
*
* @param {string} baseURL - Babel API backend address.
* @param {number} timeout - Response timeout (client side).
*
* @returns {AxiosRequestConfig} - Axios configuration object.
*/
static resolveInstanceConfig(baseURL, timeout) {
return {
baseURL,
timeout: timeout || 30000,
responseType: 'json',
};
}
/**
* Creates and provides an Axios instance to communicate
* with Babel's translation service.
*
* @param {string} apiKey - Authentication API Key with Babel service.
* @param {string} baseURL - Babel API backend address.
* @param {number} timeout - Response timeout (client side).
* @returns {AxiosInstance}
*/
static getAxiosInstance({ apiKey, baseURL, timeout, }) {
const axiosInstance = axios_1.default.create(BabelProxyHttp.resolveInstanceConfig(baseURL, timeout));
/**
* Interceptor to add API Key to every request.
* Add any extra header here.
*/
axiosInstance.interceptors.request.use((config) => {
if (config.headers) {
config.headers[enums_1.ALLOWED_HEADERS.API_KEY] = apiKey;
}
return config;
}, (error) => {
throw error;
});
return axiosInstance;
}
}
exports.BabelProxyHttp = BabelProxyHttp;