47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
import axios from "axios";
|
|
import { serializeError } from "serialize-error";
|
|
|
|
|
|
function defaultResponseProcessor(response, dataProcessor) {
|
|
return {error: false, data: dataProcessor(response.data), status: response.status};
|
|
}
|
|
function defaultErrorProcessor(error) {
|
|
return {error: true, data: serializeError(error), status: (error.code === "ERR_NETWORK" ? 502:serializeError(error).status)};
|
|
}
|
|
function defaultDataResponseProcessor(data) {
|
|
return data;
|
|
}
|
|
|
|
|
|
export const API_PATH_MAIN = "https://hostapay.trade/api/v1/";
|
|
export const API_PATH_TEST = "https://test.0x000f.ru/api/v1/";
|
|
|
|
let BasicURLValue = API_PATH_TEST;
|
|
|
|
async function makePost(url, data, options = undefined,
|
|
responseProcessor = defaultResponseProcessor,
|
|
dataResponseProcessor = defaultDataResponseProcessor,
|
|
errorProcessor = defaultErrorProcessor)
|
|
{
|
|
try {
|
|
const res = await axios.post(BasicURLValue + url, data, options);
|
|
return responseProcessor(res, dataResponseProcessor);
|
|
} catch (error) {
|
|
return errorProcessor(error);
|
|
}
|
|
}
|
|
|
|
async function makeGet(url, options = undefined,
|
|
responseProcessor = defaultResponseProcessor,
|
|
dataResponseProcessor = defaultDataResponseProcessor,
|
|
errorProcessor = defaultErrorProcessor)
|
|
{
|
|
try {
|
|
const res = await axios.get(BasicURLValue + url, options);
|
|
return responseProcessor(res, dataResponseProcessor);
|
|
} catch (error) {
|
|
return errorProcessor(error);
|
|
}
|
|
}
|
|
|
|
export {BasicURLValue, makePost, makeGet, defaultResponseProcessor, defaultDataResponseProcessor, defaultErrorProcessor}; |