76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
"use client";
|
|
import { jsx as b } from "react/jsx-runtime";
|
|
import { useRef as f, useMemo as j } from "react";
|
|
import z from "axios";
|
|
import { d as B } from "./chunks/feature-registry-B2iuEiEx.js";
|
|
import { u as L, A as P } from "./chunks/provider-runtime-CxEpkUq5.js";
|
|
import { a as U } from "./chunks/provider-runtime-CxEpkUq5.js";
|
|
function _({
|
|
children: u,
|
|
baseUrl: i = "",
|
|
auth: o = !0,
|
|
// Optional for modular projects. Legacy mode continues to use the old
|
|
// configureApi/401/logout behavior without any change.
|
|
logoutOnUnauthorized: l = !1,
|
|
onUnauthorized: m,
|
|
axiosConfig: h,
|
|
getToken: k
|
|
}) {
|
|
const w = L(), r = f(w);
|
|
r.current = w;
|
|
const p = f(k);
|
|
p.current = k;
|
|
const d = f({ onUnauthorized: m, logoutOnUnauthorized: l });
|
|
d.current = { onUnauthorized: m, logoutOnUnauthorized: l };
|
|
const x = j(() => {
|
|
const c = z.create({ ...h, baseURL: i });
|
|
return c.interceptors.request.use(async (t) => {
|
|
var n;
|
|
const e = r.current;
|
|
if (o) {
|
|
let a = null;
|
|
if (e != null && e.authenticated) {
|
|
try {
|
|
await e.updateToken(5);
|
|
} catch {
|
|
}
|
|
a = ((n = e.instance) == null ? void 0 : n.token) || e.token || null;
|
|
}
|
|
!a && p.current && (a = await p.current() || null), a && (t.headers = t.headers || {}, t.headers.Authorization = `Bearer ${a}`);
|
|
}
|
|
return t;
|
|
}), c.interceptors.response.use(
|
|
(t) => t,
|
|
async (t) => {
|
|
var a, y, A, R, v;
|
|
const e = r.current, n = t == null ? void 0 : t.config;
|
|
if (o && ((a = t == null ? void 0 : t.response) == null ? void 0 : a.status) === 401 && n && !n._biRetried && (e != null && e.authenticated)) {
|
|
n._biRetried = !0;
|
|
try {
|
|
await e.updateToken(30);
|
|
const s = ((y = e.instance) == null ? void 0 : y.token) ?? e.token;
|
|
if (s)
|
|
return n.headers = n.headers || {}, n.headers.Authorization = `Bearer ${s}`, await c(n);
|
|
} catch {
|
|
}
|
|
}
|
|
if (((A = t == null ? void 0 : t.response) == null ? void 0 : A.status) === 401 && ((v = (R = d.current).onUnauthorized) == null || v.call(R, t), d.current.logoutOnUnauthorized && (e != null && e.logout))) {
|
|
const s = typeof window < "u" ? window.location.origin : void 0;
|
|
await e.logout(s ? { redirectUri: s } : void 0);
|
|
}
|
|
return Promise.reject(t);
|
|
}
|
|
), c;
|
|
}, [i, h, o]);
|
|
return /* @__PURE__ */ b(P.Provider, { value: x, children: u });
|
|
}
|
|
const K = B("Api", 50, _);
|
|
async function M(u, i) {
|
|
return (await u.get(i)).data;
|
|
}
|
|
export {
|
|
K as Api,
|
|
M as fetcher,
|
|
U as useApi
|
|
};
|