@ventlio/tanstack-query 0.5.0 → 0.5.2

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
@@ -180,7 +180,7 @@ const useQueryModel = (keyTracker) => {
180
180
  const data = get();
181
181
  newData = lodashSet(data, path, newData);
182
182
  }
183
- return queryClient.setQueryData(queryKey, newData);
183
+ return queryClient.setQueryData(queryKey, () => newData);
184
184
  };
185
185
  const getModelConfig = () => {
186
186
  const { options } = config;
@@ -344,38 +344,19 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
344
344
  // configure body
345
345
  body = (isFormData ? axios.toFormData(body) : body);
346
346
  // configure request header1
347
- if (!isFormData) {
348
- headers['Content-Type'] = ContentType.APPLICATION_JSON;
349
- }
350
- else {
351
- if (isApp) {
352
- headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
353
- // add the app files
354
- for (const fileKey in appFiles) {
355
- const currentFile = appFiles[fileKey];
356
- if (Array.isArray(currentFile)) {
357
- for (const innerFile of currentFile) {
358
- body.append(fileKey, innerFile);
359
- }
360
- }
361
- else {
362
- body.append(fileKey, currentFile);
363
- }
364
- }
365
- }
366
- else {
367
- delete headers['Content-Type'];
368
- }
369
- }
347
+ configureRequestHeader(isFormData, headers, isApp, appFiles, body);
370
348
  try {
371
349
  const axiosRequest = axiosInstance({ baseURL, headers, timeout });
372
- // send request
373
- const resp = await axiosRequest({
350
+ const axiosRequestConfig = {
374
351
  url: path,
375
352
  method,
376
- data: body,
377
353
  onUploadProgress,
378
- });
354
+ };
355
+ if (Object.keys(body).length > 0) {
356
+ axiosRequestConfig.data = body;
357
+ }
358
+ // send request
359
+ const resp = await axiosRequest(axiosRequestConfig);
379
360
  // get response json
380
361
  const jsonResp = await resp.data;
381
362
  // get response code
@@ -400,6 +381,29 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
400
381
  });
401
382
  }
402
383
  }
