update createApi

This commit is contained in:
Mohamadzadeh 2026-06-16 11:26:44 +03:30
parent c6eb027ba2
commit c4f84e0fee
2 changed files with 67 additions and 59 deletions

119
dist/core.js vendored
View File

@ -1,5 +1,5 @@
import I from "axios"; import I from "axios";
import { jsx as Z, jsxs as f } from "react/jsx-runtime"; import { jsx as Z, jsxs as A } from "react/jsx-runtime";
import G from "keycloak-js"; import G from "keycloak-js";
import { createContext as D, useState as y, useRef as P, useCallback as b, useEffect as T, useMemo as K, useContext as J } from "react"; import { createContext as D, useState as y, useRef as P, useCallback as b, useEffect as T, useMemo as K, useContext as J } from "react";
import { useQueryClient as H, useMutation as k, useQuery as Y, QueryClient as Q, QueryClientProvider as j } from "@tanstack/react-query"; import { useQueryClient as H, useMutation as k, useQuery as Y, QueryClient as Q, QueryClientProvider as j } from "@tanstack/react-query";
@ -30,7 +30,7 @@ const ne = D(null), oe = ({ url: e, realm: t, clientId: r }) => (o || (o = new G
initOptions: r, initOptions: r,
loading: i = null loading: i = null
}) => { }) => {
const [a, l] = y(!1), [n, s] = y(!1), [S, m] = y(null), u = P(!1), c = b(() => { const [a, l] = y(!1), [n, s] = y(!1), [F, m] = y(null), u = P(!1), c = b(() => {
o && (s(!!o.authenticated), m(o.token ?? null)); o && (s(!!o.authenticated), m(o.token ?? null));
}, []); }, []);
T(() => (u.current = !0, U({ config: t, initOptions: r }).then((d) => { T(() => (u.current = !0, U({ config: t, initOptions: r }).then((d) => {
@ -40,25 +40,25 @@ const ne = D(null), oe = ({ url: e, realm: t, clientId: r }) => (o || (o = new G
}), () => { }), () => {
u.current = !1; u.current = !1;
}), [t, r]); }), [t, r]);
const E = b(() => o == null ? void 0 : o.login(), []), h = b((d) => o == null ? void 0 : o.logout(d), []), F = b( const E = b(() => o == null ? void 0 : o.login(), []), S = b((d) => o == null ? void 0 : o.logout(d), []), h = b(
async (d = 30) => { async (d = 30) => {
if (!o) return !1; if (!o) return !1;
const p = await o.updateToken(d); const q = await o.updateToken(d);
return c(), p; return c(), q;
}, },
[c] [c]
), q = K( ), p = K(
() => ({ () => ({
initialized: a, initialized: a,
authenticated: n, authenticated: n,
token: S, token: F,
login: E, login: E,
logout: h, logout: S,
updateToken: F updateToken: h
}), }),
[a, n, S, E, h, F] [a, n, F, E, S, h]
); );
return a ? /* @__PURE__ */ Z(ne.Provider, { value: q, children: e }) : i; return a ? /* @__PURE__ */ Z(ne.Provider, { value: p, children: e }) : i;
}; };
let z = null; let z = null;
const ae = (e) => { const ae = (e) => {
@ -81,9 +81,9 @@ const ae = (e) => {
} }
return n; return n;
}, l = async (n) => { }, l = async (n) => {
var S; var F;
const s = n.config; const s = n.config;
if (((S = n == null ? void 0 : n.response) == null ? void 0 : S.status) === 401 && !(s != null && s._retry)) { if (((F = n == null ? void 0 : n.response) == null ? void 0 : F.status) === 401 && !(s != null && s._retry)) {
s._retry = !0; s._retry = !0;
try { try {
await M(30); await M(30);
@ -119,13 +119,13 @@ const ae = (e) => {
function O(e) { function O(e) {
return Array.isArray(e) ? e : [e]; return Array.isArray(e) ? e : [e];
} }
function Fe(e) { function he(e) {
return [...O(e), "list"]; return [...O(e), "list"];
} }
function v(e, t) { function v(e, t) {
return [...O(e), t]; return [...O(e), t];
} }
function Se(e, t) { function Fe(e, t) {
if (!t) return e; if (!t) return e;
const r = new URLSearchParams(); const r = new URLSearchParams();
Object.entries(t).forEach(([a, l]) => { Object.entries(t).forEach(([a, l]) => {
@ -134,7 +134,7 @@ function Se(e, t) {
const i = r.toString(); const i = r.toString();
return i ? `${e}?${i}` : e; return i ? `${e}?${i}` : e;
} }
function he(e, t) { function Se(e, t) {
return typeof e == "function" ? e(t == null ? void 0 : t.id, t) : e; return typeof e == "function" ? e(t == null ? void 0 : t.id, t) : e;
} }
function me(e = {}) { function me(e = {}) {
@ -148,15 +148,15 @@ function De({
axiosInstance: i = V, axiosInstance: i = V,
fetcher: a = le fetcher: a = le
}) { }) {
const l = O(e), n = Fe(e); const l = O(e), n = he(e);
function s(u, c = {}) { function s(u, c = {}) {
return Y({ return Y({
queryKey: u ? [...n, u] : n, queryKey: u ? [...n, u] : n,
queryFn: () => a(Se(t, u)), queryFn: () => a(Fe(t, u)),
...c ...c
}); });
} }
function S(u, c = {}) { function F(u, c = {}) {
return Y({ return Y({
queryKey: u ? v(e, u) : null, queryKey: u ? v(e, u) : null,
queryFn: () => a(`${t}${u}`), queryFn: () => a(`${t}${u}`),
@ -165,25 +165,28 @@ function De({
}); });
} }
function m(u, c = {}) { function m(u, c = {}) {
const E = H(), h = r[u]; const E = H(), S = r[u];
if (!h) if (!S)
throw new Error(`Action "${u}" is not defined.`); throw new Error(`Action "${u}" is not defined.`);
return k({ return k({
mutationKey: [...l, u], mutationKey: [...l, u],
mutationFn: async (F = {}) => { mutationFn: async (h = {}) => {
const q = h.method || "post", d = he(h.url || t, F), p = h.body ? h.body(F) : h.sendRawPayload ? F : me(F); const p = S.method || "post", d = Se(S.url || t, h), q = S.body ? S.body(h) : S.sendRawPayload ? h : me(h);
return (q === "delete" ? await i.delete(d, { data: p }) : await i[q](d, p)).data; return (p === "delete" ? await i.delete(
d,
q && Object.keys(q).length > 0 ? { data: q } : void 0
) : await i[p](d, q)).data;
}, },
onSuccess: (...F) => { onSuccess: (...h) => {
var g; var g;
const [q, d] = F; const [p, d] = h;
E.invalidateQueries({ E.invalidateQueries({
queryKey: n queryKey: n
}); });
const p = (d == null ? void 0 : d.id) ?? (q == null ? void 0 : q.id); const q = (d == null ? void 0 : d.id) ?? (p == null ? void 0 : p.id);
p && E.invalidateQueries({ q && E.invalidateQueries({
queryKey: v(e, p) queryKey: v(e, q)
}), (g = c.onSuccess) == null || g.call(c, ...F); }), (g = c.onSuccess) == null || g.call(c, ...h);
}, },
...c ...c
}); });
@ -192,7 +195,7 @@ function De({
key: l, key: l,
listKey: n, listKey: n,
useGet: s, useGet: s,
useItem: S, useItem: F,
useAction: m useAction: m
}; };
} }
@ -205,28 +208,28 @@ const N = D(null), x = "permissions", qe = (e) => (e == null ? void 0 : e.storag
}) => { }) => {
const l = qe(r), [n, s] = y( const l = qe(r), [n, s] = y(
() => Ee(l) () => Ee(l)
), { data: S, isLoading: m, error: u } = Xe(r), c = S ?? n; ), { data: F, isLoading: m, error: u } = Xe(r), c = F ?? n;
T(() => { T(() => {
S && (Ze(l, S), s(S)); F && (Ze(l, F), s(F));
}, [S, l]); }, [F, l]);
const E = K( const E = K(
() => be(c), () => be(c),
[c] [c]
), h = b( ), S = b(
(d = "") => ye(E, d), (d = "") => ye(E, d),
[E] [E]
), F = b((d = "") => !h(d), [h]), q = K( ), h = b((d = "") => !S(d), [S]), p = K(
() => ({ () => ({
user: e, user: e,
can: h, can: S,
cannot: F, cannot: h,
permissions: c, permissions: c,
isLoading: m, isLoading: m,
error: u error: u
}), }),
[e, h, F, c, m, u] [e, S, h, c, m, u]
); );
return u && !c ? a : m && !c ? i : /* @__PURE__ */ Z(N.Provider, { value: q, children: t }); return u && !c ? a : m && !c ? i : /* @__PURE__ */ Z(N.Provider, { value: p, children: t });
}, Ee = (e = x) => { }, Ee = (e = x) => {
if (!(typeof window > "u")) if (!(typeof window > "u"))
try { try {
@ -271,7 +274,7 @@ const N = D(null), x = "permissions", qe = (e) => (e == null ? void 0 : e.storag
if (!i.ok) if (!i.ok)
throw new Error("Failed to load permissions"); throw new Error("Failed to load permissions");
return i.json(); return i.json();
}, A = () => { }, f = () => {
const e = J(N); const e = J(N);
if (!e) if (!e)
throw new Error("usePermission must be used within PermissionProvider"); throw new Error("usePermission must be used within PermissionProvider");
@ -288,13 +291,13 @@ const N = D(null), x = "permissions", qe = (e) => (e == null ? void 0 : e.storag
children: t, children: t,
fallback: r = /* @__PURE__ */ Z("div", { children: "You are not allowed to do this" }) fallback: r = /* @__PURE__ */ Z("div", { children: "You are not allowed to do this" })
}) => { }) => {
const { can: i } = A(); const { can: i } = f();
return i(e) ? t : r; return i(e) ? t : r;
}, Le = ({ children: e }) => { }, Le = ({ children: e }) => {
const { can: t, permissions: r } = A(); const { can: t, permissions: r } = f();
return r && t("admin") ? e : null; return r && t("admin") ? e : null;
}, Ue = ({ children: e }) => { }, Ue = ({ children: e }) => {
const { can: t, isLoading: r } = A(); const { can: t, isLoading: r } = f();
return T(() => { return T(() => {
r || t("panel_access") || L(); r || t("panel_access") || L();
}, [r, t]), e; }, [r, t]), e;
@ -402,49 +405,49 @@ function Ve({
updateChecker: l = !1, updateChecker: l = !1,
updateCheckerProps: n, updateCheckerProps: n,
keycloakUrl: s = "https://auth.ibagher.ir", keycloakUrl: s = "https://auth.ibagher.ir",
keycloakRealm: S = "bi", keycloakRealm: F = "bi",
permissionUrl: m = "https://api.d.aiengines.ir/user_api/v1/permissions/list", permissionUrl: m = "https://api.d.aiengines.ir/user_api/v1/permissions/list",
keycloakEnabled: u, keycloakEnabled: u,
permissionEnabled: c permissionEnabled: c
}) { }) {
const [E, h] = y(!1), F = u ?? !!r, q = c ?? !!i, d = K( const [E, S] = y(!1), h = u ?? !!r, p = c ?? !!i, d = K(
() => ({ () => ({
api: { api: {
baseUrl: t || "" baseUrl: t || ""
}, },
keycloak: F ? { keycloak: h ? {
url: s, url: s,
realm: S, realm: F,
clientId: r clientId: r
} : null, } : null,
permission: q ? { permission: p ? {
url: m, url: m,
clientId: i clientId: i
} : null } : null
}), }),
[ [
t, t,
F, h,
q, p,
s, s,
S, F,
r, r,
m, m,
i i
] ]
); );
if (T(() => { if (T(() => {
h(!0); S(!0);
}, []), T(() => { }, []), T(() => {
E && de(d); E && de(d);
}, [E, d]), !E) }, [E, d]), !E)
return null; return null;
let p = e; let q = e;
return q && (p = /* @__PURE__ */ Z(pe, { config: d.permission, loading: a, children: p })), F && (p = /* @__PURE__ */ Z(ue, { config: d.keycloak, loading: a, children: p })), /* @__PURE__ */ f(ee, { theme: ge, children: [ return p && (q = /* @__PURE__ */ Z(pe, { config: d.permission, loading: a, children: q })), h && (q = /* @__PURE__ */ Z(ue, { config: d.keycloak, loading: a, children: q })), /* @__PURE__ */ A(ee, { theme: ge, children: [
/* @__PURE__ */ Z(Be, {}), /* @__PURE__ */ Z(Be, {}),
/* @__PURE__ */ f(j, { client: ze, children: [ /* @__PURE__ */ A(j, { client: ze, children: [
l && /* @__PURE__ */ Z(Ke, { ...n }), l && /* @__PURE__ */ Z(Ke, { ...n }),
p q
] }) ] })
] }); ] });
} }
@ -474,6 +477,6 @@ export {
R as permissionKeys, R as permissionKeys,
Ze as setCachedPermissions, Ze as setCachedPermissions,
M as updateKeycloakToken, M as updateKeycloakToken,
A as usePermission, f as usePermission,
Xe as usePermissionsQuery Xe as usePermissionsQuery
}; };

View File

@ -92,7 +92,12 @@ export function createApi({
const response = const response =
method === "delete" method === "delete"
? await axiosInstance.delete(requestUrl, { data: payload }) ? await axiosInstance.delete(
requestUrl,
payload && Object.keys(payload).length > 0
? { data: payload }
: undefined,
)
: await axiosInstance[method](requestUrl, payload); : await axiosInstance[method](requestUrl, payload);
return response.data; return response.data;