query-harbor 0.0.5 → 0.0.6
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.
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AxiosRequestHeaders, AxiosResponse } from 'axios';
|
|
2
|
+
interface ActionHandlerParams {
|
|
3
|
+
action: "GET" | "POST" | "PUT" | "DELETE";
|
|
4
|
+
url: string;
|
|
5
|
+
data?: any;
|
|
6
|
+
headers?: AxiosRequestHeaders;
|
|
7
|
+
}
|
|
8
|
+
export interface ApiSuccessResponse {
|
|
9
|
+
status: true;
|
|
10
|
+
data: any;
|
|
11
|
+
message?: string;
|
|
12
|
+
statusCode: number;
|
|
13
|
+
}
|
|
14
|
+
interface ApiErrorResponse {
|
|
15
|
+
status: false;
|
|
16
|
+
error: string;
|
|
17
|
+
message: string;
|
|
18
|
+
statusCode?: number;
|
|
19
|
+
type?: string;
|
|
20
|
+
}
|
|
21
|
+
type ApiResponse = ApiSuccessResponse | ApiErrorResponse;
|
|
22
|
+
export declare const ActionHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<AxiosResponse>;
|
|
23
|
+
export declare const APIHandler: ({ action, url, data, headers }: ActionHandlerParams) => Promise<ApiResponse>;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @example
|
|
4
|
+
* // Usage example:
|
|
5
|
+
* const { cookie, setCookie, removeCookie } = useCookie({ cookieName: "accessToken" });
|
|
6
|
+
*
|
|
7
|
+
* // Get cookie value
|
|
8
|
+
* const token = cookie.accessToken;
|
|
9
|
+
*
|
|
10
|
+
* // Set new cookie
|
|
11
|
+
* setCookie("accessToken", "new-token-value", { path: "/", maxAge: 3600 });
|
|
12
|
+
*
|
|
13
|
+
* // Remove cookie
|
|
14
|
+
* removeCookie("accessToken", { path: "/" });
|
|
15
|
+
*/
|
|
16
|
+
export declare const useCookie: ({ cookieName }: {
|
|
17
|
+
cookieName: string;
|
|
18
|
+
}) => {
|
|
19
|
+
cookie: {
|
|
20
|
+
[x: string]: any;
|
|
21
|
+
};
|
|
22
|
+
setCookie: (name: string, value: import('universal-cookie').Cookie, options?: import('universal-cookie').CookieSetOptions) => void;
|
|
23
|
+
removeCookie: (name: string, options?: import('universal-cookie').CookieSetOptions) => void;
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface UseGlobalInfiniteQueryParams<T> {
|
|
2
|
+
url: string;
|
|
3
|
+
queryKey: string[];
|
|
4
|
+
methodType: "GET" | "POST" | "PUT" | "DELETE";
|
|
5
|
+
data?: T;
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
staleTime?: number;
|
|
8
|
+
}
|
|
9
|
+
interface QueryResult<T> {
|
|
10
|
+
data: T[];
|
|
11
|
+
nextPage?: number;
|
|
12
|
+
hasMore: boolean;
|
|
13
|
+
totalCount: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Custom hook for handling infinite scrolling queries with React Query.
|
|
17
|
+
*/
|
|
18
|
+
export declare const useGlobalInfiniteQuery: <T>({ url, queryKey, methodType, data, enabled, staleTime, }: UseGlobalInfiniteQueryParams<T>) => {
|
|
19
|
+
refetchQuery: () => void;
|
|
20
|
+
queryData: any[];
|
|
21
|
+
isLoading: boolean;
|
|
22
|
+
isError: boolean;
|
|
23
|
+
error: Error;
|
|
24
|
+
fetchNextPage: (options?: import('@tanstack/react-query').FetchNextPageOptions) => Promise<import('@tanstack/react-query').InfiniteQueryObserverResult<import('@tanstack/react-query').InfiniteData<QueryResult<T>, unknown>, Error>>;
|
|
25
|
+
hasNextPage: boolean;
|
|
26
|
+
totalCount: number;
|
|
27
|
+
};
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface UseGlobalMutationParams<T> {
|
|
2
|
+
url: string;
|
|
3
|
+
queriesToInvalidate?: string[];
|
|
4
|
+
methodType: "POST" | "PUT" | "DELETE";
|
|
5
|
+
data?: T;
|
|
6
|
+
isFormData?: boolean;
|
|
7
|
+
closePopup?: (state: boolean) => void;
|
|
8
|
+
excludedIndexKeys?: string[];
|
|
9
|
+
}
|
|
10
|
+
interface MutationFunctionParams<T> {
|
|
11
|
+
isPriorityDataAvailable?: boolean;
|
|
12
|
+
priorityData?: T;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Custom hook for handling global mutations with React Query.
|
|
16
|
+
* @param {string} url - The endpoint URL for the mutation.
|
|
17
|
+
* @param {Array<string>} queriesToInvalidate - Array of query keys to invalidate after successful mutation.
|
|
18
|
+
* @param {string} methodType - The HTTP method type (POST, PUT, DELETE).
|
|
19
|
+
* @param {Object} [data] - The default data to be sent with the mutation.
|
|
20
|
+
* @param {boolean} [isFormData] - Whether the data should be processed as FormData.
|
|
21
|
+
* @param {Function} [closePopup] - Optional callback to close a popup after successful mutation.
|
|
22
|
+
* @param {Array<string>} [excludedIndexKeys] - Keys for which array indices should not be included in FormData.
|
|
23
|
+
* @returns {Object} An object containing mutation-related functions and state.
|
|
24
|
+
*/
|
|
25
|
+
export declare const useGlobalMutation: <T extends Record<string, any>>({ url, queriesToInvalidate, methodType, data, isFormData, closePopup, excludedIndexKeys, }: UseGlobalMutationParams<T>) => {
|
|
26
|
+
runMutation: (params?: MutationFunctionParams<T>) => void;
|
|
27
|
+
mutationLoading: boolean;
|
|
28
|
+
mutationData: any;
|
|
29
|
+
mutationError: string;
|
|
30
|
+
isMutationSucceeded: boolean;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { QueryKey } from '@tanstack/react-query';
|
|
2
|
+
interface UseGlobalQueryParams {
|
|
3
|
+
url: string;
|
|
4
|
+
queryKey: QueryKey;
|
|
5
|
+
methodType: "GET" | "POST" | "PUT" | "DELETE";
|
|
6
|
+
data?: any;
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
staleTime?: number;
|
|
9
|
+
refetchOnWindowFocus?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface UseGlobalQueryReturn {
|
|
12
|
+
refetchQuery: () => void;
|
|
13
|
+
queryData: any;
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
isError: boolean;
|
|
16
|
+
error: unknown;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Custom hook for handling global queries with React Query.
|
|
20
|
+
* @param {string} url - The endpoint URL for the query.
|
|
21
|
+
* @param {QueryKey} queryKey - The unique key for identifying and caching the query.
|
|
22
|
+
* @param {"GET" | "POST" | "PUT" | "DELETE"} methodType - The HTTP method type.
|
|
23
|
+
* @param {any} [data] - The data to be sent with the request (for POST, PUT methods).
|
|
24
|
+
* @param {boolean} [enabled=true] - Whether the query is enabled or disabled.
|
|
25
|
+
* @param {boolean} [refetchOnWindowFocus] - Refetch on window focus if the data is stale (default: false).
|
|
26
|
+
* @param {number} [staleTime] - Duration in milliseconds until the data is considered stale (default: 5 minutes).
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* Return type for the `useGlobalQuery` hook.
|
|
30
|
+
* @return {Function} refetchQuery - Function to invalidate and refetch the query.
|
|
31
|
+
* @return {any} queryData - The data returned from the query.
|
|
32
|
+
* @return {boolean} isLoading - Whether the query is currently loading.
|
|
33
|
+
* @return {boolean} isError - Whether the query encountered an error.
|
|
34
|
+
* @return {unknown} error - The error object if the query failed.
|
|
35
|
+
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Example usage:
|
|
39
|
+
* const { queryData, isLoading, isError, error, refetchQuery } = useGlobalQuery({
|
|
40
|
+
* url: "/api/data",
|
|
41
|
+
* queryKey: ["data"],
|
|
42
|
+
* methodType: "GET",
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* if (isLoading) return <div>Loading...</div>;
|
|
46
|
+
* if (isError) return <div>Error: {error.message}</div>;
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* <h1>Data:</h1>
|
|
51
|
+
* <pre>{queryData.map((data) => <p>{data}</p>)}</pre>
|
|
52
|
+
* <button onClick={refetchQuery}>Refresh Data</button>
|
|
53
|
+
* </div>
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
export declare const useGlobalQuery: ({ url, queryKey, methodType, data, enabled, staleTime, refetchOnWindowFocus }: UseGlobalQueryParams) => UseGlobalQueryReturn;
|
|
58
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "query-harbor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"files": [
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
6
8
|
"main": "./dist/query-harbor.umd.js",
|
|
7
9
|
"module": "./dist/query-harbor.es.js",
|
|
8
|
-
"keywords": [
|
|
10
|
+
"keywords": [
|
|
11
|
+
"react",
|
|
12
|
+
"react-query",
|
|
13
|
+
"hooks",
|
|
14
|
+
"tanstack",
|
|
15
|
+
"query",
|
|
16
|
+
"react-hooks",
|
|
17
|
+
"harbor",
|
|
18
|
+
"query-harbor",
|
|
19
|
+
"react-query-hooks",
|
|
20
|
+
"custom-hooks"
|
|
21
|
+
],
|
|
9
22
|
"author": "Rohit Chaware",
|
|
10
23
|
"license": "MIT",
|
|
11
24
|
"description": "A React Query utility package for efficient data fetching and caching",
|
|
@@ -42,6 +55,7 @@
|
|
|
42
55
|
"eslint-plugin-react-hooks": "^5.0.0",
|
|
43
56
|
"eslint-plugin-react-refresh": "^0.4.18",
|
|
44
57
|
"globals": "^15.14.0",
|
|
45
|
-
"vite": "^6.1.0"
|
|
58
|
+
"vite": "^6.1.0",
|
|
59
|
+
"vite-plugin-dts": "^4.5.0"
|
|
46
60
|
}
|
|
47
61
|
}
|