@ventlio/tanstack-query 0.2.45 → 0.2.47

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
@@ -200,30 +200,30 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
200
200
  }
201
201
  }
202
202
 
203
- const useDeleteRequest = () => {
203
+ const useDeleteRequest = (deleteOptions) => {
204
+ const { baseUrl, headers } = deleteOptions ?? {};
204
205
  const [requestPath, updateDeletePath] = useState('');
205
206
  const [options, setOptions] = useState();
206
207
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
207
208
  const { getHeaders } = useQueryHeaders();
208
- const query = useQuery([requestPath, {}], () => new Promise((res, rej) => {
209
- setTimeout(async () => {
210
- // get request headers
211
- const headers = getHeaders();
212
- const postResponse = await makeRequest({
213
- path: requestPath,
214
- headers,
215
- method: HttpMethod.DELETE,
216
- baseURL: API_URL,
217
- timeout: TIMEOUT,
218
- });
219
- if (postResponse.status) {
220
- res(postResponse);
221
- }
222
- else {
223
- rej(postResponse);
224
- }
225
- }, 200);
226
- }), { enabled: false, ...options });
209
+ const sendRequest = async (res, rej) => {
210
+ // get request headers
211
+ const globalHeaders = getHeaders();
212
+ const postResponse = await makeRequest({
213
+ path: requestPath,
214
+ headers: { ...globalHeaders, ...headers },
215
+ method: HttpMethod.DELETE,
216
+ baseURL: baseUrl ?? API_URL,
217
+ timeout: TIMEOUT,
218
+ });
219
+ if (postResponse.status) {
220
+ res(postResponse);
221
+ }
222
+ else {
223
+ rej(postResponse);
224
+ }
225
+ };
226
+ const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), { enabled: false, ...options });
227
227
  const updatedPathAsync = async (link) => {
228
228
  return updateDeletePath(link);
229
229
  };
@@ -241,7 +241,7 @@ const useDeleteRequest = () => {
241
241
  return { destroy, ...query };
242
242
  };
243
243
 
244
- const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
244
+ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
245
245
  const [requestPath, updatePath] = useState(path);
246
246
  const [options, setOptions] = useState(queryOptions);
247
247
  const [page, setPage] = useState(1);
@@ -252,11 +252,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
252
252
  queryClient = useMemo(() => queryClient, []);
253
253
  const sendRequest = async (res, rej) => {
254
254
  // get request headers
255
- const headers = getHeaders();
255
+ const globalHeaders = getHeaders();
256
256
  const getResponse = await makeRequest({
257
257
  path: requestPath,
258
- headers,
259
- baseURL: API_URL,
258
+ headers: { ...globalHeaders, ...headers },
259
+ baseURL: baseUrl ?? API_URL,
260
260
  timeout: TIMEOUT,
261
261
  });
262
262
  if (getResponse.status) {
@@ -266,9 +266,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
266
266
  rej(getResponse);
267
267
  }
268
268
  };
269
- const query = useQuery([requestPath, {}], () => new Promise((res, rej) => {
270
- return sendRequest(res, rej);
271
- }), {
269
+ const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
272
270
  enabled: load,
273
271
  ...options,
274
272
  });
@@ -348,82 +346,82 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
348
346
  };
349
347
  };
350
348
 
