query-harbor 0.0.10 → 0.1.0

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.
@@ -1,26 +1,22 @@
1
1
  import { AxiosResponse } from 'axios';
2
- export type ApiHeaders = Record<string, string | number | boolean>;
3
- interface ActionHandlerParams {
4
- action: "GET" | "POST" | "PUT" | "DELETE";
2
+ type ApiAction = "GET" | "POST" | "PUT" | "DELETE";
3
+ interface ApiRequestParams {
4
+ action: ApiAction;
5
5
  url: string;
6
- data?: any;
7
- headers?: ApiHeaders;
6
+ data?: Record<string, any>;
7
+ headers?: any;
8
+ baseURL?: string;
8
9
  }
9
- export interface ApiSuccessResponse {
10
- status: true;
11
- data: any;
10
+ interface ApiResponse<T = any> {
11
+ status: boolean;
12
+ data?: T;
12
13
  message?: string;
13
- statusCode: number;
14
- }
15
- interface ApiErrorResponse {
16
- status: false;
17
- error: string;
18
- message: string;
19
- statusCode?: number;
14
+ error?: string;
20
15
  type?: string;
16
+ statusCode?: number;
17
+ errorType?: string;
21
18
  }
22
- type ApiResponse = ApiSuccessResponse | ApiErrorResponse;
23
- export declare const ActionHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<AxiosResponse>;
24
- export declare const APIHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<ApiResponse>;
19
+ export declare const ActionHandler: ({ action, url, data, headers, baseURL }: ApiRequestParams) => Promise<AxiosResponse<any>>;
20
+ export declare const APIHandler: <T = any>({ action, url, data, headers, baseURL }: ApiRequestParams) => Promise<ApiResponse<T>>;
25
21
  export {};
26
22
  //# sourceMappingURL=apiHandler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"apiHandler.d.ts","sourceRoot":"","sources":["../../src/hooks/apiHandler.ts"],"names":[],"mappings":"AAAA,OAAc,EAAuB,aAAa,EAAE,MAAM,OAAO,CAAC;AAGlE,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM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,CAAC,GAAG,GAAG;IACzB,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,eAAO,MAAM,aAAa,4CAAmD,gBAAgB,KAAG,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,CA4CzH,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,CAAC,iDAAiD,gBAAgB,KAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAwD3H,CAAC"}
@@ -21,7 +21,7 @@ interface ApiResponse<T> {
21
21
  page?: number;
22
22
  totalPages?: number;
23
23
  }
24
- declare const useGlobalInfiniteQuery: <T>({ url, queryKey, method, data, enabled, staleTime, responseDataKey, }: InfiniteQueryParams) => {
24
+ export declare const useGlobalInfiniteQuery: <T>({ url, queryKey, method, data, enabled, staleTime, responseDataKey, }: InfiniteQueryParams) => {
25
25
  refetchQuery: () => Promise<void>;
26
26
  queryData: any[];
27
27
  isLoading: boolean;
@@ -32,5 +32,5 @@ declare const useGlobalInfiniteQuery: <T>({ url, queryKey, method, data, enabled
32
32
  totalCount: number;
33
33
  otherData: any;
34
34
  };
35
- export default useGlobalInfiniteQuery;
35
+ export {};
36
36
  //# sourceMappingURL=useGlobalInfiniteQuery.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useGlobalInfiniteQuery.d.ts","sourceRoot":"","sources":["../../src/hooks/useGlobalInfiniteQuery.ts"],"names":[],"mappings":"AAIA,UAAU,mBAAmB;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACzB,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,C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G,GAAG,CAAC;IACzB,UAAU,CAAC,EAAE;QACT,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,UAAU,EAAE,MAAM,CAAC;KACtB,CAAC;IACF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,sBAAsB,GAAI,CAAC,yEAQrC,mBAAmB;;;;;;;;;;CAqFrB,CAAC"}
@@ -1,33 +1,28 @@
1
- interface UseGlobalMutationParams<T> {
1
+ export interface MutationParams {
2
2
  url: string;
3
- queriesToInvalidate?: string[];
3
+ queriesToInvalidate?: any[];
4
4
  method: "GET" | "POST" | "PUT" | "DELETE";
5
- data?: T;
5
+ data?: Record<string, any>;
6
6
  isFormData?: boolean;
7
- onSuccess?: (data: any) => void;
8
7
  excludedIndexKeys?: string[];
8
+ onMutationSuccess?: (data: any) => void;
9
9
  }
10
- interface MutationFunctionParams<T> {
10
+ interface MutationPayload {
11
11
  isPriorityDataAvailable?: boolean;
12
- priorityData?: T;
12
+ priorityData?: any;
13
13
  url?: string;
14
14
  }
15
- /**
16
- * Custom hook for handling global mutations with React Query.
17
- * @param {string} url - The endpoint URL for the mutation.
18
- * @param {Array<string>} queriesToInvalidate - Array of query keys to invalidate after successful mutation.
19
- * @param {string} methodType - The HTTP method type (POST, PUT, DELETE).
20
- * @param {Object} [data] - The default data to be sent with the mutation.
21
- * @param {boolean} [isFormData] - Whether the data should be processed as FormData.
22
- * @param {Function} [closePopup] - Optional callback to close a popup after successful mutation.
23
- * @param {Array<string>} [excludedIndexKeys] - Keys for which array indices should not be included in FormData.
24
- * @returns {Object} An object containing mutation-related functions and state.
25
- */
26
- export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate, method, data, isFormData, onSuccess: onMutationSuccess, excludedIndexKeys, }: UseGlobalMutationParams<T>) => {
27
- runMutation: (params?: MutationFunctionParams<T>) => void;
15
+ export declare const useGlobalMutation: ({ url, queriesToInvalidate, method, data, isFormData, excludedIndexKeys, onMutationSuccess, }: MutationParams) => {
16
+ runMutation: ({ isPriorityDataAvailable, priorityData, url, }?: MutationPayload) => void;
28
17
  mutationLoading: boolean;
29
- mutationData: any;
30
- mutationError: string | undefined;
18
+ mutationData: {
19
+ data: any;
20
+ } | undefined;
21
+ mutationError: {
22
+ type: string;
23
+ message: string;
24
+ errorType: string | undefined;
25
+ } | undefined;
31
26
  isMutationSucceeded: boolean;
32
27
  };
33
28
  export {};
@@ -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,4GAQ5D,uBAAuB,CAAC,CAAC,CAAC;2BA8FK,sBAAsB,CAAC,CAAC,CAAC;;;;;CAe1D,CAAC"}
1
+ {"version":3,"file":"useGlobalMutation.d.ts","sourceRoot":"","sources":["../../src/hooks/useGlobalMutation.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,cAAc;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,mBAAmB,CAAC,EAAE,GAAG,EAAE,CAAC;IAC5B,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,iBAAiB,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;CAC3C;AAED,UAAU,eAAe;IACrB,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,YAAY,CAAC,EAAE,GAAG,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB;AAMD,eAAO,MAAM,iBAAiB,kGAQ3B,cAAc;oEA6JV,eAAe;;;;;;;;;;;CAyBrB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { useGlobalQuery } from './hooks/useGlobalQuery';
2
2
  export { useGlobalMutation } from './hooks/useGlobalMutation';
3
- export * from './hooks/useGlobalInfiniteQuery';
3
+ export { useGlobalInfiniteQuery } from './hooks/useGlobalInfiniteQuery';
4
4
  export { useCookie } from './hooks/useCookie';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,cAAc,gCAAgC,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC"}
@@ -1,174 +1,302 @@
1
- import { useQueryClient as $, useQuery as v, useMutation as M } from "@tanstack/react-query";
2
- import { useCookies as N } from "react-cookie";
3
- import k from "axios";
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
- switch (o) {
1
+ import { useQueryClient as q, useQuery as j, useMutation as b, useInfiniteQuery as z } from "@tanstack/react-query";
2
+ import { useCookies as B } from "react-cookie";
3
+ import P from "axios";
4
+ const S = ({ cookieName: n }) => {
5
+ const [c, a, w] = B([n]);
6
+ return { cookie: c, setCookie: a, removeCookie: w };
7
+ }, W = async ({ action: n, url: c, data: a, headers: w, baseURL: l }) => {
8
+ const d = l ? P.create({ baseURL: l }) : P;
9
+ if (l && d !== P) {
10
+ const g = l.endsWith("/") && c.startsWith("/") ? `${l}${c.slice(1)}` : `${l}${c}`, o = { headers: w };
11
+ switch (n) {
12
+ case "GET":
13
+ return await P.get(g, o);
14
+ case "POST":
15
+ return await P.post(g, a, o);
16
+ case "PUT":
17
+ return await P.put(g, a, o);
18
+ case "DELETE":
19
+ return await P.delete(g, { ...o, data: a });
20
+ default:
21
+ throw new Error(`Invalid action: ${n}`);
22
+ }
23
+ }
24
+ const f = { headers: w };
25
+ switch (n) {
10
26
  case "GET":
11
- return await k.get(a, { headers: n });
27
+ return await d.get(c, f);
12
28
  case "POST":
13
- return await k.post(a, e, { headers: n });
29
+ return await d.post(c, a, f);
14
30
  case "PUT":
15
- return await k.put(a, e, { headers: n });
31
+ return await d.put(c, a, f);
16
32
  case "DELETE":
17
- return await k.delete(a, { data: e, headers: n });
33
+ return await d.delete(c, { ...f, data: a });
18
34
  default:
19
- throw new Error(`Invalid action: ${o}`);
35
+ throw new Error(`Invalid action: ${n}`);
20
36
  }
21
- }, Q = async ({ action: o, url: a, data: e, headers: d }) => {
22
- var n, m, E, y, i, g, r;
37
+ }, F = async ({ action: n, url: c, data: a, headers: w, baseURL: l }) => {
38
+ var d, f, g, o, E, t, x, s, m, i;
23
39
  try {
24
- const s = await j({ action: o, url: a, data: e, headers: d });
25
- return s.status >= 200 && s.status <= 299 ? {
40
+ const r = await W({
41
+ action: n,
42
+ url: c,
43
+ data: a,
44
+ headers: w,
45
+ baseURL: l
46
+ });
47
+ return r.status >= 200 && r.status <= 299 ? {
26
48
  status: !0,
27
- data: s.data,
28
- message: s.data.message,
29
- statusCode: s.status
49
+ data: r.data,
50
+ message: r.data.message || "Success",
51
+ statusCode: r.status
30
52
  } : {
31
53
  status: !1,
32
54
  error: "API Failed",
33
55
  message: "API Failed",
34
- statusCode: s.status
56
+ statusCode: r.status
35
57
  };
36
- } catch (s) {
37
- if (k.isAxiosError(s)) {
38
- const t = s;
39
- return t.message === "Network Error" ? {
40
- status: !1,
41
- error: "Network Error",
42
- message: "Network Error"
43
- } : {
44
- status: !1,
45
- type: (m = (n = t.response) == null ? void 0 : n.data) == null ? void 0 : m.type,
46
- message: ((y = (E = t.response) == null ? void 0 : E.data) == null ? void 0 : y.message) || "API Failed",
47
- error: ((g = (i = t.response) == null ? void 0 : i.data) == null ? void 0 : g.error) || "API Failed",
48
- statusCode: (r = t.response) == null ? void 0 : r.status
49
- };
50
- }
51
- return {
58
+ } catch (r) {
59
+ return P.isAxiosError(r) ? ((d = r.response) == null || d.status, r.message === "Network Error" ? {
60
+ status: !1,
61
+ error: "Network Error",
62
+ message: "Network Error"
63
+ } : {
64
+ status: !1,
65
+ type: (g = (f = r.response) == null ? void 0 : f.data) == null ? void 0 : g.type,
66
+ message: ((E = (o = r.response) == null ? void 0 : o.data) == null ? void 0 : E.message) || "An error occurred",
67
+ error: ((x = (t = r.response) == null ? void 0 : t.data) == null ? void 0 : x.error) || "Unknown error",
68
+ statusCode: (s = r.response) == null ? void 0 : s.status,
69
+ errorType: (i = (m = r.response) == null ? void 0 : m.data) == null ? void 0 : i.errorType
70
+ }) : {
52
71
  status: !1,
53
72
  error: "API Failed",
54
- message: "API Failed"
73
+ message: "An unexpected error occurred"
55
74
  };
56
75
  }
57
- }, z = ({
58
- url: o,
59
- baseUrl: a,
60
- queryKey: e,
61
- method: d,
62
- data: n,
63
- enabled: m = !0,
64
- staleTime: E = 5 * 60 * 1e3,
65
- refetchOnWindowFocus: y = !1,
66
- select: i
76
+ }, V = ({
77
+ url: n,
78
+ baseUrl: c,
79
+ queryKey: a,
80
+ method: w,
81
+ data: l,
82
+ enabled: d = !0,
83
+ staleTime: f = 5 * 60 * 1e3,
84
+ refetchOnWindowFocus: g = !1,
85
+ select: o
67
86
  }) => {
68
- const g = $(), { cookie: r } = b({ cookieName: "accessToken" });
69
- let s = {};
70
- r != null && r.accessToken && (s = { Authorization: `Bearer ${r.accessToken}` });
71
- const t = v({
72
- queryKey: e,
87
+ const E = q(), { cookie: t } = S({ cookieName: "accessToken" });
88
+ let x = {};
89
+ t != null && t.accessToken && (x = { Authorization: `Bearer ${t.accessToken}` });
90
+ const s = j({
91
+ queryKey: a,
73
92
  queryFn: async () => {
74
93
  try {
75
- const u = await Q({
76
- action: d,
77
- url: o,
78
- data: n,
79
- headers: s
94
+ const i = await F({
95
+ action: w,
96
+ url: n,
97
+ data: l,
98
+ headers: x
80
99
  });
81
- return u != null && u.data ? u.data : { totalCount: 0, data: [] };
82
- } catch (u) {
83
- throw console.error("Query Error:", u), u;
100
+ return i != null && i.data ? i.data : { totalCount: 0, data: [] };
101
+ } catch (i) {
102
+ throw console.error("Query Error:", i), i;
84
103
  }
85
104
  },
86
- enabled: m,
87
- staleTime: E,
88
- refetchOnWindowFocus: y,
89
- select: i
105
+ enabled: d,
106
+ staleTime: f,
107
+ refetchOnWindowFocus: g,
108
+ select: o
90
109
  });
91
110
  return {
92
111
  refetchQuery: () => {
93
- g.invalidateQueries({ queryKey: e });
112
+ E.invalidateQueries({ queryKey: a });
94
113
  },
95
- queryData: t.data,
96
- isLoading: t.isLoading,
97
- isError: t.isError,
98
- error: t.error
114
+ queryData: s.data,
115
+ isLoading: s.isLoading,
116
+ isError: s.isError,
117
+ error: s.error
99
118
  };
100
- }, B = ({
101
- url: o,
102
- queriesToInvalidate: a,
103
- method: e,
104
- data: d,
105
- isFormData: n,
106
- onSuccess: m,
107
- excludedIndexKeys: E
119
+ }, X = ({
120
+ url: n,
121
+ queriesToInvalidate: c,
122
+ method: a,
123
+ data: w = {},
124
+ isFormData: l = !1,
125
+ excludedIndexKeys: d = [],
126
+ onMutationSuccess: f
108
127
  }) => {
109
- var t;
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
- if (n) {
117
- const F = new FormData(), A = (w, c, h = "", I = {}) => {
118
- if (c == null || c == null) return;
119
- const { excludedIndexKeys: P = [] } = I;
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
- };
127
- Object.keys(p || {}).forEach((w) => {
128
- A(F, p == null ? void 0 : p[w], w, { excludedIndexKeys: E });
128
+ const g = q(), { cookie: o } = S({ cookieName: "accessToken" });
129
+ let E = {};
130
+ o != null && o.accessToken && (E = { Authorization: `Bearer ${o == null ? void 0 : o.accessToken}` });
131
+ const t = b({
132
+ mutationFn: async ({
133
+ isPriorityDataAvailable: s,
134
+ priorityData: m,
135
+ url: i = n
136
+ }) => {
137
+ const r = s ? m : w;
138
+ if (l) {
139
+ let $ = new FormData();
140
+ const C = (h, e, u = "", k = {}) => {
141
+ if (e == null) return;
142
+ const { excludedIndexKeys: T = [] } = k;
143
+ Array.isArray(e) ? e.forEach((p, y) => {
144
+ const M = T.includes(u) ? u : u ? `${u}[${y}]` : `${y}`;
145
+ p instanceof File ? h.append(u, p) : typeof p == "object" && p !== null ? C(h, p, M, k) : h.append(M, p);
146
+ }) : typeof e == "object" && e !== null ? e instanceof File ? h.append(u, e) : Object.keys(e).forEach((p) => {
147
+ const y = e[p], I = u ? `${u}[${p}]` : p;
148
+ C(
149
+ h,
150
+ y,
151
+ I,
152
+ k
153
+ );
154
+ }) : e !== "" && e !== void 0 && e !== null && h.append(u, e);
155
+ }, v = { excludedIndexKeys: d };
156
+ Object.keys(r).forEach((h) => {
157
+ C($, r[h], h, v);
129
158
  });
130
- const { status: C, message: q, data: x } = await Q({
131
- action: e,
132
- url: o,
133
- data: F,
134
- headers: g
159
+ const {
160
+ status: A,
161
+ message: D,
162
+ data: Q
163
+ } = await F({
164
+ action: a,
165
+ url: n,
166
+ data: $,
167
+ headers: E
135
168
  });
136
- if (C)
137
- return x;
138
- throw new Error(q || "Something went wrong!");
169
+ if (A)
170
+ return {
171
+ data: Q
172
+ };
173
+ throw new Error(
174
+ D || "Something went wrong!"
175
+ );
139
176
  } else {
140
- const { status: F, message: A, data: C } = await Q({
141
- action: e,
142
- url: o,
143
- data: p,
144
- headers: g
177
+ const {
178
+ status: $,
179
+ message: C,
180
+ data: v
181
+ } = await F({
182
+ action: a,
183
+ url: i,
184
+ data: r,
185
+ headers: E
145
186
  });
146
- if (F)
147
- return C;
148
- throw new Error(A || "Something went wrong!");
187
+ if ($)
188
+ return {
189
+ data: v
190
+ };
191
+ throw new Error(
192
+ C || "Something went wrong!"
193
+ );
149
194
  }
150
195
  },
151
- onSuccess: (f) => {
152
- y.invalidateQueries({ queryKey: a }), m && m(f);
196
+ onSuccess: ({
197
+ data: s
198
+ }) => {
199
+ c && g.invalidateQueries({
200
+ queryKey: c
201
+ }), f && f(s);
153
202
  },
154
- onError: (f) => (console.error("mutationError", f.message), f.message)
203
+ onError: (s) => (console.error("Mutation Error:", s), {
204
+ type: "Error",
205
+ message: (s == null ? void 0 : s.message) || "Something went wrong!",
206
+ errorType: s == null ? void 0 : s.errorType
207
+ })
155
208
  });
156
209
  return {
157
- runMutation: (f) => {
210
+ runMutation: ({
211
+ isPriorityDataAvailable: s,
212
+ priorityData: m,
213
+ url: i
214
+ } = {}) => {
158
215
  try {
159
- r.mutate(f || {});
216
+ t.mutate({
217
+ isPriorityDataAvailable: s,
218
+ priorityData: m,
219
+ url: i
220
+ });
221
+ } catch (r) {
222
+ console.error("Mutation Execution Error:", r);
223
+ }
224
+ },
225
+ mutationLoading: t.isPending,
226
+ mutationData: t.data,
227
+ mutationError: t.error ? {
228
+ type: "Error",
229
+ message: t.error.message,
230
+ errorType: t.error.errorType
231
+ } : void 0,
232
+ isMutationSucceeded: t.isSuccess
233
+ };
234
+ }, Y = ({
235
+ url: n,
236
+ queryKey: c,
237
+ method: a,
238
+ data: w,
239
+ enabled: l = !0,
240
+ staleTime: d = 5 * 60 * 1e3,
241
+ // Default: 5 minutes
242
+ responseDataKey: f = "data"
243
+ }) => {
244
+ var r, $, C, v, A, D, Q, h;
245
+ const g = q(), { cookie: o } = S({ cookieName: "accessToken" }), E = o != null && o.accessToken ? { Authorization: `Bearer ${o.accessToken}` } : void 0, t = z({
246
+ queryKey: c,
247
+ initialPageParam: 1,
248
+ // @ts-ignore
249
+ queryFn: async ({ pageParam: e = 1 }) => {
250
+ try {
251
+ let u = "";
252
+ a === "GET" && (u = n != null && n.includes("?") ? `&page=${e}` : `?page=${e}`);
253
+ const k = await F({
254
+ action: a,
255
+ url: `${n}${u}`,
256
+ data: { ...w, page: e },
257
+ headers: E
258
+ });
259
+ if (k.status && k.data) {
260
+ const T = k.data, p = T[f] || [], y = T.pagination, I = T.totalCount, M = T.limit, N = T.page, G = T.totalPages, { data: H, ...O } = T;
261
+ return y ? {
262
+ data: p,
263
+ nextPage: y.page < y.totalPages ? y.page + 1 : void 0,
264
+ hasMore: y.page < y.totalPages,
265
+ totalCount: y.totalCount,
266
+ otherData: O
267
+ } : {
268
+ data: p,
269
+ nextPage: N < G ? N + 1 : void 0,
270
+ hasMore: N < G,
271
+ totalCount: I,
272
+ otherData: O
273
+ };
274
+ }
275
+ return { totalCount: 0, data: [], hasMore: !1 };
160
276
  } catch (u) {
161
- console.error("Mutation Error: ", u);
277
+ throw console.error("Query Error:", u), u;
162
278
  }
163
279
  },
164
- mutationLoading: r.isPending,
165
- mutationData: r.data,
166
- mutationError: (t = r.error) == null ? void 0 : t.message,
167
- isMutationSucceeded: r.isSuccess
280
+ getNextPageParam: (e) => e != null && e.hasMore ? e.nextPage : void 0,
281
+ enabled: l,
282
+ staleTime: d,
283
+ refetchOnWindowFocus: !1
284
+ }), x = () => g.invalidateQueries({ queryKey: c }), s = (($ = (r = t.data) == null ? void 0 : r.pages) == null ? void 0 : $.flatMap((e) => e == null ? void 0 : e.data)) ?? [], m = ((A = (v = (C = t.data) == null ? void 0 : C.pages) == null ? void 0 : v[0]) == null ? void 0 : A.totalCount) ?? 0, i = ((h = (Q = (D = t.data) == null ? void 0 : D.pages) == null ? void 0 : Q[0]) == null ? void 0 : h.otherData) ?? {};
285
+ return {
286
+ refetchQuery: x,
287
+ queryData: s,
288
+ isLoading: t.isLoading,
289
+ isError: t.isError,
290
+ error: t.error,
291
+ fetchNextPage: t.fetchNextPage,
292
+ hasNextPage: t.hasNextPage,
293
+ totalCount: m,
294
+ otherData: i
168
295
  };
169
296
  };
170
297
  export {
171
- b as useCookie,
172
- B as useGlobalMutation,
173
- z as useGlobalQuery
298
+ S as useCookie,
299
+ Y as useGlobalInfiniteQuery,
300
+ X as useGlobalMutation,
301
+ V as useGlobalQuery
174
302
  };
@@ -1 +1 @@
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"})});
1
+ (function(p,T){typeof exports=="object"&&typeof module<"u"?T(exports,require("@tanstack/react-query"),require("react-cookie"),require("axios")):typeof define=="function"&&define.amd?define(["exports","@tanstack/react-query","react-cookie","axios"],T):(p=typeof globalThis<"u"?globalThis:p||self,T(p.QueryHarbor={},p.ReactQuery,p.ReactCookie,p.axios))})(this,function(p,T,O,P){"use strict";const D=({cookieName:n})=>{const[i,a,w]=O.useCookies([n]);return{cookie:i,setCookie:a,removeCookie:w}},z=async({action:n,url:i,data:a,headers:w,baseURL:l})=>{const d=l?P.create({baseURL:l}):P;if(l&&d!==P){const g=l.endsWith("/")&&i.startsWith("/")?`${l}${i.slice(1)}`:`${l}${i}`,o={headers:w};switch(n){case"GET":return await P.get(g,o);case"POST":return await P.post(g,a,o);case"PUT":return await P.put(g,a,o);case"DELETE":return await P.delete(g,{...o,data:a});default:throw new Error(`Invalid action: ${n}`)}}const f={headers:w};switch(n){case"GET":return await d.get(i,f);case"POST":return await d.post(i,a,f);case"PUT":return await d.put(i,a,f);case"DELETE":return await d.delete(i,{...f,data:a});default:throw new Error(`Invalid action: ${n}`)}},I=async({action:n,url:i,data:a,headers:w,baseURL:l})=>{var d,f,g,o,m,t,$,s,C,c;try{const r=await z({action:n,url:i,data:a,headers:w,baseURL:l});return r.status>=200&&r.status<=299?{status:!0,data:r.data,message:r.data.message||"Success",statusCode:r.status}:{status:!1,error:"API Failed",message:"API Failed",statusCode:r.status}}catch(r){return P.isAxiosError(r)?((d=r.response)==null||d.status,r.message==="Network Error"?{status:!1,error:"Network Error",message:"Network Error"}:{status:!1,type:(g=(f=r.response)==null?void 0:f.data)==null?void 0:g.type,message:((m=(o=r.response)==null?void 0:o.data)==null?void 0:m.message)||"An error occurred",error:(($=(t=r.response)==null?void 0:t.data)==null?void 0:$.error)||"Unknown error",statusCode:(s=r.response)==null?void 0:s.status,errorType:(c=(C=r.response)==null?void 0:C.data)==null?void 0:c.errorType}):{status:!1,error:"API Failed",message:"An unexpected error occurred"}}},B=({url:n,baseUrl:i,queryKey:a,method:w,data:l,enabled:d=!0,staleTime:f=3e5,refetchOnWindowFocus:g=!1,select:o})=>{const m=T.useQueryClient(),{cookie:t}=D({cookieName:"accessToken"});let $={};t!=null&&t.accessToken&&($={Authorization:`Bearer ${t.accessToken}`});const s=T.useQuery({queryKey:a,queryFn:async()=>{try{const c=await I({action:w,url:n,data:l,headers:$});return c!=null&&c.data?c.data:{totalCount:0,data:[]}}catch(c){throw console.error("Query Error:",c),c}},enabled:d,staleTime:f,refetchOnWindowFocus:g,select:o});return{refetchQuery:()=>{m.invalidateQueries({queryKey:a})},queryData:s.data,isLoading:s.isLoading,isError:s.isError,error:s.error}},H=({url:n,queriesToInvalidate:i,method:a,data:w={},isFormData:l=!1,excludedIndexKeys:d=[],onMutationSuccess:f})=>{const g=T.useQueryClient(),{cookie:o}=D({cookieName:"accessToken"});let m={};o!=null&&o.accessToken&&(m={Authorization:`Bearer ${o==null?void 0:o.accessToken}`});const t=T.useMutation({mutationFn:async({isPriorityDataAvailable:s,priorityData:C,url:c=n})=>{const r=s?C:w;if(l){let x=new FormData;const v=(h,e,u="",Q={})=>{if(e==null)return;const{excludedIndexKeys:k=[]}=Q;Array.isArray(e)?e.forEach((y,E)=>{const S=k.includes(u)?u:u?`${u}[${E}]`:`${E}`;y instanceof File?h.append(u,y):typeof y=="object"&&y!==null?v(h,y,S,Q):h.append(S,y)}):typeof e=="object"&&e!==null?e instanceof File?h.append(u,e):Object.keys(e).forEach(y=>{const E=e[y],N=u?`${u}[${y}]`:y;v(h,E,N,Q)}):e!==""&&e!==void 0&&e!==null&&h.append(u,e)},q={excludedIndexKeys:d};Object.keys(r).forEach(h=>{v(x,r[h],h,q)});const{status:A,message:M,data:F}=await I({action:a,url:n,data:x,headers:m});if(A)return{data:F};throw new Error(M||"Something went wrong!")}else{const{status:x,message:v,data:q}=await I({action:a,url:c,data:r,headers:m});if(x)return{data:q};throw new Error(v||"Something went wrong!")}},onSuccess:({data:s})=>{i&&g.invalidateQueries({queryKey:i}),f&&f(s)},onError:s=>(console.error("Mutation Error:",s),{type:"Error",message:(s==null?void 0:s.message)||"Something went wrong!",errorType:s==null?void 0:s.errorType})});return{runMutation:({isPriorityDataAvailable:s,priorityData:C,url:c}={})=>{try{t.mutate({isPriorityDataAvailable:s,priorityData:C,url:c})}catch(r){console.error("Mutation Execution Error:",r)}},mutationLoading:t.isPending,mutationData:t.data,mutationError:t.error?{type:"Error",message:t.error.message,errorType:t.error.errorType}:void 0,isMutationSucceeded:t.isSuccess}},W=({url:n,queryKey:i,method:a,data:w,enabled:l=!0,staleTime:d=3e5,responseDataKey:f="data"})=>{var r,x,v,q,A,M,F,h;const g=T.useQueryClient(),{cookie:o}=D({cookieName:"accessToken"}),m=o!=null&&o.accessToken?{Authorization:`Bearer ${o.accessToken}`}:void 0,t=T.useInfiniteQuery({queryKey:i,initialPageParam:1,queryFn:async({pageParam:e=1})=>{try{let u="";a==="GET"&&(u=n!=null&&n.includes("?")?`&page=${e}`:`?page=${e}`);const Q=await I({action:a,url:`${n}${u}`,data:{...w,page:e},headers:m});if(Q.status&&Q.data){const k=Q.data,y=k[f]||[],E=k.pagination,N=k.totalCount,S=k.limit,G=k.page,b=k.totalPages,{data:L,...j}=k;return E?{data:y,nextPage:E.page<E.totalPages?E.page+1:void 0,hasMore:E.page<E.totalPages,totalCount:E.totalCount,otherData:j}:{data:y,nextPage:G<b?G+1:void 0,hasMore:G<b,totalCount:N,otherData:j}}return{totalCount:0,data:[],hasMore:!1}}catch(u){throw console.error("Query Error:",u),u}},getNextPageParam:e=>e!=null&&e.hasMore?e.nextPage:void 0,enabled:l,staleTime:d,refetchOnWindowFocus:!1}),$=()=>g.invalidateQueries({queryKey:i}),s=((x=(r=t.data)==null?void 0:r.pages)==null?void 0:x.flatMap(e=>e==null?void 0:e.data))??[],C=((A=(q=(v=t.data)==null?void 0:v.pages)==null?void 0:q[0])==null?void 0:A.totalCount)??0,c=((h=(F=(M=t.data)==null?void 0:M.pages)==null?void 0:F[0])==null?void 0:h.otherData)??{};return{refetchQuery:$,queryData:s,isLoading:t.isLoading,isError:t.isError,error:t.error,fetchNextPage:t.fetchNextPage,hasNextPage:t.hasNextPage,totalCount:C,otherData:c}};p.useCookie=D,p.useGlobalInfiniteQuery=W,p.useGlobalMutation=H,p.useGlobalQuery=B,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-harbor",
3
- "version": "0.0.10",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"