@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.
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/scrollToTop.d.ts +1 -0
- package/dist/helpers/scrollToTop.js +11 -0
- package/dist/helpers/scrollToTop.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +215 -15
- package/dist/index.mjs.map +1 -1
- package/dist/queries/index.d.ts +4 -0
- package/dist/queries/queries.interface.d.ts +11 -0
- package/dist/queries/useDeleteRequest.d.ts +116 -0
- package/dist/queries/useDeleteRequest.js +45 -0
- package/dist/queries/useDeleteRequest.js.map +1 -0
- package/dist/queries/useGetRequest.d.ts +141 -0
- package/dist/queries/useGetRequest.js +104 -0
- package/dist/queries/useGetRequest.js.map +1 -0
- package/dist/queries/usePatchRequest.d.ts +78 -0
- package/dist/queries/usePatchRequest.js +46 -0
- package/dist/queries/usePatchRequest.js.map +1 -0
- package/dist/queries/usePostRequest.d.ts +78 -0
- package/dist/queries/usePostRequest.js +48 -0
- package/dist/queries/usePostRequest.js.map +1 -0
- package/dist/request/buildFormData.d.ts +1 -3
- package/dist/request/buildFormData.js +1 -1
- package/dist/request/make-request.d.ts +1 -1
- package/dist/request/make-request.js +5 -8
- package/dist/request/make-request.js.map +1 -1
- package/dist/request/request.interface.d.ts +2 -2
- package/package.json +5 -8
- package/src/helpers/index.ts +1 -0
- package/src/helpers/scrollToTop.ts +6 -0
- package/src/index.ts +2 -1
- package/src/queries/index.ts +4 -0
- package/src/queries/queries.interface.ts +18 -0
- package/src/queries/useDeleteRequest.ts +52 -0
- package/src/queries/useGetRequest.ts +143 -0
- package/src/queries/usePatchRequest.ts +61 -0
- package/src/queries/usePostRequest.ts +59 -0
- package/src/request/buildFormData.ts +4 -2
- package/src/request/make-request.ts +7 -10
- package/src/request/request.interface.ts +2 -2
- package/dist/useMyHook.d.ts +0 -1
- package/dist/useMyHook.js +0 -14
- package/dist/useMyHook.js.map +0 -1
- package/src/useMyHook.ts +0 -11
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
2
|
+
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
import { startTransition, useState } from 'react';
|
|
4
|
+
import type { IRequestError, IRequestSuccess } from '../request';
|
|
5
|
+
import { makeRequest } from '../request';
|
|
6
|
+
import type { IPagination, TanstackQueryOption } from './queries.interface';
|
|
7
|
+
|
|
8
|
+
export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
9
|
+
path,
|
|
10
|
+
load = false,
|
|
11
|
+
queryOptions,
|
|
12
|
+
}: {
|
|
13
|
+
path: string;
|
|
14
|
+
load?: boolean;
|
|
15
|
+
queryOptions?: TanstackQueryOption<TResponse>;
|
|
16
|
+
}) => {
|
|
17
|
+
const [requestPath, updatePath] = useState<string>(path);
|
|
18
|
+
const authToken = '';
|
|
19
|
+
const [options, setOptions] = useState<any>(queryOptions);
|
|
20
|
+
const [page, setPage] = useState<number>(1);
|
|
21
|
+
|
|
22
|
+
const sendRequest = async (
|
|
23
|
+
res: (
|
|
24
|
+
value:
|
|
25
|
+
| IRequestError
|
|
26
|
+
| IRequestSuccess<TResponse>
|
|
27
|
+
| PromiseLike<IRequestError | IRequestSuccess<TResponse>>
|
|
28
|
+
) => void,
|
|
29
|
+
rej: (reason?: any) => void
|
|
30
|
+
) => {
|
|
31
|
+
const postResponse = await makeRequest<TResponse>({
|
|
32
|
+
path: requestPath,
|
|
33
|
+
bearerToken: authToken,
|
|
34
|
+
});
|
|
35
|
+
if (postResponse.status) {
|
|
36
|
+
res(postResponse as IRequestSuccess<TResponse>);
|
|
37
|
+
} else {
|
|
38
|
+
rej(postResponse);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const query = useQuery<any, any, IRequestSuccess<TResponse>>(
|
|
43
|
+
[requestPath, {}],
|
|
44
|
+
() =>
|
|
45
|
+
new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => {
|
|
46
|
+
return sendRequest(res, rej);
|
|
47
|
+
}),
|
|
48
|
+
{
|
|
49
|
+
enabled: load,
|
|
50
|
+
...options,
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const nextPage = () => {
|
|
55
|
+
if (query.data?.data.pagination) {
|
|
56
|
+
const pagination: IPagination = query.data.data.pagination;
|
|
57
|
+
if (
|
|
58
|
+
pagination.next_page !== pagination.current_page &&
|
|
59
|
+
pagination.next_page > pagination.current_page
|
|
60
|
+
) {
|
|
61
|
+
updatePath(constructPaginationLink(requestPath, pagination.next_page));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const prevPage = () => {
|
|
67
|
+
if (query.data?.data.pagination) {
|
|
68
|
+
const pagination: IPagination = query.data.data.pagination;
|
|
69
|
+
if (
|
|
70
|
+
pagination.previous_page !== pagination.current_page &&
|
|
71
|
+
pagination.previous_page < pagination.current_page
|
|
72
|
+
) {
|
|
73
|
+
updatePath(
|
|
74
|
+
constructPaginationLink(requestPath, pagination.previous_page)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const constructPaginationLink = (link: string, pageNumber: number) => {
|
|
81
|
+
const oldLink = link;
|
|
82
|
+
if (link.includes('?')) {
|
|
83
|
+
if (link.includes('?page=')) {
|
|
84
|
+
// replace current page number with new number
|
|
85
|
+
link = link.replace(/\?page=(\d+)/gim, `?page=${pageNumber}`);
|
|
86
|
+
} else if (link.includes('&page=')) {
|
|
87
|
+
link = link.replace(/&page=(\d+)/gim, `&page=${pageNumber}`);
|
|
88
|
+
} else {
|
|
89
|
+
link = `${link}&page=${pageNumber}`;
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
link = `${link}?page=${pageNumber}`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// only update page when pagination is done
|
|
96
|
+
if (oldLink !== link) {
|
|
97
|
+
setPage(pageNumber);
|
|
98
|
+
}
|
|
99
|
+
return link;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const gotoPage = (pageNumber: number) => {
|
|
103
|
+
updatePath(constructPaginationLink(requestPath, pageNumber));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const updatedPathAsync = async (link: string) => {
|
|
107
|
+
startTransition(() => {
|
|
108
|
+
updatePath(link);
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const setOptionsAsync = async (fetchOptions: any) => {
|
|
113
|
+
startTransition(() => {
|
|
114
|
+
setOptions(fetchOptions);
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const get = async (
|
|
119
|
+
link: string,
|
|
120
|
+
fetchOptions?: UseQueryOptions<
|
|
121
|
+
IRequestSuccess<TResponse | undefined>,
|
|
122
|
+
IRequestError,
|
|
123
|
+
IRequestSuccess<TResponse | undefined>,
|
|
124
|
+
Array<any>
|
|
125
|
+
>
|
|
126
|
+
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
127
|
+
await setOptionsAsync(fetchOptions);
|
|
128
|
+
await updatedPathAsync(link);
|
|
129
|
+
|
|
130
|
+
return query.data;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
...query,
|
|
135
|
+
updatePath,
|
|
136
|
+
nextPage,
|
|
137
|
+
prevPage,
|
|
138
|
+
get,
|
|
139
|
+
gotoPage,
|
|
140
|
+
page,
|
|
141
|
+
queryKey: [requestPath, {}],
|
|
142
|
+
};
|
|
143
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation } from '@tanstack/react-query';
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { scrollToTop } from '../helpers';
|
|
5
|
+
import { HttpMethod, makeRequest } from '../request';
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
IRequestError,
|
|
9
|
+
IRequestSuccess,
|
|
10
|
+
} from '../request/request.interface';
|
|
11
|
+
|
|
12
|
+
export const usePatchRequest = <TResponse>({
|
|
13
|
+
path,
|
|
14
|
+
isFormData = false,
|
|
15
|
+
}: {
|
|
16
|
+
path: string;
|
|
17
|
+
isFormData?: boolean;
|
|
18
|
+
}) => {
|
|
19
|
+
const authToken = '';
|
|
20
|
+
const [resetForm, setResetForm] = useState<boolean>(false);
|
|
21
|
+
|
|
22
|
+
// register post mutation
|
|
23
|
+
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
|
|
24
|
+
(postData: any) =>
|
|
25
|
+
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
26
|
+
setResetForm(false);
|
|
27
|
+
|
|
28
|
+
makeRequest<TResponse>({
|
|
29
|
+
path: path,
|
|
30
|
+
body: postData,
|
|
31
|
+
method: HttpMethod.PATCH,
|
|
32
|
+
bearerToken: authToken,
|
|
33
|
+
isFormData,
|
|
34
|
+
}).then((postResponse) => {
|
|
35
|
+
if (postResponse.status) {
|
|
36
|
+
setResetForm(true);
|
|
37
|
+
// scroll to top after success
|
|
38
|
+
scrollToTop();
|
|
39
|
+
res(postResponse as IRequestSuccess<TResponse>);
|
|
40
|
+
} else {
|
|
41
|
+
// scroll to top after error
|
|
42
|
+
window.scrollTo({
|
|
43
|
+
top: 0,
|
|
44
|
+
behavior: 'smooth',
|
|
45
|
+
});
|
|
46
|
+
rej(postResponse);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
})
|
|
50
|
+
);
|
|
51
|
+
const patch = async (
|
|
52
|
+
postData: any,
|
|
53
|
+
options?:
|
|
54
|
+
| MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown>
|
|
55
|
+
| undefined
|
|
56
|
+
): Promise<IRequestSuccess<TResponse>> => {
|
|
57
|
+
return mutation.mutateAsync(postData, options);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return { patch, ...mutation, resetForm };
|
|
61
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation } 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 usePostRequest = <TResponse>({
|
|
8
|
+
path,
|
|
9
|
+
isFormData = false,
|
|
10
|
+
}: {
|
|
11
|
+
path: string;
|
|
12
|
+
isFormData?: boolean;
|
|
13
|
+
}) => {
|
|
14
|
+
const authToken = '';
|
|
15
|
+
|
|
16
|
+
const [resetForm, setResetForm] = useState<boolean>(false);
|
|
17
|
+
|
|
18
|
+
// register post mutation
|
|
19
|
+
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
|
|
20
|
+
async (postData: any) =>
|
|
21
|
+
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
22
|
+
setResetForm(false);
|
|
23
|
+
makeRequest<TResponse>({
|
|
24
|
+
path: path,
|
|
25
|
+
body: postData,
|
|
26
|
+
method: HttpMethod.POST,
|
|
27
|
+
bearerToken: authToken,
|
|
28
|
+
isFormData,
|
|
29
|
+
}).then((postResponse) => {
|
|
30
|
+
if (postResponse.status) {
|
|
31
|
+
setResetForm(true);
|
|
32
|
+
// scroll to top after success
|
|
33
|
+
window.scrollTo({
|
|
34
|
+
top: 0,
|
|
35
|
+
behavior: 'smooth',
|
|
36
|
+
});
|
|
37
|
+
res(postResponse as IRequestSuccess<TResponse>);
|
|
38
|
+
} else {
|
|
39
|
+
// scroll to top after error
|
|
40
|
+
window.scrollTo({
|
|
41
|
+
top: 0,
|
|
42
|
+
behavior: 'smooth',
|
|
43
|
+
});
|
|
44
|
+
rej(postResponse);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
const post = async (
|
|
50
|
+
postData: any,
|
|
51
|
+
options?:
|
|
52
|
+
| MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown>
|
|
53
|
+
| undefined
|
|
54
|
+
): Promise<IRequestSuccess<TResponse>> => {
|
|
55
|
+
return mutation.mutateAsync(postData, options);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return { post, ...mutation, resetForm };
|
|
59
|
+
};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
export const buildFormData = (body: any) => {
|
|
1
|
+
export const buildFormData = (body: Record<string, any>) => {
|
|
2
2
|
const formData = new FormData();
|
|
3
3
|
|
|
4
4
|
const bodyKeys = Object.keys(body);
|
|
5
|
+
|
|
5
6
|
bodyKeys.forEach((key) => {
|
|
6
7
|
formData.append(key, body[key]);
|
|
7
8
|
});
|
|
9
|
+
|
|
8
10
|
body = formData;
|
|
9
11
|
|
|
10
|
-
return
|
|
12
|
+
return body;
|
|
11
13
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { RawAxiosRequestHeaders } from 'axios';
|
|
2
2
|
import { axiosInstance } from './axios-instance';
|
|
3
3
|
|
|
4
4
|
import { buildFormData } from './buildFormData';
|
|
@@ -11,20 +11,17 @@ export async function makeRequest<TResponse>({
|
|
|
11
11
|
method = HttpMethod.GET,
|
|
12
12
|
path,
|
|
13
13
|
bearerToken,
|
|
14
|
-
|
|
14
|
+
isFormData,
|
|
15
15
|
}: IMakeRequest) {
|
|
16
|
-
// construct api full url
|
|
17
|
-
const apiFullUrl = `${API_BASE_URL}${path}`;
|
|
18
|
-
|
|
19
16
|
// configure body
|
|
20
|
-
body =
|
|
17
|
+
body = isFormData ? buildFormData(body as Record<string, any>) : body;
|
|
21
18
|
|
|
22
19
|
// configure request header
|
|
23
|
-
const headers:
|
|
20
|
+
const headers: RawAxiosRequestHeaders = {
|
|
24
21
|
Authorization: `Bearer ${bearerToken}`,
|
|
25
22
|
};
|
|
26
23
|
|
|
27
|
-
if (!
|
|
24
|
+
if (!isFormData) {
|
|
28
25
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
29
26
|
}
|
|
30
27
|
|
|
@@ -33,9 +30,9 @@ export async function makeRequest<TResponse>({
|
|
|
33
30
|
|
|
34
31
|
// send request
|
|
35
32
|
const resp = await axios({
|
|
36
|
-
url:
|
|
33
|
+
url: path,
|
|
37
34
|
method,
|
|
38
|
-
data:
|
|
35
|
+
data: body,
|
|
39
36
|
});
|
|
40
37
|
|
|
41
38
|
// get response json
|
|
@@ -2,10 +2,10 @@ import type { HttpMethod } from './request.enum';
|
|
|
2
2
|
|
|
3
3
|
export interface IMakeRequest {
|
|
4
4
|
path: string;
|
|
5
|
-
body?:
|
|
5
|
+
body?: Record<string, any> | null;
|
|
6
6
|
method?: HttpMethod;
|
|
7
7
|
bearerToken?: string;
|
|
8
|
-
|
|
8
|
+
isFormData?: boolean;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export interface AxiosInstanceOption {
|
package/dist/useMyHook.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useMyHook: () => [string, (newValue: string) => void];
|
package/dist/useMyHook.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var react = require('react');
|
|
4
|
-
|
|
5
|
-
const useMyHook = () => {
|
|
6
|
-
const [value, setValue] = react.useState('');
|
|
7
|
-
const updateValue = (newValue) => {
|
|
8
|
-
setValue(newValue);
|
|
9
|
-
};
|
|
10
|
-
return [value, updateValue];
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
exports.useMyHook = useMyHook;
|
|
14
|
-
//# sourceMappingURL=useMyHook.js.map
|
package/dist/useMyHook.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMyHook.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
package/src/useMyHook.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
2
|
-
|
|
3
|
-
export const useMyHook = (): [string, (newValue: string) => void] => {
|
|
4
|
-
const [value, setValue] = useState('');
|
|
5
|
-
|
|
6
|
-
const updateValue = (newValue: string) => {
|
|
7
|
-
setValue(newValue);
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
return [value, updateValue];
|
|
11
|
-
};
|