@ventlio/tanstack-query 0.1.2 → 0.1.4

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.
Files changed (46) hide show
  1. package/dist/helpers/index.d.ts +1 -0
  2. package/dist/helpers/scrollToTop.d.ts +1 -0
  3. package/dist/helpers/scrollToTop.js +11 -0
  4. package/dist/helpers/scrollToTop.js.map +1 -0
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.js +10 -2
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +215 -15
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/queries/index.d.ts +4 -0
  11. package/dist/queries/queries.interface.d.ts +11 -0
  12. package/dist/queries/useDeleteRequest.d.ts +116 -0
  13. package/dist/queries/useDeleteRequest.js +45 -0
  14. package/dist/queries/useDeleteRequest.js.map +1 -0
  15. package/dist/queries/useGetRequest.d.ts +141 -0
  16. package/dist/queries/useGetRequest.js +104 -0
  17. package/dist/queries/useGetRequest.js.map +1 -0
  18. package/dist/queries/usePatchRequest.d.ts +78 -0
  19. package/dist/queries/usePatchRequest.js +46 -0
  20. package/dist/queries/usePatchRequest.js.map +1 -0
  21. package/dist/queries/usePostRequest.d.ts +78 -0
  22. package/dist/queries/usePostRequest.js +48 -0
  23. package/dist/queries/usePostRequest.js.map +1 -0
  24. package/dist/request/buildFormData.d.ts +1 -3
  25. package/dist/request/buildFormData.js +1 -1
  26. package/dist/request/make-request.d.ts +1 -1
  27. package/dist/request/make-request.js +5 -8
  28. package/dist/request/make-request.js.map +1 -1
  29. package/dist/request/request.interface.d.ts +2 -2
  30. package/package.json +5 -8
  31. package/src/helpers/index.ts +1 -0
  32. package/src/helpers/scrollToTop.ts +6 -0
  33. package/src/index.ts +2 -1
  34. package/src/queries/index.ts +4 -0
  35. package/src/queries/queries.interface.ts +18 -0
  36. package/src/queries/useDeleteRequest.ts +52 -0
  37. package/src/queries/useGetRequest.ts +143 -0
  38. package/src/queries/usePatchRequest.ts +61 -0
  39. package/src/queries/usePostRequest.ts +59 -0
  40. package/src/request/buildFormData.ts +4 -2
  41. package/src/request/make-request.ts +7 -10
  42. package/src/request/request.interface.ts +2 -2
  43. package/dist/useMyHook.d.ts +0 -1
  44. package/dist/useMyHook.js +0 -14
  45. package/dist/useMyHook.js.map +0 -1
  46. package/src/useMyHook.ts +0 -11
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ var reactQuery = require('@tanstack/react-query');
4
+ var react = require('react');
5
+ require('axios');
6
+ var makeRequest = require('../request/make-request.js');
7
+ require('../request/request.enum.js');
8
+
9
+ const useGetRequest = ({ path, load = false, queryOptions, }) => {
10
+ const [requestPath, updatePath] = react.useState(path);
11
+ const authToken = '';
12
+ const [options, setOptions] = react.useState(queryOptions);
13
+ const [page, setPage] = react.useState(1);
14
+ const sendRequest = async (res, rej) => {
15
+ const postResponse = await makeRequest.makeRequest({
16
+ path: requestPath,
17
+ bearerToken: authToken,
18
+ });
19
+ if (postResponse.status) {
20
+ res(postResponse);
21
+ }
22
+ else {
23
+ rej(postResponse);
24
+ }
25
+ };
26
+ const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => {
27
+ return sendRequest(res, rej);
28
+ }), {
29
+ enabled: load,
30
+ ...options,
31
+ });
32
+ const nextPage = () => {
33
+ if (query.data?.data.pagination) {
34
+ const pagination = query.data.data.pagination;
35
+ if (pagination.next_page !== pagination.current_page &&
36
+ pagination.next_page > pagination.current_page) {
37
+ updatePath(constructPaginationLink(requestPath, pagination.next_page));
38
+ }
39
+ }
40
+ };
41
+ const prevPage = () => {
42
+ if (query.data?.data.pagination) {
43
+ const pagination = query.data.data.pagination;
44
+ if (pagination.previous_page !== pagination.current_page &&
45
+ pagination.previous_page < pagination.current_page) {
46
+ updatePath(constructPaginationLink(requestPath, pagination.previous_page));
47
+ }
48
+ }
49
+ };
50
+ const constructPaginationLink = (link, pageNumber) => {
51
+ const oldLink = link;
52
+ if (link.includes('?')) {
53
+ if (link.includes('?page=')) {
54
+ // replace current page number with new number
55
+ link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
56
+ }
57
+ else if (link.includes('&page=')) {
58
+ link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
59
+ }
60
+ else {
61
+ link = `${link}&page=${pageNumber}`;
62
+ }
63
+ }
64
+ else {
65
+ link = `${link}?page=${pageNumber}`;
66
+ }
67
+ // only update page when pagination is done
68
+ if (oldLink !== link) {
69
+ setPage(pageNumber);
70
+ }
71
+ return link;
72
+ };
73
+ const gotoPage = (pageNumber) => {
74
+ updatePath(constructPaginationLink(requestPath, pageNumber));
75
+ };
76
+ const updatedPathAsync = async (link) => {
77
+ react.startTransition(() => {
78
+ updatePath(link);
79
+ });
80
+ };
81
+ const setOptionsAsync = async (fetchOptions) => {
82
+ react.startTransition(() => {
83
+ setOptions(fetchOptions);
84
+ });
85
+ };
86
+ const get = async (link, fetchOptions) => {
87
+ await setOptionsAsync(fetchOptions);
88
+ await updatedPathAsync(link);
89
+ return query.data;
90
+ };
91
+ return {
92
+ ...query,
93
+ updatePath,
94
+ nextPage,
95
+ prevPage,
96
+ get,
97
+ gotoPage,
98
+ page,
99
+ queryKey: [requestPath, {}],
100
+ };
101
+ };
102
+
103
+ exports.useGetRequest = useGetRequest;
104
+ //# sourceMappingURL=useGetRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,78 @@
1
+ import type { MutateOptions } from '@tanstack/react-query';
2
+ import type { IRequestError, IRequestSuccess } from '../request/request.interface';
3
+ export declare const usePatchRequest: <TResponse>({ path, isFormData, }: {
4
+ path: string;
5
+ isFormData?: boolean | undefined;
6
+ }) => {
7
+ resetForm: boolean;
8
+ data: undefined;
9
+ error: null;
10
+ isError: false;
11
+ isIdle: true;
12
+ isLoading: false;
13
+ isSuccess: false;
14
+ status: "idle";
15
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
16
+ reset: () => void;
17
+ context: unknown;
18
+ failureCount: number;
19
+ failureReason: IRequestError | null;
20
+ isPaused: boolean;
21
+ variables: void | undefined;
22
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
23
+ patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
24
+ } | {
25
+ resetForm: boolean;
26
+ data: undefined;
27
+ error: null;
28
+ isError: false;
29
+ isIdle: false;
30
+ isLoading: true;
31
+ isSuccess: false;
32
+ status: "loading";
33
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
34
+ reset: () => void;
35
+ context: unknown;
36
+ failureCount: number;
37
+ failureReason: IRequestError | null;
38
+ isPaused: boolean;
39
+ variables: void | undefined;
40
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
41
+ patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
42
+ } | {
43
+ resetForm: boolean;
44
+ data: undefined;
45
+ error: IRequestError;
46
+ isError: true;
47
+ isIdle: false;
48
+ isLoading: false;
49
+ isSuccess: false;
50
+ status: "error";
51
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
52
+ reset: () => void;
53
+ context: unknown;
54
+ failureCount: number;
55
+ failureReason: IRequestError | null;
56
+ isPaused: boolean;
57
+ variables: void | undefined;
58
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
59
+ patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
60
+ } | {
61
+ resetForm: boolean;
62
+ data: IRequestSuccess<TResponse>;
63
+ error: null;
64
+ isError: false;
65
+ isIdle: false;
66
+ isLoading: false;
67
+ isSuccess: true;
68
+ status: "success";
69
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
70
+ reset: () => void;
71
+ context: unknown;
72
+ failureCount: number;
73
+ failureReason: IRequestError | null;
74
+ isPaused: boolean;
75
+ variables: void | undefined;
76
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
77
+ patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
78
+ };
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ var reactQuery = require('@tanstack/react-query');
4
+ var react = require('react');
5
+ var scrollToTop = require('../helpers/scrollToTop.js');
6
+ require('axios');
7
+ var makeRequest = require('../request/make-request.js');
8
+ var request_enum = require('../request/request.enum.js');
9
+
10
+ const usePatchRequest = ({ path, isFormData = false, }) => {
11
+ const authToken = '';
12
+ const [resetForm, setResetForm] = react.useState(false);
13
+ // register post mutation
14
+ const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
15
+ setResetForm(false);
16
+ makeRequest.makeRequest({
17
+ path: path,
18
+ body: postData,
19
+ method: request_enum.HttpMethod.PATCH,
20
+ bearerToken: authToken,
21
+ isFormData,
22
+ }).then((postResponse) => {
23
+ if (postResponse.status) {
24
+ setResetForm(true);
25
+ // scroll to top after success
26
+ scrollToTop.scrollToTop();
27
+ res(postResponse);
28
+ }
29
+ else {
30
+ // scroll to top after error
31
+ window.scrollTo({
32
+ top: 0,
33
+ behavior: 'smooth',
34
+ });
35
+ rej(postResponse);
36
+ }
37
+ });
38
+ }));
39
+ const patch = async (postData, options) => {
40
+ return mutation.mutateAsync(postData, options);
41
+ };
42
+ return { patch, ...mutation, resetForm };
43
+ };
44
+
45
+ exports.usePatchRequest = usePatchRequest;
46
+ //# sourceMappingURL=usePatchRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,78 @@
1
+ import type { MutateOptions } from '@tanstack/react-query';
2
+ import type { IRequestError, IRequestSuccess } from '../request';
3
+ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
4
+ path: string;
5
+ isFormData?: boolean | undefined;
6
+ }) => {
7
+ resetForm: boolean;
8
+ data: undefined;
9
+ error: null;
10
+ isError: false;
11
+ isIdle: true;
12
+ isLoading: false;
13
+ isSuccess: false;
14
+ status: "idle";
15
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
16
+ reset: () => void;
17
+ context: unknown;
18
+ failureCount: number;
19
+ failureReason: IRequestError | null;
20
+ isPaused: boolean;
21
+ variables: void | undefined;
22
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
23
+ post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
24
+ } | {
25
+ resetForm: boolean;
26
+ data: undefined;
27
+ error: null;
28
+ isError: false;
29
+ isIdle: false;
30
+ isLoading: true;
31
+ isSuccess: false;
32
+ status: "loading";
33
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
34
+ reset: () => void;
35
+ context: unknown;
36
+ failureCount: number;
37
+ failureReason: IRequestError | null;
38
+ isPaused: boolean;
39
+ variables: void | undefined;
40
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
41
+ post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
42
+ } | {
43
+ resetForm: boolean;
44
+ data: undefined;
45
+ error: IRequestError;
46
+ isError: true;
47
+ isIdle: false;
48
+ isLoading: false;
49
+ isSuccess: false;
50
+ status: "error";
51
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
52
+ reset: () => void;
53
+ context: unknown;
54
+ failureCount: number;
55
+ failureReason: IRequestError | null;
56
+ isPaused: boolean;
57
+ variables: void | undefined;
58
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
59
+ post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
60
+ } | {
61
+ resetForm: boolean;
62
+ data: IRequestSuccess<TResponse>;
63
+ error: null;
64
+ isError: false;
65
+ isIdle: false;
66
+ isLoading: false;
67
+ isSuccess: true;
68
+ status: "success";
69
+ mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
70
+ reset: () => void;
71
+ context: unknown;
72
+ failureCount: number;
73
+ failureReason: IRequestError | null;
74
+ isPaused: boolean;
75
+ variables: void | undefined;
76
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
77
+ post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
78
+ };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var reactQuery = require('@tanstack/react-query');
4
+ var react = require('react');
5
+ require('axios');
6
+ var makeRequest = require('../request/make-request.js');
7
+ var request_enum = require('../request/request.enum.js');
8
+
9
+ const usePostRequest = ({ path, isFormData = false, }) => {
10
+ const authToken = '';
11
+ const [resetForm, setResetForm] = react.useState(false);
12
+ // register post mutation
13
+ const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => {
14
+ setResetForm(false);
15
+ makeRequest.makeRequest({
16
+ path: path,
17
+ body: postData,
18
+ method: request_enum.HttpMethod.POST,
19
+ bearerToken: authToken,
20
+ isFormData,
21
+ }).then((postResponse) => {
22
+ if (postResponse.status) {
23
+ setResetForm(true);
24
+ // scroll to top after success
25
+ window.scrollTo({
26
+ top: 0,
27
+ behavior: 'smooth',
28
+ });
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
+ });
40
+ }));
41
+ const post = async (postData, options) => {
42
+ return mutation.mutateAsync(postData, options);
43
+ };
44
+ return { post, ...mutation, resetForm };
45
+ };
46
+
47
+ exports.usePostRequest = usePostRequest;
48
+ //# sourceMappingURL=usePostRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,3 +1 @@
1
- export declare const buildFormData: (body: any) => {
2
- body: any;
3
- };
1
+ export declare const buildFormData: (body: Record<string, any>) => Record<string, any>;
@@ -7,7 +7,7 @@ const buildFormData = (body) => {
7
7
  formData.append(key, body[key]);
8
8
  });
