@ventlio/tanstack-query 0.5.14 → 0.6.1
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/helpers/index.d.ts +0 -1
- package/dist/index.mjs +158 -171
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useDeleteRequest.d.ts +93 -39
- package/dist/queries/useGetRequest.d.ts +125 -12
- package/dist/src/index.js +0 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/queries/useDeleteRequest.js +39 -55
- package/dist/src/queries/useDeleteRequest.js.map +1 -1
- package/dist/src/queries/useGetInfiniteRequest.js +8 -3
- package/dist/src/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/src/queries/useGetRequest.js +98 -88
- package/dist/src/queries/useGetRequest.js.map +1 -1
- package/dist/src/queries/usePatchRequest.js +8 -12
- package/dist/src/queries/usePatchRequest.js.map +1 -1
- package/dist/src/queries/usePostRequest.js +10 -12
- package/dist/src/queries/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +17 -1
- package/package.json +1 -1
- package/src/helpers/index.ts +0 -1
- package/src/queries/useDeleteRequest.ts +43 -68
- package/src/queries/useGetInfiniteRequest.ts +9 -3
- package/src/queries/useGetRequest.ts +167 -157
- package/src/queries/usePatchRequest.ts +10 -12
- package/src/queries/usePostRequest.ts +11 -13
- package/src/queries/usePutRequest.ts +10 -13
- package/src/types/index.ts +17 -1
- package/dist/helpers/scrollToTop.d.ts +0 -1
- package/dist/src/helpers/scrollToTop.js +0 -9
- package/dist/src/helpers/scrollToTop.js.map +0 -1
- package/src/helpers/scrollToTop.ts +0 -6
|
@@ -3,9 +3,8 @@ import { useMutation } from '@tanstack/react-query';
|
|
|
3
3
|
import { useEnvironmentVariables, useReactNativeEnv } from '../config';
|
|
4
4
|
|
|
5
5
|
import { useStore } from '@tanstack/react-store';
|
|
6
|
-
import { useEffect, useState } from 'react';
|
|
6
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
7
7
|
import { bootStore } from '../config/bootStore';
|
|
8
|
-
import { scrollToTop } from '../helpers';
|
|
9
8
|
import { useUploadProgress } from '../hooks';
|
|
10
9
|
import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
11
10
|
import { HttpMethod, makeRequest } from '../request';
|
|
@@ -25,9 +24,17 @@ export const usePostRequest = <TResponse>({
|
|
|
25
24
|
} & DefaultRequestOptions) => {
|
|
26
25
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
27
26
|
|
|
28
|
-
const {
|
|
27
|
+
const { headerProvider } = useStore(bootStore);
|
|
29
28
|
|
|
30
|
-
const
|
|
29
|
+
const storeHeaders = useHeaderStore((state) => state.headers);
|
|
30
|
+
|
|
31
|
+
// Get headers from both the store and the headerProvider (if configured)
|
|
32
|
+
// headerProvider allows reading from cookies/localStorage synchronously
|
|
33
|
+
const globalHeaders = useMemo(() => {
|
|
34
|
+
const providerHeaders = headerProvider ? headerProvider() : undefined;
|
|
35
|
+
// Merge: store headers take precedence over provider headers
|
|
36
|
+
return { ...providerHeaders, ...storeHeaders };
|
|
37
|
+
}, [storeHeaders, headerProvider]);
|
|
31
38
|
const { isApp } = useReactNativeEnv();
|
|
32
39
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
33
40
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
@@ -80,17 +87,8 @@ export const usePostRequest = <TResponse>({
|
|
|
80
87
|
// }
|
|
81
88
|
|
|
82
89
|
if (postResponse.status) {
|
|
83
|
-
// scroll to top after success
|
|
84
|
-
|
|
85
|
-
if (context !== 'app') {
|
|
86
|
-
scrollToTop();
|
|
87
|
-
}
|
|
88
90
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
89
91
|
} else {
|
|
90
|
-
// scroll to top after error
|
|
91
|
-
if (context !== 'app') {
|
|
92
|
-
scrollToTop();
|
|
93
|
-
}
|
|
94
92
|
rej(postResponse);
|
|
95
93
|
}
|
|
96
94
|
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
3
|
import { useStore } from '@tanstack/react-store';
|
|
4
|
-
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
5
5
|
import { useEnvironmentVariables } from '../config';
|
|
6
6
|
import { bootStore } from '../config/bootStore';
|
|
7
|
-
import { scrollToTop } from '../helpers';
|
|
8
7
|
import { useUploadProgress } from '../hooks';
|
|
9
8
|
import { HttpMethod, makeRequest } from '../request';
|
|
10
9
|
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
@@ -14,14 +13,20 @@ import type { DefaultRequestOptions } from './queries.interface';
|
|
|
14
13
|
export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
|
|
15
14
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
16
15
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
17
|
-
const
|
|
16
|
+
const { headerProvider } = useStore(bootStore);
|
|
17
|
+
|
|
18
|
+
const storeHeaders = useHeaderStore((state) => state.headers);
|
|
19
|
+
|
|
20
|
+
// Get headers from both the store and the headerProvider (if configured)
|
|
21
|
+
const globalHeaders = useMemo(() => {
|
|
22
|
+
const providerHeaders = headerProvider ? headerProvider() : undefined;
|
|
23
|
+
return { ...providerHeaders, ...storeHeaders };
|
|
24
|
+
}, [storeHeaders, headerProvider]);
|
|
18
25
|
|
|
19
26
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
20
27
|
|
|
21
28
|
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
22
29
|
|
|
23
|
-
const { context } = useStore(bootStore);
|
|
24
|
-
|
|
25
30
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
26
31
|
// get request headers
|
|
27
32
|
|
|
@@ -51,16 +56,8 @@ export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: str
|
|
|
51
56
|
const putResponse = await makeRequest<TResponse>(requestOptions);
|
|
52
57
|
// }
|
|
53
58
|
if (putResponse.status) {
|
|
54
|
-
// scroll to top after success
|
|
55
|
-
if (context !== 'app') {
|
|
56
|
-
scrollToTop();
|
|
57
|
-
}
|
|
58
59
|
res(putResponse as IRequestSuccess<TResponse>);
|
|
59
60
|
} else {
|
|
60
|
-
// scroll to top after error
|
|
61
|
-
if (context !== 'app') {
|
|
62
|
-
scrollToTop();
|
|
63
|
-
}
|
|
64
61
|
rej(putResponse);
|
|
65
62
|
}
|
|
66
63
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AxiosProgressEvent, RawAxiosRequestHeaders } from 'axios';
|
|
2
|
-
import type { AppFileConfig, HttpMethod, IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
3
2
|
import type { IPagination } from '../queries';
|
|
3
|
+
import type { AppFileConfig, HttpMethod, IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
4
4
|
|
|
5
5
|
// Enhanced middleware types
|
|
6
6
|
export type MiddlewareFunction<T = any> = (
|
|
@@ -37,6 +37,22 @@ export interface BootstrapConfig {
|
|
|
37
37
|
middleware?: MiddlewareFunction[] | LegacyMiddlewareFunction;
|
|
38
38
|
// Custom pagination configuration
|
|
39
39
|
pagination?: PaginationConfig;
|
|
40
|
+
/**
|
|
41
|
+
* Optional function to provide headers synchronously.
|
|
42
|
+
* This is called on every request to get the current headers.
|
|
43
|
+
* Use this to read auth tokens from cookies or other persistent storage.
|
|
44
|
+
* The returned headers are merged with headers from the header store.
|
|
45
|
+
*
|
|
46
|
+
* Example:
|
|
47
|
+
* ```
|
|
48
|
+
* headerProvider: () => {
|
|
49
|
+
* const token = getCookie('authToken');
|
|
50
|
+
* const spaceId = getCookie('spaceId');
|
|
51
|
+
* return token ? { Authorization: `Bearer ${token}`, } : undefined;
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
headerProvider?: () => QueryHeaders;
|
|
40
56
|
}
|
|
41
57
|
|
|
42
58
|
export interface PaginationConfig {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const scrollToTop: () => void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"scrollToTop.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|