uikit/dist/provider-api.js
Mohamadzadeh f11313909d
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Has been skipped
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Has been skipped
add new BiProvider and v1.1.0
2026-10-25 16:15:10 +03:30

70 lines
2.4 KiB
JavaScript

"use client";
import { jsx as v } from "react/jsx-runtime";
import { useRef as A, useMemo as x } from "react";
import b from "axios";
import { d as g } from "./chunks/_shared-DHh79jO9.js";
import { u as j, A as z } from "./chunks/_runtime-B6XIrDzf.js";
import { a as O } from "./chunks/_runtime-B6XIrDzf.js";
function B({
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: r = !1,
onUnauthorized: f,
axiosConfig: m
}) {
const h = j(), p = A(h);
p.current = h;
const d = A({ onUnauthorized: f, logoutOnUnauthorized: r });
d.current = { onUnauthorized: f, logoutOnUnauthorized: r };
const R = x(() => {
const c = b.create({ ...m, baseURL: i });
return c.interceptors.request.use(async (t) => {
var n;
const e = p.current;
if (o && (e != null && e.authenticated)) {
try {
await e.updateToken(5);
} catch {
}
const s = ((n = e.instance) == null ? void 0 : n.token) ?? e.token;
s && (t.headers = t.headers || {}, t.headers.Authorization = `Bearer ${s}`);
}
return t;
}), c.interceptors.response.use(
(t) => t,
async (t) => {
var s, k, l, w, y;
const e = p.current, n = t == null ? void 0 : t.config;
if (o && ((s = t == null ? void 0 : t.response) == null ? void 0 : s.status) === 401 && n && !n._biRetried && (e != null && e.authenticated)) {
n._biRetried = !0;
try {
await e.updateToken(30);
const a = ((k = e.instance) == null ? void 0 : k.token) ?? e.token;
if (a)
return n.headers = n.headers || {}, n.headers.Authorization = `Bearer ${a}`, await c(n);
} catch {
}
}
if (((l = t == null ? void 0 : t.response) == null ? void 0 : l.status) === 401 && ((y = (w = d.current).onUnauthorized) == null || y.call(w, t), d.current.logoutOnUnauthorized && (e != null && e.logout))) {
const a = typeof window < "u" ? window.location.origin : void 0;
await e.logout(a ? { redirectUri: a } : void 0);
}
return Promise.reject(t);
}
), c;
}, [i, m, o]);
return /* @__PURE__ */ v(z.Provider, { value: R, children: u });
}
const q = g("Api", 50, B);
async function F(u, i) {
return (await u.get(i)).data;
}
export {
q as Api,
F as fetcher,
O as useApi
};