351
- const usePatchRequest = ({ path }) => {
349
+ const usePatchRequest = ({ path, baseUrl, headers, }) => {
352
350
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
353
351
  const { getHeaders } = useQueryHeaders();
352
+ const sendRequest = async (res, rej, data) => {
353
+ // get request headers
354
+ const globalHeaders = getHeaders();
355
+ makeRequest({
356
+ path: path,
357
+ body: data,
358
+ method: HttpMethod.PATCH,
359
+ headers: { ...globalHeaders, ...headers },
360
+ baseURL: baseUrl ?? API_URL,
361
+ timeout: TIMEOUT,
362
+ }).then((postResponse) => {
363
+ if (postResponse.status) {
364
+ // scroll to top after success
365
+ scrollToTop();
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
+ });
377
+ };
354
378
  // register post mutation
355
- const mutation = useMutation((postData) => new Promise((res, rej) => {
356
- return (async () => {
357
- // get request headers
358
- const headers = getHeaders();
359
- makeRequest({
360
- path: path,
361
- body: postData,
362
- method: HttpMethod.PATCH,
363
- headers,
364
- baseURL: API_URL,
365
- timeout: TIMEOUT,
366
- }).then((postResponse) => {
367
- if (postResponse.status) {
368
- // scroll to top after success
369
- scrollToTop();
370
- res(postResponse);
371
- }
372
- else {
373
- // scroll to top after error
374
- window.scrollTo({
375
- top: 0,
376
- behavior: 'smooth',
377
- });
378
- rej(postResponse);
379
- }
380
- });
381
- })();
379
+ const mutation = useMutation((dataData) => new Promise((res, rej) => {
380
+ return sendRequest(res, rej, dataData);
382
381
  }));
383
- const patch = async (postData, options) => {
384
- return mutation.mutateAsync(postData, options);
382
+ const patch = async (data, options) => {
383
+ return mutation.mutateAsync(data, options);
385
384
  };
386
385
  return { patch, ...mutation };
387
386
  };
388
387
 
389
- const usePostRequest = ({ path, isFormData = false, }) => {
388
+ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
390
389
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
391
390
  const { getHeaders } = useQueryHeaders();
391
+ const sendRequest = async (res, rej, postData) => {
392
+ // get request headers
393
+ const globalHeaders = getHeaders();
394
+ makeRequest({
395
+ path: path,
396
+ body: postData,
397
+ method: HttpMethod.POST,
398
+ isFormData,
399
+ headers: { ...globalHeaders, ...headers },
400
+ baseURL: baseUrl ?? API_URL,
401
+ timeout: TIMEOUT,
402
+ }).then((postResponse) => {
403
+ if (postResponse.status) {
404
+ // scroll to top after success
405
+ window.scrollTo({
406
+ top: 0,
407
+ behavior: 'smooth',
408
+ });
409
+ res(postResponse);
410
+ }
411
+ else {
412
+ // scroll to top after error
413
+ window.scrollTo({
414
+ top: 0,
415
+ behavior: 'smooth',
416
+ });
417
+ rej(postResponse);
418
+ }
419
+ });
420
+ };
392
421
  // register post mutation
393
- const mutation = useMutation(async (postData) => new Promise((res, rej) => {
394
- return (async () => {
395
- // get request headers
396
- const headers = getHeaders();
397
- makeRequest({
398
- path: path,
399
- body: postData,
400
- method: HttpMethod.POST,
401
- isFormData,
402
- headers,
403
- baseURL: API_URL,
404
- timeout: TIMEOUT,
405
- }).then((postResponse) => {
406
- if (postResponse.status) {
407
- // scroll to top after success
408
- window.scrollTo({
409
- top: 0,
410
- behavior: 'smooth',
411
- });
412
- res(postResponse);
413
- }
414
- else {
415
- // scroll to top after error
416
- window.scrollTo({
417
- top: 0,
418
- behavior: 'smooth',
419
- });
420
- rej(postResponse);
421
- }
422
- });
423
- })();
424
- }));
425
- const post = async (postData, options) => {
426
- return mutation.mutateAsync(postData, options);
422
+ const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
423
+ const post = async (data, options) => {
424
+ return mutation.mutateAsync(data, options);
427
425
  };
428
426
  return { post, ...mutation };
429
427
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,4 +1,5 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
+ import type { RawAxiosRequestHeaders } from 'axios';
2
3
  import type { IRequestError, IRequestSuccess } from '../request';
3
4
  export interface IPagination {
4
5
  current_page: number;
@@ -9,3 +10,7 @@ export interface IPagination {
9
10
  total: number;
10
11
  }
11
12
  export type TanstackQueryOption<TResponse> = UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, Array<any>>;
13
+ export interface DefaultRequestOptions {
14
+ baseUrl?: string;
15
+ headers?: RawAxiosRequestHeaders;
16
+ }
@@ -1,6 +1,7 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
2
  import type { IRequestError, IRequestSuccess } from '../request';
3
- export declare const useDeleteRequest: <TResponse>() => {
3
+ import type { DefaultRequestOptions } from './queries.interface';
4
+ export declare const useDeleteRequest: <TResponse>(deleteOptions: DefaultRequestOptions | undefined) => {
4
5
  data: IRequestSuccess<TResponse>;
5
6
  error: any;
6
7
  isError: true;
@@ -8,30 +8,30 @@ require('axios');
8
8
  var makeRequest = require('../request/make-request.js');
9
9
  var request_enum = require('../request/request.enum.js');
10
10
 
11
- const useDeleteRequest = () => {
11
+ const useDeleteRequest = (deleteOptions) => {
12
+ const { baseUrl, headers } = deleteOptions ?? {};
12
13
  const [requestPath, updateDeletePath] = react.useState('');
13
14
  const [options, setOptions] = react.useState();
14
15
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
15
16
  const { getHeaders } = useQueryHeaders.useQueryHeaders();
16
- const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => {
17
- setTimeout(async () => {
18
- // get request headers
19
- const headers = getHeaders();
20
- const postResponse = await makeRequest.makeRequest({
21
- path: requestPath,
22
- headers,
23
- method: request_enum.HttpMethod.DELETE,
24
- baseURL: API_URL,
25
- timeout: TIMEOUT,
26
- });
27
- if (postResponse.status) {
28
- res(postResponse);
29
- }
30
- else {
31
- rej(postResponse);
32
- }
33
- }, 200);
34
- }), { enabled: false, ...options });
17
+ const sendRequest = async (res, rej) => {
18
+ // get request headers
19
+ const globalHeaders = getHeaders();
20
+ const postResponse = await makeRequest.makeRequest({
21
+ path: requestPath,
22
+ headers: { ...globalHeaders, ...headers },
23
+ method: request_enum.HttpMethod.DELETE,
24
+ baseURL: baseUrl ?? API_URL,
25
+ timeout: TIMEOUT,
26
+ });
27
+ if (postResponse.status) {
28
+ res(postResponse);
29
+ }
30
+ else {
31
+ rej(postResponse);
32
+ }
33
+ };
34
+ const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), { enabled: false, ...options });
35
35
  const updatedPathAsync = async (link) => {
36
36
  return updateDeletePath(link);
37
37
  };
@@ -1,13 +1,13 @@
1
1
  /// <reference types="react" />
2
2
  import type { UseQueryOptions } from '@tanstack/react-query';
3
3
  import type { IRequestError, IRequestSuccess } from '../request';
4
- import type { TanstackQueryOption } from './queries.interface';
5
- export declare const useGetRequest: <TResponse extends Record<string, any>>({ path, load, queryOptions, keyTracker, }: {
4
+ import type { DefaultRequestOptions, TanstackQueryOption } from './queries.interface';
5
+ export declare const useGetRequest: <TResponse extends Record<string, any>>({ path, load, queryOptions, keyTracker, baseUrl, headers, }: {
6
6
  path: string;
7
7
  load?: boolean | undefined;
8
8
  queryOptions?: TanstackQueryOption<TResponse> | undefined;
9
9
  keyTracker?: string | undefined;
10
- }) => {
10
+ } & DefaultRequestOptions) => {
11
11
  updatePath: import("react").Dispatch<import("react").SetStateAction<string>>;
12
12
  nextPage: () => void;
13
13
  prevPage: () => void;
@@ -8,7 +8,7 @@ require('axios');
8
8
  var makeRequest = require('../request/make-request.js');
9
9
  require('../request/request.enum.js');
10
10
 
11
- const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
11
+ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
12
12
  const [requestPath, updatePath] = react.useState(path);
13
13
  const [options, setOptions] = react.useState(queryOptions);
14
14
  const [page, setPage] = react.useState(1);
@@ -19,11 +19,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
19
19
  queryClient = react.useMemo(() => queryClient, []);
20
20
  const sendRequest = async (res, rej) => {
21
21
  // get request headers
22
- const headers = getHeaders();
22
+ const globalHeaders = getHeaders();
23
23
  const getResponse = await makeRequest.makeRequest({
24
24
  path: requestPath,
25
- headers,
26
- baseURL: API_URL,
25
+ headers: { ...globalHeaders, ...headers },
26
+ baseURL: baseUrl ?? API_URL,
27
27
  timeout: TIMEOUT,
28
28
  });
29
29
  if (getResponse.status) {
@@ -33,9 +33,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
33
33
  rej(getResponse);
34
34
  }
35
35
  };
36
- const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => {
37
- return sendRequest(res, rej);
38
- }), {
36
+ const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
39
37
  enabled: load,
40
38
  ...options,
41
39
  });
@@ -1 +1 @@
1
- {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,8 +1,9 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
2
  import type { IRequestError, IRequestSuccess } from '../request/request.interface';
3
- export declare const usePatchRequest: <TResponse>({ path }: {
3
+ import type { DefaultRequestOptions } from './queries.interface';
4
+ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers, }: {
4
5
  path: string;
5
- }) => {
6
+ } & DefaultRequestOptions) => {
6
7
  data: undefined;
7
8
  error: null;
8
9
  isError: false;
@@ -18,7 +19,7 @@ export declare const usePatchRequest: <TResponse>({ path }: {
18
19
  isPaused: boolean;
19
20
  variables: void | undefined;
20
21
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
21
- patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
22
+ patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
22
23
  } | {
23
24
  data: undefined;
24
25
  error: null;
@@ -35,7 +36,7 @@ export declare const usePatchRequest: <TResponse>({ path }: {
35
36
  isPaused: boolean;
36
37
  variables: void | undefined;
37
38
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
38
- patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
39
+ patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
39
40
  } | {
40
41
  data: undefined;
41
42
  error: IRequestError;
@@ -52,7 +53,7 @@ export declare const usePatchRequest: <TResponse>({ path }: {
52
53
  isPaused: boolean;
53
54
  variables: void | undefined;
54
55
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
55
- patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
56
+ patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
56
57
  } | {
57
58
  data: IRequestSuccess<TResponse>;
58
59
  error: null;
@@ -69,5 +70,5 @@ export declare const usePatchRequest: <TResponse>({ path }: {
69
70
  isPaused: boolean;
70
71
  variables: void | undefined;
71
72
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
72
- patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
73
+ patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
73
74
  };
@@ -8,40 +8,41 @@ require('axios');
8
8
  var makeRequest = require('../request/make-request.js');
9
9
  var request_enum = require('../request/request.enum.js');
10
10
 
11
- const usePatchRequest = ({ path }) => {
11
+ const usePatchRequest = ({ path, baseUrl, headers, }) => {
12
12
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
13
13
  const { getHeaders } = useQueryHeaders.useQueryHeaders();
14
+ const sendRequest = async (res, rej, data) => {
15
+ // get request headers
16
+ const globalHeaders = getHeaders();
17
+ makeRequest.makeRequest({
18
+ path: path,
19
+ body: data,
20
+ method: request_enum.HttpMethod.PATCH,
21
+ headers: { ...globalHeaders, ...headers },
22
+ baseURL: baseUrl ?? API_URL,
23
+ timeout: TIMEOUT,
24
+ }).then((postResponse) => {
25
+ if (postResponse.status) {
26
+ // scroll to top after success
27
+ scrollToTop.scrollToTop();
28
+ res(postResponse);
29
+ }
30
+ else {
31
+ // scroll to top after error
32
+ window.scrollTo({
33
+ top: 0,
34
+ behavior: 'smooth',
35
+ });
36
+ rej(postResponse);
37
+ }
38
+ });
39
+ };
14
40
  // register post mutation
15
- const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
16
- return (async () => {
17
- // get request headers
18
- const headers = getHeaders();
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
- })();
41
+ const mutation = reactQuery.useMutation((dataData) => new Promise((res, rej) => {
42
+ return sendRequest(res, rej, dataData);
42
43
  }));
43
- const patch = async (postData, options) => {
44
- return mutation.mutateAsync(postData, options);
44
+ const patch = async (data, options) => {
45
+ return mutation.mutateAsync(data, options);
45
46
  };
46
47
  return { patch, ...mutation };
47
48
  };
@@ -1 +1 @@
1
- {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,9 +1,10 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
2
  import type { IRequestError, IRequestSuccess } from '../request';
3
- export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
3
+ import type { DefaultRequestOptions } from './queries.interface';
4
+ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, headers, }: {
4
5
  path: string;
5
6
  isFormData?: boolean | undefined;
6
- }) => {
7
+ } & DefaultRequestOptions) => {
7
8
  data: undefined;
8
9
  error: null;
9
10
  isError: false;
@@ -19,7 +20,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
19
20
  isPaused: boolean;
20
21
  variables: void | undefined;
21
22
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
22
- post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
23
+ post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
23
24
  } | {
24
25
  data: undefined;
25
26
  error: null;
@@ -36,7 +37,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
36
37
  isPaused: boolean;
37
38
  variables: void | undefined;
38
39
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
39
- post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
40
+ post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
40
41
  } | {
41
42
  data: undefined;
42
43
  error: IRequestError;
@@ -53,7 +54,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
53
54
  isPaused: boolean;
54
55
  variables: void | undefined;
55
56
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
56
- post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
57
+ post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
57
58
  } | {
58
59
  data: IRequestSuccess<TResponse>;
59
60
  error: null;
@@ -70,5 +71,5 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
70
71
  isPaused: boolean;
71
72
  variables: void | undefined;
72
73
  mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
73
- post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
74
+ post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
74
75
  };
@@ -7,44 +7,43 @@ require('axios');
7
7
  var makeRequest = require('../request/make-request.js');
8
8
  var request_enum = require('../request/request.enum.js');
9
9
 
10
- const usePostRequest = ({ path, isFormData = false, }) => {
10
+ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
11
11
  const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
12
12
  const { getHeaders } = useQueryHeaders.useQueryHeaders();
13
+ const sendRequest = async (res, rej, postData) => {
14
+ // get request headers
15
+ const globalHeaders = getHeaders();
16
+ makeRequest.makeRequest({
17
+ path: path,
18
+ body: postData,
19
+ method: request_enum.HttpMethod.POST,
20
+ isFormData,
21
+ headers: { ...globalHeaders, ...headers },
22
+ baseURL: baseUrl ?? API_URL,
23
+ timeout: TIMEOUT,
24
+ }).then((postResponse) => {
25
+ if (postResponse.status) {
26
+ // scroll to top after success
27
+ window.scrollTo({
28
+ top: 0,
29
+ behavior: 'smooth',
30
+ });
31
+ res(postResponse);
32
+ }
33
+ else {
34
+ // scroll to top after error
35
+ window.scrollTo({
36
+ top: 0,
37
+ behavior: 'smooth',
38
+ });
39
+ rej(postResponse);
40
+ }
41
+ });
42
+ };
13
43
  // register post mutation
14
- const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => {
15
- return (async () => {
16
- // get request headers
17
- const headers = getHeaders();
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
- })();
45
- }));
46
- const post = async (postData, options) => {
47
- return mutation.mutateAsync(postData, options);
44
+ const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
45
+ const post = async (data, options) => {
46
+ return mutation.mutateAsync(data, options);
48
47
  };
49
48
  return { post, ...mutation };
50
49
  };
@@ -1 +1 @@
1
- {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.45",
3
+ "version": "0.2.47",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -1,4 +1,5 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
+ import type { RawAxiosRequestHeaders } from 'axios';
2
3
  import type { IRequestError, IRequestSuccess } from '../request';
3
4
 
4
5
  export interface IPagination {
@@ -16,3 +17,8 @@ export type TanstackQueryOption<TResponse> = UseQueryOptions<
16
17
  IRequestSuccess<TResponse | undefined>,
17
18
  Array<any>
18
19
  >;
20
+
21
+ export interface DefaultRequestOptions {
22
+ baseUrl?: string;
23
+ headers?: RawAxiosRequestHeaders;
24
+ }
@@ -5,8 +5,12 @@ import { useState } from 'react';
5
5
  import { useEnvironmentVariables, useQueryHeaders } from '../config';
6
6
  import type { IRequestError, IRequestSuccess } from '../request';
7
7
  import { HttpMethod, makeRequest } from '../request';
8
+ import type { DefaultRequestOptions } from './queries.interface';
8
9
 
9
- export const useDeleteRequest = <TResponse>() => {
10
+ export const useDeleteRequest = <TResponse>(
11
+ deleteOptions: DefaultRequestOptions | undefined
12
+ ) => {
13
+ const { baseUrl, headers } = deleteOptions ?? {};
10
14
  const [requestPath, updateDeletePath] = useState<string>('');
11
15
  const [options, setOptions] = useState<any>();
12
16
 
@@ -14,28 +18,33 @@ export const useDeleteRequest = <TResponse>() => {
14
18
 
15
19
  const { getHeaders } = useQueryHeaders();
16
20
 
21
+ const sendRequest = async (
22
+ res: (value: any) => void,
23
+ rej: (reason?: any) => void
24
+ ) => {
25
+ // get request headers
26
+ const globalHeaders: RawAxiosRequestHeaders = getHeaders();
27
+
28
+ const postResponse = await makeRequest<TResponse>({
29
+ path: requestPath,
30
+ headers: { ...globalHeaders, ...headers },
31
+ method: HttpMethod.DELETE,
32
+ baseURL: baseUrl ?? API_URL,
33
+ timeout: TIMEOUT,
34
+ });
35
+ if (postResponse.status) {
36
+ res(postResponse as IRequestSuccess<TResponse>);
37
+ } else {
38
+ rej(postResponse);
39
+ }
40
+ };
41
+
17
42
  const query = useQuery<any, any, IRequestSuccess<TResponse>>(
18
43
  [requestPath, {}],
19
44
  () =>
20
- new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => {
21
- setTimeout(async () => {
22
- // get request headers
23
- const headers: RawAxiosRequestHeaders = getHeaders();
24
-
25
- const postResponse = await makeRequest<TResponse>({
26
- path: requestPath,
27
- headers,
28
- method: HttpMethod.DELETE,
29
- baseURL: API_URL,
30
- timeout: TIMEOUT,
31
- });
32
- if (postResponse.status) {
33
- res(postResponse as IRequestSuccess<TResponse>);
34
- } else {
35
- rej(postResponse);
36
- }
37
- }, 200);
38
- }),
45
+ new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) =>
46
+ sendRequest(res, rej)
47
+ ),
39
48
  { enabled: false, ...options }
40
49
  );
41
50
 
@@ -1,24 +1,30 @@
1
1
  import type { UseQueryOptions } from '@tanstack/react-query';
2
2
  import { useQuery, useQueryClient } from '@tanstack/react-query';
3
- import type { RawAxiosRequestHeaders } from 'axios';
4
3
  import { startTransition, useEffect, useMemo, useState } from 'react';
4
+ import type { RawAxiosRequestHeaders } from '../../node_modules/axios/index';
5
5
  import { useEnvironmentVariables, useQueryHeaders } from '../config';
6
6
 
7
7
  import type { IRequestError, IRequestSuccess } from '../request';
8
8
  import { makeRequest } from '../request';
9
- import type { IPagination, TanstackQueryOption } from './queries.interface';
9
+ import type {
10
+ DefaultRequestOptions,
11
+ IPagination,
12
+ TanstackQueryOption,
13
+ } from './queries.interface';
10
14
 
11
15
  export const useGetRequest = <TResponse extends Record<string, any>>({
12
16
  path,
13
17
  load = false,
14
18
  queryOptions,
15
19
  keyTracker,
20
+ baseUrl,
21
+ headers,
16
22
  }: {
17
23
  path: string;
18
24
  load?: boolean;
19
25
  queryOptions?: TanstackQueryOption<TResponse>;
20
26
  keyTracker?: string;
21
- }) => {
27
+ } & DefaultRequestOptions) => {
22
28
  const [requestPath, updatePath] = useState<string>(path);
23
29
  const [options, setOptions] = useState<any>(queryOptions);
24
30
  const [page, setPage] = useState<number>(1);
@@ -41,12 +47,12 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
41
47
  rej: (reason?: any) => void
42
48
  ) => {
43
49
  // get request headers
44
- const headers: RawAxiosRequestHeaders = getHeaders();
50
+ const globalHeaders: RawAxiosRequestHeaders = getHeaders();
45
51
 
46
52
  const getResponse = await makeRequest<TResponse>({
47
53
  path: requestPath,
48
- headers,
49
- baseURL: API_URL,
54
+ headers: { ...globalHeaders, ...headers },
55
+ baseURL: baseUrl ?? API_URL,
50
56
  timeout: TIMEOUT,
51
57
  });
52
58
  if (getResponse.status) {
@@ -59,9 +65,9 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
59
65
  const query = useQuery<any, any, IRequestSuccess<TResponse>>(
60
66
  [requestPath, {}],
61
67
  () =>
62
- new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => {
63
- return sendRequest(res, rej);
64
- }),
68
+ new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) =>
69
+ sendRequest(res, rej)
70
+ ),
65
71
  {
66
72
  enabled: load,
67
73
  ...options,
@@ -1,60 +1,70 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
2
  import { useMutation } from '@tanstack/react-query';
3
+ import type { RawAxiosRequestHeaders } from 'axios';
3
4
  import { useEnvironmentVariables, useQueryHeaders } from '../config';
4
5
  import { scrollToTop } from '../helpers';
5
6
  import { HttpMethod, makeRequest } from '../request';
6
-
7
- import type { RawAxiosRequestHeaders } from 'axios';
8
7
  import type {
9
8
  IRequestError,
10
9
  IRequestSuccess,
11
10
  } from '../request/request.interface';
11
+ import type { DefaultRequestOptions } from './queries.interface';
12
12
 
13
- export const usePatchRequest = <TResponse>({ path }: { path: string }) => {
13
+ export const usePatchRequest = <TResponse>({
14
+ path,
15
+ baseUrl,
16
+ headers,
17
+ }: { path: string } & DefaultRequestOptions) => {
14
18
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
15
19
 
16
20
  const { getHeaders } = useQueryHeaders();
17
21
 
22
+ const sendRequest = async (
23
+ res: (value: any) => void,
24
+ rej: (reason?: any) => void,
25
+ data: any
26
+ ) => {
27
+ // get request headers
28
+ const globalHeaders: RawAxiosRequestHeaders = getHeaders();
29
+
30
+ makeRequest<TResponse>({
31
+ path: path,
32
+ body: data,
33
+ method: HttpMethod.PATCH,
34
+ headers: { ...globalHeaders, ...headers },
35
+ baseURL: baseUrl ?? API_URL,
36
+ timeout: TIMEOUT,
37
+ }).then((postResponse) => {
38
+ if (postResponse.status) {
39
+ // scroll to top after success
40
+ scrollToTop();
41
+ res(postResponse as IRequestSuccess<TResponse>);
42
+ } else {
43
+ // scroll to top after error
44
+ window.scrollTo({
45
+ top: 0,
46
+ behavior: 'smooth',
47
+ });
48
+ rej(postResponse);
49
+ }
50
+ });
51
+ };
52
+
18
53
  // register post mutation
19
54
  const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
20
- (postData: any) =>
55
+ (dataData: any) =>
21
56
  new Promise<IRequestSuccess<TResponse>>((res, rej) => {
22
- return (async () => {
23
- // get request headers
24
- const headers: RawAxiosRequestHeaders = getHeaders();
25
-
26
- makeRequest<TResponse>({
27
- path: path,
28
- body: postData,
29
- method: HttpMethod.PATCH,
30
- headers,
31
- baseURL: API_URL,
32
- timeout: TIMEOUT,
33
- }).then((postResponse) => {
34
- if (postResponse.status) {
35
- // scroll to top after success
36
- scrollToTop();
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
- });
47
- })();
57
+ return sendRequest(res, rej, dataData);
48
58
  })
49
59
  );
50
60
 
51
61
  const patch = async (
52
- postData: any,
62
+ data: any,
53
63
  options?:
54
64
  | MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown>
55
65
  | undefined
56
66
  ): Promise<IRequestSuccess<TResponse>> => {
57
- return mutation.mutateAsync(postData, options);
67
+ return mutation.mutateAsync(data, options);
58
68
  };
59
69
 
60
70
  return { patch, ...mutation };
@@ -5,61 +5,70 @@ import { useEnvironmentVariables, useQueryHeaders } from '../config';
5
5
  import type { RawAxiosRequestHeaders } from 'axios';
6
6
  import type { IRequestError, IRequestSuccess } from '../request';
7
7
  import { HttpMethod, makeRequest } from '../request';
8
+ import type { DefaultRequestOptions } from './queries.interface';
8
9
 
9
10
  export const usePostRequest = <TResponse>({
10
11
  path,
11
12
  isFormData = false,
13
+ baseUrl,
14
+ headers,
12
15
  }: {
13
16
  path: string;
14
17
  isFormData?: boolean;
15
- }) => {
18
+ } & DefaultRequestOptions) => {
16
19
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
17
20
 
18
21
  const { getHeaders } = useQueryHeaders();
19
22
 
23
+ const sendRequest = async (
24
+ res: (value: any) => void,
25
+ rej: (reason?: any) => void,
26
+ postData: any
27
+ ) => {
28
+ // get request headers
29
+ const globalHeaders: RawAxiosRequestHeaders = getHeaders();
30
+
31
+ makeRequest<TResponse>({
32
+ path: path,
33
+ body: postData,
34
+ method: HttpMethod.POST,
35
+ isFormData,
36
+ headers: { ...globalHeaders, ...headers },
37
+ baseURL: baseUrl ?? API_URL,
38
+ timeout: TIMEOUT,
39
+ }).then((postResponse) => {
40
+ if (postResponse.status) {
41
+ // scroll to top after success
42
+ window.scrollTo({
43
+ top: 0,
44
+ behavior: 'smooth',
45
+ });
46
+ res(postResponse as IRequestSuccess<TResponse>);
47
+ } else {
48
+ // scroll to top after error
49
+ window.scrollTo({
50
+ top: 0,
51
+ behavior: 'smooth',
52
+ });
53
+ rej(postResponse);
54
+ }
55
+ });
56
+ };
57
+
20
58
  // register post mutation
21
59
  const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
22
60
  async (postData: any) =>
23
- new Promise<IRequestSuccess<TResponse>>((res, rej) => {
24
- return (async () => {
25
- // get request headers
26
- const headers: RawAxiosRequestHeaders = getHeaders();
27
-
28
- makeRequest<TResponse>({
29
- path: path,
30
- body: postData,
31
- method: HttpMethod.POST,
32
- isFormData,
33
- headers,
34
- baseURL: API_URL,
35
- timeout: TIMEOUT,
36
- }).then((postResponse) => {
37
- if (postResponse.status) {
38
- // scroll to top after success
39
- window.scrollTo({
40
- top: 0,
41
- behavior: 'smooth',
42
- });
43
- res(postResponse as IRequestSuccess<TResponse>);
44
- } else {
45
- // scroll to top after error
46
- window.scrollTo({
47
- top: 0,
48
- behavior: 'smooth',
49
- });
50
- rej(postResponse);
51
- }
52
- });
53
- })();
54
- })
61
+ new Promise<IRequestSuccess<TResponse>>((res, rej) =>
62
+ sendRequest(res, rej, postData)
63
+ )
55
64
  );
56
65
  const post = async (
57
- postData: any,
66
+ data: any,
58
67
  options?:
59
68
  | MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown>
60
69
  | undefined
61
70
  ): Promise<IRequestSuccess<TResponse>> => {
62
- return mutation.mutateAsync(postData, options);
71
+ return mutation.mutateAsync(data, options);
63
72
  };
64
73
 
65
74
  return { post, ...mutation };