@ventlio/tanstack-query 0.2.86 → 0.2.89
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/config/useQueryConfig.js +27 -1
- package/dist/config/useQueryConfig.js.map +1 -1
- package/dist/config/useQueryHeaders.js +4 -15
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/config/useReactNativeEnv.js +3 -3
- package/dist/index.mjs +261 -111
- package/dist/index.mjs.map +1 -1
- package/dist/model/useKeyTrackerModel.d.ts +1 -1
- package/dist/model/useKeyTrackerModel.js +5 -1
- package/dist/model/useKeyTrackerModel.js.map +1 -1
- package/dist/queries/useDeleteRequest.d.ts +4 -4
- package/dist/queries/useDeleteRequest.js +46 -19
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +4 -4
- package/dist/queries/useGetInfiniteRequest.js +43 -20
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +8 -8
- package/dist/queries/useGetRequest.js +48 -24
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +8 -8
- package/dist/queries/usePatchRequest.js +48 -15
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +4 -4
- package/dist/queries/usePostRequest.js +42 -14
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +15 -6
- package/package.json +1 -1
- package/src/config/useQueryConfig.ts +41 -4
- package/src/config/useQueryHeaders.ts +6 -18
- package/src/config/useReactNativeEnv.ts +3 -3
- package/src/model/useKeyTrackerModel.ts +5 -1
- package/src/queries/useDeleteRequest.ts +51 -23
- package/src/queries/useGetInfiniteRequest.ts +48 -24
- package/src/queries/useGetRequest.ts +53 -29
- package/src/queries/usePatchRequest.ts +52 -18
- package/src/queries/usePostRequest.ts +46 -17
- package/src/types/index.ts +11 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -39,7 +39,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
39
39
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
40
40
|
} & {
|
|
41
41
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
42
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
42
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
43
43
|
uploadProgressPercent: number;
|
|
44
44
|
} | {
|
|
45
45
|
data: undefined;
|
|
@@ -75,7 +75,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
75
75
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
76
76
|
} & {
|
|
77
77
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
78
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
78
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
79
79
|
uploadProgressPercent: number;
|
|
80
80
|
} | {
|
|
81
81
|
data: undefined;
|
|
@@ -111,7 +111,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
111
111
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
112
112
|
} & {
|
|
113
113
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
114
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
114
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
115
115
|
uploadProgressPercent: number;
|
|
116
116
|
} | {
|
|
117
117
|
data: IRequestSuccess<TResponse>;
|
|
@@ -147,6 +147,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
147
147
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
148
148
|
} & {
|
|
149
149
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
150
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
150
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
151
151
|
uploadProgressPercent: number;
|
|
152
152
|
};
|
|
@@ -4,6 +4,7 @@ import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
|
4
4
|
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
5
5
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
6
6
|
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
7
|
+
import { useState, useEffect } from 'react';
|
|
7
8
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
8
9
|
import { useUploadProgress } from '../hooks/useUploadProgress.js';
|
|
9
10
|
import 'axios';
|
|
@@ -13,6 +14,7 @@ import { HttpMethod } from '../request/request.enum.js';
|
|
|
13
14
|
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
14
15
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
15
16
|
const config = useQueryConfig();
|
|
17
|
+
const [mutationConfig, setMutationConfig] = useState();
|
|
16
18
|
const { getHeaders } = useQueryHeaders();
|
|
17
19
|
const { isApp } = useReactNativeEnv();
|
|
18
20
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
@@ -21,7 +23,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
21
23
|
const globalHeaders = getHeaders();
|
|
22
24
|
const { data, requestConfig } = postData;
|
|
23
25
|
delete requestConfig?.body;
|
|
24
|
-
const
|
|
26
|
+
const requestOptions = {
|
|
25
27
|
path,
|
|
26
28
|
body: data,
|
|
27
29
|
method: HttpMethod.POST,
|
|
@@ -35,20 +37,33 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
35
37
|
},
|
|
36
38
|
onUploadProgress,
|
|
37
39
|
...requestConfig,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
};
|
|
41
|
+
let shouldContinue = true;
|
|
42
|
+
if (config.options.mutationMiddleware) {
|
|
43
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
44
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
45
|
+
...requestOptions,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (shouldContinue) {
|
|
49
|
+
const postResponse = await makeRequest(requestOptions);
|
|
50
|
+
if (postResponse.status) {
|
|
51
|
+
// scroll to top after success
|
|
52
|
+
if (config.options.context !== 'app') {
|
|
53
|
+
scrollToTop();
|
|
54
|
+
}
|
|
55
|
+
res(postResponse);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// scroll to top after error
|
|
59
|
+
if (config.options.context !== 'app') {
|
|
60
|
+
scrollToTop();
|
|
61
|
+
}
|
|
62
|
+
rej(postResponse);
|
|
43
63
|
}
|
|
44
|
-
res(postResponse);
|
|
45
64
|
}
|
|
46
65
|
else {
|
|
47
|
-
|
|
48
|
-
if (config.options?.context !== 'app') {
|
|
49
|
-
scrollToTop();
|
|
50
|
-
}
|
|
51
|
-
rej(postResponse);
|
|
66
|
+
rej(null);
|
|
52
67
|
}
|
|
53
68
|
};
|
|
54
69
|
// register post mutation
|
|
@@ -56,9 +71,22 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
56
71
|
mutationKey: [path, { type: 'mutation' }],
|
|
57
72
|
});
|
|
58
73
|
const post = async (data, options) => {
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
if (!config.options.pauseFutureMutations) {
|
|
75
|
+
const { requestConfig, ...otherOptions } = options ?? {};
|
|
76
|
+
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
setMutationConfig({ data, options });
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
61
82
|
};
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
85
|
+
post(mutationConfig.data, mutationConfig.options);
|
|
86
|
+
setMutationConfig(undefined);
|
|
87
|
+
}
|
|
88
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
89
|
+
}, [config.options.pauseFutureMutations]);
|
|
62
90
|
return { post, uploadProgressPercent, ...mutation };
|
|
63
91
|
};
|
|
64
92
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { QueryKey } from '@tanstack/react-query';
|
|
1
2
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
3
|
+
import type { IMakeRequest } from '../request';
|
|
2
4
|
export interface BootstrapConfig {
|
|
3
5
|
environments?: {
|
|
4
6
|
appBaseUrl: string;
|
|
@@ -6,18 +8,25 @@ export interface BootstrapConfig {
|
|
|
6
8
|
};
|
|
7
9
|
context?: ContextType;
|
|
8
10
|
modelConfig?: BootstrapModelConfig;
|
|
9
|
-
mutationMiddleware?: (
|
|
10
|
-
|
|
11
|
+
mutationMiddleware?: (mutateRequestConfig?: IMakeRequest & {
|
|
12
|
+
mutationKey: QueryKey;
|
|
13
|
+
}) => Promise<boolean>;
|
|
14
|
+
queryMiddleware?: (queryRequestConfig?: IMakeRequest & {
|
|
15
|
+
queryKey: QueryKey;
|
|
16
|
+
}) => Promise<boolean>;
|
|
17
|
+
pauseFutureMutations?: boolean;
|
|
18
|
+
pauseFutureQueries?: boolean;
|
|
19
|
+
headers?: RawAxiosRequestHeaders;
|
|
11
20
|
}
|
|
12
21
|
export interface BootstrapModelConfig {
|
|
13
22
|
idColumn: string;
|
|
14
23
|
}
|
|
15
24
|
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
16
25
|
export interface TanstackQueryConfig {
|
|
17
|
-
|
|
18
|
-
options
|
|
26
|
+
options: BootstrapConfig;
|
|
27
|
+
setConfig: (options: BootstrapConfig) => void;
|
|
19
28
|
}
|
|
20
29
|
export interface IUseQueryHeaders {
|
|
21
|
-
getHeaders: () =>
|
|
22
|
-
setQueryHeaders: (header:
|
|
30
|
+
getHeaders: () => BootstrapConfig['headers'];
|
|
31
|
+
setQueryHeaders: (header: BootstrapConfig['headers']) => void;
|
|
23
32
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,48 @@
|
|
|
1
|
+
import type { MutationMeta, QueryMeta } from '@tanstack/react-query';
|
|
1
2
|
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
-
import type { TanstackQueryConfig } from '../types';
|
|
3
|
+
import type { BootstrapConfig, TanstackQueryConfig } from '../types';
|
|
3
4
|
|
|
4
5
|
export const useQueryConfig = (): TanstackQueryConfig => {
|
|
5
6
|
const queryClient = useQueryClient();
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
-
|
|
8
|
+
const setConfig = (options: BootstrapConfig) => {
|
|
9
|
+
let mutationMeta: MutationMeta | undefined = queryClient.getMutationDefaults()?.meta ?? {};
|
|
10
|
+
let queryMeta: QueryMeta | undefined = queryClient.getQueryDefaults()?.meta ?? {};
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
const { pauseFutureMutations, pauseFutureQueries, mutationMiddleware, queryMiddleware, ...otherOptions } = options;
|
|
13
|
+
|
|
14
|
+
if (pauseFutureMutations) {
|
|
15
|
+
mutationMeta.pauseFutureMutations = pauseFutureMutations;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (mutationMiddleware) {
|
|
19
|
+
mutationMeta.mutationMiddleware = mutationMiddleware;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
mutationMeta = { meta: { ...mutationMeta, ...otherOptions } };
|
|
23
|
+
|
|
24
|
+
if (pauseFutureQueries) {
|
|
25
|
+
queryMeta.pauseFutureQueries = pauseFutureQueries;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (queryMiddleware) {
|
|
29
|
+
queryMeta.mutationMiddleware = queryMiddleware;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
queryMeta = { meta: { ...queryMeta, ...otherOptions } };
|
|
33
|
+
|
|
34
|
+
const defaultMutationOptions = queryClient.defaultMutationOptions();
|
|
35
|
+
const defaultQueryOptions = queryClient.defaultQueryOptions();
|
|
36
|
+
|
|
37
|
+
queryClient.setDefaultOptions({
|
|
38
|
+
queries: { ...defaultQueryOptions, meta: queryMeta },
|
|
39
|
+
mutations: { ...defaultMutationOptions, meta: mutationMeta },
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as BootstrapConfig;
|
|
44
|
+
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as BootstrapConfig;
|
|
45
|
+
|
|
46
|
+
const options: BootstrapConfig = { ...queryMeta, ...mutationMeta };
|
|
47
|
+
return { options, setConfig };
|
|
11
48
|
};
|
|
@@ -1,27 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { IUseQueryHeaders, TanstackQueryConfig } from '../types';
|
|
1
|
+
import type { BootstrapConfig, IUseQueryHeaders } from '../types';
|
|
3
2
|
import { useQueryConfig } from './useQueryConfig';
|
|
4
3
|
|
|
5
4
|
export const useQueryHeaders = (): IUseQueryHeaders => {
|
|
6
|
-
const
|
|
7
|
-
const { headers, options } = useQueryConfig();
|
|
5
|
+
const { setConfig, ...config } = useQueryConfig();
|
|
8
6
|
|
|
9
|
-
const getHeaders = ():
|
|
10
|
-
return headers;
|
|
7
|
+
const getHeaders = (): BootstrapConfig['headers'] => {
|
|
8
|
+
return config.options?.headers;
|
|
11
9
|
};
|
|
12
10
|
|
|
13
|
-
const setQueryHeaders = (
|
|
14
|
-
|
|
15
|
-
headers: { ...headers, ...newHeaders },
|
|
16
|
-
options,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
queryClient.setDefaultOptions({
|
|
20
|
-
queries: {
|
|
21
|
-
meta: defaultMeta,
|
|
22
|
-
},
|
|
23
|
-
mutations: { meta: defaultMeta },
|
|
24
|
-
});
|
|
11
|
+
const setQueryHeaders = (headers: BootstrapConfig['headers']) => {
|
|
12
|
+
setConfig({ headers });
|
|
25
13
|
};
|
|
26
14
|
|
|
27
15
|
return { setQueryHeaders, getHeaders };
|
|
@@ -3,9 +3,9 @@ import { useQueryConfig } from './useQueryConfig';
|
|
|
3
3
|
export const useReactNativeEnv = () => {
|
|
4
4
|
const config = useQueryConfig();
|
|
5
5
|
|
|
6
|
-
const appUrl: string | undefined = config.options
|
|
7
|
-
const appTimeout: number | undefined = config.options
|
|
8
|
-
const isApp = config.options
|
|
6
|
+
const appUrl: string | undefined = config.options.environments?.appBaseUrl;
|
|
7
|
+
const appTimeout: number | undefined = config.options.environments?.appTimeout;
|
|
8
|
+
const isApp = config.options.context === 'app';
|
|
9
9
|
|
|
10
10
|
return { appUrl, appTimeout, isApp };
|
|
11
11
|
};
|
|
@@ -4,7 +4,11 @@ export const useKeyTrackerModel = <T>(keyTracker: string) => {
|
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
|
|
6
6
|
const getQueryKey = (innerKeyTracker?: string) => {
|
|
7
|
-
const
|
|
7
|
+
const meta = {
|
|
8
|
+
...queryClient.getDefaultOptions().mutations?.meta,
|
|
9
|
+
...queryClient.getDefaultOptions().queries?.meta,
|
|
10
|
+
};
|
|
11
|
+
const queryKey: any[] | undefined = meta[innerKeyTracker ?? keyTracker] as any[];
|
|
8
12
|
|
|
9
13
|
return queryKey;
|
|
10
14
|
};
|
|
@@ -1,39 +1,53 @@
|
|
|
1
1
|
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useQuery } from '@tanstack/react-query';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
5
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
7
|
-
import {
|
|
6
|
+
import { makeRequest } from '../request';
|
|
8
7
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
9
8
|
|
|
10
9
|
export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOptions) => {
|
|
11
10
|
const { baseUrl, headers } = deleteOptions ?? {};
|
|
12
|
-
const [requestPath,
|
|
11
|
+
const [requestPath, setRequestPath] = useState<string>('');
|
|
13
12
|
const [options, setOptions] = useState<any>();
|
|
13
|
+
const [destroyConfig, setDestroyConfig] = useState<{ link: string; internalDeleteOptions: any }>();
|
|
14
|
+
|
|
15
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
14
16
|
|
|
15
17
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
16
18
|
|
|
17
19
|
const { getHeaders } = useQueryHeaders();
|
|
18
20
|
|
|
19
|
-
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, queryKey
|
|
21
|
+
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, queryKey: QueryKey) => {
|
|
20
22
|
// get request headers
|
|
21
|
-
const globalHeaders
|
|
23
|
+
const globalHeaders = getHeaders();
|
|
22
24
|
|
|
23
|
-
const [url] =
|
|
25
|
+
const [url] = queryKey;
|
|
26
|
+
const requestUrl = (url ?? requestPath) as string;
|
|
24
27
|
|
|
25
|
-
const
|
|
26
|
-
path:
|
|
28
|
+
const requestOptions = {
|
|
29
|
+
path: requestUrl,
|
|
27
30
|
headers: { ...globalHeaders, ...headers },
|
|
28
|
-
method: HttpMethod.DELETE,
|
|
29
31
|
baseURL: baseUrl ?? API_URL,
|
|
30
32
|
timeout: TIMEOUT,
|
|
31
|
-
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let shouldContinue = true;
|
|
36
|
+
|
|
37
|
+
if (queryConfigOptions.queryMiddleware) {
|
|
38
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (shouldContinue) {
|
|
42
|
+
const postResponse = await makeRequest<TResponse>(requestOptions);
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
if (postResponse.status) {
|
|
45
|
+
res(postResponse as IRequestSuccess<TResponse>);
|
|
46
|
+
} else {
|
|
47
|
+
rej(postResponse);
|
|
48
|
+
}
|
|
35
49
|
} else {
|
|
36
|
-
rej(
|
|
50
|
+
rej(null);
|
|
37
51
|
}
|
|
38
52
|
};
|
|
39
53
|
|
|
@@ -45,7 +59,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
45
59
|
);
|
|
46
60
|
|
|
47
61
|
const updatedPathAsync = async (link: string) => {
|
|
48
|
-
return
|
|
62
|
+
return setRequestPath(link);
|
|
49
63
|
};
|
|
50
64
|
|
|
51
65
|
const setOptionsAsync = async (fetchOptions: any) => {
|
|
@@ -61,15 +75,29 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
61
75
|
Array<any>
|
|
62
76
|
> & { cached?: boolean }
|
|
63
77
|
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
78
|
+
if (!queryConfigOptions.pauseFutureQueries) {
|
|
79
|
+
// set enabled to be true for every delete
|
|
80
|
+
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
81
|
+
internalDeleteOptions.enabled = true;
|
|
67
82
|
|
|
68
|
-
|
|
69
|
-
|
|
83
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
84
|
+
await updatedPathAsync(link);
|
|
70
85
|
|
|
71
|
-
|
|
86
|
+
return query.data;
|
|
87
|
+
} else {
|
|
88
|
+
// save delete config
|
|
89
|
+
setDestroyConfig({ link, internalDeleteOptions });
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
72
92
|
};
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!queryConfigOptions.pauseFutureQueries && destroyConfig) {
|
|
96
|
+
destroy(destroyConfig.link, destroyConfig.internalDeleteOptions);
|
|
97
|
+
setDestroyConfig(undefined);
|
|
98
|
+
}
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
}, [queryConfigOptions.pauseFutureQueries]);
|
|
101
|
+
|
|
102
|
+
return { destroy, ...query, isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries };
|
|
75
103
|
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { InfiniteData, UseQueryOptions } from '@tanstack/react-query';
|
|
1
|
+
import type { InfiniteData, QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
4
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
5
|
-
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
4
|
+
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
5
|
|
|
7
6
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
8
7
|
import { makeRequest } from '../request';
|
|
@@ -32,9 +31,11 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
32
31
|
} & DefaultRequestOptions) => {
|
|
33
32
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
34
33
|
const { getHeaders } = useQueryHeaders();
|
|
35
|
-
const [requestPath,
|
|
34
|
+
const [requestPath, setRequestPath] = useState<string>(path);
|
|
35
|
+
const [queryConfig, setQueryConfig] = useState<{ link: string; fetchOptions: any }>();
|
|
36
36
|
|
|
37
37
|
const [options, setOptions] = useState<any>(queryOptions);
|
|
38
|
+
const { options: queryConfigOptions, setConfig } = useQueryConfig();
|
|
38
39
|
|
|
39
40
|
let queryClient = useQueryClient();
|
|
40
41
|
|
|
@@ -49,23 +50,36 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
49
50
|
| PromiseLike<IRequestError | IRequestSuccess<TResponse & { pagination: Pagination }>>
|
|
50
51
|
) => void,
|
|
51
52
|
rej: (reason?: any) => void,
|
|
53
|
+
queryKey: QueryKey,
|
|
52
54
|
pageParam?: string
|
|
53
55
|
) => {
|
|
54
56
|
if (load) {
|
|
55
57
|
// get request headers
|
|
56
|
-
const globalHeaders
|
|
58
|
+
const globalHeaders = getHeaders();
|
|
57
59
|
|
|
58
|
-
const
|
|
60
|
+
const requestOptions = {
|
|
59
61
|
path: pageParam ?? requestPath,
|
|
60
62
|
headers: { ...globalHeaders, ...headers },
|
|
61
63
|
baseURL: baseUrl ?? API_URL,
|
|
62
64
|
timeout: TIMEOUT,
|
|
63
|
-
}
|
|
65
|
+
};
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
let shouldContinue = true;
|
|
68
|
+
|
|
69
|
+
if (queryConfigOptions.queryMiddleware) {
|
|
70
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (shouldContinue) {
|
|
74
|
+
const getResponse = await makeRequest<TResponse>(requestOptions);
|
|
75
|
+
|
|
76
|
+
if (getResponse.status) {
|
|
77
|
+
res(getResponse as IRequestSuccess<TResponse & { pagination: Pagination }>);
|
|
78
|
+
} else {
|
|
79
|
+
rej(getResponse);
|
|
80
|
+
}
|
|
67
81
|
} else {
|
|
68
|
-
rej(
|
|
82
|
+
rej(null);
|
|
69
83
|
}
|
|
70
84
|
} else {
|
|
71
85
|
res(null as any);
|
|
@@ -96,12 +110,12 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
96
110
|
|
|
97
111
|
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
98
112
|
[requestPath, {}],
|
|
99
|
-
({ pageParam = requestPath }) =>
|
|
113
|
+
({ pageParam = requestPath, queryKey }) =>
|
|
100
114
|
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
101
|
-
sendRequest(res, rej, pageParam)
|
|
115
|
+
sendRequest(res, rej, queryKey, pageParam)
|
|
102
116
|
),
|
|
103
117
|
{
|
|
104
|
-
enabled: load,
|
|
118
|
+
enabled: load || !queryConfigOptions.pauseFutureQueries,
|
|
105
119
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
106
120
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
107
121
|
...options,
|
|
@@ -132,32 +146,42 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
132
146
|
>
|
|
133
147
|
| undefined
|
|
134
148
|
> => {
|
|
135
|
-
|
|
136
|
-
|
|
149
|
+
if (!queryConfigOptions.pauseFutureQueries) {
|
|
150
|
+
await setOptionsAsync(fetchOptions);
|
|
151
|
+
await updatedPathAsync(link);
|
|
137
152
|
|
|
138
|
-
|
|
153
|
+
return query.data;
|
|
154
|
+
} else {
|
|
155
|
+
setQueryConfig({ link, fetchOptions });
|
|
156
|
+
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
139
159
|
};
|
|
140
160
|
|
|
141
161
|
const updatedPathAsync = async (link: string) => {
|
|
142
162
|
startTransition(() => {
|
|
143
|
-
|
|
163
|
+
setRequestPath(link);
|
|
144
164
|
});
|
|
145
165
|
};
|
|
146
166
|
|
|
147
167
|
useEffect(() => {
|
|
148
168
|
if (keyTracker) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
169
|
+
setConfig({ [keyTracker]: [requestPath, {}] });
|
|
170
|
+
}
|
|
171
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
172
|
+
}, [keyTracker, requestPath]);
|
|
154
173
|
|
|
155
|
-
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
|
|
176
|
+
get(queryConfig.link, queryConfig.fetchOptions);
|
|
177
|
+
setQueryConfig(undefined);
|
|
156
178
|
}
|
|
157
|
-
|
|
179
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
180
|
+
}, [queryConfigOptions.pauseFutureQueries]);
|
|
158
181
|
|
|
159
182
|
return {
|
|
160
183
|
get,
|
|
161
184
|
...query,
|
|
185
|
+
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
162
186
|
};
|
|
163
187
|
};
|