query-harbor 0.0.9 → 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.
- package/dist/hooks/apiHandler.d.ts +14 -18
- package/dist/hooks/apiHandler.d.ts.map +1 -1
- package/dist/hooks/useGlobalInfiniteQuery.d.ts +2 -2
- package/dist/hooks/useGlobalInfiniteQuery.d.ts.map +1 -1
- package/dist/hooks/useGlobalMutation.d.ts +16 -21
- package/dist/hooks/useGlobalMutation.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/query-harbor.es.js +255 -127
- package/dist/query-harbor.umd.js +1 -1
- package/package.json +1 -1
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
import { AxiosResponse } from 'axios';
|
|
2
|
-
|
|
3
|
-
interface
|
|
4
|
-
action:
|
|
2
|
+
type ApiAction = "GET" | "POST" | "PUT" | "DELETE";
|
|
3
|
+
interface ApiRequestParams {
|
|
4
|
+
action: ApiAction;
|
|
5
5
|
url: string;
|
|
6
|
-
data?: any
|
|
7
|
-
headers?:
|
|
6
|
+
data?: Record<string, any>;
|
|
7
|
+
headers?: any;
|
|
8
|
+
baseURL?: string;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
status:
|
|
11
|
-
data
|
|
10
|
+
interface ApiResponse<T = any> {
|
|
11
|
+
status: boolean;
|
|
12
|
+
data?: T;
|
|
12
13
|
message?: string;
|
|
13
|
-
|
|
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
|
-
|
|
23
|
-
export declare const
|
|
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,
|
|
1
|
+
{"version":3,"file":"apiHandler.d.ts","sourceRoot":"","sources":["../../src/hooks/apiHandler.ts"],"names":[],"mappings":"AAAA,OAAc,EAAsB,aAAa,EAAE,MAAM,OAAO,CAAC;AAEjE,KAAK,SAAS,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEnD,UAAU,gBAAgB;IACtB,MAAM,EAAE,SAAS,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,WAAW,CAAC,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
|
|
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,CAAC;IAC1C,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,UAAU,WAAW,CAAC,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,EAAE,GAAG,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,
|
|
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,CAAC;IAC1C,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,UAAU,WAAW,CAAC,CAAC;IACnB,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,EAAE,GAAG,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
|
|
1
|
+
export interface MutationParams {
|
|
2
2
|
url: string;
|
|
3
|
-
queriesToInvalidate?:
|
|
3
|
+
queriesToInvalidate?: any[];
|
|
4
4
|
method: "GET" | "POST" | "PUT" | "DELETE";
|
|
5
|
-
data?:
|
|
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
|
|
10
|
+
interface MutationPayload {
|
|
11
11
|
isPriorityDataAvailable?: boolean;
|
|
12
|
-
priorityData?:
|
|
12
|
+
priorityData?: any;
|
|
13
13
|
url?: string;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
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, 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:
|
|
30
|
-
|
|
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,
|
|
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
|
|
3
|
+
export { useGlobalInfiniteQuery } from './hooks/useGlobalInfiniteQuery';
|
|
4
4
|
export { useCookie } from './hooks/useCookie';
|
|
5
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,
|
|
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"}
|
package/dist/query-harbor.es.js
CHANGED
|
@@ -1,174 +1,302 @@
|
|
|
1
|
-
import { useQueryClient as
|
|
2
|
-
import { useCookies as
|
|
3
|
-
import
|
|
4
|
-
const S = ({ cookieName:
|
|
5
|
-
const [
|
|
6
|
-
return { cookie:
|
|
7
|
-
},
|
|
8
|
-
const
|
|
9
|
-
|
|
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
|
|
27
|
+
return await d.get(c, f);
|
|
12
28
|
case "POST":
|
|
13
|
-
return await
|
|
29
|
+
return await d.post(c, a, f);
|
|
14
30
|
case "PUT":
|
|
15
|
-
return await
|
|
31
|
+
return await d.put(c, a, f);
|
|
16
32
|
case "DELETE":
|
|
17
|
-
return await
|
|
33
|
+
return await d.delete(c, { ...f, data: a });
|
|
18
34
|
default:
|
|
19
|
-
throw new Error(`Invalid action: ${
|
|
35
|
+
throw new Error(`Invalid action: ${n}`);
|
|
20
36
|
}
|
|
21
|
-
},
|
|
22
|
-
var
|
|
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
|
|
25
|
-
|
|
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:
|
|
28
|
-
message:
|
|
29
|
-
statusCode:
|
|
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:
|
|
56
|
+
statusCode: r.status
|
|
35
57
|
};
|
|
36
|
-
} catch (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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: "
|
|
73
|
+
message: "An unexpected error occurred"
|
|
55
74
|
};
|
|
56
75
|
}
|
|
57
|
-
},
|
|
58
|
-
url:
|
|
59
|
-
baseUrl:
|
|
60
|
-
queryKey:
|
|
61
|
-
method:
|
|
62
|
-
data:
|
|
63
|
-
enabled:
|
|
64
|
-
staleTime:
|
|
65
|
-
refetchOnWindowFocus:
|
|
66
|
-
select:
|
|
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
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
queryKey:
|
|
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
|
|
76
|
-
action:
|
|
77
|
-
url:
|
|
78
|
-
data:
|
|
79
|
-
headers:
|
|
94
|
+
const i = await F({
|
|
95
|
+
action: w,
|
|
96
|
+
url: n,
|
|
97
|
+
data: l,
|
|
98
|
+
headers: x
|
|
80
99
|
});
|
|
81
|
-
return
|
|
82
|
-
} catch (
|
|
83
|
-
throw console.error("Query Error:",
|
|
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:
|
|
87
|
-
staleTime:
|
|
88
|
-
refetchOnWindowFocus:
|
|
89
|
-
select:
|
|
105
|
+
enabled: d,
|
|
106
|
+
staleTime: f,
|
|
107
|
+
refetchOnWindowFocus: g,
|
|
108
|
+
select: o
|
|
90
109
|
});
|
|
91
110
|
return {
|
|
92
111
|
refetchQuery: () => {
|
|
93
|
-
|
|
112
|
+
E.invalidateQueries({ queryKey: a });
|
|
94
113
|
},
|
|
95
|
-
queryData:
|
|
96
|
-
isLoading:
|
|
97
|
-
isError:
|
|
98
|
-
error:
|
|
114
|
+
queryData: s.data,
|
|
115
|
+
isLoading: s.isLoading,
|
|
116
|
+
isError: s.isError,
|
|
117
|
+
error: s.error
|
|
99
118
|
};
|
|
100
|
-
},
|
|
101
|
-
url:
|
|
102
|
-
queriesToInvalidate:
|
|
103
|
-
method:
|
|
104
|
-
data:
|
|
105
|
-
isFormData:
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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 {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
data:
|
|
134
|
-
|
|
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 (
|
|
137
|
-
return
|
|
138
|
-
|
|
169
|
+
if (A)
|
|
170
|
+
return {
|
|
171
|
+
data: Q
|
|
172
|
+
};
|
|
173
|
+
throw new Error(
|
|
174
|
+
D || "Something went wrong!"
|
|
175
|
+
);
|
|
139
176
|
} else {
|
|
140
|
-
const {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
data:
|
|
144
|
-
|
|
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 (
|
|
147
|
-
return
|
|
148
|
-
|
|
187
|
+
if ($)
|
|
188
|
+
return {
|
|
189
|
+
data: v
|
|
190
|
+
};
|
|
191
|
+
throw new Error(
|
|
192
|
+
C || "Something went wrong!"
|
|
193
|
+
);
|
|
149
194
|
}
|
|
150
195
|
},
|
|
151
|
-
onSuccess: (
|
|
152
|
-
|
|
196
|
+
onSuccess: ({
|
|
197
|
+
data: s
|
|
198
|
+
}) => {
|
|
199
|
+
c && g.invalidateQueries({
|
|
200
|
+
queryKey: c
|
|
201
|
+
}), f && f(s);
|
|
153
202
|
},
|
|
154
|
-
onError: (
|
|
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: (
|
|
210
|
+
runMutation: ({
|
|
211
|
+
isPriorityDataAvailable: s,
|
|
212
|
+
priorityData: m,
|
|
213
|
+
url: i
|
|
214
|
+
} = {}) => {
|
|
158
215
|
try {
|
|
159
|
-
|
|
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("
|
|
277
|
+
throw console.error("Query Error:", u), u;
|
|
162
278
|
}
|
|
163
279
|
},
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
298
|
S as useCookie,
|
|
172
|
-
|
|
173
|
-
|
|
299
|
+
Y as useGlobalInfiniteQuery,
|
|
300
|
+
X as useGlobalMutation,
|
|
301
|
+
V as useGlobalQuery
|
|
174
302
|
};
|
package/dist/query-harbor.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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"})});
|