384
+ const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
385
+ if (!isFormData) {
386
+ headers['Content-Type'] = ContentType.APPLICATION_JSON;
387
+ }
388
+ else if (isApp) {
389
+ headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
390
+ // add the app files
391
+ for (const fileKey in appFiles) {
392
+ const currentFile = appFiles[fileKey];
393
+ if (Array.isArray(currentFile)) {
394
+ for (const innerFile of currentFile) {
395
+ body.append(fileKey, innerFile);
396
+ }
397
+ }
398
+ else {
399
+ body.append(fileKey, currentFile);
400
+ }
401
+ }
402
+ }
403
+ else {
404
+ delete headers['Content-Type'];
405
+ }
406
+ };
403
407
  function getAppFiles(body, fileSelectors = []) {
404
408
  const files = {};
405
409
  if (body) {
@@ -465,7 +469,9 @@ const useDeleteRequest = (deleteOptions) => {
465
469
  const destroy = async (link, internalDeleteOptions) => {
466
470
  if (!isFutureQueriesPaused) {
467
471
  // set enabled to be true for every delete
468
- internalDeleteOptions = internalDeleteOptions ?? { queryKey: [] };
472
+ internalDeleteOptions = internalDeleteOptions
473
+ ? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
474
+ : { queryKey: [link, {}], enabled: true };
469
475
  await setOptionsAsync(internalDeleteOptions);
470
476
  await updatedPathAsync(link);
471
477
  return query.data;
@@ -772,10 +778,10 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
772
778
  };
773
779
  // register post mutation
774
780
  const mutation = useMutation({
781
+ mutationKey: [path, { type: 'mutation' }],
775
782
  mutationFn: (dataData) => new Promise((res, rej) => {
776
783
  return sendRequest(res, rej, dataData);
777
784
  }),
778
- mutationKey: [path, { type: 'mutation' }],
779
785
  });
780
786
  const patch = async (data, options) => {
781
787
  if (!isFutureMutationsPaused) {
@@ -853,8 +859,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
853
859
  };
854
860
  // register post mutation
855
861
  const mutation = useMutation({
856
- mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
857
862
  mutationKey: [path, { type: 'mutation' }],
863
+ mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
858
864
  });
859
865
  const post = async (data, options) => {
860
866
  if (!isFutureMutationsPaused) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -64,7 +64,7 @@ const useQueryModel = (keyTracker) => {
64
64
  const data = get();
65
65
  newData = lodashSet(data, path, newData);
66
66
  }
67
- return queryClient.setQueryData(queryKey, newData);
67
+ return queryClient.setQueryData(queryKey, () => newData);
68
68
  };
69
69
  const getModelConfig = () => {
70
70
  const { options } = config;
@@ -1,3 +1,3 @@
1
1
  export declare const useRefetchQuery: (queryKey: any[]) => Promise<{
2
- refetchQuery: <T>(innerQueryKey?: any[]) => Promise<[import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").t, T | undefined][]>;
2
+ refetchQuery: <T>(innerQueryKey?: any[]) => Promise<[import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").z, T | undefined][]>;
3
3
  }>;
@@ -24,8 +24,8 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
24
24
  isPlaceholderData: boolean;
25
25
  isRefetching: boolean;
26
26
  isStale: boolean;
27
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse>, any>>;
28
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
27
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
28
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
29
29
  destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
30
30
  cached?: boolean | undefined;
31
31
  }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -52,8 +52,8 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
52
52
  isPlaceholderData: boolean;
53
53
  isRefetching: boolean;
54
54
  isStale: boolean;
55
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse>, any>>;
56
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
55
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
56
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
57
57
  destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
58
58
  cached?: boolean | undefined;
59
59
  }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -60,7 +60,9 @@ const useDeleteRequest = (deleteOptions) => {
60
60
  const destroy = async (link, internalDeleteOptions) => {
61
61
  if (!isFutureQueriesPaused) {
62
62
  // set enabled to be true for every delete
63
- internalDeleteOptions = internalDeleteOptions ?? { queryKey: [] };
63
+ internalDeleteOptions = internalDeleteOptions
64
+ ? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
65
+ : { queryKey: [link, {}], enabled: true };
64
66
  await setOptionsAsync(internalDeleteOptions);
65
67
  await updatedPathAsync(link);
66
68
  return query.data;
@@ -1 +1 @@
1
- {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,4 +1,4 @@
1
- import type { UseQueryOptions } from '@tanstack/react-query';
1
+ import type { InfiniteData, UseQueryOptions } from '@tanstack/react-query';
2
2
  import type { IRequestError, IRequestSuccess } from '../request';
3
3
  import type { DefaultRequestOptions, TanstackInfiniteQueryOption } from './queries.interface';
4
4
  interface Pagination {
@@ -18,22 +18,24 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
18
18
  keyTracker?: string | undefined;
19
19
  } & DefaultRequestOptions) => {
20
20
  isLoading: boolean;
21
- data: IRequestSuccess<TResponse & {
21
+ data: InfiniteData<IRequestSuccess<TResponse & {
22
22
  pagination: Pagination;
23
- }>;
23
+ }>, unknown>;
24
24
  error: any;
25
25
  isError: true;
26
26
  isPending: false;
27
27
  isLoadingError: false;
28
28
  isRefetchError: true;
29
+ isFetchNextPageError: false;
30
+ isFetchPreviousPageError: false;
29
31
  isSuccess: false;
30
32
  status: "error";
31
- fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a9 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").as<IRequestSuccess<TResponse & {
33
+ fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ah | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
32
34
  pagination: Pagination;
33
- }>, any>>;
34
- fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").aa | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").as<IRequestSuccess<TResponse & {
35
+ }>, unknown>, any>>;
36
+ fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ai | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
35
37
  pagination: Pagination;
36
- }>, any>>;
38
+ }>, unknown>, any>>;
37
39
  hasNextPage: boolean;
38
40
  hasPreviousPage: boolean;
39
41
  isFetchingNextPage: boolean;
@@ -51,31 +53,33 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
51
53
  isPlaceholderData: boolean;
52
54
  isRefetching: boolean;
53
55
  isStale: boolean;
54
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse & {
56
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<InfiniteData<IRequestSuccess<TResponse & {
55
57
  pagination: Pagination;
56
- }>, any>>;
57
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
58
- get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse & {
58
+ }>, unknown>, any>>;
59
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
60
+ get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
59
61
  pagination: Pagination;
60
- }> | undefined>;
62
+ }>, unknown> | undefined>;
61
63
  } | {
62
64
  isLoading: boolean;
63
- data: IRequestSuccess<TResponse & {
65
+ data: InfiniteData<IRequestSuccess<TResponse & {
64
66
  pagination: Pagination;
65
- }>;
67
+ }>, unknown>;
66
68
  error: null;
67
69
  isError: false;
68
70
  isPending: false;
69
71
  isLoadingError: false;
70
72
  isRefetchError: false;
73
+ isFetchNextPageError: false;
74
+ isFetchPreviousPageError: false;
71
75
  isSuccess: true;
72
76
  status: "success";
73
- fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a9 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").as<IRequestSuccess<TResponse & {
77
+ fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ah | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
74
78
  pagination: Pagination;
75
- }>, any>>;
76
- fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").aa | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").as<IRequestSuccess<TResponse & {
79
+ }>, unknown>, any>>;
80
+ fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ai | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
77
81
  pagination: Pagination;
78
- }>, any>>;
82
+ }>, unknown>, any>>;
79
83
  hasNextPage: boolean;
80
84
  hasPreviousPage: boolean;
81
85
  isFetchingNextPage: boolean;
@@ -93,12 +97,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
93
97
  isPlaceholderData: boolean;
94
98
  isRefetching: boolean;
95
99
  isStale: boolean;
96
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse & {
100
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<InfiniteData<IRequestSuccess<TResponse & {
97
101
  pagination: Pagination;
98
- }>, any>>;
99
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
100
- get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse & {
102
+ }>, unknown>, any>>;
103
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
104
+ get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
101
105
  pagination: Pagination;
102
- }> | undefined>;
106
+ }>, unknown> | undefined>;
103
107
  };
