@ventlio/tanstack-query 0.2.23 → 0.2.25

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.
@@ -1,18 +1,24 @@
1
1
  'use strict';
2
2
 
3
3
  var reactQuery = require('@tanstack/react-query');
4
- var useQueryConfig = require('./useQueryConfig.js');
5
4
 
6
5
  const useQueryHeaders = () => {
7
- const { headers } = useQueryConfig.useQueryConfig();
8
6
  const queryClient = reactQuery.useQueryClient();
7
+ const getHeadersAsync = async () => {
8
+ return queryClient.ensureQueryData({
9
+ queryKey: ['config'],
10
+ queryFn: () => {
11
+ return queryClient.getQueryData(['config'])?.headers;
12
+ },
13
+ });
14
+ };
9
15
  const setQueryHeaders = (newHeaders) => {
10
16
  queryClient.setQueryData(['config'], (config) => {
11
17
  const newConfig = { ...config, headers: newHeaders };
12
18
  return newConfig;
13
19
  });
14
20
  };
15
- return { headers, setQueryHeaders };
21
+ return { setQueryHeaders, getHeadersAsync };
16
22
  };
17
23
 
18
24
  exports.useQueryHeaders = useQueryHeaders;
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -18,15 +18,22 @@ const useQueryConfig = () => {
18
18
  };
19
19
 
20
20
  const useQueryHeaders = () => {
21
- const { headers } = useQueryConfig();
22
21
  const queryClient = useQueryClient();
22
+ const getHeadersAsync = async () => {
23
+ return queryClient.ensureQueryData({
24
+ queryKey: ['config'],
25
+ queryFn: () => {
26
+ return queryClient.getQueryData(['config'])?.headers;
27
+ },
28
+ });
29
+ };
23
30
  const setQueryHeaders = (newHeaders) => {
24
31
  queryClient.setQueryData(['config'], (config) => {
25
32
  const newConfig = { ...config, headers: newHeaders };
26
33
  return newConfig;
27
34
  });
28
35
  };
29
- return { headers, setQueryHeaders };
36
+ return { setQueryHeaders, getHeadersAsync };
30
37
  };
31
38
 