9
9
  body = formData;
10
- return { body };
10
+ return body;
11
11
  };
12
12
 
13
13
  exports.buildFormData = buildFormData;
@@ -1,2 +1,2 @@
1
1
  import type { IMakeRequest } from './request.interface';
2
- export declare function makeRequest<TResponse>({ body, method, path, bearerToken, formData, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
2
+ export declare function makeRequest<TResponse>({ body, method, path, bearerToken, isFormData, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
@@ -1,30 +1,27 @@
1
1
  'use strict';
2
2
 
3
- var index = require('../constants/index.js');
4
3
  var axiosInstance = require('./axios-instance.js');
5
4
  var buildFormData = require('./buildFormData.js');
6
5
  var request_enum = require('./request.enum.js');
7
6
  var transformer = require('./transformer.js');
8
7
 
9
- async function makeRequest({ body, method = request_enum.HttpMethod.GET, path, bearerToken, formData, }) {
10
- // construct api full url
11
- const apiFullUrl = `${index.API_BASE_URL}${path}`;
8
+ async function makeRequest({ body, method = request_enum.HttpMethod.GET, path, bearerToken, isFormData, }) {
12
9
  // configure body
13
- body = formData ? buildFormData.buildFormData(body) : body;
10
+ body = isFormData ? buildFormData.buildFormData(body) : body;
14
11
  // configure request header
15
12
  const headers = {
16
13
  Authorization: `Bearer ${bearerToken}`,
17
14
  };
18
- if (!formData) {
15
+ if (!isFormData) {
19
16
  headers['Content-Type'] = request_enum.ContentType.APPLICATION_JSON;
20
17
  }
21
18
  try {
22
19
  const axios = axiosInstance.axiosInstance(headers);
23
20
  // send request
24
21
  const resp = await axios({
25
- url: apiFullUrl,
22
+ url: path,
26
23
  method,
27
- data: formData ? body : JSON.stringify(body),
24
+ data: body,
28
25
  });
29
26
  // get response json
30
27
  const jsonResp = await resp.data();
@@ -1 +1 @@
1
- {"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,10 +1,10 @@
1
1
  import type { HttpMethod } from './request.enum';
2
2
  export interface IMakeRequest {
3
3
  path: string;
4
- body?: BodyInit | null;
4
+ body?: Record<string, any> | null;
5
5
  method?: HttpMethod;
6
6
  bearerToken?: string;
7
- formData?: boolean;
7
+ isFormData?: boolean;
8
8
  }
9
9
  export interface AxiosInstanceOption {
10
10
  bearerToken?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -14,13 +14,13 @@
14
14
  "url": "https://ventlio.com"
15
15
  },
16
16
  "scripts": {
17
- "build": "rollup --config rollup.config.ts --configPlugin rollup-plugin-typescript2",
17
+ "build": "rimraf dist && rollup --config rollup.config.ts --configPlugin rollup-plugin-typescript2",
18
18
  "build:watch": "yarn build -w"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "@tanstack/react-query": "^4.26.1",
22
- "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
23
22
  "axios": "^1.3.4",
23
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
24
24
  "react-native": "*"
25
25
  },
26
26
  "peerDependenciesMeta": {
@@ -55,6 +55,7 @@
55
55
  "eslint-plugin-react-hooks": "^4.6.0",
56
56
  "prettier": "^2.8.4",
57
57
  "react": "^18.2.0",
58
+ "rimraf": "^4.4.0",
58
59
  "rollup": "^3.19.1",
59
60
  "rollup-plugin-typescript2": "^0.34.1",
60
61
  "ts-node": "^10.9.1",
@@ -62,11 +63,7 @@
62
63
  "typescript": "^4.9.5"
63
64
  },
64
65
  "files": [
65
- "dist/app/*",
66
66
  "dist/**/*",
67
67
  "src"
68
- ],
69
- "dependencies": {
70
- "rmf": "^2.0.0"
71
- }
68
+ ]
72
69
  }
@@ -0,0 +1 @@
1
+ export * from './scrollToTop';
@@ -0,0 +1,6 @@
1
+ export const scrollToTop = () => {
2
+ window.scrollTo({
3
+ top: 0,
4
+ behavior: 'smooth',
5
+ });
6
+ };
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './constants';
2
+ export * from './helpers';
3
+ export * from './queries';
2
4
  export * from './request';
3
- export * from './useMyHook';
@@ -0,0 +1,4 @@
1
+ export * from './useDeleteRequest';
2
+ export * from './useGetRequest';
3
+ export * from './usePatchRequest';
4
+ export * from './usePostRequest';
@@ -0,0 +1,18 @@
1
+ import type { UseQueryOptions } from '@tanstack/react-query';
2
+ import type { IRequestError, IRequestSuccess } from '../request';
3
+
4
+ export interface IPagination {
5
+ current_page: number;
6
+ next_page: number;
7
+ page_count: number;
8
+ previous_page: number;
9
+ size: number;
10
+ total: number;
11
+ }
12
+
13
+ export type TanstackQueryOption<TResponse> = UseQueryOptions<
14
+ IRequestSuccess<TResponse | undefined>,
15
+ IRequestError,
16
+ IRequestSuccess<TResponse | undefined>,
17
+ Array<any>
18
+ >;
@@ -0,0 +1,52 @@
1
+ import type { MutateOptions, QueryObserverResult } from '@tanstack/react-query';
2
+ import { useQuery } from '@tanstack/react-query';
3
+ import { useState } from 'react';
4
+ import type { IRequestError, IRequestSuccess } from '../request';
5
+ import { HttpMethod, makeRequest } from '../request';
6
+
7
+ export const useDeleteRequest = <TResponse>() => {
8
+ const [requestPath, updateDeletePath] = useState<string>('');
9
+ const [options, setOptions] = useState<any>();
10
+ const authToken = '';
11
+ const query = useQuery<any, any, IRequestSuccess<TResponse>>(
12
+ [requestPath, {}],
13
+ () =>
14
+ new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => {
15
+ setTimeout(async () => {
16
+ const postResponse = await makeRequest<TResponse>({
17
+ path: requestPath,
18
+ bearerToken: authToken,
19
+ method: HttpMethod.DELETE,
20
+ });
21
+ if (postResponse.status) {
22
+ res(postResponse as IRequestSuccess<TResponse>);
23
+ } else {
24
+ rej(postResponse);
25
+ }
26
+ }, 200);
27
+ }),
28
+ { ...options }
29
+ );
30
+
31
+ const updatedPathAsync = async (link: string) => {
32
+ return updateDeletePath(link);
33
+ };
34
+
35
+ const setOptionsAsync = async (fetchOptions: any) => {
36
+ return setOptions(fetchOptions);
37
+ };
38
+ const deleteR = async (
39
+ link: string,
40
+ fetchOptions?:
41
+ | MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown>
42
+ | undefined
43
+ ): Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>> => {
44
+ await updatedPathAsync(link);
45
+ await setOptionsAsync(fetchOptions);
46
+
47
+ return query.refetch<TResponse>({
48
+ queryKey: [link, {}],
49
+ });
50
+ };
51
+ return { updateDeletePath, deleteR, ...query };
52
+ };