gapp-checkout
Version:
Mobile Gapp flow for Checkout
75 lines • 2.16 kB
JavaScript
import axios from 'axios';
const API_Timeout = 60000;
export const httpRequest = _ref => {
let {
url,
method,
requestParams,
requestPostData,
cancelRequest,
timeout = API_Timeout,
bearerToken,
tokenType = 'Bearer',
header
} = _ref;
return httpRequestWithoutAuthorization({
url,
method,
requestParams,
requestPostData,
timeout,
cancelRequest,
header: {
Authorization: `${tokenType} ${bearerToken}`,
...header
}
});
};
export const httpRequestWithoutAuthorization = _ref2 => {
let {
url,
method,
requestParams,
requestPostData,
cancelRequest,
timeout = API_Timeout,
header
} = _ref2;
const instance = axios.create();
instance.interceptors.request.use(config => {
config.meta = config.meta || {};
config.meta.requestStartedAt = new Date().getTime();
return config;
});
instance.interceptors.response.use(response => {
const config = response.config;
if (config.meta !== undefined) {
config.meta = config.meta || {};
config.meta.responseTime = new Date().getTime() - (config === null || config === void 0 ? void 0 : config.meta.requestStartedAt);
console.log(`Execution time for: ${config.url} - ${new Date().getTime() - (config === null || config === void 0 ? void 0 : config.meta.requestStartedAt)} ms`);
}
return response;
}, response => {
const config = response.config;
if (config.meta !== undefined) {
config.meta = config.meta || {};
config.meta.responseTime = new Date().getTime() - (config === null || config === void 0 ? void 0 : config.meta.requestStartedAt);
console.warn(`Execution time for: ${config.url} - ${new Date().getTime() - config.meta.requestStartedAt} ms`);
}
throw response;
});
return instance.request({
url: url,
method: method,
headers: {
// Accept: 'application/json',
// 'Content-type': 'application/json;charset=utf-8',
...header
},
params: requestParams,
data: requestPostData,
timeout: timeout,
signal: cancelRequest
});
};
//# sourceMappingURL=httpRequest.js.map