UNPKG

phx-react

Version:

PHX REACT

48 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiError = void 0; exports.apiFetch = apiFetch; const axiosInstance_1 = require("../../axiosInstance"); class ApiError extends Error { constructor(status, message, raw) { super(message); this.name = 'ApiError'; this.status = status; this.raw = raw; Object.setPrototypeOf(this, ApiError.prototype); } } exports.ApiError = ApiError; async function apiFetch(endpoint, options = {}) { var _a, _b; const { body, method = 'GET', params } = options; const finalMethod = method.toLowerCase(); const url = `${process.env.NEXT_PUBLIC_API_GATEWAY}${endpoint}`; const config = { headers: { ...options.headers, }, }; // truyền params cho method GET if (finalMethod === 'get' && params) { config.params = params; } const methodMap = { get: () => axiosInstance_1.PHXAxiosInstance.get(url, config), delete: () => axiosInstance_1.PHXAxiosInstance.delete(url, config), post: () => axiosInstance_1.PHXAxiosInstance.post(url, body, config), put: () => axiosInstance_1.PHXAxiosInstance.put(url, body, config), patch: () => axiosInstance_1.PHXAxiosInstance.patch(url, body, config), }; try { const res = await methodMap[finalMethod](); return res.data; } catch (error) { const status = ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status) || 500; const data = ((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data) || null; const message = (data === null || data === void 0 ? void 0 : data.message) || (data === null || data === void 0 ? void 0 : data.error) || (error === null || error === void 0 ? void 0 : error.message) || 'Unexpected error occurred'; throw new ApiError(status, message, data); } } //# sourceMappingURL=apiClient.js.map