react-api-kit 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react-api-kit.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import c from "axios";
|
|
2
2
|
import { useQuery as p, useQueryClient as d, useMutation as l } from "@tanstack/react-query";
|
|
3
3
|
let o;
|
|
4
|
-
const
|
|
4
|
+
const m = (e) => {
|
|
5
5
|
o = e;
|
|
6
6
|
}, n = c.create();
|
|
7
7
|
n.interceptors.request.use((e) => {
|
|
@@ -37,19 +37,19 @@ function g(e, t, s, r, a) {
|
|
|
37
37
|
...a
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
-
function y(e, t, s
|
|
41
|
-
const
|
|
40
|
+
function y(e, t, s) {
|
|
41
|
+
const r = d();
|
|
42
42
|
return l({
|
|
43
43
|
mutationKey: e,
|
|
44
|
-
mutationFn: (i) => u[t](
|
|
44
|
+
mutationFn: ({ endpoint: a, data: i }) => u[t](a, i),
|
|
45
45
|
onSuccess: () => {
|
|
46
|
-
|
|
46
|
+
s && r.invalidateQueries({ queryKey: s });
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
export {
|
|
51
51
|
n as api,
|
|
52
|
-
|
|
52
|
+
m as setupApi,
|
|
53
53
|
y as useApiMutation,
|
|
54
54
|
g as useApiQuery
|
|
55
55
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("axios"),require("@tanstack/react-query")):typeof define=="function"&&define.amd?define(["exports","axios","@tanstack/react-query"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n.ReactApiKit={},n.axios,n.reactQuery))})(this,(function(n,r,u){"use strict";let o;const d=e=>{o=e},i=r.create();i.interceptors.request.use(e=>{o?.baseURL&&(e.baseURL=o.baseURL);const t=o?.getToken?.();return t&&(e.headers.Authorization=`Token ${t}`),e}),i.interceptors.response.use(e=>(o?.onSuccess?.(e.data),e),e=>{const t=e?.response?.status,s={400:"Bad request. Please check your input.",401:"Session expired. Please login again.",403:"You do not have permission to perform this action.",404:"Requested resource not found.",409:"Conflict occurred. Please try again.",422:"Validation error. Please check the form.",500:"Server error. Please try again later."},a=e?.response?.data?.message||t&&o?.errorMessages?.[t]||t&&s[t]||e?.message||"Network Error";return o?.toast?.error(a,o.toastOptions),o?.onError?.(e,a),t===401&&o?.onLogout?.(),Promise.reject(e)});const p={GET:(e,t)=>i.get(e,{params:t}).then(s=>s.data),POST:(e,t)=>i.post(e,t).then(s=>s.data),PATCH:(e,t)=>i.patch(e,t).then(s=>s.data),DELETE:e=>i.delete(e).then(t=>t.data)};function f(e,t,s,a,c){return u.useQuery({queryKey:e,queryFn:()=>p[t](s,a),...c})}function l(e,t,s){const a=u.useQueryClient();return u.useMutation({mutationKey:e,mutationFn:({endpoint:c,data:h})=>p[t](c,h),onSuccess:()=>{s&&a.invalidateQueries({queryKey:s})}})}n.api=i,n.setupApi=d,n.useApiMutation=l,n.useApiQuery=f,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|