@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.
@@ -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 { context } = useStore(bootStore);
27
+ const { headerProvider } = useStore(bootStore);
29
28
 
30
- const globalHeaders = useHeaderStore((state) => state.headers);
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 globalHeaders = useHeaderStore((state) => state.headers);
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
  };
@@ -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,9 +0,0 @@
1
- const scrollToTop = () => {
2
- window.scrollTo({
3
- top: 0,
4
- behavior: 'smooth',
5
- });
6
- };
7
-
8
- export { scrollToTop };
9
- //# sourceMappingURL=scrollToTop.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"scrollToTop.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
@@ -1,6 +0,0 @@
1
- export const scrollToTop = () => {
2
- window.scrollTo({
3
- top: 0,
4
- behavior: 'smooth',
5
- });
6
- };