104
108
  export {};
@@ -37,8 +37,8 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
37
37
  isPlaceholderData: boolean;
38
38
  isRefetching: boolean;
39
39
  isStale: boolean;
40
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse>, any>>;
41
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
40
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
41
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
42
42
  } | {
43
43
  isLoading: boolean;
44
44
  setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
@@ -69,6 +69,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
69
69
  isPlaceholderData: boolean;
70
70
  isRefetching: boolean;
71
71
  isStale: boolean;
72
- refetch: (options?: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").a4 | undefined) => Promise<import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ak<IRequestSuccess<TResponse>, any>>;
73
- fetchStatus: import("@tanstack/query-core/build/legacy/queryClient-K0zFyarY").ac;
72
+ refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
73
+ fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
74
74
  };
@@ -59,10 +59,10 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
59
59
  };
60
60
  // register post mutation
61
61
  const mutation = useMutation({
62
+ mutationKey: [path, { type: 'mutation' }],
62
63
  mutationFn: (dataData) => new Promise((res, rej) => {
63
64
  return sendRequest(res, rej, dataData);
64
65
  }),
65
- mutationKey: [path, { type: 'mutation' }],
66
66
  });
67
67
  const patch = async (data, options) => {
68
68
  if (!isFutureMutationsPaused) {
@@ -69,8 +69,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
69
69
  };
70
70
  // register post mutation
71
71
  const mutation = useMutation({
72
- mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
73
72
  mutationKey: [path, { type: 'mutation' }],
73
+ mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
74
74
  });
75
75
  const post = async (data, options) => {
76
76
  if (!isFutureMutationsPaused) {
@@ -0,0 +1,86 @@
1
+ import type { MutateOptions } from '@tanstack/react-query';
2
+ import type { IRequestError, IRequestSuccess } from '../request/request.interface';
3
+ import type { DefaultRequestOptions } from './queries.interface';
4
+ export declare const usePutRequest: <TResponse>({ path, baseUrl, headers }: {
5
+ path: string;
6
+ } & DefaultRequestOptions) => {
7
+ isLoading: boolean;
8
+ data: undefined;
9
+ variables: undefined;
10
+ error: null;
11
+ isError: false;
12
+ isIdle: true;
13
+ isPending: false;
14
+ isSuccess: false;
15
+ status: "idle";
16
+ mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
17
+ reset: () => void;
18
+ context: unknown;
19
+ failureCount: number;
20
+ failureReason: IRequestError | null;
21
+ isPaused: boolean;
22
+ submittedAt: number;
23
+ mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
24
+ put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
25
+ uploadProgressPercent: number;
26
+ } | {
27
+ isLoading: boolean;
28
+ data: undefined;
29
+ variables: void;
30
+ error: null;
31
+ isError: false;
32
+ isIdle: false;
33
+ isPending: true;
34
+ isSuccess: false;
35
+ status: "pending";
36
+ mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
37
+ reset: () => void;
38
+ context: unknown;
39
+ failureCount: number;
40
+ failureReason: IRequestError | null;
41
+ isPaused: boolean;
42
+ submittedAt: number;
43
+ mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
44
+ put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
45
+ uploadProgressPercent: number;
46
+ } | {
47
+ isLoading: boolean;
48
+ data: undefined;
49
+ error: IRequestError;
50
+ variables: void;
51
+ isError: true;
52
+ isIdle: false;
53
+ isPending: false;
54
+ isSuccess: false;
55
+ status: "error";
56
+ mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
57
+ reset: () => void;
58
+ context: unknown;
59
+ failureCount: number;
60
+ failureReason: IRequestError | null;
61
+ isPaused: boolean;
62
+ submittedAt: number;
63
+ mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
64
+ put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
65
+ uploadProgressPercent: number;
66
+ } | {
67
+ isLoading: boolean;
68
+ data: IRequestSuccess<TResponse>;
69
+ error: null;
70
+ variables: void;
71
+ isError: false;
72
+ isIdle: false;
73
+ isPending: false;
74
+ isSuccess: true;
75
+ status: "success";
76
+ mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
77
+ reset: () => void;
78
+ context: unknown;
79
+ failureCount: number;
80
+ failureReason: IRequestError | null;
81
+ isPaused: boolean;
82
+ submittedAt: number;
83
+ mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
84
+ put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
85
+ uploadProgressPercent: number;
86
+ };
@@ -1,6 +1,6 @@
1
1
  import axios from 'axios';
2
2
  import { axiosInstance } from './axios-instance.js';
3
- import { ContentType, HttpMethod } from './request.enum.js';
3
+ import { HttpMethod, ContentType } from './request.enum.js';
4
4
  import { errorTransformer, successTransformer } from './transformer.js';
5
5
 
6
6
  async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
@@ -11,38 +11,19 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
11
11
  // configure body
12
12
  body = (isFormData ? axios.toFormData(body) : body);
13
13
  // configure request header1
14
- if (!isFormData) {
15
- headers['Content-Type'] = ContentType.APPLICATION_JSON;
16
- }
17
- else {
18
- if (isApp) {
19
- headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
20
- // add the app files
21
- for (const fileKey in appFiles) {
22
- const currentFile = appFiles[fileKey];
23
- if (Array.isArray(currentFile)) {
24
- for (const innerFile of currentFile) {
25
- body.append(fileKey, innerFile);
26
- }
27
- }
28
- else {
29
- body.append(fileKey, currentFile);
30
- }
31
- }
32
- }
33
- else {
34
- delete headers['Content-Type'];
35
- }
36
- }
14
+ configureRequestHeader(isFormData, headers, isApp, appFiles, body);
37
15
  try {
38
16
  const axiosRequest = axiosInstance({ baseURL, headers, timeout });
39
- // send request
40
- const resp = await axiosRequest({
17
+ const axiosRequestConfig = {
41
18
  url: path,
42
19
  method,
43
- data: body,
44
20
  onUploadProgress,
45
- });
21
+ };
22
+ if (Object.keys(body).length > 0) {
23
+ axiosRequestConfig.data = body;
24
+ }
25
+ // send request
26
+ const resp = await axiosRequest(axiosRequestConfig);
46
27
  // get response json
47
28
  const jsonResp = await resp.data;
48
29
  // get response code
@@ -67,6 +48,29 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
67
48
  });
68
49
  }
69
50
  }