32
39
  const scrollToTop = () => {
@@ -171,9 +178,11 @@ const useDeleteRequest = () => {
171
178
  const [requestPath, updateDeletePath] = useState('');
172
179
  const [options, setOptions] = useState();
173
180
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
174
- const { headers } = useQueryConfig();
181
+ const { getHeadersAsync } = useQueryHeaders();
175
182
  const query = useQuery([requestPath, {}], () => new Promise((res, rej) => {
176
183
  setTimeout(async () => {
184
+ // get request headers
185
+ const headers = await getHeadersAsync();
177
186
  const postResponse = await makeRequest({
178
187
  path: requestPath,
179
188
  headers,
@@ -214,11 +223,13 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
214
223
  const [options, setOptions] = useState(queryOptions);
215
224
  const [page, setPage] = useState(1);
216
225
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
217
- const { headers } = useQueryConfig();
226
+ const { getHeadersAsync } = useQueryHeaders();
218
227
  let queryClient = useQueryClient();
219
228
  // eslint-disable-next-line react-hooks/exhaustive-deps
220
229
  queryClient = useMemo(() => queryClient, []);
221
230
  const sendRequest = async (res, rej) => {
231
+ // get request headers
232
+ const headers = await getHeadersAsync();
222
233
  const postResponse = await makeRequest({
223
234
  path: requestPath,
224
235
  headers,
@@ -311,31 +322,35 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
311
322
 
312
323
  const usePatchRequest = ({ path }) => {
313
324
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
314
- const { headers } = useQueryConfig();
325
+ const { getHeadersAsync } = useQueryHeaders();
315
326
  // register post mutation
316
327
  const mutation = useMutation((postData) => new Promise((res, rej) => {
317
- makeRequest({
318
- path: path,
319
- body: postData,
320
- method: HttpMethod.PATCH,
321
- headers,
322
- baseURL: API_URL,
323
- timeout: TIMEOUT,
324
- }).then((postResponse) => {
325
- if (postResponse.status) {
326
- // scroll to top after success
327
- scrollToTop();
328
- res(postResponse);
329
- }
330
- else {
331
- // scroll to top after error
332
- window.scrollTo({
333
- top: 0,
334
- behavior: 'smooth',
335
- });
336
- rej(postResponse);
337
- }
338
- });
328
+ return (async () => {
329
+ // get request headers
330
+ const headers = await getHeadersAsync();
331
+ makeRequest({
332
+ path: path,
333
+ body: postData,
334
+ method: HttpMethod.PATCH,
335
+ headers,
336
+ baseURL: API_URL,
337
+ timeout: TIMEOUT,
338
+ }).then((postResponse) => {
339
+ if (postResponse.status) {
340
+ // scroll to top after success
341
+ scrollToTop();
342
+ res(postResponse);
343
+ }
344
+ else {
345
+ // scroll to top after error
346
+ window.scrollTo({
347
+ top: 0,
348
+ behavior: 'smooth',
349
+ });
350
+ rej(postResponse);
351
+ }
352
+ });
353
+ })();
339
354
  }));
340
355
  const patch = async (postData, options) => {
341
356
  return mutation.mutateAsync(postData, options);
@@ -345,35 +360,39 @@ const usePatchRequest = ({ path }) => {
345
360
 
346
361
  const usePostRequest = ({ path, isFormData = false, }) => {
347
362
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
348
- const { headers } = useQueryConfig();
363
+ const { getHeadersAsync } = useQueryHeaders();
349
364
  // register post mutation
350
365
  const mutation = useMutation(async (postData) => new Promise((res, rej) => {
351
- makeRequest({
352
- path: path,
353
- body: postData,
354
- method: HttpMethod.POST,
355
- isFormData,
356
- headers,
357
- baseURL: API_URL,
358
- timeout: TIMEOUT,
359
- }).then((postResponse) => {
360
- if (postResponse.status) {
361
- // scroll to top after success
362
- window.scrollTo({
363
- top: 0,
364
- behavior: 'smooth',
365
- });
366
- res(postResponse);
367
- }
368
- else {
369
- // scroll to top after error
370
- window.scrollTo({
371
- top: 0,
372
- behavior: 'smooth',
373
- });
374
- rej(postResponse);
375
- }
376
- });
366
+ return (async () => {
367
+ // get request headers
368
+ const headers = await getHeadersAsync();
369
+ makeRequest({
370
+ path: path,
371
+ body: postData,
372
+ method: HttpMethod.POST,
373
+ isFormData,
374
+ headers,
375
+ baseURL: API_URL,
376
+ timeout: TIMEOUT,
377
+ }).then((postResponse) => {
378
+ if (postResponse.status) {
379
+ // scroll to top after success
380
+ window.scrollTo({
381
+ top: 0,
382
+ behavior: 'smooth',
383
+ });
384
+ res(postResponse);
385
+ }
386
+ else {
387
+ // scroll to top after error
388
+ window.scrollTo({
389
+ top: 0,
390
+ behavior: 'smooth',
391
+ });
392
+ rej(postResponse);
393
+ }
394
+ });
395
+ })();
377
396
  }));
378
397
  const post = async (postData, options) => {
379
398
  return mutation.mutateAsync(postData, options);
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3,7 +3,7 @@
3
3
  var reactQuery = require('@tanstack/react-query');
4
4
  var react = require('react');
5
5
  var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
6
- var useQueryConfig = require('../config/useQueryConfig.js');
6
+ var useQueryHeaders = require('../config/useQueryHeaders.js');
7
7
  require('axios');
8
8
  var makeRequest = require('../request/make-request.js');
9
9
  var request_enum = require('../request/request.enum.js');
@@ -12,9 +12,11 @@ const useDeleteRequest = () => {
12
12
  const [requestPath, updateDeletePath] = react.useState('');
13
13
  const [options, setOptions] = react.useState();
14
14
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
15
- const { headers } = useQueryConfig.useQueryConfig();
15
+ const { getHeadersAsync } = useQueryHeaders.useQueryHeaders();
16
16
  const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => {
17
17
  setTimeout(async () => {
18
+ // get request headers
19
+ const headers = await getHeadersAsync();
18
20
  const postResponse = await makeRequest.makeRequest({
19
21
  path: requestPath,
20
22
  headers,
@@ -1 +1 @@
1
- {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3,7 +3,7 @@
3
3
  var reactQuery = require('@tanstack/react-query');
4
4
  var react = require('react');
5
5
  var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
6
- var useQueryConfig = require('../config/useQueryConfig.js');
6
+ var useQueryHeaders = require('../config/useQueryHeaders.js');
7
7
  require('axios');
8
8
  var makeRequest = require('../request/make-request.js');
9
9
  require('../request/request.enum.js');
@@ -13,11 +13,13 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
13
13
  const [options, setOptions] = react.useState(queryOptions);
14
14
  const [page, setPage] = react.useState(1);
15
15
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
16
- const { headers } = useQueryConfig.useQueryConfig();
16
+ const { getHeadersAsync } = useQueryHeaders.useQueryHeaders();
17
17
  let queryClient = reactQuery.useQueryClient();
18
18
  // eslint-disable-next-line react-hooks/exhaustive-deps
19
19
  queryClient = react.useMemo(() => queryClient, []);
20
20
  const sendRequest = async (res, rej) => {
21
+ // get request headers
22
+ const headers = await getHeadersAsync();
21
23
  const postResponse = await makeRequest.makeRequest({
22
24
  path: requestPath,
23
25
  headers,
@@ -1 +1 @@
1
- {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,40 +1,44 @@
1
1
  'use strict';
2
2
 
3
3
  var reactQuery = require('@tanstack/react-query');
4
+ var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
5
+ var useQueryHeaders = require('../config/useQueryHeaders.js');
4
6
  var scrollToTop = require('../helpers/scrollToTop.js');
5
7
  require('axios');
6
8
  var makeRequest = require('../request/make-request.js');
7
9
  var request_enum = require('../request/request.enum.js');
8
- var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
9
- var useQueryConfig = require('../config/useQueryConfig.js');
10
10
 
11
11
  const usePatchRequest = ({ path }) => {
12
12
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
13
- const { headers } = useQueryConfig.useQueryConfig();
13
+ const { getHeadersAsync } = useQueryHeaders.useQueryHeaders();
14
14
  // register post mutation
15
15
  const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
16
- makeRequest.makeRequest({
17
- path: path,
18
- body: postData,
19
- method: request_enum.HttpMethod.PATCH,
20
- headers,
21
- baseURL: API_URL,
22
- timeout: TIMEOUT,
23
- }).then((postResponse) => {
24
- if (postResponse.status) {
25
- // scroll to top after success
26
- scrollToTop.scrollToTop();
27
- res(postResponse);
28
- }
29
- else {
30
- // scroll to top after error
31
- window.scrollTo({
32
- top: 0,
33
- behavior: 'smooth',
34
- });
35
- rej(postResponse);
36
- }
37
- });
16
+ return (async () => {
17
+ // get request headers
18
+ const headers = await getHeadersAsync();
19
+ makeRequest.makeRequest({
20
+ path: path,
21
+ body: postData,
22
+ method: request_enum.HttpMethod.PATCH,
23
+ headers,
24
+ baseURL: API_URL,
25
+ timeout: TIMEOUT,
26
+ }).then((postResponse) => {
27
+ if (postResponse.status) {
28
+ // scroll to top after success
29
+ scrollToTop.scrollToTop();
30
+ res(postResponse);
31
+ }
32
+ else {
33
+ // scroll to top after error
34
+ window.scrollTo({
35
+ top: 0,
36
+ behavior: 'smooth',
37
+ });
38
+ rej(postResponse);
39
+ }
40
+ });
41
+ })();
38
42
  }));
39
43
  const patch = async (postData, options) => {
40
44
  return mutation.mutateAsync(postData, options);
@@ -1 +1 @@
1
- {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -2,42 +2,46 @@
2
2
 
3
3
  var reactQuery = require('@tanstack/react-query');
4
4
  var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
5
- var useQueryConfig = require('../config/useQueryConfig.js');
5
+ var useQueryHeaders = require('../config/useQueryHeaders.js');
6
6
  require('axios');
7
7
  var makeRequest = require('../request/make-request.js');
8
8
  var request_enum = require('../request/request.enum.js');
9
9
 
10
10
  const usePostRequest = ({ path, isFormData = false, }) => {
11
11
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
12
- const { headers } = useQueryConfig.useQueryConfig();
12
+ const { getHeadersAsync } = useQueryHeaders.useQueryHeaders();
13
13
  // register post mutation
14
14
  const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => {
15
- makeRequest.makeRequest({
16
- path: path,
17
- body: postData,
18
- method: request_enum.HttpMethod.POST,
19
- isFormData,
20
- headers,
21
- baseURL: API_URL,
22
- timeout: TIMEOUT,
23
- }).then((postResponse) => {
24
- if (postResponse.status) {
25
- // scroll to top after success
26
- window.scrollTo({
27
- top: 0,
28
- behavior: 'smooth',
29
- });
30
- res(postResponse);
31
- }
32
- else {
33
- // scroll to top after error
34
- window.scrollTo({
35
- top: 0,
36
- behavior: 'smooth',
37
- });
38
- rej(postResponse);
39
- }
40
- });
15
+ return (async () => {
16
+ // get request headers
17
+ const headers = await getHeadersAsync();
18
+ makeRequest.makeRequest({
19
+ path: path,
20
+ body: postData,
21
+ method: request_enum.HttpMethod.POST,
22
+ isFormData,
23
+ headers,
24
+ baseURL: API_URL,
25
+ timeout: TIMEOUT,
26
+ }).then((postResponse) => {
27
+ if (postResponse.status) {
28
+ // scroll to top after success
29
+ window.scrollTo({
30
+ top: 0,
31
+ behavior: 'smooth',
32
+ });
33
+ res(postResponse);
34
+ }
35
+ else {
36
+ // scroll to top after error
37
+ window.scrollTo({
38
+ top: 0,
39
+ behavior: 'smooth',
40
+ });
41
+ rej(postResponse);
42
+ }
43
+ });
44
+ })();
41
45
  }));
42
46
  const post = async (postData, options) => {
43
47
  return mutation.mutateAsync(postData, options);
@@ -1 +1 @@
1
- {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3,6 +3,6 @@ export interface TanstackQueryConfig {
3
3
  headers: RawAxiosRequestHeaders;
4
4
  }
5
5
  export interface IUseQueryHeaders {
6
- headers: TanstackQueryConfig['headers'];
6
+ getHeadersAsync: () => Promise<RawAxiosRequestHeaders>;
7
7
  setQueryHeaders: (header: TanstackQueryConfig['headers']) => void;
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.23",
3
+ "version": "0.2.25",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -28,6 +28,7 @@
28
28
  "@tanstack/react-query": "^4.26.1",
29
29
  "axios": "^1.3.4",
30
30
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
31
+ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
31
32
  "react-native": "*"
32
33
  },
33
34
  "peerDependenciesMeta": {
@@ -48,7 +49,8 @@
48
49
  "@tanstack/react-query": "^4.26.1",
49
50
  "@types/axios": "^0.14.0",
50
51
  "@types/node": "*",
51
- "@types/react": "^18.0.28",
52
+ "@types/react": "^18.2.0",
53
+ "@types/react-dom": "^18.2.0",
52
54
  "@typescript-eslint/eslint-plugin": "^5.54.1",
53
55
  "@typescript-eslint/parser": "^5.54.1",
54
56
  "axios": "^1.3.4",
@@ -62,6 +64,7 @@
62
64
  "eslint-plugin-react-hooks": "^4.6.0",
63
65
  "prettier": "^2.8.4",
64
66
  "react": "^18.2.0",
67
+ "react-dom": "^18.2.0",
65
68
  "rimraf": "^4.4.0",
66
69
  "rollup": "^3.19.1",
67
70
  "rollup-plugin-typescript2": "^0.34.1",
@@ -1,11 +1,19 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
+ import type { RawAxiosRequestHeaders } from 'axios';
2
3
  import type { IUseQueryHeaders, TanstackQueryConfig } from '../types';
3
- import { useQueryConfig } from './useQueryConfig';
4
4
 
5
5
  export const useQueryHeaders = (): IUseQueryHeaders => {
6
- const { headers } = useQueryConfig();
7
6
  const queryClient = useQueryClient();
8
7
 
8
+ const getHeadersAsync = async (): Promise<RawAxiosRequestHeaders> => {
9
+ return queryClient.ensureQueryData({
10
+ queryKey: ['config'],
11
+ queryFn: () => {
12
+ return (queryClient.getQueryData(['config']) as any)?.headers;
13
+ },
14
+ });
15
+ };
16
+
9
17
  const setQueryHeaders = (newHeaders: TanstackQueryConfig['headers']) => {
10
18
  queryClient.setQueryData<TanstackQueryConfig>(['config'], (config): any => {
11
19
  const newConfig = { ...config, headers: newHeaders };
@@ -13,5 +21,5 @@ export const useQueryHeaders = (): IUseQueryHeaders => {
13
21
  });
14
22
  };
15
23
 
16
- return { headers, setQueryHeaders };
24
+ return { setQueryHeaders, getHeadersAsync };
17
25
  };
@@ -1,7 +1,8 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
2
  import { useQuery } from '@tanstack/react-query';
3
+ import type { RawAxiosRequestHeaders } from 'axios';
3
4
  import { useState } from 'react';
4
- import { useEnvironmentVariables, useQueryConfig } from '../config';
5
+ import { useEnvironmentVariables, useQueryHeaders } from '../config';
5
6
  import type { IRequestError, IRequestSuccess } from '../request';
6
7
  import { HttpMethod, makeRequest } from '../request';
7
8
 
@@ -11,13 +12,16 @@ export const useDeleteRequest = <TResponse>() => {
11
12
 
12
13
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
13
14
 
14
- const { headers } = useQueryConfig();
15
+ const { getHeadersAsync } = useQueryHeaders();
15
16
 
16
17
  const query = useQuery<any, any, IRequestSuccess<TResponse>>(
17
18
  [requestPath, {}],
18
19
  () =>
19
20
  new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => {
20
21
  setTimeout(async () => {
22
+ // get request headers
23
+ const headers: RawAxiosRequestHeaders = await getHeadersAsync();
24
+
21
25
  const postResponse = await makeRequest<TResponse>({
22
26
  path: requestPath,
23
27
  headers,
@@ -1,7 +1,9 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
2
  import { useQuery, useQueryClient } from '@tanstack/react-query';
3
+ import type { RawAxiosRequestHeaders } from 'axios';
3
4
  import { startTransition, useEffect, useMemo, useState } from 'react';
4
- import { useEnvironmentVariables, useQueryConfig } from '../config';
5
+ import { useEnvironmentVariables, useQueryHeaders } from '../config';
6
+
5
7
  import type { IRequestError, IRequestSuccess } from '../request';
6
8
  import { makeRequest } from '../request';
7
9
  import type { IPagination, TanstackQueryOption } from './queries.interface';
@@ -22,7 +24,7 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
22
24
  const [page, setPage] = useState<number>(1);
23
25
 
24
26
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
25
- const { headers } = useQueryConfig();
27
+ const { getHeadersAsync } = useQueryHeaders();
26
28
 
27
29
  let queryClient = useQueryClient();
28
30
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -37,6 +39,9 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
37
39
  ) => void,
38
40
  rej: (reason?: any) => void
39
41
  ) => {
42
+ // get request headers
43
+ const headers: RawAxiosRequestHeaders = await getHeadersAsync();
44
+
40
45
  const postResponse = await makeRequest<TResponse>({
41
46
  path: requestPath,
42
47
  headers,
@@ -1,9 +1,10 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
2
  import { useMutation } from '@tanstack/react-query';
3
+ import { useEnvironmentVariables, useQueryHeaders } from '../config';
3
4
  import { scrollToTop } from '../helpers';
4
5
  import { HttpMethod, makeRequest } from '../request';
5
6
 
6
- import { useEnvironmentVariables, useQueryConfig } from '../config';
7
+ import type { RawAxiosRequestHeaders } from 'axios';
7
8
  import type {
8
9
  IRequestError,
9
10
  IRequestSuccess,
@@ -11,33 +12,38 @@ import type {
11
12
 
12
13
  export const usePatchRequest = <TResponse>({ path }: { path: string }) => {
13
14
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
14
- const { headers } = useQueryConfig();
15
+
16
+ const { getHeadersAsync } = useQueryHeaders();
15
17
 
16
18
  // register post mutation
17
19
  const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
18
20
  (postData: any) =>
19
21
  new Promise<IRequestSuccess<TResponse>>((res, rej) => {
20
- makeRequest<TResponse>({
21
- path: path,
22
- body: postData,
23
- method: HttpMethod.PATCH,
24
- headers,
25
- baseURL: API_URL,
26
- timeout: TIMEOUT,
27
- }).then((postResponse) => {
28
- if (postResponse.status) {
29
- // scroll to top after success
30
- scrollToTop();
31
- res(postResponse as IRequestSuccess<TResponse>);
32
- } else {
33
- // scroll to top after error
34
- window.scrollTo({
35
- top: 0,
36
- behavior: 'smooth',
37
- });
38
- rej(postResponse);
39
- }
40
- });
22
+ return (async () => {
23
+ // get request headers
24
+ const headers: RawAxiosRequestHeaders = await getHeadersAsync();
25
+ makeRequest<TResponse>({
26
+ path: path,
27
+ body: postData,
28
+ method: HttpMethod.PATCH,
29
+ headers,
30
+ baseURL: API_URL,
31
+ timeout: TIMEOUT,
32
+ }).then((postResponse) => {
33
+ if (postResponse.status) {
34
+ // scroll to top after success
35
+ scrollToTop();
36
+ res(postResponse as IRequestSuccess<TResponse>);
37
+ } else {
38
+ // scroll to top after error
39
+ window.scrollTo({
40
+ top: 0,
41
+ behavior: 'smooth',
42
+ });
43
+ rej(postResponse);
44
+ }
45
+ });
46
+ })();
41
47
  })
42
48
  );
43
49
 
@@ -1,6 +1,8 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
2
  import { useMutation } from '@tanstack/react-query';
3
- import { useEnvironmentVariables, useQueryConfig } from '../config';
3
+ import { useEnvironmentVariables, useQueryHeaders } from '../config';
4
+
5
+ import type { RawAxiosRequestHeaders } from 'axios';
4
6
  import type { IRequestError, IRequestSuccess } from '../request';
5
7
  import { HttpMethod, makeRequest } from '../request';
6
8
 
@@ -13,37 +15,41 @@ export const usePostRequest = <TResponse>({
13
15
  }) => {
14
16
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
15
17
 
16
- const { headers } = useQueryConfig();
18
+ const { getHeadersAsync } = useQueryHeaders();
17
19
 
18
20
  // register post mutation
19
21
  const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
20
22
  async (postData: any) =>
21
23
  new Promise<IRequestSuccess<TResponse>>((res, rej) => {
22
- makeRequest<TResponse>({
23
- path: path,
24
- body: postData,
25
- method: HttpMethod.POST,
26
- isFormData,
27
- headers,
28
- baseURL: API_URL,
29
- timeout: TIMEOUT,
30
- }).then((postResponse) => {
31
- if (postResponse.status) {
32
- // scroll to top after success
33
- window.scrollTo({
34
- top: 0,
35
- behavior: 'smooth',
36
- });
37
- res(postResponse as IRequestSuccess<TResponse>);
38
- } else {
39
- // scroll to top after error
40
- window.scrollTo({
41
- top: 0,
42
- behavior: 'smooth',
43
- });
44
- rej(postResponse);
45
- }
46
- });
24
+ return (async () => {
25
+ // get request headers
26
+ const headers: RawAxiosRequestHeaders = await getHeadersAsync();
27
+ makeRequest<TResponse>({
28
+ path: path,
29
+ body: postData,
30
+ method: HttpMethod.POST,
31
+ isFormData,
32
+ headers,
33
+ baseURL: API_URL,
34
+ timeout: TIMEOUT,
35
+ }).then((postResponse) => {
36
+ if (postResponse.status) {
37
+ // scroll to top after success
38
+ window.scrollTo({
39
+ top: 0,
40
+ behavior: 'smooth',
41
+ });
42
+ res(postResponse as IRequestSuccess<TResponse>);
43
+ } else {
44
+ // scroll to top after error
45
+ window.scrollTo({
46
+ top: 0,
47
+ behavior: 'smooth',
48
+ });
49
+ rej(postResponse);
50
+ }
51
+ });
52
+ })();
47
53
  })
48
54
  );
49
55
  const post = async (
@@ -5,6 +5,6 @@ export interface TanstackQueryConfig {
5
5
  }
6
6
 
7
7
  export interface IUseQueryHeaders {
8
- headers: TanstackQueryConfig['headers'];
8
+ getHeadersAsync: () => Promise<RawAxiosRequestHeaders>;
9
9
  setQueryHeaders: (header: TanstackQueryConfig['headers']) => void;
10
10
  }