query-harbor 0.0.7 → 0.0.9
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 +4 -2
- package/dist/hooks/apiHandler.d.ts.map +1 -0
- package/dist/hooks/useCookie.d.ts +1 -0
- package/dist/hooks/useCookie.d.ts.map +1 -0
- package/dist/hooks/useGlobalInfiniteQuery.d.ts +25 -17
- package/dist/hooks/useGlobalInfiniteQuery.d.ts.map +1 -0
- package/dist/hooks/useGlobalMutation.d.ts +6 -4
- package/dist/hooks/useGlobalMutation.d.ts.map +1 -0
- package/dist/hooks/useGlobalQuery.d.ts +5 -2
- package/dist/hooks/useGlobalQuery.d.ts.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/query-harbor.es.js +114 -168
- package/dist/query-harbor.umd.js +1 -1
- package/package.json +5 -4
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
export type ApiHeaders = Record<string, string | number | boolean>;
|
|
2
3
|
interface ActionHandlerParams {
|
|
3
4
|
action: "GET" | "POST" | "PUT" | "DELETE";
|
|
4
5
|
url: string;
|
|
5
6
|
data?: any;
|
|
6
|
-
headers?:
|
|
7
|
+
headers?: ApiHeaders;
|
|
7
8
|
}
|
|
8
9
|
export interface ApiSuccessResponse {
|
|
9
10
|
status: true;
|
|
@@ -22,3 +23,4 @@ type ApiResponse = ApiSuccessResponse | ApiErrorResponse;
|
|
|
22
23
|
export declare const ActionHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<AxiosResponse>;
|
|
23
24
|
export declare const APIHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<ApiResponse>;
|
|
24
25
|
export {};
|
|
26
|
+
//# sourceMappingURL=apiHandler.d.ts.map
|
|
@@ -0,0 +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,GAAG,OAAO,CAAC,CAAC;AAGnE,UAAU,mBAAmB;IACzB,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,UAAU,CAAC;CACxB;AAGD,MAAM,WAAW,kBAAkB;IAC/B,MAAM,EAAE,IAAI,CAAC;IACb,IAAI,EAAE,GAAG,CAAC;IACV,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACtB;AAED,UAAU,gBAAgB;IACtB,MAAM,EAAE,KAAK,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,KAAK,WAAW,GAAG,kBAAkB,GAAG,gBAAgB,CAAC;AAGzD,eAAO,MAAM,aAAa,mCAA0C,mBAAmB,KAAG,OAAO,CAAC,aAAa,CAkB9G,CAAC;AAGF,eAAO,MAAM,UAAU,mCAA0C,mBAAmB,KAAG,OAAO,CAAC,WAAW,CA8CzG,CAAC"}
|
|
@@ -22,3 +22,4 @@ export declare const useCookie: ({ cookieName }: {
|
|
|
22
22
|
setCookie: (name: string, value: import('universal-cookie').Cookie, options?: import('universal-cookie').CookieSetOptions) => void;
|
|
23
23
|
removeCookie: (name: string, options?: import('universal-cookie').CookieSetOptions) => void;
|
|
24
24
|
};
|
|
25
|
+
//# sourceMappingURL=useCookie.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCookie.d.ts","sourceRoot":"","sources":["../../src/hooks/useCookie.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AAEH,eAAO,MAAM,SAAS,mBAAoB;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE;;;;;;CAI/D,CAAC"}
|
|
@@ -1,28 +1,36 @@
|
|
|
1
|
-
interface
|
|
1
|
+
interface InfiniteQueryParams {
|
|
2
2
|
url: string;
|
|
3
|
-
queryKey: string[];
|
|
4
|
-
|
|
5
|
-
data?:
|
|
3
|
+
queryKey: string[] | any;
|
|
4
|
+
method: "GET" | "POST" | "PUT" | "DELETE";
|
|
5
|
+
data?: any;
|
|
6
6
|
enabled?: boolean;
|
|
7
|
+
cacheTime?: number;
|
|
7
8
|
staleTime?: number;
|
|
9
|
+
responseDataKey?: string;
|
|
8
10
|
}
|
|
9
|
-
interface
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface ApiResponse<T> {
|
|
12
|
+
[key: string]: T[] | any;
|
|
13
|
+
pagination?: {
|
|
14
|
+
totalCount: number;
|
|
15
|
+
page: number;
|
|
16
|
+
limit: number;
|
|
17
|
+
totalPages: number;
|
|
18
|
+
};
|
|
19
|
+
totalCount?: number;
|
|
20
|
+
limit?: number;
|
|
21
|
+
page?: number;
|
|
22
|
+
totalPages?: number;
|
|
14
23
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
18
|
-
export declare const useGlobalInfiniteQuery: <T>({ url, queryKey, methodType, data, enabled, staleTime, }: UseGlobalInfiniteQueryParams<T>) => {
|
|
19
|
-
refetchQuery: () => void;
|
|
24
|
+
declare const useGlobalInfiniteQuery: <T>({ url, queryKey, method, data, enabled, staleTime, responseDataKey, }: InfiniteQueryParams) => {
|
|
25
|
+
refetchQuery: () => Promise<void>;
|
|
20
26
|
queryData: any[];
|
|
21
27
|
isLoading: boolean;
|
|
22
28
|
isError: boolean;
|
|
23
|
-
error: Error;
|
|
24
|
-
fetchNextPage: (options?: import('@tanstack/react-query').FetchNextPageOptions) => Promise<import('@tanstack/react-query').InfiniteQueryObserverResult<import('@tanstack/react-query').InfiniteData<
|
|
29
|
+
error: Error | null;
|
|
30
|
+
fetchNextPage: (options?: import('@tanstack/react-query').FetchNextPageOptions) => Promise<import('@tanstack/react-query').InfiniteQueryObserverResult<import('@tanstack/react-query').InfiniteData<ApiResponse<T>, unknown>, Error>>;
|
|
25
31
|
hasNextPage: boolean;
|
|
26
32
|
totalCount: number;
|
|
33
|
+
otherData: any;
|
|
27
34
|
};
|
|
28
|
-
export
|
|
35
|
+
export default useGlobalInfiniteQuery;
|
|
36
|
+
//# sourceMappingURL=useGlobalInfiniteQuery.d.ts.map
|
|
@@ -0,0 +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,QAAA,MAAM,sBAAsB,GAAI,CAAC,yEAQ9B,mBAAmB;;;;;;;;;;CAqFrB,CAAC;AAEF,eAAe,sBAAsB,CAAC"}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
interface UseGlobalMutationParams<T> {
|
|
2
2
|
url: string;
|
|
3
3
|
queriesToInvalidate?: string[];
|
|
4
|
-
|
|
4
|
+
method: "GET" | "POST" | "PUT" | "DELETE";
|
|
5
5
|
data?: T;
|
|
6
6
|
isFormData?: boolean;
|
|
7
|
-
|
|
7
|
+
onSuccess?: (data: any) => void;
|
|
8
8
|
excludedIndexKeys?: string[];
|
|
9
9
|
}
|
|
10
10
|
interface MutationFunctionParams<T> {
|
|
11
11
|
isPriorityDataAvailable?: boolean;
|
|
12
12
|
priorityData?: T;
|
|
13
|
+
url?: string;
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
16
|
* Custom hook for handling global mutations with React Query.
|
|
@@ -22,11 +23,12 @@ interface MutationFunctionParams<T> {
|
|
|
22
23
|
* @param {Array<string>} [excludedIndexKeys] - Keys for which array indices should not be included in FormData.
|
|
23
24
|
* @returns {Object} An object containing mutation-related functions and state.
|
|
24
25
|
*/
|
|
25
|
-
export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate,
|
|
26
|
+
export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate, method, data, isFormData, onSuccess, excludedIndexKeys, }: UseGlobalMutationParams<T>) => {
|
|
26
27
|
runMutation: (params?: MutationFunctionParams<T>) => void;
|
|
27
28
|
mutationLoading: boolean;
|
|
28
29
|
mutationData: any;
|
|
29
|
-
mutationError: string;
|
|
30
|
+
mutationError: string | undefined;
|
|
30
31
|
isMutationSucceeded: boolean;
|
|
31
32
|
};
|
|
32
33
|
export {};
|
|
34
|
+
//# sourceMappingURL=useGlobalMutation.d.ts.map
|
|
@@ -0,0 +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,yFAQ5D,uBAAuB,CAAC,CAAC,CAAC;2BA8FK,sBAAsB,CAAC,CAAC,CAAC;;;;;CAe1D,CAAC"}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { QueryKey } from '@tanstack/react-query';
|
|
2
2
|
interface UseGlobalQueryParams {
|
|
3
3
|
url: string;
|
|
4
|
+
baseUrl?: string;
|
|
4
5
|
queryKey: QueryKey;
|
|
5
|
-
|
|
6
|
+
method: "GET" | "POST" | "PUT" | "DELETE";
|
|
6
7
|
data?: any;
|
|
7
8
|
enabled?: boolean;
|
|
8
9
|
staleTime?: number;
|
|
9
10
|
refetchOnWindowFocus?: boolean;
|
|
11
|
+
select?: (data: any) => any;
|
|
10
12
|
}
|
|
11
13
|
interface UseGlobalQueryReturn {
|
|
12
14
|
refetchQuery: () => void;
|
|
@@ -54,5 +56,6 @@ interface UseGlobalQueryReturn {
|
|
|
54
56
|
* );
|
|
55
57
|
*
|
|
56
58
|
*/
|
|
57
|
-
export declare const useGlobalQuery: ({ url, queryKey,
|
|
59
|
+
export declare const useGlobalQuery: ({ url, baseUrl, queryKey, method, data, enabled, staleTime, refetchOnWindowFocus, select }: UseGlobalQueryParams) => UseGlobalQueryReturn;
|
|
58
60
|
export {};
|
|
61
|
+
//# sourceMappingURL=useGlobalQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useGlobalQuery.d.ts","sourceRoot":"","sources":["../../src/hooks/useGlobalQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4C,QAAQ,EAAmB,MAAM,uBAAuB,CAAC;AAK5G,UAAU,oBAAoB;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;IACnB,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,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,CAAC;CAC/B;AAGD,UAAU,oBAAoB;IAC1B,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,SAAS,EAAE,GAAG,CAAC;IACf,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,eAAO,MAAM,cAAc,+FAUxB,oBAAoB,KAAG,oBAiDzB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { useGlobalQuery } from './hooks/useGlobalQuery';
|
|
2
2
|
export { useGlobalMutation } from './hooks/useGlobalMutation';
|
|
3
|
-
export
|
|
3
|
+
export * from './hooks/useGlobalInfiniteQuery';
|
|
4
4
|
export { useCookie } from './hooks/useCookie';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/query-harbor.es.js
CHANGED
|
@@ -1,50 +1,51 @@
|
|
|
1
|
-
import { useQueryClient as
|
|
2
|
-
import { useCookies as
|
|
3
|
-
import
|
|
4
|
-
const
|
|
5
|
-
const [
|
|
6
|
-
return { cookie:
|
|
7
|
-
},
|
|
8
|
-
|
|
1
|
+
import { useQueryClient as b, useQuery as M, useMutation as v } from "@tanstack/react-query";
|
|
2
|
+
import { useCookies as N } from "react-cookie";
|
|
3
|
+
import k from "axios";
|
|
4
|
+
const S = ({ cookieName: o }) => {
|
|
5
|
+
const [a, e, l] = N([o]);
|
|
6
|
+
return { cookie: a, setCookie: e, removeCookie: l };
|
|
7
|
+
}, j = async ({ action: o, url: a, data: e, headers: l }) => {
|
|
8
|
+
const n = l;
|
|
9
|
+
switch (o) {
|
|
9
10
|
case "GET":
|
|
10
|
-
return await
|
|
11
|
+
return await k.get(a, { headers: n });
|
|
11
12
|
case "POST":
|
|
12
|
-
return await
|
|
13
|
+
return await k.post(a, e, { headers: n });
|
|
13
14
|
case "PUT":
|
|
14
|
-
return await
|
|
15
|
+
return await k.put(a, e, { headers: n });
|
|
15
16
|
case "DELETE":
|
|
16
|
-
return await
|
|
17
|
+
return await k.delete(a, { data: e, headers: n });
|
|
17
18
|
default:
|
|
18
|
-
throw new Error(`Invalid action: ${
|
|
19
|
+
throw new Error(`Invalid action: ${o}`);
|
|
19
20
|
}
|
|
20
|
-
},
|
|
21
|
-
var
|
|
21
|
+
}, Q = async ({ action: o, url: a, data: e, headers: l }) => {
|
|
22
|
+
var n, h, E, y, i, w, r;
|
|
22
23
|
try {
|
|
23
|
-
const
|
|
24
|
-
return
|
|
24
|
+
const s = await j({ action: o, url: a, data: e, headers: l });
|
|
25
|
+
return s.status >= 200 && s.status <= 299 ? {
|
|
25
26
|
status: !0,
|
|
26
|
-
data:
|
|
27
|
-
message:
|
|
28
|
-
statusCode:
|
|
27
|
+
data: s.data,
|
|
28
|
+
message: s.data.message,
|
|
29
|
+
statusCode: s.status
|
|
29
30
|
} : {
|
|
30
31
|
status: !1,
|
|
31
32
|
error: "API Failed",
|
|
32
33
|
message: "API Failed",
|
|
33
|
-
statusCode:
|
|
34
|
+
statusCode: s.status
|
|
34
35
|
};
|
|
35
|
-
} catch (
|
|
36
|
-
if (
|
|
37
|
-
const t =
|
|
36
|
+
} catch (s) {
|
|
37
|
+
if (k.isAxiosError(s)) {
|
|
38
|
+
const t = s;
|
|
38
39
|
return t.message === "Network Error" ? {
|
|
39
40
|
status: !1,
|
|
40
41
|
error: "Network Error",
|
|
41
42
|
message: "Network Error"
|
|
42
43
|
} : {
|
|
43
44
|
status: !1,
|
|
44
|
-
type: (
|
|
45
|
-
message: ((
|
|
46
|
-
error: ((
|
|
47
|
-
statusCode: (
|
|
45
|
+
type: (h = (n = t.response) == null ? void 0 : n.data) == null ? void 0 : h.type,
|
|
46
|
+
message: ((y = (E = t.response) == null ? void 0 : E.data) == null ? void 0 : y.message) || "API Failed",
|
|
47
|
+
error: ((w = (i = t.response) == null ? void 0 : i.data) == null ? void 0 : w.error) || "API Failed",
|
|
48
|
+
statusCode: (r = t.response) == null ? void 0 : r.status
|
|
48
49
|
};
|
|
49
50
|
}
|
|
50
51
|
return {
|
|
@@ -53,176 +54,121 @@ const M = ({ cookieName: i }) => {
|
|
|
53
54
|
message: "API Failed"
|
|
54
55
|
};
|
|
55
56
|
}
|
|
56
|
-
},
|
|
57
|
-
url:
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
}, z = ({
|
|
58
|
+
url: o,
|
|
59
|
+
baseUrl: a,
|
|
60
|
+
queryKey: e,
|
|
61
|
+
method: l,
|
|
60
62
|
data: n,
|
|
61
|
-
enabled:
|
|
62
|
-
staleTime:
|
|
63
|
-
refetchOnWindowFocus:
|
|
63
|
+
enabled: h = !0,
|
|
64
|
+
staleTime: E = 5 * 60 * 1e3,
|
|
65
|
+
refetchOnWindowFocus: y = !1,
|
|
66
|
+
select: i
|
|
64
67
|
}) => {
|
|
65
|
-
const
|
|
66
|
-
let
|
|
67
|
-
r != null && r.accessToken && (
|
|
68
|
-
const
|
|
69
|
-
queryKey:
|
|
68
|
+
const w = b(), { cookie: r } = S({ cookieName: "accessToken" });
|
|
69
|
+
let s = {};
|
|
70
|
+
r != null && r.accessToken && (s = { Authorization: `Bearer ${r.accessToken}` });
|
|
71
|
+
const t = M({
|
|
72
|
+
queryKey: e,
|
|
70
73
|
queryFn: async () => {
|
|
71
74
|
try {
|
|
72
|
-
const
|
|
73
|
-
action:
|
|
74
|
-
url:
|
|
75
|
+
const u = await Q({
|
|
76
|
+
action: l,
|
|
77
|
+
url: o,
|
|
75
78
|
data: n,
|
|
76
|
-
headers:
|
|
79
|
+
headers: s
|
|
77
80
|
});
|
|
78
|
-
return
|
|
79
|
-
} catch (
|
|
80
|
-
throw console.error("Query Error:",
|
|
81
|
+
return u != null && u.data ? u.data : { totalCount: 0, data: [] };
|
|
82
|
+
} catch (u) {
|
|
83
|
+
throw console.error("Query Error:", u), u;
|
|
81
84
|
}
|
|
82
85
|
},
|
|
83
|
-
enabled:
|
|
84
|
-
staleTime:
|
|
85
|
-
refetchOnWindowFocus:
|
|
86
|
+
enabled: h,
|
|
87
|
+
staleTime: E,
|
|
88
|
+
refetchOnWindowFocus: y,
|
|
89
|
+
select: i
|
|
86
90
|
});
|
|
87
91
|
return {
|
|
88
92
|
refetchQuery: () => {
|
|
89
|
-
|
|
93
|
+
w.invalidateQueries({ queryKey: e });
|
|
90
94
|
},
|
|
91
|
-
queryData:
|
|
92
|
-
isLoading:
|
|
93
|
-
isError:
|
|
94
|
-
error:
|
|
95
|
+
queryData: t.data,
|
|
96
|
+
isLoading: t.isLoading,
|
|
97
|
+
isError: t.isError,
|
|
98
|
+
error: t.error
|
|
95
99
|
};
|
|
96
|
-
},
|
|
97
|
-
url:
|
|
98
|
-
queriesToInvalidate:
|
|
99
|
-
|
|
100
|
-
data:
|
|
101
|
-
isFormData:
|
|
102
|
-
|
|
103
|
-
excludedIndexKeys:
|
|
100
|
+
}, B = ({
|
|
101
|
+
url: o,
|
|
102
|
+
queriesToInvalidate: a,
|
|
103
|
+
method: e,
|
|
104
|
+
data: l,
|
|
105
|
+
isFormData: n,
|
|
106
|
+
onSuccess: h,
|
|
107
|
+
excludedIndexKeys: E
|
|
104
108
|
}) => {
|
|
105
109
|
var t;
|
|
106
|
-
const
|
|
107
|
-
let
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
mutationFn: async ({ isPriorityDataAvailable:
|
|
111
|
-
const
|
|
112
|
-
if (
|
|
113
|
-
const
|
|
114
|
-
if (
|
|
115
|
-
const { excludedIndexKeys:
|
|
116
|
-
Array.isArray(
|
|
117
|
-
const T =
|
|
118
|
-
|
|
119
|
-
}) : typeof
|
|
120
|
-
|
|
121
|
-
}) :
|
|
110
|
+
const y = b(), { cookie: i } = S({ cookieName: "accessToken" });
|
|
111
|
+
let w = {};
|
|
112
|
+
i != null && i.accessToken && (w = { Authorization: `Bearer ${i == null ? void 0 : i.accessToken}` });
|
|
113
|
+
const r = v({
|
|
114
|
+
mutationFn: async ({ isPriorityDataAvailable: m, priorityData: u, url: L = o }) => {
|
|
115
|
+
const p = m ? u : l;
|
|
116
|
+
if (n) {
|
|
117
|
+
const F = new FormData(), A = (f, c, g = "", I = {}) => {
|
|
118
|
+
if (c == null || c == null) return;
|
|
119
|
+
const { excludedIndexKeys: P = [] } = I;
|
|
120
|
+
Array.isArray(c) ? c.forEach((d, $) => {
|
|
121
|
+
const T = (P == null ? void 0 : P.includes(g)) ? g : g ? `${g}[${$}]` : `${$}`;
|
|
122
|
+
d instanceof File ? f.append(T, d) : typeof d == "object" && d !== null ? A(f, d, T, I) : f.append(T, String(d));
|
|
123
|
+
}) : typeof c == "object" && c !== null ? c instanceof File ? f.append(g, c) : Object.keys(c).forEach((d) => {
|
|
124
|
+
A(f, c[d], g ? `${g}[${d}]` : d, I);
|
|
125
|
+
}) : f.append(g, String(c));
|
|
122
126
|
};
|
|
123
|
-
Object.keys(
|
|
124
|
-
|
|
127
|
+
Object.keys(p || {}).forEach((f) => {
|
|
128
|
+
A(F, p == null ? void 0 : p[f], f, { excludedIndexKeys: E });
|
|
125
129
|
});
|
|
126
|
-
const { status:
|
|
127
|
-
action:
|
|
128
|
-
url:
|
|
129
|
-
data:
|
|
130
|
-
headers:
|
|
130
|
+
const { status: C, message: q, data: x } = await Q({
|
|
131
|
+
action: e,
|
|
132
|
+
url: o,
|
|
133
|
+
data: F,
|
|
134
|
+
headers: w
|
|
131
135
|
});
|
|
132
|
-
if (
|
|
133
|
-
return
|
|
134
|
-
throw new Error(
|
|
136
|
+
if (C)
|
|
137
|
+
return x;
|
|
138
|
+
throw new Error(q || "Something went wrong!");
|
|
135
139
|
} else {
|
|
136
|
-
const { status:
|
|
137
|
-
action:
|
|
138
|
-
url:
|
|
139
|
-
data:
|
|
140
|
-
headers:
|
|
140
|
+
const { status: F, message: A, data: C } = await Q({
|
|
141
|
+
action: e,
|
|
142
|
+
url: o,
|
|
143
|
+
data: p,
|
|
144
|
+
headers: w
|
|
141
145
|
});
|
|
142
|
-
if (
|
|
143
|
-
return
|
|
144
|
-
throw new Error(
|
|
146
|
+
if (F)
|
|
147
|
+
return C;
|
|
148
|
+
throw new Error(A || "Something went wrong!");
|
|
145
149
|
}
|
|
146
150
|
},
|
|
147
151
|
onSuccess: () => {
|
|
148
|
-
|
|
152
|
+
y.invalidateQueries({ queryKey: a }), h && h(l);
|
|
149
153
|
},
|
|
150
|
-
onError: (
|
|
154
|
+
onError: (m) => (console.error("mutationError", m.message), m.message)
|
|
151
155
|
});
|
|
152
156
|
return {
|
|
153
|
-
runMutation: (
|
|
157
|
+
runMutation: (m) => {
|
|
154
158
|
try {
|
|
155
|
-
|
|
156
|
-
} catch (
|
|
157
|
-
console.error("Mutation Error: ",
|
|
159
|
+
r.mutate(m || {});
|
|
160
|
+
} catch (u) {
|
|
161
|
+
console.error("Mutation Error: ", u);
|
|
158
162
|
}
|
|
159
163
|
},
|
|
160
|
-
mutationLoading:
|
|
161
|
-
mutationData:
|
|
162
|
-
mutationError: (t =
|
|
163
|
-
isMutationSucceeded:
|
|
164
|
-
};
|
|
165
|
-
}, W = ({
|
|
166
|
-
url: i,
|
|
167
|
-
queryKey: s,
|
|
168
|
-
methodType: o,
|
|
169
|
-
data: n,
|
|
170
|
-
enabled: y = !0,
|
|
171
|
-
staleTime: m = 5 * 60 * 1e3
|
|
172
|
-
}) => {
|
|
173
|
-
var w, k, C, x, p;
|
|
174
|
-
const P = N(), { cookie: d } = M({ cookieName: "accessToken" });
|
|
175
|
-
let r = {};
|
|
176
|
-
d != null && d.accessToken && (r = { Authorization: `Bearer ${d.accessToken}` });
|
|
177
|
-
const e = L({
|
|
178
|
-
queryKey: s,
|
|
179
|
-
queryFn: async ({ pageParam: f = 1 }) => {
|
|
180
|
-
try {
|
|
181
|
-
const E = await I({
|
|
182
|
-
action: o,
|
|
183
|
-
url: i,
|
|
184
|
-
data: {
|
|
185
|
-
...n,
|
|
186
|
-
page: f
|
|
187
|
-
},
|
|
188
|
-
headers: r
|
|
189
|
-
});
|
|
190
|
-
if (E != null && E.data) {
|
|
191
|
-
const { data: Q, page: u, totalPages: l, totalCount: g } = E.data, A = u < l;
|
|
192
|
-
return {
|
|
193
|
-
data: Q,
|
|
194
|
-
nextPage: A ? u + 1 : void 0,
|
|
195
|
-
hasMore: A,
|
|
196
|
-
totalCount: g
|
|
197
|
-
};
|
|
198
|
-
} else
|
|
199
|
-
return { totalCount: 0, data: [], hasMore: !1 };
|
|
200
|
-
} catch (E) {
|
|
201
|
-
throw console.error("Query Error:", E), E;
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
initialPageParam: 1,
|
|
205
|
-
getNextPageParam: (f) => f.hasMore ? f.nextPage : void 0,
|
|
206
|
-
enabled: y,
|
|
207
|
-
staleTime: m,
|
|
208
|
-
refetchOnWindowFocus: !1
|
|
209
|
-
}), a = () => {
|
|
210
|
-
P.invalidateQueries({ queryKey: s });
|
|
211
|
-
}, c = ((k = (w = e == null ? void 0 : e.data) == null ? void 0 : w.pages) == null ? void 0 : k.flatMap((f) => f.data)) || [], t = ((p = (x = (C = e == null ? void 0 : e.data) == null ? void 0 : C.pages) == null ? void 0 : x[0]) == null ? void 0 : p.totalCount) ?? 0;
|
|
212
|
-
return {
|
|
213
|
-
refetchQuery: a,
|
|
214
|
-
queryData: c,
|
|
215
|
-
isLoading: e.isLoading,
|
|
216
|
-
isError: e.isError,
|
|
217
|
-
error: e.error,
|
|
218
|
-
fetchNextPage: e.fetchNextPage,
|
|
219
|
-
hasNextPage: e.hasNextPage,
|
|
220
|
-
totalCount: t
|
|
164
|
+
mutationLoading: r.isPending,
|
|
165
|
+
mutationData: r.data,
|
|
166
|
+
mutationError: (t = r.error) == null ? void 0 : t.message,
|
|
167
|
+
isMutationSucceeded: r.isSuccess
|
|
221
168
|
};
|
|
222
169
|
};
|
|
223
170
|
export {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
B as useGlobalQuery
|
|
171
|
+
S as useCookie,
|
|
172
|
+
B as useGlobalMutation,
|
|
173
|
+
z as useGlobalQuery
|
|
228
174
|
};
|
package/dist/query-harbor.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("@tanstack/react-query"),require("react-cookie"),require("axios")):typeof define=="function"&&define.amd?define(["exports","@tanstack/react-query","react-cookie","axios"],w):(n=typeof globalThis<"u"?globalThis:n||self,w(n.QueryHarbor={},n.ReactQuery,n.ReactCookie,n.axios))})(this,function(n,w,$,C){"use strict";const q=({cookieName:o})=>{const[a,e,d]=$.useCookies([o]);return{cookie:a,setCookie:e,removeCookie:d}},j=async({action:o,url:a,data:e,headers:d})=>{const u=d;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:d})=>{var u,E,k,p,i,y,r;try{const s=await j({action:o,url:a,data:e,headers:d});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:(E=(u=t.response)==null?void 0:u.data)==null?void 0:E.type,message:((p=(k=t.response)==null?void 0:k.data)==null?void 0:p.message)||"API Failed",error:((y=(i=t.response)==null?void 0:i.data)==null?void 0:y.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:d,data:u,enabled:E=!0,staleTime:k=3e5,refetchOnWindowFocus:p=!1,select:i})=>{const y=w.useQueryClient(),{cookie:r}=q({cookieName:"accessToken"});let s={};r!=null&&r.accessToken&&(s={Authorization:`Bearer ${r.accessToken}`});const t=w.useQuery({queryKey:e,queryFn:async()=>{try{const c=await P({action:d,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:E,staleTime:k,refetchOnWindowFocus:p,select:i});return{refetchQuery:()=>{y.invalidateQueries({queryKey:e})},queryData:t.data,isLoading:t.isLoading,isError:t.isError,error:t.error}},G=({url:o,queriesToInvalidate:a,method:e,data:d,isFormData:u,onSuccess:E,excludedIndexKeys:k})=>{var t;const p=w.useQueryClient(),{cookie:i}=q({cookieName:"accessToken"});let y={};i!=null&&i.accessToken&&(y={Authorization:`Bearer ${i==null?void 0:i.accessToken}`});const r=w.useMutation({mutationFn:async({isPriorityDataAvailable:m,priorityData:c,url:L=o})=>{const A=m?c:d;if(u){const F=new FormData,T=(h,l,g="",I={})=>{if(l==null||l==null)return;const{excludedIndexKeys:Q=[]}=I;Array.isArray(l)?l.forEach((f,M)=>{const S=(Q==null?void 0:Q.includes(g))?g:g?`${g}[${M}]`:`${M}`;f instanceof File?h.append(S,f):typeof f=="object"&&f!==null?T(h,f,S,I):h.append(S,String(f))}):typeof l=="object"&&l!==null?l instanceof File?h.append(g,l):Object.keys(l).forEach(f=>{T(h,l[f],g?`${g}[${f}]`:f,I)}):h.append(g,String(l))};Object.keys(A||{}).forEach(h=>{T(F,A==null?void 0:A[h],h,{excludedIndexKeys:k})});const{status:b,message:N,data:H}=await P({action:e,url:o,data:F,headers:y});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:y});if(F)return b;throw new Error(T||"Something went wrong!")}},onSuccess:()=>{p.invalidateQueries({queryKey:a}),E&&E(d)},onError:m=>(console.error("mutationError",m.message),m.message)});return{runMutation:m=>{try{r.mutate(m||{})}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"})});
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-harbor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
7
7
|
],
|
|
8
|
-
"main": "
|
|
9
|
-
"module": "
|
|
10
|
-
"types": "
|
|
8
|
+
"main": "dist/query-harbor.umd.js",
|
|
9
|
+
"module": "dist/query-harbor.es.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
11
|
"keywords": [
|
|
12
12
|
"react",
|
|
13
13
|
"react-query",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
32
33
|
"import": "./dist/query-harbor.es.js",
|
|
33
34
|
"require": "./dist/query-harbor.umd.js"
|
|
34
35
|
}
|