query-harbor 0.0.3 → 0.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/query-harbor.es.js +26 -0
- package/dist/query-harbor.umd.js +1 -1
- package/package.json +1 -1
package/dist/query-harbor.es.js
CHANGED
|
@@ -110,6 +110,15 @@ const b = ({ cookieName: u }) => {
|
|
|
110
110
|
let t = {};
|
|
111
111
|
e != null && e.accessToken && (t = { Authorization: `Bearer ${e == null ? void 0 : e.accessToken}` });
|
|
112
112
|
const s = S({
|
|
113
|
+
/**
|
|
114
|
+
* Mutation function that processes and sends data to the API.
|
|
115
|
+
* @async
|
|
116
|
+
* @param {Object} params - Mutation parameters.
|
|
117
|
+
* @param {boolean} [params.isPriorityDataAvailable] - Whether to use priority data.
|
|
118
|
+
* @param {*} [params.priorityData] - Priority data to override default data.
|
|
119
|
+
* @returns {Promise<*>} The response data from the API.
|
|
120
|
+
* @throws {Error} If the API request fails.
|
|
121
|
+
*/
|
|
113
122
|
mutationFn: async ({ isPriorityDataAvailable: l, priorityData: P }) => {
|
|
114
123
|
const p = l ? P : c;
|
|
115
124
|
if (y) {
|
|
@@ -164,9 +173,18 @@ const b = ({ cookieName: u }) => {
|
|
|
164
173
|
throw new Error(T || "Something went wrong!");
|
|
165
174
|
}
|
|
166
175
|
},
|
|
176
|
+
/**
|
|
177
|
+
* Callback executed on successful mutation.
|
|
178
|
+
* Invalidates specified queries and closes popup if provided.
|
|
179
|
+
*/
|
|
167
180
|
onSuccess: () => {
|
|
168
181
|
m.invalidateQueries({ queryKey: n }), h && h(!1);
|
|
169
182
|
},
|
|
183
|
+
/**
|
|
184
|
+
* Callback executed on mutation error.
|
|
185
|
+
* @param {Error} error - The error object from the failed mutation.
|
|
186
|
+
* @returns {string} The error message.
|
|
187
|
+
*/
|
|
170
188
|
onError: (l) => (console.log("mutationError", l == null ? void 0 : l.message), l == null ? void 0 : l.message)
|
|
171
189
|
});
|
|
172
190
|
return {
|
|
@@ -222,6 +240,13 @@ const b = ({ cookieName: u }) => {
|
|
|
222
240
|
throw console.error("Query Error:", d), d;
|
|
223
241
|
}
|
|
224
242
|
},
|
|
243
|
+
/**
|
|
244
|
+
* Function to determine the next page parameter for pagination.
|
|
245
|
+
* @param {Object} lastPage - The data from the last fetched page.
|
|
246
|
+
* @param {boolean} lastPage.hasMore - Whether more pages exist.
|
|
247
|
+
* @param {number} [lastPage.nextPage] - The next page number.
|
|
248
|
+
* @returns {number|undefined} The next page parameter or undefined if no more pages.
|
|
249
|
+
*/
|
|
225
250
|
getNextPageParam: (w) => w.hasMore ? w.nextPage : void 0,
|
|
226
251
|
enabled: y,
|
|
227
252
|
cacheTime: h,
|
|
@@ -242,6 +267,7 @@ const b = ({ cookieName: u }) => {
|
|
|
242
267
|
};
|
|
243
268
|
};
|
|
244
269
|
export {
|
|
270
|
+
b as useCookie,
|
|
245
271
|
J as useGlobalInfiniteQuery,
|
|
246
272
|
W as useGlobalMutation,
|
|
247
273
|
U as useGlobalQuery
|
package/dist/query-harbor.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(c,g){typeof exports=="object"&&typeof module<"u"?g(exports,require("@tanstack/react-query"),require("react-cookie"),require("axios")):typeof define=="function"&&define.amd?define(["exports","@tanstack/react-query","react-cookie","axios"],g):(c=typeof globalThis<"u"?globalThis:c||self,g(c.QueryHarbor={},c.ReactQuery,c.ReactCookie,c.axios))})(this,function(c,g,v,I){"use strict";const b=({cookieName:i})=>{const[r,a,u]=v.useCookies([i]);return{cookie:r,setCookie:a,removeCookie:u}},G=({url:i,queryKey:r,methodType:a,data:u,enabled:T=!0,cacheTime:w=3e5,staleTime:C=3e5})=>{const p=g.useQueryClient(),{cookie:e}=b({cookieName:"accessToken"});let t={};e!=null&&e.accessToken&&(t={Authorization:`Bearer ${e==null?void 0:e.accessToken}`});const s=g.useQuery({queryKey:r,queryFn:async()=>{try{const l=await APIHandler({action:a,url:i,data:u,headers:t});return l!=null&&l.data?l.data:{totalCount:0,data:[]}}catch(l){throw console.error("Query Error:",l),l}},enabled:T,cacheTime:w,staleTime:C,refetchOnWindowFocus:!1});return{refetchQuery:()=>{p.invalidateQueries({queryKey:r})},queryData:s.data,isLoading:s.isLoading,isError:s.isError,error:s.error}},j=async({action:i,url:r,data:a,headers:u})=>{switch(i){case"GET":return await I.get(r,{headers:u});case"POST":return await I.post(r,a,{headers:u});case"PUT":return await I.put(r,a,{headers:u});case"DELETE":return await I.delete(r,{data:a,headers:u});default:throw new Error(`Invalid action: ${i}`)}},x=async({action:i,url:r,data:a,headers:u})=>{var T,w,C,p,e;try{const t=await j({action:i,url:r,data:a,headers:u});return t.status>=200&&t.status<=299?{status:!0,data:t.data,message:t.data.message,statusCode:t.status}:{status:!1,error:"API Failed",message:"API Failed",statusCode:t.status}}catch(t){return I.isAxiosError(t)?t.message==="Network Error"?{status:!1,error:"Network Error",message:"Network Error"}:{status:!1,type:(w=(T=t.response)==null?void 0:T.data)==null?void 0:w.type,message:(C=t.response)==null?void 0:C.data.message,error:(p=t.response)==null?void 0:p.data.error,statusCode:(e=t.response)==null?void 0:e.status}:{status:!1,error:"API Failed",message:"API Failed"}}},L=({url:i,queriesToInvalidate:r,methodType:a,data:u,isFormData:T,closePopup:w,excludedIndexKeys:C})=>{var l;const p=g.useQueryClient(),{cookie:e}=b({cookieName:"accessToken"});let t={};e!=null&&e.accessToken&&(t={Authorization:`Bearer ${e==null?void 0:e.accessToken}`});const s=g.useMutation({mutationFn:async({isPriorityDataAvailable:d,priorityData:Q})=>{const k=d?Q:u;if(T){let m=new FormData;const P=(n,o,h="",F={})=>{if(o==null)return;const{excludedIndexKeys:H=[]}=F;Array.isArray(o)?o.forEach((y,q)=>{const $=H.includes(h)?h:h?`${h}[${q}]`:`${q}`;y instanceof File?n==null||n.append($,y):typeof y=="object"&&y!==null?P(n,y,$,F):n==null||n.append($,y)}):typeof o=="object"&&o!==null?o instanceof File?n.append(h,o):Object.keys(o).forEach(y=>{const q=o[y],S=h?`${h}[${y}]`:y;P(n,q,S,F)}):o!==""&&o!==void 0&&o!==null&&(n==null||n.append(h,o))},A={excludedIndexKeys:C};Object.keys(k).forEach(n=>{P(m,k[n],n,A)});const{status:E,message:f,data:N}=await x({action:a,url:i,data:m,headers:t});if(E)return N;throw new Error(f||"Something went wrong!")}else{const{status:m,message:P,data:A}=await x({action:a,url:i,data:k,headers:t});if(m)return A;throw new Error(P||"Something went wrong!")}},onSuccess:()=>{p.invalidateQueries({queryKey:r}),w&&w(!1)},onError:d=>(console.log("mutationError",d==null?void 0:d.message),d==null?void 0:d.message)});return{runMutation:({isPriorityDataAvailable:d,priorityData:Q}={})=>{try{s.mutate({isPriorityDataAvailable:d,priorityData:Q})}catch(k){console.log("Mutation Error: ",k)}},mutationLoading:s.isPending,mutationData:s.data,mutationError:(l=s.error)==null?void 0:l.message,isMutationSucceeded:s.isSuccess}},O=({url:i,queryKey:r,methodType:a,data:u,enabled:T=!0,cacheTime:w=3e5,staleTime:C=3e5})=>{var Q,k,m,P,A;const p=g.useQueryClient(),{cookie:e}=b({cookieName:"accessToken"});let t={};e!=null&&e.accessToken&&(t={Authorization:`Bearer ${e==null?void 0:e.accessToken}`});const s=g.useInfiniteQuery({queryKey:r,queryFn:async({pageParam:E})=>{try{const f=await x({action:a,url:i,data:{...u,page:E},headers:t});if(f!=null&&f.data){const{data:N,page:n,totalPages:o,totalCount:h}=f==null?void 0:f.data,F=n<o;return{data:N,nextPage:F?n+1:void 0,hasMore:F,totalCount:h}}else return{totalCount:0,data:[]}}catch(f){throw console.error("Query Error:",f),f}},getNextPageParam:E=>E.hasMore?E.nextPage:void 0,enabled:T,cacheTime:w,staleTime:C,refetchOnWindowFocus:!1}),M=()=>{p.invalidateQueries({queryKey:r})},l=((k=(Q=s==null?void 0:s.data)==null?void 0:Q.pages)==null?void 0:k.flatMap(E=>E.data))||[],d=((A=(P=(m=s==null?void 0:s.data)==null?void 0:m.pages)==null?void 0:P[0])==null?void 0:A.totalCount)??0;return{refetchQuery:M,queryData:l,isLoading:s.isLoading,isError:s.isError,error:s.error,fetchNextPage:s.fetchNextPage,hasNextPage:s.hasNextPage,totalCount:d}};c.useCookie=b,c.useGlobalInfiniteQuery=O,c.useGlobalMutation=L,c.useGlobalQuery=G,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
|