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 { 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 { 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";
@ -30,7 +30,7 @@ const ne = D(null), oe = ({ url: e, realm: t, clientId: r }) => (o || (o = new G
initOptions: r,
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));
}, []);
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;
}), [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) => {
if (!o) return !1;
const p = await o.updateToken(d);
return c(), p;
const q = await o.updateToken(d);
return c(), q;
},
[c]
), q = K(
), p = K(
() => ({
initialized: a,
authenticated: n,
token: S,
token: F,
login: E,
logout: h,
updateToken: F
logout: S,
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;
const ae = (e) => {
@ -81,9 +81,9 @@ const ae = (e) => {
}
return n;
}, l = async (n) => {
var S;
var F;
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;
try {
await M(30);
@ -119,13 +119,13 @@ const ae = (e) => {
function O(e) {
return Array.isArray(e) ? e : [e];
}
function Fe(e) {
function he(e) {
return [...O(e), "list"];
}
function v(e, t) {
return [...O(e), t];
}
function Se(e, t) {
function Fe(e, t) {
if (!t) return e;
const r = new URLSearchParams();
Object.entries(t).forEach(([a, l]) => {
@ -134,7 +134,7 @@ function Se(e, t) {
const i = r.toString();
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;
}
function me(e = {}) {
@ -148,15 +148,15 @@ function De({
axiosInstance: i = V,
fetcher: a = le
}) {
const l = O(e), n = Fe(e);
const l = O(e), n = he(e);
function s(u, c = {}) {
return Y({
queryKey: u ? [...n, u] : n,
queryFn: () => a(Se(t, u)),
queryFn: () => a(Fe(t, u)),
...c
});
}
function S(u, c = {}) {
function F(u, c = {}) {
return Y({
queryKey: u ? v(e, u) : null,
queryFn: () => a(`${t}${u}`),
@ -165,25 +165,28 @@ function De({
});
}
function m(u, c = {}) {
const E = H(), h = r[u];
if (!h)
const E = H(), S = r[u];
if (!S)
throw new Error(`Action "${u}" is not defined.`);
return k({
mutationKey: [...l, u],
mutationFn: async (F = {}) => {
const q = h.method || "post", d = he(h.url || t, F), p = h.body ? h.body(F) : h.sendRawPayload ? F : me(F);
return (q === "delete" ? await i.delete(d, { data: p }) : await i[q](d, p)).data;
mutationFn: async (h = {}) => {
const p = S.method || "post", d = Se(S.url || t, h), q = S.body ? S.body(h) : S.sendRawPayload ? h : me(h);
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;
const [q, d] = F;
const [p, d] = h;
E.invalidateQueries({
queryKey: n
});
const p = (d == null ? void 0 : d.id) ?? (q == null ? void 0 : q.id);
p && E.invalidateQueries({
queryKey: v(e, p)
}), (g = c.onSuccess) == null || g.call(c, ...F);
const q = (d == null ? void 0 : d.id) ?? (p == null ? void 0 : p.id);
q && E.invalidateQueries({
queryKey: v(e, q)
}), (g = c.onSuccess) == null || g.call(c, ...h);
},
...c
});
@ -192,7 +195,7 @@ function De({
key: l,
listKey: n,
useGet: s,
useItem: S,
useItem: F,
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(
() => 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(() => {
S && (Ze(l, S), s(S));
}, [S, l]);
F && (Ze(l, F), s(F));
}, [F, l]);
const E = K(
() => be(c),
[c]
), h = b(
), S = b(
(d = "") => ye(E, d),
[E]
), F = b((d = "") => !h(d), [h]), q = K(
), h = b((d = "") => !S(d), [S]), p = K(
() => ({
user: e,
can: h,
cannot: F,
can: S,
cannot: h,
permissions: c,
isLoading: m,
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) => {
if (!(typeof window > "u"))
try {
@ -271,7 +274,7 @@ const N = D(null), x = "permissions", qe = (e) => (e == null ? void 0 : e.storag
if (!i.ok)
throw new Error("Failed to load permissions");
return i.json();
}, A = () => {
}, f = () => {
const e = J(N);
if (!e)
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,
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;
}, Le = ({ children: e }) => {
const { can: t, permissions: r } = A();
const { can: t, permissions: r } = f();
return r && t("admin") ? e : null;
}, Ue = ({ children: e }) => {
const { can: t, isLoading: r } = A();
const { can: t, isLoading: r } = f();
return T(() => {
r || t("panel_access") || L();
}, [r, t]), e;
@ -402,49 +405,49 @@ function Ve({
updateChecker: l = !1,
updateCheckerProps: n,
keycloakUrl: s = "https://auth.ibagher.ir",
keycloakRealm: S = "bi",
keycloakRealm: F = "bi",
permissionUrl: m = "https://api.d.aiengines.ir/user_api/v1/permissions/list",
keycloakEnabled: u,
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: {
baseUrl: t || ""
},
keycloak: F ? {
keycloak: h ? {
url: s,
realm: S,
realm: F,
clientId: r
} : null,
permission: q ? {
permission: p ? {
url: m,
clientId: i
} : null
}),
[
t,
F,
q,
h,
p,
s,
S,
F,
r,
m,
i
]
);
if (T(() => {
h(!0);
S(!0);
}, []), T(() => {
E && de(d);
}, [E, d]), !E)
return null;
let p = 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: [
let q = e;
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__ */ f(j, { client: ze, children: [
/* @__PURE__ */ A(j, { client: ze, children: [
l && /* @__PURE__ */ Z(Ke, { ...n }),
p
q
] })
] });
}
@ -474,6 +477,6 @@ export {
R as permissionKeys,
Ze as setCachedPermissions,
M as updateKeycloakToken,
A as usePermission,
f as usePermission,
Xe as usePermissionsQuery
};

View File

@ -92,7 +92,12 @@ export function createApi({
const response =
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);
return response.data;