@ventlio/tanstack-query 0.1.13 → 0.2.0
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.d.ts +0 -1
- package/dist/index.js +2 -2
- package/dist/index.mjs +44 -36
- package/dist/index.mjs.map +1 -1
- package/dist/queries/index.d.ts +2 -0
- package/dist/queries/useDeleteRequest.d.ts +9 -14
- package/dist/queries/useDeleteRequest.js +15 -8
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.js +5 -2
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +1 -5
- package/dist/queries/usePatchRequest.js +7 -9
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +0 -4
- package/dist/queries/usePostRequest.js +6 -7
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/queries/useQueryConfig.d.ts +2 -0
- package/dist/queries/useQueryConfig.js +14 -0
- package/dist/queries/useQueryConfig.js.map +1 -0
- package/dist/request/axios-instance.d.ts +3 -2
- package/dist/request/axios-instance.js +3 -4
- package/dist/request/axios-instance.js.map +1 -1
- package/dist/request/make-request.d.ts +1 -1
- package/dist/request/make-request.js +2 -5
- package/dist/request/make-request.js.map +1 -1
- package/dist/request/request.interface.d.ts +4 -1
- package/dist/types/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/index.ts +0 -1
- package/src/queries/index.ts +2 -0
- package/src/queries/useDeleteRequest.ts +29 -13
- package/src/queries/useGetRequest.ts +6 -2
- package/src/queries/usePatchRequest.ts +7 -10
- package/src/queries/usePostRequest.ts +6 -8
- package/src/queries/useQueryConfig.ts +12 -0
- package/src/request/axios-instance.ts +9 -7
- package/src/request/make-request.ts +4 -6
- package/src/request/request.interface.ts +4 -1
- package/src/types/index.ts +2 -2
- package/dist/constants/index.d.ts +0 -1
- package/dist/constants/index.js +0 -6
- package/dist/constants/index.js.map +0 -1
- package/src/constants/index.ts +0 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var index = require('./constants/index.js');
|
|
4
3
|
var scrollToTop = require('./helpers/scrollToTop.js');
|
|
5
4
|
var useDeleteRequest = require('./queries/useDeleteRequest.js');
|
|
6
5
|
var useGetRequest = require('./queries/useGetRequest.js');
|
|
7
6
|
var usePatchRequest = require('./queries/usePatchRequest.js');
|
|
8
7
|
var usePostRequest = require('./queries/usePostRequest.js');
|
|
8
|
+
var useQueryConfig = require('./queries/useQueryConfig.js');
|
|
9
9
|
var axiosInstance = require('./request/axios-instance.js');
|
|
10
10
|
var buildFormData = require('./request/buildFormData.js');
|
|
11
11
|
var makeRequest = require('./request/make-request.js');
|
|
@@ -14,12 +14,12 @@ var transformer = require('./request/transformer.js');
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
exports.API_BASE_URL = index.API_BASE_URL;
|
|
18
17
|
exports.scrollToTop = scrollToTop.scrollToTop;
|
|
19
18
|
exports.useDeleteRequest = useDeleteRequest.useDeleteRequest;
|
|
20
19
|
exports.useGetRequest = useGetRequest.useGetRequest;
|
|
21
20
|
exports.usePatchRequest = usePatchRequest.usePatchRequest;
|
|
22
21
|
exports.usePostRequest = usePostRequest.usePostRequest;
|
|
22
|
+
exports.useQueryConfig = useQueryConfig.useQueryConfig;
|
|
23
23
|
exports.axiosInstance = axiosInstance.axiosInstance;
|
|
24
24
|
exports.buildFormData = buildFormData.buildFormData;
|
|
25
25
|
exports.makeRequest = makeRequest.makeRequest;
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { useQuery, useMutation } from '@tanstack/react-query';
|
|
1
|
+
import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
|
|
2
2
|
import { useState, useEffect, startTransition } from 'react';
|
|
3
3
|
import axios from 'axios';
|
|
4
4
|
|
|
5
|
-
const API_BASE_URL = 'https://pokeapi.co/api/v2';
|
|
6
|
-
|
|
7
5
|
const scrollToTop = () => {
|
|
8
6
|
window.scrollTo({
|
|
9
7
|
top: 0,
|
|
@@ -11,10 +9,10 @@ const scrollToTop = () => {
|
|
|
11
9
|
});
|
|
12
10
|
};
|
|
13
11
|
|
|
14
|
-
const axiosInstance = (headers) => {
|
|
12
|
+
const axiosInstance = ({ baseURL, timeout, headers, }) => {
|
|
15
13
|
return axios.create({
|
|
16
|
-
baseURL
|
|
17
|
-
timeout
|
|
14
|
+
baseURL,
|
|
15
|
+
timeout,
|
|
18
16
|
headers,
|
|
19
17
|
});
|
|
20
18
|
};
|
|
@@ -64,18 +62,15 @@ const successTransformer = (data) => {
|
|
|
64
62
|
};
|
|
65
63
|
};
|
|
66
64
|
|
|
67
|
-
async function makeRequest({ body, method = HttpMethod.GET, path,
|
|
65
|
+
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, }) {
|
|
68
66
|
// configure body
|
|
69
67
|
body = isFormData ? buildFormData(body) : body;
|
|
70
68
|
// configure request header
|
|
71
|
-
const headers = {
|
|
72
|
-
Authorization: `Bearer ${bearerToken}`,
|
|
73
|
-
};
|
|
74
69
|
if (!isFormData) {
|
|
75
70
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
76
71
|
}
|
|
77
72
|
try {
|
|
78
|
-
const axios = axiosInstance(headers);
|
|
73
|
+
const axios = axiosInstance({ baseURL, headers, timeout });
|
|
79
74
|
// send request
|
|
80
75
|
const resp = await axios({
|
|
81
76
|
url: path,
|
|
@@ -104,16 +99,26 @@ async function makeRequest({ body, method = HttpMethod.GET, path, bearerToken, i
|
|
|
104
99
|
}
|
|
105
100
|
}
|
|
106
101
|
|
|
102
|
+
const useQueryConfig = () => {
|
|
103
|
+
const { getQueryData } = useQueryClient();
|
|
104
|
+
const { headers, baseURL, timeout } = getQueryData([
|
|
105
|
+
'config',
|
|
106
|
+
]);
|
|
107
|
+
return { headers, baseURL, timeout };
|
|
108
|
+
};
|
|
109
|
+
|
|
107
110
|
const useDeleteRequest = () => {
|
|
108
111
|
const [requestPath, updateDeletePath] = useState('');
|
|
109
112
|
const [options, setOptions] = useState();
|
|
110
|
-
const
|
|
113
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
111
114
|
const query = useQuery([requestPath, {}], () => new Promise((res, rej) => {
|
|
112
115
|
setTimeout(async () => {
|
|
113
116
|
const postResponse = await makeRequest({
|
|
114
117
|
path: requestPath,
|
|
115
|
-
|
|
118
|
+
headers,
|
|
116
119
|
method: HttpMethod.DELETE,
|
|
120
|
+
baseURL,
|
|
121
|
+
timeout,
|
|
117
122
|
});
|
|
118
123
|
if (postResponse.status) {
|
|
119
124
|
res(postResponse);
|
|
@@ -129,25 +134,31 @@ const useDeleteRequest = () => {
|
|
|
129
134
|
const setOptionsAsync = async (fetchOptions) => {
|
|
130
135
|
return setOptions(fetchOptions);
|
|
131
136
|
};
|
|
132
|
-
const
|
|
137
|
+
const destroy = async (link, deleteOptions) => {
|
|
138
|
+
// set enabled to be true for every delete
|
|
139
|
+
deleteOptions = deleteOptions ?? {};
|
|
140
|
+
deleteOptions.enabled = true;
|
|
133
141
|
await updatedPathAsync(link);
|
|
134
|
-
await setOptionsAsync(
|
|
135
|
-
return query.refetch({
|
|
136
|
-
|
|
137
|
-
});
|
|
142
|
+
await setOptionsAsync(deleteOptions);
|
|
143
|
+
// return query.refetch<TResponse>({
|
|
144
|
+
// queryKey: [link, {}],
|
|
145
|
+
// });
|
|
146
|
+
return query.data;
|
|
138
147
|
};
|
|
139
|
-
return {
|
|
148
|
+
return { destroy, ...query };
|
|
140
149
|
};
|
|
141
150
|
|
|
142
151
|
const useGetRequest = ({ path, load = false, queryOptions, }) => {
|
|
143
152
|
const [requestPath, updatePath] = useState(path);
|
|
144
|
-
const authToken = '';
|
|
145
153
|
const [options, setOptions] = useState(queryOptions);
|
|
146
154
|
const [page, setPage] = useState(1);
|
|
155
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
147
156
|
const sendRequest = async (res, rej) => {
|
|
148
157
|
const postResponse = await makeRequest({
|
|
149
158
|
path: requestPath,
|
|
150
|
-
|
|
159
|
+
headers,
|
|
160
|
+
baseURL,
|
|
161
|
+
timeout,
|
|
151
162
|
});
|
|
152
163
|
if (postResponse.status) {
|
|
153
164
|
res(postResponse);
|
|
@@ -228,21 +239,19 @@ const useGetRequest = ({ path, load = false, queryOptions, }) => {
|
|
|
228
239
|
};
|
|
229
240
|
};
|
|
230
241
|
|
|
231
|
-
const usePatchRequest = ({ path,
|
|
232
|
-
const
|
|
233
|
-
const [resetForm, setResetForm] = useState(false);
|
|
242
|
+
const usePatchRequest = ({ path, }) => {
|
|
243
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
234
244
|
// register post mutation
|
|
235
245
|
const mutation = useMutation((postData) => new Promise((res, rej) => {
|
|
236
|
-
setResetForm(false);
|
|
237
246
|
makeRequest({
|
|
238
247
|
path: path,
|
|
239
248
|
body: postData,
|
|
240
249
|
method: HttpMethod.PATCH,
|
|
241
|
-
|
|
242
|
-
|
|
250
|
+
headers,
|
|
251
|
+
baseURL,
|
|
252
|
+
timeout,
|
|
243
253
|
}).then((postResponse) => {
|
|
244
254
|
if (postResponse.status) {
|
|
245
|
-
setResetForm(true);
|
|
246
255
|
// scroll to top after success
|
|
247
256
|
scrollToTop();
|
|
248
257
|
res(postResponse);
|
|
@@ -260,24 +269,23 @@ const usePatchRequest = ({ path, isFormData = false, }) => {
|
|
|
260
269
|
const patch = async (postData, options) => {
|
|
261
270
|
return mutation.mutateAsync(postData, options);
|
|
262
271
|
};
|
|
263
|
-
return { patch, ...mutation
|
|
272
|
+
return { patch, ...mutation };
|
|
264
273
|
};
|
|
265
274
|
|
|
266
275
|
const usePostRequest = ({ path, isFormData = false, }) => {
|
|
267
|
-
const
|
|
268
|
-
const [resetForm, setResetForm] = useState(false);
|
|
276
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
269
277
|
// register post mutation
|
|
270
278
|
const mutation = useMutation(async (postData) => new Promise((res, rej) => {
|
|
271
|
-
setResetForm(false);
|
|
272
279
|
makeRequest({
|
|
273
280
|
path: path,
|
|
274
281
|
body: postData,
|
|
275
282
|
method: HttpMethod.POST,
|
|
276
|
-
bearerToken: authToken,
|
|
277
283
|
isFormData,
|
|
284
|
+
headers,
|
|
285
|
+
baseURL,
|
|
286
|
+
timeout,
|
|
278
287
|
}).then((postResponse) => {
|
|
279
288
|
if (postResponse.status) {
|
|
280
|
-
setResetForm(true);
|
|
281
289
|
// scroll to top after success
|
|
282
290
|
window.scrollTo({
|
|
283
291
|
top: 0,
|
|
@@ -298,8 +306,8 @@ const usePostRequest = ({ path, isFormData = false, }) => {
|
|
|
298
306
|
const post = async (postData, options) => {
|
|
299
307
|
return mutation.mutateAsync(postData, options);
|
|
300
308
|
};
|
|
301
|
-
return { post, ...mutation
|
|
309
|
+
return { post, ...mutation };
|
|
302
310
|
};
|
|
303
311
|
|
|
304
|
-
export {
|
|
312
|
+
export { ContentType, HttpMethod, axiosInstance, buildFormData, errorTransformer, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useGetRequest, usePatchRequest, usePostRequest, useQueryConfig };
|
|
305
313
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/queries/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import type { MutateOptions, QueryObserverResult } from '@tanstack/react-query';
|
|
1
|
+
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
3
2
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
4
3
|
export declare const useDeleteRequest: <TResponse>() => {
|
|
5
4
|
data: IRequestSuccess<TResponse>;
|
|
@@ -24,11 +23,10 @@ export declare const useDeleteRequest: <TResponse>() => {
|
|
|
24
23
|
isPreviousData: boolean;
|
|
25
24
|
isRefetching: boolean;
|
|
26
25
|
isStale: boolean;
|
|
27
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
26
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
28
27
|
remove: () => void;
|
|
29
28
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
30
|
-
|
|
31
|
-
deleteR: (link: string, fetchOptions?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
29
|
+
destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
32
30
|
} | {
|
|
33
31
|
data: IRequestSuccess<TResponse>;
|
|
34
32
|
error: null;
|
|
@@ -52,11 +50,10 @@ export declare const useDeleteRequest: <TResponse>() => {
|
|
|
52
50
|
isPreviousData: boolean;
|
|
53
51
|
isRefetching: boolean;
|
|
54
52
|
isStale: boolean;
|
|
55
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
53
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
56
54
|
remove: () => void;
|
|
57
55
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
58
|
-
|
|
59
|
-
deleteR: (link: string, fetchOptions?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
56
|
+
destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
60
57
|
} | {
|
|
61
58
|
data: undefined;
|
|
62
59
|
error: any;
|
|
@@ -80,11 +77,10 @@ export declare const useDeleteRequest: <TResponse>() => {
|
|
|
80
77
|
isPreviousData: boolean;
|
|
81
78
|
isRefetching: boolean;
|
|
82
79
|
isStale: boolean;
|
|
83
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
80
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
84
81
|
remove: () => void;
|
|
85
82
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
86
|
-
|
|
87
|
-
deleteR: (link: string, fetchOptions?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
83
|
+
destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
88
84
|
} | {
|
|
89
85
|
data: undefined;
|
|
90
86
|
error: null;
|
|
@@ -108,9 +104,8 @@ export declare const useDeleteRequest: <TResponse>() => {
|
|
|
108
104
|
isPreviousData: boolean;
|
|
109
105
|
isRefetching: boolean;
|
|
110
106
|
isStale: boolean;
|
|
111
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
107
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
112
108
|
remove: () => void;
|
|
113
109
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
114
|
-
|
|
115
|
-
deleteR: (link: string, fetchOptions?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<QueryObserverResult<IRequestSuccess<TResponse>, any>>;
|
|
110
|
+
destroy: (link: string, deleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
116
111
|
};
|
|
@@ -5,17 +5,20 @@ var react = require('react');
|
|
|
5
5
|
require('axios');
|
|
6
6
|
var makeRequest = require('../request/make-request.js');
|
|
7
7
|
var request_enum = require('../request/request.enum.js');
|
|
8
|
+
var useQueryConfig = require('./useQueryConfig.js');
|
|
8
9
|
|
|
9
10
|
const useDeleteRequest = () => {
|
|
10
11
|
const [requestPath, updateDeletePath] = react.useState('');
|
|
11
12
|
const [options, setOptions] = react.useState();
|
|
12
|
-
const
|
|
13
|
+
const { headers, baseURL, timeout } = useQueryConfig.useQueryConfig();
|
|
13
14
|
const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => {
|
|
14
15
|
setTimeout(async () => {
|
|
15
16
|
const postResponse = await makeRequest.makeRequest({
|
|
16
17
|
path: requestPath,
|
|
17
|
-
|
|
18
|
+
headers,
|
|
18
19
|
method: request_enum.HttpMethod.DELETE,
|
|
20
|
+
baseURL,
|
|
21
|
+
timeout,
|
|
19
22
|
});
|
|
20
23
|
if (postResponse.status) {
|
|
21
24
|
res(postResponse);
|
|
@@ -31,14 +34,18 @@ const useDeleteRequest = () => {
|
|
|
31
34
|
const setOptionsAsync = async (fetchOptions) => {
|
|
32
35
|
return setOptions(fetchOptions);
|
|
33
36
|
};
|
|
34
|
-
const
|
|
37
|
+
const destroy = async (link, deleteOptions) => {
|
|
38
|
+
// set enabled to be true for every delete
|
|
39
|
+
deleteOptions = deleteOptions ?? {};
|
|
40
|
+
deleteOptions.enabled = true;
|
|
35
41
|
await updatedPathAsync(link);
|
|
36
|
-
await setOptionsAsync(
|
|
37
|
-
return query.refetch({
|
|
38
|
-
|
|
39
|
-
});
|
|
42
|
+
await setOptionsAsync(deleteOptions);
|
|
43
|
+
// return query.refetch<TResponse>({
|
|
44
|
+
// queryKey: [link, {}],
|
|
45
|
+
// });
|
|
46
|
+
return query.data;
|
|
40
47
|
};
|
|
41
|
-
return {
|
|
48
|
+
return { destroy, ...query };
|
|
42
49
|
};
|
|
43
50
|
|
|
44
51
|
exports.useDeleteRequest = useDeleteRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -5,16 +5,19 @@ var react = require('react');
|
|
|
5
5
|
require('axios');
|
|
6
6
|
var makeRequest = require('../request/make-request.js');
|
|
7
7
|
require('../request/request.enum.js');
|
|
8
|
+
var useQueryConfig = require('./useQueryConfig.js');
|
|
8
9
|
|
|
9
10
|
const useGetRequest = ({ path, load = false, queryOptions, }) => {
|
|
10
11
|
const [requestPath, updatePath] = react.useState(path);
|
|
11
|
-
const authToken = '';
|
|
12
12
|
const [options, setOptions] = react.useState(queryOptions);
|
|
13
13
|
const [page, setPage] = react.useState(1);
|
|
14
|
+
const { headers, baseURL, timeout } = useQueryConfig.useQueryConfig();
|
|
14
15
|
const sendRequest = async (res, rej) => {
|
|
15
16
|
const postResponse = await makeRequest.makeRequest({
|
|
16
17
|
path: requestPath,
|
|
17
|
-
|
|
18
|
+
headers,
|
|
19
|
+
baseURL,
|
|
20
|
+
timeout,
|
|
18
21
|
});
|
|
19
22
|
if (postResponse.status) {
|
|
20
23
|
res(postResponse);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,10 +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
|
+
export declare const usePatchRequest: <TResponse>({ path, }: {
|
|
4
4
|
path: string;
|
|
5
5
|
isFormData?: boolean | undefined;
|
|
6
6
|
}) => {
|
|
7
|
-
resetForm: boolean;
|
|
8
7
|
data: undefined;
|
|
9
8
|
error: null;
|
|
10
9
|
isError: false;
|
|
@@ -22,7 +21,6 @@ export declare const usePatchRequest: <TResponse>({ path, isFormData, }: {
|
|
|
22
21
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
23
22
|
patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
24
23
|
} | {
|
|
25
|
-
resetForm: boolean;
|
|
26
24
|
data: undefined;
|
|
27
25
|
error: null;
|
|
28
26
|
isError: false;
|
|
@@ -40,7 +38,6 @@ export declare const usePatchRequest: <TResponse>({ path, isFormData, }: {
|
|
|
40
38
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
41
39
|
patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
42
40
|
} | {
|
|
43
|
-
resetForm: boolean;
|
|
44
41
|
data: undefined;
|
|
45
42
|
error: IRequestError;
|
|
46
43
|
isError: true;
|
|
@@ -58,7 +55,6 @@ export declare const usePatchRequest: <TResponse>({ path, isFormData, }: {
|
|
|
58
55
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
59
56
|
patch: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
60
57
|
} | {
|
|
61
|
-
resetForm: boolean;
|
|
62
58
|
data: IRequestSuccess<TResponse>;
|
|
63
59
|
error: null;
|
|
64
60
|
isError: false;
|
|
@@ -1,27 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var reactQuery = require('@tanstack/react-query');
|
|
4
|
-
var react = require('react');
|
|
5
4
|
var scrollToTop = require('../helpers/scrollToTop.js');
|
|
6
5
|
require('axios');
|
|
7
6
|
var makeRequest = require('../request/make-request.js');
|
|
8
7
|
var request_enum = require('../request/request.enum.js');
|
|
8
|
+
var useQueryConfig = require('./useQueryConfig.js');
|
|
9
9
|
|
|
10
|
-
const usePatchRequest = ({ path,
|
|
11
|
-
const
|
|
12
|
-
const [resetForm, setResetForm] = react.useState(false);
|
|
10
|
+
const usePatchRequest = ({ path, }) => {
|
|
11
|
+
const { headers, baseURL, timeout } = useQueryConfig.useQueryConfig();
|
|
13
12
|
// register post mutation
|
|
14
13
|
const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
|
|
15
|
-
setResetForm(false);
|
|
16
14
|
makeRequest.makeRequest({
|
|
17
15
|
path: path,
|
|
18
16
|
body: postData,
|
|
19
17
|
method: request_enum.HttpMethod.PATCH,
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
headers,
|
|
19
|
+
baseURL,
|
|
20
|
+
timeout,
|
|
22
21
|
}).then((postResponse) => {
|
|
23
22
|
if (postResponse.status) {
|
|
24
|
-
setResetForm(true);
|
|
25
23
|
// scroll to top after success
|
|
26
24
|
scrollToTop.scrollToTop();
|
|
27
25
|
res(postResponse);
|
|
@@ -39,7 +37,7 @@ const usePatchRequest = ({ path, isFormData = false, }) => {
|
|
|
39
37
|
const patch = async (postData, options) => {
|
|
40
38
|
return mutation.mutateAsync(postData, options);
|
|
41
39
|
};
|
|
42
|
-
return { patch, ...mutation
|
|
40
|
+
return { patch, ...mutation };
|
|
43
41
|
};
|
|
44
42
|
|
|
45
43
|
exports.usePatchRequest = usePatchRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -4,7 +4,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
4
4
|
path: string;
|
|
5
5
|
isFormData?: boolean | undefined;
|
|
6
6
|
}) => {
|
|
7
|
-
resetForm: boolean;
|
|
8
7
|
data: undefined;
|
|
9
8
|
error: null;
|
|
10
9
|
isError: false;
|
|
@@ -22,7 +21,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
22
21
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
23
22
|
post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
24
23
|
} | {
|
|
25
|
-
resetForm: boolean;
|
|
26
24
|
data: undefined;
|
|
27
25
|
error: null;
|
|
28
26
|
isError: false;
|
|
@@ -40,7 +38,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
40
38
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
41
39
|
post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
42
40
|
} | {
|
|
43
|
-
resetForm: boolean;
|
|
44
41
|
data: undefined;
|
|
45
42
|
error: IRequestError;
|
|
46
43
|
isError: true;
|
|
@@ -58,7 +55,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
58
55
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
59
56
|
post: (postData: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
60
57
|
} | {
|
|
61
|
-
resetForm: boolean;
|
|
62
58
|
data: IRequestSuccess<TResponse>;
|
|
63
59
|
error: null;
|
|
64
60
|
isError: false;
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var reactQuery = require('@tanstack/react-query');
|
|
4
|
-
var react = require('react');
|
|
5
4
|
require('axios');
|
|
6
5
|
var makeRequest = require('../request/make-request.js');
|
|
7
6
|
var request_enum = require('../request/request.enum.js');
|
|
7
|
+
var useQueryConfig = require('./useQueryConfig.js');
|
|
8
8
|
|
|
9
9
|
const usePostRequest = ({ path, isFormData = false, }) => {
|
|
10
|
-
const
|
|
11
|
-
const [resetForm, setResetForm] = react.useState(false);
|
|
10
|
+
const { headers, baseURL, timeout } = useQueryConfig.useQueryConfig();
|
|
12
11
|
// register post mutation
|
|
13
12
|
const mutation = reactQuery.useMutation(async (postData) => new Promise((res, rej) => {
|
|
14
|
-
setResetForm(false);
|
|
15
13
|
makeRequest.makeRequest({
|
|
16
14
|
path: path,
|
|
17
15
|
body: postData,
|
|
18
16
|
method: request_enum.HttpMethod.POST,
|
|
19
|
-
bearerToken: authToken,
|
|
20
17
|
isFormData,
|
|
18
|
+
headers,
|
|
19
|
+
baseURL,
|
|
20
|
+
timeout,
|
|
21
21
|
}).then((postResponse) => {
|
|
22
22
|
if (postResponse.status) {
|
|
23
|
-
setResetForm(true);
|
|
24
23
|
// scroll to top after success
|
|
25
24
|
window.scrollTo({
|
|
26
25
|
top: 0,
|
|
@@ -41,7 +40,7 @@ const usePostRequest = ({ path, isFormData = false, }) => {
|
|
|
41
40
|
const post = async (postData, options) => {
|
|
42
41
|
return mutation.mutateAsync(postData, options);
|
|
43
42
|
};
|
|
44
|
-
return { post, ...mutation
|
|
43
|
+
return { post, ...mutation };
|
|
45
44
|
};
|
|
46
45
|
|
|
47
46
|
exports.usePostRequest = usePostRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var reactQuery = require('@tanstack/react-query');
|
|
4
|
+
|
|
5
|
+
const useQueryConfig = () => {
|
|
6
|
+
const { getQueryData } = reactQuery.useQueryClient();
|
|
7
|
+
const { headers, baseURL, timeout } = getQueryData([
|
|
8
|
+
'config',
|
|
9
|
+
]);
|
|
10
|
+
return { headers, baseURL, timeout };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
exports.useQueryConfig = useQueryConfig;
|
|
14
|
+
//# sourceMappingURL=useQueryConfig.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQueryConfig.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type { AxiosInstance
|
|
2
|
-
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
|
+
import type { IMakeRequest } from './request.interface';
|
|
3
|
+
export declare const axiosInstance: ({ baseURL, timeout, headers, }: Partial<IMakeRequest>) => AxiosInstance;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var axios = require('axios');
|
|
4
|
-
var index = require('../constants/index.js');
|
|
5
4
|
|
|
6
|
-
const axiosInstance = (headers) => {
|
|
5
|
+
const axiosInstance = ({ baseURL, timeout, headers, }) => {
|
|
7
6
|
return axios.create({
|
|
8
|
-
baseURL
|
|
9
|
-
timeout
|
|
7
|
+
baseURL,
|
|
8
|
+
timeout,
|
|
10
9
|
headers,
|
|
11
10
|
});
|
|
12
11
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"axios-instance.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"axios-instance.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { IMakeRequest } from './request.interface';
|
|
2
|
-
export declare function makeRequest<TResponse>({ body, method, path,
|
|
2
|
+
export declare function makeRequest<TResponse>({ body, method, path, isFormData, headers, baseURL, timeout, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
|
|
@@ -5,18 +5,15 @@ var buildFormData = require('./buildFormData.js');
|
|
|
5
5
|
var request_enum = require('./request.enum.js');
|
|
6
6
|
var transformer = require('./transformer.js');
|
|
7
7
|
|
|
8
|
-
async function makeRequest({ body, method = request_enum.HttpMethod.GET, path,
|
|
8
|
+
async function makeRequest({ body, method = request_enum.HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, }) {
|
|
9
9
|
// configure body
|
|
10
10
|
body = isFormData ? buildFormData.buildFormData(body) : body;
|
|
11
11
|
// configure request header
|
|
12
|
-
const headers = {
|
|
13
|
-
Authorization: `Bearer ${bearerToken}`,
|
|
14
|
-
};
|
|
15
12
|
if (!isFormData) {
|
|
16
13
|
headers['Content-Type'] = request_enum.ContentType.APPLICATION_JSON;
|
|
17
14
|
}
|
|
18
15
|
try {
|
|
19
|
-
const axios = axiosInstance.axiosInstance(headers);
|
|
16
|
+
const axios = axiosInstance.axiosInstance({ baseURL, headers, timeout });
|
|
20
17
|
// send request
|
|
21
18
|
const resp = await axios({
|
|
22
19
|
url: path,
|
|
@@ -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,13 @@
|
|
|
1
|
+
import type { RawAxiosRequestHeaders } from 'axios';
|
|
1
2
|
import type { HttpMethod } from './request.enum';
|
|
2
3
|
export interface IMakeRequest {
|
|
4
|
+
baseURL: string;
|
|
5
|
+
timeout?: number;
|
|
3
6
|
path: string;
|
|
4
7
|
body?: Record<string, any> | null;
|
|
5
8
|
method?: HttpMethod;
|
|
6
|
-
bearerToken?: string;
|
|
7
9
|
isFormData?: boolean;
|
|
10
|
+
headers: RawAxiosRequestHeaders;
|
|
8
11
|
}
|
|
9
12
|
export interface AxiosInstanceOption {
|
|
10
13
|
bearerToken?: string;
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/queries/index.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useQuery } from '@tanstack/react-query';
|
|
3
3
|
import { useState } from 'react';
|
|
4
4
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
5
5
|
import { HttpMethod, makeRequest } from '../request';
|
|
6
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
6
7
|
|
|
7
8
|
export const useDeleteRequest = <TResponse>() => {
|
|
8
9
|
const [requestPath, updateDeletePath] = useState<string>('');
|
|
9
10
|
const [options, setOptions] = useState<any>();
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
13
|
+
|
|
11
14
|
const query = useQuery<any, any, IRequestSuccess<TResponse>>(
|
|
12
15
|
[requestPath, {}],
|
|
13
16
|
() =>
|
|
@@ -15,8 +18,10 @@ export const useDeleteRequest = <TResponse>() => {
|
|
|
15
18
|
setTimeout(async () => {
|
|
16
19
|
const postResponse = await makeRequest<TResponse>({
|
|
17
20
|
path: requestPath,
|
|
18
|
-
|
|
21
|
+
headers,
|
|
19
22
|
method: HttpMethod.DELETE,
|
|
23
|
+
baseURL,
|
|
24
|
+
timeout,
|
|
20
25
|
});
|
|
21
26
|
if (postResponse.status) {
|
|
22
27
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
@@ -35,18 +40,29 @@ export const useDeleteRequest = <TResponse>() => {
|
|
|
35
40
|
const setOptionsAsync = async (fetchOptions: any) => {
|
|
36
41
|
return setOptions(fetchOptions);
|
|
37
42
|
};
|
|
38
|
-
|
|
43
|
+
|
|
44
|
+
const destroy = async (
|
|
39
45
|
link: string,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
deleteOptions?: UseQueryOptions<
|
|
47
|
+
IRequestSuccess<TResponse | undefined>,
|
|
48
|
+
IRequestError,
|
|
49
|
+
IRequestSuccess<TResponse | undefined>,
|
|
50
|
+
Array<any>
|
|
51
|
+
>
|
|
52
|
+
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
53
|
+
// set enabled to be true for every delete
|
|
54
|
+
deleteOptions = deleteOptions ?? {};
|
|
55
|
+
deleteOptions.enabled = true;
|
|
56
|
+
|
|
44
57
|
await updatedPathAsync(link);
|
|
45
|
-
await setOptionsAsync(
|
|
58
|
+
await setOptionsAsync(deleteOptions);
|
|
59
|
+
|
|
60
|
+
// return query.refetch<TResponse>({
|
|
61
|
+
// queryKey: [link, {}],
|
|
62
|
+
// });
|
|
46
63
|
|
|
47
|
-
return query.
|
|
48
|
-
queryKey: [link, {}],
|
|
49
|
-
});
|
|
64
|
+
return query.data;
|
|
50
65
|
};
|
|
51
|
-
|
|
66
|
+
|
|
67
|
+
return { destroy, ...query };
|
|
52
68
|
};
|
|
@@ -4,6 +4,7 @@ import { startTransition, useEffect, useState } from 'react';
|
|
|
4
4
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
5
5
|
import { makeRequest } from '../request';
|
|
6
6
|
import type { IPagination, TanstackQueryOption } from './queries.interface';
|
|
7
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
7
8
|
|
|
8
9
|
export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
9
10
|
path,
|
|
@@ -15,10 +16,11 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
15
16
|
queryOptions?: TanstackQueryOption<TResponse>;
|
|
16
17
|
}) => {
|
|
17
18
|
const [requestPath, updatePath] = useState<string>(path);
|
|
18
|
-
const authToken = '';
|
|
19
19
|
const [options, setOptions] = useState<any>(queryOptions);
|
|
20
20
|
const [page, setPage] = useState<number>(1);
|
|
21
21
|
|
|
22
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
23
|
+
|
|
22
24
|
const sendRequest = async (
|
|
23
25
|
res: (
|
|
24
26
|
value:
|
|
@@ -30,7 +32,9 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
30
32
|
) => {
|
|
31
33
|
const postResponse = await makeRequest<TResponse>({
|
|
32
34
|
path: requestPath,
|
|
33
|
-
|
|
35
|
+
headers,
|
|
36
|
+
baseURL,
|
|
37
|
+
timeout,
|
|
34
38
|
});
|
|
35
39
|
if (postResponse.status) {
|
|
36
40
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
-
import { useState } from 'react';
|
|
4
3
|
import { scrollToTop } from '../helpers';
|
|
5
4
|
import { HttpMethod, makeRequest } from '../request';
|
|
6
5
|
|
|
@@ -8,32 +7,29 @@ import type {
|
|
|
8
7
|
IRequestError,
|
|
9
8
|
IRequestSuccess,
|
|
10
9
|
} from '../request/request.interface';
|
|
10
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
11
11
|
|
|
12
12
|
export const usePatchRequest = <TResponse>({
|
|
13
13
|
path,
|
|
14
|
-
isFormData = false,
|
|
15
14
|
}: {
|
|
16
15
|
path: string;
|
|
17
16
|
isFormData?: boolean;
|
|
18
17
|
}) => {
|
|
19
|
-
const
|
|
20
|
-
const [resetForm, setResetForm] = useState<boolean>(false);
|
|
18
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
21
19
|
|
|
22
20
|
// register post mutation
|
|
23
21
|
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
|
|
24
22
|
(postData: any) =>
|
|
25
23
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
26
|
-
setResetForm(false);
|
|
27
|
-
|
|
28
24
|
makeRequest<TResponse>({
|
|
29
25
|
path: path,
|
|
30
26
|
body: postData,
|
|
31
27
|
method: HttpMethod.PATCH,
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
headers,
|
|
29
|
+
baseURL,
|
|
30
|
+
timeout,
|
|
34
31
|
}).then((postResponse) => {
|
|
35
32
|
if (postResponse.status) {
|
|
36
|
-
setResetForm(true);
|
|
37
33
|
// scroll to top after success
|
|
38
34
|
scrollToTop();
|
|
39
35
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
@@ -48,6 +44,7 @@ export const usePatchRequest = <TResponse>({
|
|
|
48
44
|
});
|
|
49
45
|
})
|
|
50
46
|
);
|
|
47
|
+
|
|
51
48
|
const patch = async (
|
|
52
49
|
postData: any,
|
|
53
50
|
options?:
|
|
@@ -57,5 +54,5 @@ export const usePatchRequest = <TResponse>({
|
|
|
57
54
|
return mutation.mutateAsync(postData, options);
|
|
58
55
|
};
|
|
59
56
|
|
|
60
|
-
return { patch, ...mutation
|
|
57
|
+
return { patch, ...mutation };
|
|
61
58
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
-
import { useState } from 'react';
|
|
4
3
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
5
4
|
import { HttpMethod, makeRequest } from '../request';
|
|
5
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
6
6
|
|
|
7
7
|
export const usePostRequest = <TResponse>({
|
|
8
8
|
path,
|
|
@@ -11,24 +11,22 @@ export const usePostRequest = <TResponse>({
|
|
|
11
11
|
path: string;
|
|
12
12
|
isFormData?: boolean;
|
|
13
13
|
}) => {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const [resetForm, setResetForm] = useState<boolean>(false);
|
|
14
|
+
const { headers, baseURL, timeout } = useQueryConfig();
|
|
17
15
|
|
|
18
16
|
// register post mutation
|
|
19
17
|
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
|
|
20
18
|
async (postData: any) =>
|
|
21
19
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
22
|
-
setResetForm(false);
|
|
23
20
|
makeRequest<TResponse>({
|
|
24
21
|
path: path,
|
|
25
22
|
body: postData,
|
|
26
23
|
method: HttpMethod.POST,
|
|
27
|
-
bearerToken: authToken,
|
|
28
24
|
isFormData,
|
|
25
|
+
headers,
|
|
26
|
+
baseURL,
|
|
27
|
+
timeout,
|
|
29
28
|
}).then((postResponse) => {
|
|
30
29
|
if (postResponse.status) {
|
|
31
|
-
setResetForm(true);
|
|
32
30
|
// scroll to top after success
|
|
33
31
|
window.scrollTo({
|
|
34
32
|
top: 0,
|
|
@@ -55,5 +53,5 @@ export const usePostRequest = <TResponse>({
|
|
|
55
53
|
return mutation.mutateAsync(postData, options);
|
|
56
54
|
};
|
|
57
55
|
|
|
58
|
-
return { post, ...mutation
|
|
56
|
+
return { post, ...mutation };
|
|
59
57
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import type { TanstackQueryConfig } from '../types';
|
|
3
|
+
|
|
4
|
+
export const useQueryConfig = (): TanstackQueryConfig => {
|
|
5
|
+
const { getQueryData } = useQueryClient();
|
|
6
|
+
|
|
7
|
+
const { headers, baseURL, timeout } = getQueryData<TanstackQueryConfig>([
|
|
8
|
+
'config',
|
|
9
|
+
]) as TanstackQueryConfig;
|
|
10
|
+
|
|
11
|
+
return { headers, baseURL, timeout };
|
|
12
|
+
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import type { AxiosInstance
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
2
|
import axios from 'axios';
|
|
3
|
-
import {
|
|
3
|
+
import type { IMakeRequest } from './request.interface';
|
|
4
4
|
|
|
5
|
-
export const axiosInstance = (
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export const axiosInstance = ({
|
|
6
|
+
baseURL,
|
|
7
|
+
timeout,
|
|
8
|
+
headers,
|
|
9
|
+
}: Partial<IMakeRequest>): AxiosInstance => {
|
|
8
10
|
return axios.create({
|
|
9
|
-
baseURL
|
|
10
|
-
timeout
|
|
11
|
+
baseURL,
|
|
12
|
+
timeout,
|
|
11
13
|
headers,
|
|
12
14
|
});
|
|
13
15
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
2
1
|
import { axiosInstance } from './axios-instance';
|
|
3
2
|
|
|
4
3
|
import { buildFormData } from './buildFormData';
|
|
@@ -10,23 +9,22 @@ export async function makeRequest<TResponse>({
|
|
|
10
9
|
body,
|
|
11
10
|
method = HttpMethod.GET,
|
|
12
11
|
path,
|
|
13
|
-
bearerToken,
|
|
14
12
|
isFormData,
|
|
13
|
+
headers = {},
|
|
14
|
+
baseURL,
|
|
15
|
+
timeout,
|
|
15
16
|
}: IMakeRequest) {
|
|
16
17
|
// configure body
|
|
17
18
|
body = isFormData ? buildFormData(body as Record<string, any>) : body;
|
|
18
19
|
|
|
19
20
|
// configure request header
|
|
20
|
-
const headers: RawAxiosRequestHeaders = {
|
|
21
|
-
Authorization: `Bearer ${bearerToken}`,
|
|
22
|
-
};
|
|
23
21
|
|
|
24
22
|
if (!isFormData) {
|
|
25
23
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
try {
|
|
29
|
-
const axios = axiosInstance(headers);
|
|
27
|
+
const axios = axiosInstance({ baseURL, headers, timeout });
|
|
30
28
|
|
|
31
29
|
// send request
|
|
32
30
|
const resp = await axios({
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import type { RawAxiosRequestHeaders } from 'axios';
|
|
1
2
|
import type { HttpMethod } from './request.enum';
|
|
2
3
|
|
|
3
4
|
export interface IMakeRequest {
|
|
5
|
+
baseURL: string;
|
|
6
|
+
timeout?: number;
|
|
4
7
|
path: string;
|
|
5
8
|
body?: Record<string, any> | null;
|
|
6
9
|
method?: HttpMethod;
|
|
7
|
-
bearerToken?: string;
|
|
8
10
|
isFormData?: boolean;
|
|
11
|
+
headers: RawAxiosRequestHeaders;
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
export interface AxiosInstanceOption {
|
package/src/types/index.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const API_BASE_URL = "https://pokeapi.co/api/v2";
|
package/dist/constants/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/src/constants/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const API_BASE_URL = 'https://pokeapi.co/api/v2';
|