@ventlio/tanstack-query 0.2.57 → 0.2.58

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/index.mjs CHANGED
@@ -282,10 +282,10 @@ const useDeleteRequest = (deleteOptions) => {
282
282
  const setOptionsAsync = async (fetchOptions) => {
283
283
  return setOptions(fetchOptions);
284
284
  };
285
- const destroy = async (link, deleteOptions) => {
285
+ const destroy = async (link, internalDeleteOptions) => {
286
286
  // set enabled to be true for every delete
287
- deleteOptions = deleteOptions ?? {};
288
- deleteOptions.enabled = true;
287
+ internalDeleteOptions = internalDeleteOptions ?? {};
288
+ internalDeleteOptions.enabled = true;
289
289
  await updatedPathAsync(link);
290
290
  await setOptionsAsync(deleteOptions);
291
291
  return query.data;
@@ -401,28 +401,24 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
401
401
  const sendRequest = async (res, rej, data) => {
402
402
  // get request headers
403
403
  const globalHeaders = getHeaders();
404
- makeRequest({
404
+ const patchResponse = await makeRequest({
405
405
  path: path,
406
406
  body: data,
407
407
  method: HttpMethod.PATCH,
408
408
  headers: { ...globalHeaders, ...headers },
409
409
  baseURL: baseUrl ?? API_URL,
410
410
  timeout: TIMEOUT,
411
- }).then((postResponse) => {
412
- if (postResponse.status) {
413
- // scroll to top after success
414
- scrollToTop();
415
- res(postResponse);
416
- }
417
- else {
418
- // scroll to top after error
419
- window.scrollTo({
420
- top: 0,
421
- behavior: 'smooth',
422
- });
423
- rej(postResponse);
424
- }
425
411
  });
412
+ if (patchResponse.status) {
413
+ // scroll to top after success
414
+ scrollToTop();
415
+ res(patchResponse);
416
+ }
417
+ else {
418
+ // scroll to top after error
419
+ scrollToTop();
420
+ rej(patchResponse);
421
+ }
426
422
  };
427
423
  // register post mutation
428
424
  const mutation = useMutation((dataData) => new Promise((res, rej) => {
@@ -436,36 +432,35 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
436
432
 
437
433
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
438
434
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
435
+ const queryClient = useQueryClient();
439
436
  const { getHeaders } = useQueryHeaders();
440
437
  const sendRequest = async (res, rej, postData) => {
441
438
  // get request headers
442
439
  const globalHeaders = getHeaders();
443
- makeRequest({
444
- path: path,
440
+ const config = queryClient.getQueryData(['config']);
441
+ const postResponse = await makeRequest({
442
+ path,
445
443
  body: postData,
446
444
  method: HttpMethod.POST,
447
445
  isFormData,
448
446
  headers: { ...globalHeaders, ...headers },
449
447
  baseURL: baseUrl ?? API_URL,
450
448
  timeout: TIMEOUT,
451
- }).then((postResponse) => {
452
- if (postResponse.status) {
453
- // scroll to top after success
454
- window.scrollTo({
455
- top: 0,
456
- behavior: 'smooth',
457
- });
458
- res(postResponse);
449
+ });
450
+ if (postResponse.status) {
451
+ // scroll to top after success
452
+ if (config?.options?.context !== 'app') {
453
+ scrollToTop();
459
454
  }
460
- else {
461
- // scroll to top after error
462
- window.scrollTo({
463
- top: 0,
464
- behavior: 'smooth',
465
- });
466
- rej(postResponse);
455
+ res(postResponse);
456
+ }
457
+ else {
458
+ // scroll to top after error
459
+ if (config?.options?.context !== 'app') {
460
+ scrollToTop();
467
461
  }
468
- });
462
+ rej(postResponse);
463
+ }
469
464
  };
470
465
  // register post mutation
471
466
  const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -27,7 +27,7 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
27
27
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
28
28
  remove: () => void;
29
29
  fetchStatus: import("@tanstack/react-query").FetchStatus;
30
- destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
30
+ destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
31
31
  } | {
32
32
  data: IRequestSuccess<TResponse>;
33
33
  error: null;
@@ -54,7 +54,7 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
54
54
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
55
55
  remove: () => void;
56
56
  fetchStatus: import("@tanstack/react-query").FetchStatus;
57
- destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
57
+ destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
58
58
  } | {
59
59
  data: undefined;
60
60
  error: any;
@@ -81,7 +81,7 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
81
81
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
82
82
  remove: () => void;
83
83
  fetchStatus: import("@tanstack/react-query").FetchStatus;
84
- destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
84
+ destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
85
85
  } | {
86
86
  data: undefined;
87
87
  error: null;
@@ -108,5 +108,5 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
108
108
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
109
109
  remove: () => void;
110
110
  fetchStatus: import("@tanstack/react-query").FetchStatus;
111
- destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
111
+ destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
112
112
  };
@@ -38,10 +38,10 @@ const useDeleteRequest = (deleteOptions) => {
38
38
  const setOptionsAsync = async (fetchOptions) => {
39
39
  return setOptions(fetchOptions);
40
40
  };
41
- const destroy = async (link, deleteOptions) => {
41
+ const destroy = async (link, internalDeleteOptions) => {
42
42
  // set enabled to be true for every delete
43
- deleteOptions = deleteOptions ?? {};
44
- deleteOptions.enabled = true;
43
+ internalDeleteOptions = internalDeleteOptions ?? {};
44
+ internalDeleteOptions.enabled = true;
45
45
  await updatedPathAsync(link);
46
46
  await setOptionsAsync(deleteOptions);
47
47
  return query.data;
@@ -15,28 +15,24 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
15
15
  const sendRequest = async (res, rej, data) => {
16
16
  // get request headers
17
17
  const globalHeaders = getHeaders();
18
- makeRequest.makeRequest({
18
+ const patchResponse = await makeRequest.makeRequest({
19
19
  path: path,
20
20
  body: data,
21
21
  method: request_enum.HttpMethod.PATCH,
22
22
  headers: { ...globalHeaders, ...headers },
23
23
  baseURL: baseUrl ?? API_URL,
24
24
  timeout: TIMEOUT,
25
- }).then((postResponse) => {
26
- if (postResponse.status) {
27
- // scroll to top after success
28
- scrollToTop.scrollToTop();
29
- res(postResponse);
30
- }
31
- else {
32
- // scroll to top after error
33
- window.scrollTo({
34
- top: 0,
35
- behavior: 'smooth',
36
- });
37
- rej(postResponse);
38
- }
39
25
  });
26
+ if (patchResponse.status) {
27
+ // scroll to top after success
28
+ scrollToTop.scrollToTop();
29
+ res(patchResponse);
30
+ }
31
+ else {
32
+ // scroll to top after error
33
+ scrollToTop.scrollToTop();
34
+ rej(patchResponse);
35
+ }
40
36
  };
41
37
  // register post mutation
42
38
  const mutation = reactQuery.useMutation((dataData) => new Promise((res, rej) => {
@@ -1 +1 @@
1
- {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -4,42 +4,42 @@ var reactQuery = require('@tanstack/react-query');
4
4
  var useEnvironmentVariables = require('../config/useEnvironmentVariables.js');
5
5
  var useQueryHeaders = require('../config/useQueryHeaders.js');
6
6
  require('react');
7
+ var scrollToTop = require('../helpers/scrollToTop.js');
7
8
  require('axios');
8
9
  var makeRequest = require('../request/make-request.js');
9
10
  var request_enum = require('../request/request.enum.js');
10
11
 
11
12
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
12
13
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
14
+ const queryClient = reactQuery.useQueryClient();
13
15
  const { getHeaders } = useQueryHeaders.useQueryHeaders();
14
16
  const sendRequest = async (res, rej, postData) => {
15
17
  // get request headers
16
18
  const globalHeaders = getHeaders();
17
- makeRequest.makeRequest({
18
- path: path,
19
+ const config = queryClient.getQueryData(['config']);
20
+ const postResponse = await makeRequest.makeRequest({
21
+ path,
19
22
  body: postData,
20
23
  method: request_enum.HttpMethod.POST,
21
24
  isFormData,
22
25
  headers: { ...globalHeaders, ...headers },
23
26
  baseURL: baseUrl ?? API_URL,
24
27
  timeout: TIMEOUT,
25
- }).then((postResponse) => {
26
- if (postResponse.status) {
27
- // scroll to top after success
28
- window.scrollTo({
29
- top: 0,
30
- behavior: 'smooth',
31
- });
32
- res(postResponse);
28
+ });
29
+ if (postResponse.status) {
30
+ // scroll to top after success
31
+ if (config?.options?.context !== 'app') {
32
+ scrollToTop.scrollToTop();
33
33
  }
34
- else {
35
- // scroll to top after error
36
- window.scrollTo({
37
- top: 0,
38
- behavior: 'smooth',
39
- });
40
- rej(postResponse);
34
+ res(postResponse);
35
+ }
36
+ else {
37
+ // scroll to top after error
38
+ if (config?.options?.context !== 'app') {
39
+ scrollToTop.scrollToTop();
41
40
  }
42
- });
41
+ rej(postResponse);
42
+ }
43
43
  };
44
44
  // register post mutation
45
45
  const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.57",
3
+ "version": "0.2.58",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -30,8 +30,7 @@
30
30
  "axios": "^1.3.4",
31
31
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
32
32
  "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
33
- "react-native": "*",
34
- "react-native-dotenv": "3.4.8"
33
+ "react-native": "*"
35
34
  },
36
35
  "peerDependenciesMeta": {
37
36
  "react-native": {
@@ -55,7 +54,6 @@
55
54
  "@types/node": "*",
56
55
  "@types/react": "^18.2.0",
57
56
  "@types/react-dom": "^18.2.0",
58
- "@types/react-native-dotenv": "^0.2.0",
59
57
  "@typescript-eslint/eslint-plugin": "^5.54.1",
60
58
  "@typescript-eslint/parser": "^5.54.1",
61
59
  "axios": "^1.3.4",
@@ -73,7 +71,6 @@
73
71
  "prettier": "^2.8.4",
74
72
  "react": "^18.2.0",
75
73
  "react-dom": "^18.2.0",
76
- "react-native-dotenv": "3.4.8",
77
74
  "rimraf": "^4.4.0",
78
75
  "rollup": "^3.19.1",
79
76
  "rollup-plugin-typescript2": "^0.34.1",
@@ -27,6 +27,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
27
27
  baseURL: baseUrl ?? API_URL,
28
28
  timeout: TIMEOUT,
29
29
  });
30
+
30
31
  if (postResponse.status) {
31
32
  res(postResponse as IRequestSuccess<TResponse>);
32
33
  } else {
@@ -50,7 +51,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
50
51
 
51
52
  const destroy = async (
52
53
  link: string,
53
- deleteOptions?: UseQueryOptions<
54
+ internalDeleteOptions?: UseQueryOptions<
54
55
  IRequestSuccess<TResponse | undefined>,
55
56
  IRequestError,
56
57
  IRequestSuccess<TResponse | undefined>,
@@ -58,8 +59,8 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
58
59
  >
59
60
  ): Promise<IRequestSuccess<TResponse> | undefined> => {
60
61
  // set enabled to be true for every delete
61
- deleteOptions = deleteOptions ?? {};
62
- deleteOptions.enabled = true;
62
+ internalDeleteOptions = internalDeleteOptions ?? {};
63
+ internalDeleteOptions.enabled = true;
63
64
 
64
65
  await updatedPathAsync(link);
65
66
  await setOptionsAsync(deleteOptions);
@@ -48,6 +48,7 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
48
48
  baseURL: baseUrl ?? API_URL,
49
49
  timeout: TIMEOUT,
50
50
  });
51
+
51
52
  if (getResponse.status) {
52
53
  res(getResponse as IRequestSuccess<TResponse>);
53
54
  } else {
@@ -16,27 +16,24 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
16
16
  // get request headers
17
17
  const globalHeaders: RawAxiosRequestHeaders = getHeaders();
18
18
 
19
- makeRequest<TResponse>({
19
+ const patchResponse = await makeRequest<TResponse>({
20
20
  path: path,
21
21
  body: data,
22
22
  method: HttpMethod.PATCH,
23
23
  headers: { ...globalHeaders, ...headers },
24
24
  baseURL: baseUrl ?? API_URL,
25
25
  timeout: TIMEOUT,
26
- }).then((postResponse) => {
27
- if (postResponse.status) {
28
- // scroll to top after success
29
- scrollToTop();
30
- res(postResponse as IRequestSuccess<TResponse>);
31
- } else {
32
- // scroll to top after error
33
- window.scrollTo({
34
- top: 0,
35
- behavior: 'smooth',
36
- });
37
- rej(postResponse);
38
- }
39
26
  });
27
+
28
+ if (patchResponse.status) {
29
+ // scroll to top after success
30
+ scrollToTop();
31
+ res(patchResponse as IRequestSuccess<TResponse>);
32
+ } else {
33
+ // scroll to top after error
34
+ scrollToTop();
35
+ rej(patchResponse);
36
+ }
40
37
  };
41
38
 
42
39
  // register post mutation
@@ -1,10 +1,12 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
- import { useMutation } from '@tanstack/react-query';
2
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
3
3
  import { useEnvironmentVariables, useQueryHeaders } from '../config';
4
4
 
5
5
  import type { RawAxiosRequestHeaders } from 'axios';
6
+ import { scrollToTop } from '../helpers';
6
7
  import type { IRequestError, IRequestSuccess } from '../request';
7
8
  import { HttpMethod, makeRequest } from '../request';
9
+ import type { TanstackQueryConfig } from '../types';
8
10
  import type { DefaultRequestOptions } from './queries.interface';
9
11
 
10
12
  export const usePostRequest = <TResponse>({
@@ -17,38 +19,38 @@ export const usePostRequest = <TResponse>({
17
19
  isFormData?: boolean;
18
20
  } & DefaultRequestOptions) => {
19
21
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
20
-
22
+ const queryClient = useQueryClient();
21
23
  const { getHeaders } = useQueryHeaders();
22
24
 
23
25
  const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, postData: any) => {
24
26
  // get request headers
25
27
  const globalHeaders: RawAxiosRequestHeaders = getHeaders();
28
+ const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
26
29
 
27
- makeRequest<TResponse>({
28
- path: path,
30
+ const postResponse = await makeRequest<TResponse>({
31
+ path,
29
32
  body: postData,
30
33
  method: HttpMethod.POST,
31
34
  isFormData,
32
35
  headers: { ...globalHeaders, ...headers },
33
36
  baseURL: baseUrl ?? API_URL,
34
37
  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
38
  });
39
+
40
+ if (postResponse.status) {
41
+ // scroll to top after success
42
+
43
+ if (config?.options?.context !== 'app') {
44
+ scrollToTop();
45
+ }
46
+ res(postResponse as IRequestSuccess<TResponse>);
47
+ } else {
48
+ // scroll to top after error
49
+ if (config?.options?.context !== 'app') {
50
+ scrollToTop();
51
+ }
52
+ rej(postResponse);
53
+ }
52
54
  };
53
55
 
54
56
  // register post mutation