65 lines
1.6 KiB
JavaScript
65 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/";
|
|
|
|
export const API_PATH_VALUE = API_PATH_MAIN;
|
|
|
|
async function makePost(
|
|
url,
|
|
data,
|
|
options = undefined,
|
|
responseProcessor = defaultResponseProcessor,
|
|
dataResponseProcessor = defaultDataResponseProcessor,
|
|
errorProcessor = defaultErrorProcessor
|
|
) {
|
|
try {
|
|
const res = await axios.post(API_PATH_VALUE + 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(API_PATH_VALUE + url, options);
|
|
return responseProcessor(res, dataResponseProcessor);
|
|
} catch (error) {
|
|
return errorProcessor(error);
|
|
}
|
|
}
|
|
|
|
export {
|
|
makePost,
|
|
makeGet,
|
|
defaultResponseProcessor,
|
|
defaultDataResponseProcessor,
|
|
defaultErrorProcessor,
|
|
};
|