UNPKG

react-api-utils

Version:

A React utility library for making API calls using React Query and Axios, providing context-based configuration and custom hooks for seamless integration.

735 lines (732 loc) 23.4 kB
import xe, { createContext as pr, useContext as yr } from "react"; import { QueryClient as gr, useQuery as Er, useMutation as mr } from "@tanstack/react-query"; import { PersistQueryClientProvider as hr } from "@tanstack/react-query-persist-client"; import { createAsyncStoragePersister as Rr } from "@tanstack/query-async-storage-persister"; import Se from "axios"; import br from "@react-native-async-storage/async-storage"; var V = { exports: {} }, I = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Te; function _r() { if (Te) return I; Te = 1; var p = xe, y = Symbol.for("react.element"), C = Symbol.for("react.fragment"), E = Object.prototype.hasOwnProperty, m = p.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, T = { key: !0, ref: !0, __self: !0, __source: !0 }; function R(b, s, h) { var f, w = {}, P = null, W = null; h !== void 0 && (P = "" + h), s.key !== void 0 && (P = "" + s.key), s.ref !== void 0 && (W = s.ref); for (f in s) E.call(s, f) && !T.hasOwnProperty(f) && (w[f] = s[f]); if (b && b.defaultProps) for (f in s = b.defaultProps, s) w[f] === void 0 && (w[f] = s[f]); return { $$typeof: y, type: b, key: P, ref: W, props: w, _owner: m.current }; } return I.Fragment = C, I.jsx = R, I.jsxs = R, I; } var $ = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var we; function Tr() { return we || (we = 1, process.env.NODE_ENV !== "production" && function() { var p = xe, y = Symbol.for("react.element"), C = Symbol.for("react.portal"), E = Symbol.for("react.fragment"), m = Symbol.for("react.strict_mode"), T = Symbol.for("react.profiler"), R = Symbol.for("react.provider"), b = Symbol.for("react.context"), s = Symbol.for("react.forward_ref"), h = Symbol.for("react.suspense"), f = Symbol.for("react.suspense_list"), w = Symbol.for("react.memo"), P = Symbol.for("react.lazy"), W = Symbol.for("react.offscreen"), Q = Symbol.iterator, ke = "@@iterator"; function Ae(e) { if (e === null || typeof e != "object") return null; var r = Q && e[Q] || e[ke]; return typeof r == "function" ? r : null; } var j = p.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function d(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; Fe("error", e, t); } } function Fe(e, r, t) { { var n = j.ReactDebugCurrentFrame, i = n.getStackAddendum(); i !== "" && (r += "%s", t = t.concat([i])); var u = t.map(function(o) { return String(o); }); u.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, u); } } var De = !1, Ie = !1, $e = !1, We = !1, Ye = !1, X; X = Symbol.for("react.module.reference"); function qe(e) { return !!(typeof e == "string" || typeof e == "function" || e === E || e === T || Ye || e === m || e === h || e === f || We || e === W || De || Ie || $e || typeof e == "object" && e !== null && (e.$$typeof === P || e.$$typeof === w || e.$$typeof === R || e.$$typeof === b || e.$$typeof === s || // This needs to include all possible module reference object // types supported by any Flight configuration anywhere since // we don't know which Flight build this will end up being used // with. e.$$typeof === X || e.getModuleId !== void 0)); } function Le(e, r, t) { var n = e.displayName; if (n) return n; var i = r.displayName || r.name || ""; return i !== "" ? t + "(" + i + ")" : t; } function Z(e) { return e.displayName || "Context"; } function O(e) { if (e == null) return null; if (typeof e.tag == "number" && d("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case E: return "Fragment"; case C: return "Portal"; case T: return "Profiler"; case m: return "StrictMode"; case h: return "Suspense"; case f: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case b: var r = e; return Z(r) + ".Consumer"; case R: var t = e; return Z(t._context) + ".Provider"; case s: return Le(e, e.render, "ForwardRef"); case w: var n = e.displayName || null; return n !== null ? n : O(e.type) || "Memo"; case P: { var i = e, u = i._payload, o = i._init; try { return O(o(u)); } catch { return null; } } } return null; } var x = Object.assign, F = 0, ee, re, te, ne, ae, oe, ie; function ue() { } ue.__reactDisabledLog = !0; function Me() { { if (F === 0) { ee = console.log, re = console.info, te = console.warn, ne = console.error, ae = console.group, oe = console.groupCollapsed, ie = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: ue, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } F++; } } function Ve() { { if (F--, F === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: x({}, e, { value: ee }), info: x({}, e, { value: re }), warn: x({}, e, { value: te }), error: x({}, e, { value: ne }), group: x({}, e, { value: ae }), groupCollapsed: x({}, e, { value: oe }), groupEnd: x({}, e, { value: ie }) }); } F < 0 && d("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var U = j.ReactCurrentDispatcher, N; function Y(e, r, t) { { if (N === void 0) try { throw Error(); } catch (i) { var n = i.stack.trim().match(/\n( *(at )?)/); N = n && n[1] || ""; } return ` ` + N + e; } } var J = !1, q; { var Ue = typeof WeakMap == "function" ? WeakMap : Map; q = new Ue(); } function se(e, r) { if (!e || J) return ""; { var t = q.get(e); if (t !== void 0) return t; } var n; J = !0; var i = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var u; u = U.current, U.current = null, Me(); try { if (r) { var o = function() { throw Error(); }; if (Object.defineProperty(o.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(o, []); } catch (g) { n = g; } Reflect.construct(e, [], o); } else { try { o.call(); } catch (g) { n = g; } e.call(o.prototype); } } else { try { throw Error(); } catch (g) { n = g; } e(); } } catch (g) { if (g && n && typeof g.stack == "string") { for (var a = g.stack.split(` `), v = n.stack.split(` `), l = a.length - 1, c = v.length - 1; l >= 1 && c >= 0 && a[l] !== v[c]; ) c--; for (; l >= 1 && c >= 0; l--, c--) if (a[l] !== v[c]) { if (l !== 1 || c !== 1) do if (l--, c--, c < 0 || a[l] !== v[c]) { var _ = ` ` + a[l].replace(" at new ", " at "); return e.displayName && _.includes("<anonymous>") && (_ = _.replace("<anonymous>", e.displayName)), typeof e == "function" && q.set(e, _), _; } while (l >= 1 && c >= 0); break; } } } finally { J = !1, U.current = u, Ve(), Error.prepareStackTrace = i; } var A = e ? e.displayName || e.name : "", S = A ? Y(A) : ""; return typeof e == "function" && q.set(e, S), S; } function Ne(e, r, t) { return se(e, !1); } function Je(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function L(e, r, t) { if (e == null) return ""; if (typeof e == "function") return se(e, Je(e)); if (typeof e == "string") return Y(e); switch (e) { case h: return Y("Suspense"); case f: return Y("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case s: return Ne(e.render); case w: return L(e.type, r, t); case P: { var n = e, i = n._payload, u = n._init; try { return L(u(i), r, t); } catch { } } } return ""; } var D = Object.prototype.hasOwnProperty, le = {}, ce = j.ReactDebugCurrentFrame; function M(e) { if (e) { var r = e._owner, t = L(e.type, e._source, r ? r.type : null); ce.setExtraStackFrame(t); } else ce.setExtraStackFrame(null); } function Ke(e, r, t, n, i) { { var u = Function.call.bind(D); for (var o in e) if (u(e, o)) { var a = void 0; try { if (typeof e[o] != "function") { var v = Error((n || "React class") + ": " + t + " type `" + o + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[o] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw v.name = "Invariant Violation", v; } a = e[o](r, o, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (l) { a = l; } a && !(a instanceof Error) && (M(i), d("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, o, typeof a), M(null)), a instanceof Error && !(a.message in le) && (le[a.message] = !0, M(i), d("Failed %s type: %s", t, a.message), M(null)); } } } var Be = Array.isArray; function K(e) { return Be(e); } function Ge(e) { { var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object"; return t; } } function He(e) { try { return fe(e), !1; } catch { return !0; } } function fe(e) { return "" + e; } function de(e) { if (He(e)) return d("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Ge(e)), fe(e); } var ve = j.ReactCurrentOwner, ze = { key: !0, ref: !0, __self: !0, __source: !0 }, pe, ye; function Qe(e) { if (D.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function Xe(e) { if (D.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function Ze(e, r) { typeof e.ref == "string" && ve.current; } function er(e, r) { { var t = function() { pe || (pe = !0, d("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function rr(e, r) { { var t = function() { ye || (ye = !0, d("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var tr = function(e, r, t, n, i, u, o) { var a = { // This tag allows us to uniquely identify this as a React Element $$typeof: y, // Built-in properties that belong on the element type: e, key: r, ref: t, props: o, // Record the component responsible for creating this element. _owner: u }; return a._store = {}, Object.defineProperty(a._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(a, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(a, "_source", { configurable: !1, enumerable: !1, writable: !1, value: i }), Object.freeze && (Object.freeze(a.props), Object.freeze(a)), a; }; function nr(e, r, t, n, i) { { var u, o = {}, a = null, v = null; t !== void 0 && (de(t), a = "" + t), Xe(r) && (de(r.key), a = "" + r.key), Qe(r) && (v = r.ref, Ze(r, i)); for (u in r) D.call(r, u) && !ze.hasOwnProperty(u) && (o[u] = r[u]); if (e && e.defaultProps) { var l = e.defaultProps; for (u in l) o[u] === void 0 && (o[u] = l[u]); } if (a || v) { var c = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; a && er(o, c), v && rr(o, c); } return tr(e, a, v, i, n, ve.current, o); } } var B = j.ReactCurrentOwner, ge = j.ReactDebugCurrentFrame; function k(e) { if (e) { var r = e._owner, t = L(e.type, e._source, r ? r.type : null); ge.setExtraStackFrame(t); } else ge.setExtraStackFrame(null); } var G; G = !1; function H(e) { return typeof e == "object" && e !== null && e.$$typeof === y; } function Ee() { { if (B.current) { var e = O(B.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function ar(e) { return ""; } var me = {}; function or(e) { { var r = Ee(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function he(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = or(r); if (me[t]) return; me[t] = !0; var n = ""; e && e._owner && e._owner !== B.current && (n = " It was passed a child from " + O(e._owner.type) + "."), k(e), d('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), k(null); } } function Re(e, r) { { if (typeof e != "object") return; if (K(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; H(n) && he(n, r); } else if (H(e)) e._store && (e._store.validated = !0); else if (e) { var i = Ae(e); if (typeof i == "function" && i !== e.entries) for (var u = i.call(e), o; !(o = u.next()).done; ) H(o.value) && he(o.value, r); } } } function ir(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === s || // Note: Memo only checks outer props here. // Inner props are checked in the reconciler. r.$$typeof === w)) t = r.propTypes; else return; if (t) { var n = O(r); Ke(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !G) { G = !0; var i = O(r); d("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", i || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && d("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function ur(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { k(e), d("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), k(null); break; } } e.ref !== null && (k(e), d("Invalid attribute `ref` supplied to `React.Fragment`."), k(null)); } } var be = {}; function _e(e, r, t, n, i, u) { { var o = qe(e); if (!o) { var a = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (a += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var v = ar(); v ? a += v : a += Ee(); var l; e === null ? l = "null" : K(e) ? l = "array" : e !== void 0 && e.$$typeof === y ? (l = "<" + (O(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : l = typeof e, d("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", l, a); } var c = nr(e, r, t, i, u); if (c == null) return c; if (o) { var _ = r.children; if (_ !== void 0) if (n) if (K(_)) { for (var A = 0; A < _.length; A++) Re(_[A], e); Object.freeze && Object.freeze(_); } else d("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."); else Re(_, e); } if (D.call(r, "key")) { var S = O(e), g = Object.keys(r).filter(function(vr) { return vr !== "key"; }), z = g.length > 0 ? "{key: someKey, " + g.join(": ..., ") + ": ...}" : "{key: someKey}"; if (!be[S + z]) { var dr = g.length > 0 ? "{" + g.join(": ..., ") + ": ...}" : "{}"; d(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, z, S, dr, S), be[S + z] = !0; } } return e === E ? ur(c) : ir(c), c; } } function sr(e, r, t) { return _e(e, r, t, !0); } function lr(e, r, t) { return _e(e, r, t, !1); } var cr = lr, fr = sr; $.Fragment = E, $.jsx = cr, $.jsxs = fr; }()), $; } var Ce; function wr() { return Ce || (Ce = 1, process.env.NODE_ENV === "production" ? V.exports = _r() : V.exports = Tr()), V.exports; } var Pe = wr(); const je = pr(null), Cr = { timeout: 1e4 }, Pr = { defaultOptions: { queries: { staleTime: 5 * 60 * 1e3, retry: 3, refetchOnWindowFocus: !1 } } }, Dr = ({ baseURL: p, axiosConfig: y = {}, queryClientConfig: C = {}, children: E }) => { const m = Se.create({ baseURL: p, ...Cr, ...y }), T = new gr({ ...Pr, ...C }), R = Rr({ storage: br }); return /* @__PURE__ */ Pe.jsx(je.Provider, { value: { axiosInstance: m }, children: /* @__PURE__ */ Pe.jsx( hr, { client: T, persistOptions: { persister: R }, children: E } ) }); }, Or = () => { const p = yr(je); if (!p) throw new Error("useAPIHelper must be used within APIHelperProvider"); return p; }, Oe = async (p, y, C, E) => { var m, T, R, b; try { return (await p.request({ url: C, method: y, ...E })).data; } catch (s) { if (Se.isAxiosError(s)) { const h = s; throw { message: ((T = (m = h.response) == null ? void 0 : m.data) == null ? void 0 : T.message) || h.message, status: ((R = h.response) == null ? void 0 : R.status) || 500, data: ((b = h.response) == null ? void 0 : b.data) || null }; } throw { message: s.message || "An unknown error occurred", status: 500 }; } }; function Ir({ url: p, params: y, queryKey: C, axiosOptions: E, method: m, onSuccess: T, onError: R, ...b }) { const { axiosInstance: s } = Or(), h = C || [ p, ...y ? Object.values(y) : [] ], f = Er({ queryKey: h, queryFn: async () => await Oe(s, "GET", p, { params: y, ...E }), enabled: m === "GET", ...b }), w = mr({ mutationFn: async (P) => await Oe(s, m, p, { data: P, params: y, ...E }), onSuccess: T, onError: R }); return m === "GET" ? f : w; } export { Dr as ApiHelperProvider, Ir as useApiHelper }; //# sourceMappingURL=react-api-utils-es.js.map