burdy
Version:
Open Source Headless Platform
35 lines (34 loc) • 1.76 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useApiCallback = void 0;
const axios_1 = __importDefault(require("axios"));
const react_async_hook_1 = require("react-async-hook");
const apiAxios = axios_1.default.create({
baseURL: process.env.PUBLIC_API_URL
});
const useApiCallback = (asyncFunction, options) => {
return (0, react_async_hook_1.useAsyncCallback)((...args) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const result = (yield asyncFunction(...args));
return result === null || result === void 0 ? void 0 : result.data;
}
catch (e) {
throw (_b = (_a = e === null || e === void 0 ? void 0 : e.response) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : {};
}
}), options);
};
exports.useApiCallback = useApiCallback;
exports.default = apiAxios;