51
+ const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
52
+ if (!isFormData) {
53
+ headers['Content-Type'] = ContentType.APPLICATION_JSON;
54
+ }
55
+ else if (isApp) {
56
+ headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
57
+ // add the app files
58
+ for (const fileKey in appFiles) {
59
+ const currentFile = appFiles[fileKey];
60
+ if (Array.isArray(currentFile)) {
61
+ for (const innerFile of currentFile) {
62
+ body.append(fileKey, innerFile);
63
+ }
64
+ }
65
+ else {
66
+ body.append(fileKey, currentFile);
67
+ }
68
+ }
69
+ }
70
+ else {
71
+ delete headers['Content-Type'];
72
+ }
73
+ };
70
74
  function getAppFiles(body, fileSelectors = []) {
71
75
  const files = {};
72
76
  if (body) {
@@ -1 +1 @@
1
- {"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -47,7 +47,7 @@
47
47
  "@rollup/plugin-json": "^6.0.0",
48
48
  "@rollup/plugin-node-resolve": "^15.0.1",
49
49
  "@rollup/plugin-typescript": "^11.0.0",
50
- "@tanstack/react-query": "^5.29.2",
50
+ "@tanstack/react-query": "5.45.1",
51
51
  "@testing-library/react": "^13.1",
52
52
  "@testing-library/react-hooks": "^8.0.1",
53
53
  "@types/axios": "^0.14.0",
@@ -75,7 +75,7 @@ export const useQueryModel = <T>(keyTracker: string): QueryModelBuilder<T> => {
75
75
  newData = lodashSet(data, path, newData);
76
76
  }
77
77
 
78
- return queryClient.setQueryData(queryKey, newData as DataType) as DataType;
78
+ return queryClient.setQueryData(queryKey, () => newData as any) as DataType;
79
79
  };
80
80
 
81
81
  const getModelConfig = () => {
@@ -79,7 +79,9 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
79
79
  ): Promise<IRequestSuccess<TResponse> | undefined> => {
80
80
  if (!isFutureQueriesPaused) {
81
81
  // set enabled to be true for every delete
82
- internalDeleteOptions = internalDeleteOptions ?? { queryKey: [] };
82
+ internalDeleteOptions = internalDeleteOptions
83
+ ? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
84
+ : { queryKey: [link, {}], enabled: true };
83
85
 
84
86
  await setOptionsAsync(internalDeleteOptions);
85
87
  await updatedPathAsync(link);
@@ -1,4 +1,4 @@
1
- import type { QueryKey, 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
3
  import { startTransition, useEffect, useMemo, useState } from 'react';
4
4
  import { useEnvironmentVariables, useQueryConfig } from '../config';
@@ -110,7 +110,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
110
110
  return pathname + '?' + queryParams.toString();
111
111
  };
112
112
 
113
- const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>({
113
+ const query = useInfiniteQuery<any, any, InfiniteData<IRequestSuccess<TResponse & { pagination: Pagination }>>>({
114
114
  queryKey: [requestPath, {}],
115
115
  queryFn: ({ pageParam = requestPath, queryKey }) =>
116
116
  new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
@@ -137,10 +137,12 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
137
137
  Array<any>
138
138
  >
139
139
  ): Promise<
140
- | IRequestSuccess<
141
- TResponse & {
142
- pagination: Pagination;
143
- }
140
+ | InfiniteData<
141
+ IRequestSuccess<
142
+ TResponse & {
143
+ pagination: Pagination;
144
+ }
145
+ >
144
146
  >
145
147
  | undefined
146
148
  > => {
@@ -64,11 +64,11 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
64
64
 
65
65
  // register post mutation
66
66
  const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>({
67
+ mutationKey: [path, { type: 'mutation' }],
67
68
  mutationFn: (dataData: any) =>
68
69
  new Promise<IRequestSuccess<TResponse>>((res, rej) => {
69
70
  return sendRequest(res, rej, dataData);
70
71
  }),
71
- mutationKey: [path, { type: 'mutation' }],
72
72
  });
73
73
 
74
74
  const patch = async (
@@ -96,9 +96,9 @@ export const usePostRequest = <TResponse>({
96
96
  IRequestError,
97
97
  { data: any; requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
98
98
  >({
99
+ mutationKey: [path, { type: 'mutation' }],
99
100
  mutationFn: async (postData) =>
100
101
  new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)),
101
- mutationKey: [path, { type: 'mutation' }],
102
102
  });
103
103
 
104
104
  const post = async <T>(
@@ -0,0 +1,95 @@
1
+ import type { MutateOptions } from '@tanstack/react-query';
2
+ import { useMutation } from '@tanstack/react-query';
3
+ import { useEffect, useState } from 'react';
4
+ import { useEnvironmentVariables, useQueryConfig } from '../config';
5
+ import { scrollToTop } from '../helpers';
6
+ import { useUploadProgress } from '../hooks';
7
+ import { HttpMethod, makeRequest } from '../request';
8
+ import type { IRequestError, IRequestSuccess } from '../request/request.interface';
9
+ import { useHeaderStore, usePauseFutureRequests } from '../stores';
10
+ import type { DefaultRequestOptions } from './queries.interface';
11
+
12
+ export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
13
+ const { API_URL, TIMEOUT } = useEnvironmentVariables();
14
+ const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
15
+ const globalHeaders = useHeaderStore((state) => state.headers);
16
+
17
+ const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
18
+
19
+ const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
20
+
21
+ const config = useQueryConfig();
22
+
23
+ const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
24
+ // get request headers
25
+
26
+ const requestOptions = {
27
+ path: path,
28
+ body: data,
29
+ method: HttpMethod.PUT,
30
+ headers: { ...globalHeaders, ...headers },
31
+ baseURL: baseUrl ?? API_URL,
32
+ timeout: TIMEOUT,
33
+ onUploadProgress,
34
+ };
35
+
36
+ let shouldContinue = true;
37
+
38
+ if (config.options?.mutationMiddleware) {
39
+ shouldContinue = await config.options.mutationMiddleware({
40
+ mutationKey: [path, { type: 'mutation' }],
41
+ ...requestOptions,
42
+ });
43
+ }
44
+
45
+ if (shouldContinue) {
46
+ const putResponse = await makeRequest<TResponse>(requestOptions);
47
+ if (putResponse.status) {
48
+ // scroll to top after success
49
+ if (config.options?.context !== 'app') {
50
+ scrollToTop();
51
+ }
52
+ res(putResponse as IRequestSuccess<TResponse>);
53
+ } else {
54
+ // scroll to top after error
55
+ if (config.options?.context !== 'app') {
56
+ scrollToTop();
57
+ }
58
+ rej(putResponse);
59
+ }
60
+ } else {
61
+ rej(null);
62
+ }
63
+ };
64
+
65
+ // register post mutation
66
+ const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>({
67
+ mutationKey: [path, { type: 'mutation' }],
68
+ mutationFn: (dataData: any) =>
69
+ new Promise<IRequestSuccess<TResponse>>((res, rej) => {
70
+ return sendRequest(res, rej, dataData);
71
+ }),
72
+ });
73
+
74
+ const put = async (
75
+ data: any,
76
+ options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined
77
+ ): Promise<IRequestSuccess<TResponse> | undefined> => {
78
+ if (!isFutureMutationsPaused) {
79
+ return mutation.mutateAsync(data, options);
80
+ } else {
81
+ setRequestPayload({ data, options });
82
+ return undefined;
83
+ }
84
+ };
85
+
86
+ useEffect(() => {
87
+ if (!isFutureMutationsPaused && requestPayload) {
88
+ put(requestPayload.data, requestPayload.options);
89
+ setRequestPayload(undefined);
90
+ }
91
+ // eslint-disable-next-line react-hooks/exhaustive-deps
92
+ }, [isFutureMutationsPaused]);
93
+
94
+ return { put, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
95
+ };
@@ -1,3 +1,4 @@
1
+ import type { AxiosRequestConfig, RawAxiosRequestHeaders } from 'axios';
1
2
  import axios from 'axios';
2
3
  import { axiosInstance } from './axios-instance';
3
4
 
@@ -25,37 +26,23 @@ export async function makeRequest<TResponse>({
25
26
  body = (isFormData ? axios.toFormData(body as FormData) : body) as FormData;
26
27
 
27
28
  // configure request header1
28
- if (!isFormData) {
29
- headers['Content-Type'] = ContentType.APPLICATION_JSON;
30
- } else {
31
- if (isApp) {
32
- headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
33
- // add the app files
34
- for (const fileKey in appFiles) {
35
- const currentFile = appFiles[fileKey];
36
- if (Array.isArray(currentFile)) {
37
- for (const innerFile of currentFile) {
38
- body.append(fileKey, innerFile);
39
- }
40
- } else {
41
- body.append(fileKey, currentFile);
42
- }
43
- }
44
- } else {
45
- delete headers['Content-Type'];
46
- }
47
- }
29
+ configureRequestHeader(isFormData, headers, isApp, appFiles, body);
48
30
 
49
31
  try {
50
32
  const axiosRequest = axiosInstance({ baseURL, headers, timeout });
51
33
 
52
- // send request
53
- const resp = await axiosRequest({
34
+ const axiosRequestConfig: AxiosRequestConfig<Record<string, any>> = {
54
35
  url: path,
55
36
  method,
56
- data: body,
57
37
  onUploadProgress,
58
- });
38
+ };
39
+
40
+ if (Object.keys(body).length > 0) {
41
+ axiosRequestConfig.data = body;
42
+ }
43
+
44
+ // send request
45
+ const resp = await axiosRequest(axiosRequestConfig);
59
46
 
60
47
  // get response json
61
48
  const jsonResp: any = await resp.data;
@@ -83,6 +70,34 @@ export async function makeRequest<TResponse>({
83
70
  });
84
71
  }
85
72
  }
73
+
74
+ const configureRequestHeader = (
75
+ isFormData: boolean | undefined,
76
+ headers: RawAxiosRequestHeaders,
77
+ isApp: boolean | undefined,
78
+ appFiles: Record<string, string>,
79
+ body: Record<string, any>
80
+ ) => {
81
+ if (!isFormData) {
82
+ headers['Content-Type'] = ContentType.APPLICATION_JSON;
83
+ } else if (isApp) {
84
+ headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
85
+ // add the app files
86
+ for (const fileKey in appFiles) {
87
+ const currentFile = appFiles[fileKey];
88
+ if (Array.isArray(currentFile)) {
89
+ for (const innerFile of currentFile) {
90
+ body.append(fileKey, innerFile);
91
+ }
92
+ } else {
93
+ body.append(fileKey, currentFile);
94
+ }
95
+ }
96
+ } else {
97
+ delete headers['Content-Type'];
98
+ }
99
+ };
100
+
86
101
  function getAppFiles(body: any, fileSelectors: string[] = []) {
87
102
  const files: Record<string, string> = {};
88
103