query-harbor 0.0.9 → 0.0.10
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.
|
@@ -23,7 +23,7 @@ interface MutationFunctionParams<T> {
|
|
|
23
23
|
* @param {Array<string>} [excludedIndexKeys] - Keys for which array indices should not be included in FormData.
|
|
24
24
|
* @returns {Object} An object containing mutation-related functions and state.
|
|
25
25
|
*/
|
|
26
|
-
export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate, method, data, isFormData, onSuccess, excludedIndexKeys, }: UseGlobalMutationParams<T>) => {
|
|
26
|
+
export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate, method, data, isFormData, onSuccess: onMutationSuccess, excludedIndexKeys, }: UseGlobalMutationParams<T>) => {
|
|
27
27
|
runMutation: (params?: MutationFunctionParams<T>) => void;
|
|
28
28
|
mutationLoading: boolean;
|
|
29
29
|
mutationData: any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGlobalMutation.d.ts","sourceRoot":"","sources":["../../src/hooks/useGlobalMutation.ts"],"names":[],"mappings":"AAIA,UAAU,uBAAuB,CAAC,CAAC;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;IAC1C,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAChC,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;CAChC;AAED,UAAU,sBAAsB,CAAC,CAAC;IAC9B,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,YAAY,CAAC,EAAE,CAAC,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"useGlobalMutation.d.ts","sourceRoot":"","sources":["../../src/hooks/useGlobalMutation.ts"],"names":[],"mappings":"AAIA,UAAU,uBAAuB,CAAC,CAAC;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;IAC1C,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;IAChC,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;CAChC;AAED,UAAU,sBAAsB,CAAC,CAAC;IAC9B,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,YAAY,CAAC,EAAE,CAAC,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,4GAQ5D,uBAAuB,CAAC,CAAC,CAAC;2BA8FK,sBAAsB,CAAC,CAAC,CAAC;;;;;CAe1D,CAAC"}
|
package/dist/query-harbor.es.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { useQueryClient as
|
|
1
|
+
import { useQueryClient as $, useQuery as v, useMutation as M } from "@tanstack/react-query";
|
|
2
2
|
import { useCookies as N } from "react-cookie";
|
|
3
3
|
import k from "axios";
|
|
4
|
-
const
|
|
5
|
-
const [a, e,
|
|
6
|
-
return { cookie: a, setCookie: e, removeCookie:
|
|
7
|
-
}, j = async ({ action: o, url: a, data: e, headers:
|
|
8
|
-
const n =
|
|
4
|
+
const b = ({ cookieName: o }) => {
|
|
5
|
+
const [a, e, d] = N([o]);
|
|
6
|
+
return { cookie: a, setCookie: e, removeCookie: d };
|
|
7
|
+
}, j = async ({ action: o, url: a, data: e, headers: d }) => {
|
|
8
|
+
const n = d;
|
|
9
9
|
switch (o) {
|
|
10
10
|
case "GET":
|
|
11
11
|
return await k.get(a, { headers: n });
|
|
@@ -18,10 +18,10 @@ const S = ({ cookieName: o }) => {
|
|
|
18
18
|
default:
|
|
19
19
|
throw new Error(`Invalid action: ${o}`);
|
|
20
20
|
}
|
|
21
|
-
}, Q = async ({ action: o, url: a, data: e, headers:
|
|
22
|
-
var n,
|
|
21
|
+
}, Q = async ({ action: o, url: a, data: e, headers: d }) => {
|
|
22
|
+
var n, m, E, y, i, g, r;
|
|
23
23
|
try {
|
|
24
|
-
const s = await j({ action: o, url: a, data: e, headers:
|
|
24
|
+
const s = await j({ action: o, url: a, data: e, headers: d });
|
|
25
25
|
return s.status >= 200 && s.status <= 299 ? {
|
|
26
26
|
status: !0,
|
|
27
27
|
data: s.data,
|
|
@@ -42,9 +42,9 @@ const S = ({ cookieName: o }) => {
|
|
|
42
42
|
message: "Network Error"
|
|
43
43
|
} : {
|
|
44
44
|
status: !1,
|
|
45
|
-
type: (
|
|
45
|
+
type: (m = (n = t.response) == null ? void 0 : n.data) == null ? void 0 : m.type,
|
|
46
46
|
message: ((y = (E = t.response) == null ? void 0 : E.data) == null ? void 0 : y.message) || "API Failed",
|
|
47
|
-
error: ((
|
|
47
|
+
error: ((g = (i = t.response) == null ? void 0 : i.data) == null ? void 0 : g.error) || "API Failed",
|
|
48
48
|
statusCode: (r = t.response) == null ? void 0 : r.status
|
|
49
49
|
};
|
|
50
50
|
}
|
|
@@ -58,22 +58,22 @@ const S = ({ cookieName: o }) => {
|
|
|
58
58
|
url: o,
|
|
59
59
|
baseUrl: a,
|
|
60
60
|
queryKey: e,
|
|
61
|
-
method:
|
|
61
|
+
method: d,
|
|
62
62
|
data: n,
|
|
63
|
-
enabled:
|
|
63
|
+
enabled: m = !0,
|
|
64
64
|
staleTime: E = 5 * 60 * 1e3,
|
|
65
65
|
refetchOnWindowFocus: y = !1,
|
|
66
66
|
select: i
|
|
67
67
|
}) => {
|
|
68
|
-
const
|
|
68
|
+
const g = $(), { cookie: r } = b({ cookieName: "accessToken" });
|
|
69
69
|
let s = {};
|
|
70
70
|
r != null && r.accessToken && (s = { Authorization: `Bearer ${r.accessToken}` });
|
|
71
|
-
const t =
|
|
71
|
+
const t = v({
|
|
72
72
|
queryKey: e,
|
|
73
73
|
queryFn: async () => {
|
|
74
74
|
try {
|
|
75
75
|
const u = await Q({
|
|
76
|
-
action:
|
|
76
|
+
action: d,
|
|
77
77
|
url: o,
|
|
78
78
|
data: n,
|
|
79
79
|
headers: s
|
|
@@ -83,14 +83,14 @@ const S = ({ cookieName: o }) => {
|
|
|
83
83
|
throw console.error("Query Error:", u), u;
|
|
84
84
|
}
|
|
85
85
|
},
|
|
86
|
-
enabled:
|
|
86
|
+
enabled: m,
|
|
87
87
|
staleTime: E,
|
|
88
88
|
refetchOnWindowFocus: y,
|
|
89
89
|
select: i
|
|
90
90
|
});
|
|
91
91
|
return {
|
|
92
92
|
refetchQuery: () => {
|
|
93
|
-
|
|
93
|
+
g.invalidateQueries({ queryKey: e });
|
|
94
94
|
},
|
|
95
95
|
queryData: t.data,
|
|
96
96
|
isLoading: t.isLoading,
|
|
@@ -101,37 +101,37 @@ const S = ({ cookieName: o }) => {
|
|
|
101
101
|
url: o,
|
|
102
102
|
queriesToInvalidate: a,
|
|
103
103
|
method: e,
|
|
104
|
-
data:
|
|
104
|
+
data: d,
|
|
105
105
|
isFormData: n,
|
|
106
|
-
onSuccess:
|
|
106
|
+
onSuccess: m,
|
|
107
107
|
excludedIndexKeys: E
|
|
108
108
|
}) => {
|
|
109
109
|
var t;
|
|
110
|
-
const y =
|
|
111
|
-
let
|
|
112
|
-
i != null && i.accessToken && (
|
|
113
|
-
const r =
|
|
114
|
-
mutationFn: async ({ isPriorityDataAvailable:
|
|
115
|
-
const p =
|
|
110
|
+
const y = $(), { cookie: i } = b({ cookieName: "accessToken" });
|
|
111
|
+
let g = {};
|
|
112
|
+
i != null && i.accessToken && (g = { Authorization: `Bearer ${i == null ? void 0 : i.accessToken}` });
|
|
113
|
+
const r = M({
|
|
114
|
+
mutationFn: async ({ isPriorityDataAvailable: f, priorityData: u, url: L = o }) => {
|
|
115
|
+
const p = f ? u : d;
|
|
116
116
|
if (n) {
|
|
117
|
-
const F = new FormData(), A = (
|
|
117
|
+
const F = new FormData(), A = (w, c, h = "", I = {}) => {
|
|
118
118
|
if (c == null || c == null) return;
|
|
119
119
|
const { excludedIndexKeys: P = [] } = I;
|
|
120
|
-
Array.isArray(c) ? c.forEach((
|
|
121
|
-
const T = (P == null ? void 0 : P.includes(
|
|
122
|
-
|
|
123
|
-
}) : typeof c == "object" && c !== null ? c instanceof File ?
|
|
124
|
-
A(
|
|
125
|
-
}) :
|
|
120
|
+
Array.isArray(c) ? c.forEach((l, S) => {
|
|
121
|
+
const T = (P == null ? void 0 : P.includes(h)) ? h : h ? `${h}[${S}]` : `${S}`;
|
|
122
|
+
l instanceof File ? w.append(T, l) : typeof l == "object" && l !== null ? A(w, l, T, I) : w.append(T, String(l));
|
|
123
|
+
}) : typeof c == "object" && c !== null ? c instanceof File ? w.append(h, c) : Object.keys(c).forEach((l) => {
|
|
124
|
+
A(w, c[l], h ? `${h}[${l}]` : l, I);
|
|
125
|
+
}) : w.append(h, String(c));
|
|
126
126
|
};
|
|
127
|
-
Object.keys(p || {}).forEach((
|
|
128
|
-
A(F, p == null ? void 0 : p[
|
|
127
|
+
Object.keys(p || {}).forEach((w) => {
|
|
128
|
+
A(F, p == null ? void 0 : p[w], w, { excludedIndexKeys: E });
|
|
129
129
|
});
|
|
130
130
|
const { status: C, message: q, data: x } = await Q({
|
|
131
131
|
action: e,
|
|
132
132
|
url: o,
|
|
133
133
|
data: F,
|
|
134
|
-
headers:
|
|
134
|
+
headers: g
|
|
135
135
|
});
|
|
136
136
|
if (C)
|
|
137
137
|
return x;
|
|
@@ -141,22 +141,22 @@ const S = ({ cookieName: o }) => {
|
|
|
141
141
|
action: e,
|
|
142
142
|
url: o,
|
|
143
143
|
data: p,
|
|
144
|
-
headers:
|
|
144
|
+
headers: g
|
|
145
145
|
});
|
|
146
146
|
if (F)
|
|
147
147
|
return C;
|
|
148
148
|
throw new Error(A || "Something went wrong!");
|
|
149
149
|
}
|
|
150
150
|
},
|
|
151
|
-
onSuccess: () => {
|
|
152
|
-
y.invalidateQueries({ queryKey: a }),
|
|
151
|
+
onSuccess: (f) => {
|
|
152
|
+
y.invalidateQueries({ queryKey: a }), m && m(f);
|
|
153
153
|
},
|
|
154
|
-
onError: (
|
|
154
|
+
onError: (f) => (console.error("mutationError", f.message), f.message)
|
|
155
155
|
});
|
|
156
156
|
return {
|
|
157
|
-
runMutation: (
|
|
157
|
+
runMutation: (f) => {
|
|
158
158
|
try {
|
|
159
|
-
r.mutate(
|
|
159
|
+
r.mutate(f || {});
|
|
160
160
|
} catch (u) {
|
|
161
161
|
console.error("Mutation Error: ", u);
|
|
162
162
|
}
|
|
@@ -168,7 +168,7 @@ const S = ({ cookieName: o }) => {
|
|
|
168
168
|
};
|
|
169
169
|
};
|
|
170
170
|
export {
|
|
171
|
-
|
|
171
|
+
b as useCookie,
|
|
172
172
|
B as useGlobalMutation,
|
|
173
173
|
z as useGlobalQuery
|
|
174
174
|
};
|
package/dist/query-harbor.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n,
|
|
1
|
+
(function(n,y){typeof exports=="object"&&typeof module<"u"?y(exports,require("@tanstack/react-query"),require("react-cookie"),require("axios")):typeof define=="function"&&define.amd?define(["exports","@tanstack/react-query","react-cookie","axios"],y):(n=typeof globalThis<"u"?globalThis:n||self,y(n.QueryHarbor={},n.ReactQuery,n.ReactCookie,n.axios))})(this,function(n,y,M,C){"use strict";const q=({cookieName:o})=>{const[a,e,f]=M.useCookies([o]);return{cookie:a,setCookie:e,removeCookie:f}},j=async({action:o,url:a,data:e,headers:f})=>{const u=f;switch(o){case"GET":return await C.get(a,{headers:u});case"POST":return await C.post(a,e,{headers:u});case"PUT":return await C.put(a,e,{headers:u});case"DELETE":return await C.delete(a,{data:e,headers:u});default:throw new Error(`Invalid action: ${o}`)}},P=async({action:o,url:a,data:e,headers:f})=>{var u,m,k,p,i,g,r;try{const s=await j({action:o,url:a,data:e,headers:f});return s.status>=200&&s.status<=299?{status:!0,data:s.data,message:s.data.message,statusCode:s.status}:{status:!1,error:"API Failed",message:"API Failed",statusCode:s.status}}catch(s){if(C.isAxiosError(s)){const t=s;return t.message==="Network Error"?{status:!1,error:"Network Error",message:"Network Error"}:{status:!1,type:(m=(u=t.response)==null?void 0:u.data)==null?void 0:m.type,message:((p=(k=t.response)==null?void 0:k.data)==null?void 0:p.message)||"API Failed",error:((g=(i=t.response)==null?void 0:i.data)==null?void 0:g.error)||"API Failed",statusCode:(r=t.response)==null?void 0:r.status}}return{status:!1,error:"API Failed",message:"API Failed"}}},v=({url:o,baseUrl:a,queryKey:e,method:f,data:u,enabled:m=!0,staleTime:k=3e5,refetchOnWindowFocus:p=!1,select:i})=>{const g=y.useQueryClient(),{cookie:r}=q({cookieName:"accessToken"});let s={};r!=null&&r.accessToken&&(s={Authorization:`Bearer ${r.accessToken}`});const t=y.useQuery({queryKey:e,queryFn:async()=>{try{const c=await P({action:f,url:o,data:u,headers:s});return c!=null&&c.data?c.data:{totalCount:0,data:[]}}catch(c){throw console.error("Query Error:",c),c}},enabled:m,staleTime:k,refetchOnWindowFocus:p,select:i});return{refetchQuery:()=>{g.invalidateQueries({queryKey:e})},queryData:t.data,isLoading:t.isLoading,isError:t.isError,error:t.error}},G=({url:o,queriesToInvalidate:a,method:e,data:f,isFormData:u,onSuccess:m,excludedIndexKeys:k})=>{var t;const p=y.useQueryClient(),{cookie:i}=q({cookieName:"accessToken"});let g={};i!=null&&i.accessToken&&(g={Authorization:`Bearer ${i==null?void 0:i.accessToken}`});const r=y.useMutation({mutationFn:async({isPriorityDataAvailable:h,priorityData:c,url:L=o})=>{const A=h?c:f;if(u){const F=new FormData,T=(w,l,E="",I={})=>{if(l==null||l==null)return;const{excludedIndexKeys:S=[]}=I;Array.isArray(l)?l.forEach((d,$)=>{const Q=(S==null?void 0:S.includes(E))?E:E?`${E}[${$}]`:`${$}`;d instanceof File?w.append(Q,d):typeof d=="object"&&d!==null?T(w,d,Q,I):w.append(Q,String(d))}):typeof l=="object"&&l!==null?l instanceof File?w.append(E,l):Object.keys(l).forEach(d=>{T(w,l[d],E?`${E}[${d}]`:d,I)}):w.append(E,String(l))};Object.keys(A||{}).forEach(w=>{T(F,A==null?void 0:A[w],w,{excludedIndexKeys:k})});const{status:b,message:N,data:H}=await P({action:e,url:o,data:F,headers:g});if(b)return H;throw new Error(N||"Something went wrong!")}else{const{status:F,message:T,data:b}=await P({action:e,url:o,data:A,headers:g});if(F)return b;throw new Error(T||"Something went wrong!")}},onSuccess:h=>{p.invalidateQueries({queryKey:a}),m&&m(h)},onError:h=>(console.error("mutationError",h.message),h.message)});return{runMutation:h=>{try{r.mutate(h||{})}catch(c){console.error("Mutation Error: ",c)}},mutationLoading:r.isPending,mutationData:r.data,mutationError:(t=r.error)==null?void 0:t.message,isMutationSucceeded:r.isSuccess}};n.useCookie=q,n.useGlobalMutation=G,n.useGlobalQuery=v,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|