@ventlio/tanstack-query 0.2.45 → 0.2.46
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 +89 -92
- package/dist/index.mjs.map +1 -1
- package/dist/queries/queries.interface.d.ts +5 -0
- package/dist/queries/useDeleteRequest.d.ts +2 -1
- package/dist/queries/useDeleteRequest.js +19 -20
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +3 -3
- package/dist/queries/useGetRequest.js +5 -7
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +7 -6
- package/dist/queries/usePatchRequest.js +31 -30
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +7 -6
- package/dist/queries/usePostRequest.js +34 -35
- package/dist/queries/usePostRequest.js.map +1 -1
- package/package.json +1 -1
- package/src/queries/queries.interface.ts +6 -0
- package/src/queries/useDeleteRequest.ts +29 -20
- package/src/queries/useGetRequest.ts +15 -9
- package/src/queries/usePatchRequest.ts +42 -32
- package/src/queries/usePostRequest.ts +44 -35
package/dist/index.mjs
CHANGED
|
@@ -200,30 +200,29 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
const useDeleteRequest = () => {
|
|
203
|
+
const useDeleteRequest = ({ baseUrl, headers, }) => {
|
|
204
204
|
const [requestPath, updateDeletePath] = useState('');
|
|
205
205
|
const [options, setOptions] = useState();
|
|
206
206
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
207
207
|
const { getHeaders } = useQueryHeaders();
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}), { enabled: false, ...options });
|
|
208
|
+
const sendRequest = async (res, rej) => {
|
|
209
|
+
// get request headers
|
|
210
|
+
const globalHeaders = getHeaders();
|
|
211
|
+
const postResponse = await makeRequest({
|
|
212
|
+
path: requestPath,
|
|
213
|
+
headers: { ...globalHeaders, ...headers },
|
|
214
|
+
method: HttpMethod.DELETE,
|
|
215
|
+
baseURL: baseUrl ?? API_URL,
|
|
216
|
+
timeout: TIMEOUT,
|
|
217
|
+
});
|
|
218
|
+
if (postResponse.status) {
|
|
219
|
+
res(postResponse);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
rej(postResponse);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), { enabled: false, ...options });
|
|
227
226
|
const updatedPathAsync = async (link) => {
|
|
228
227
|
return updateDeletePath(link);
|
|
229
228
|
};
|
|
@@ -241,7 +240,7 @@ const useDeleteRequest = () => {
|
|
|
241
240
|
return { destroy, ...query };
|
|
242
241
|
};
|
|
243
242
|
|
|
244
|
-
const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
243
|
+
const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
245
244
|
const [requestPath, updatePath] = useState(path);
|
|
246
245
|
const [options, setOptions] = useState(queryOptions);
|
|
247
246
|
const [page, setPage] = useState(1);
|
|
@@ -252,11 +251,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
252
251
|
queryClient = useMemo(() => queryClient, []);
|
|
253
252
|
const sendRequest = async (res, rej) => {
|
|
254
253
|
// get request headers
|
|
255
|
-
const
|
|
254
|
+
const globalHeaders = getHeaders();
|
|
256
255
|
const getResponse = await makeRequest({
|
|
257
256
|
path: requestPath,
|
|
258
|
-
headers,
|
|
259
|
-
baseURL: API_URL,
|
|
257
|
+
headers: { ...globalHeaders, ...headers },
|
|
258
|
+
baseURL: baseUrl ?? API_URL,
|
|
260
259
|
timeout: TIMEOUT,
|
|
261
260
|
});
|
|
262
261
|
if (getResponse.status) {
|
|
@@ -266,9 +265,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
266
265
|
rej(getResponse);
|
|
267
266
|
}
|
|
268
267
|
};
|
|
269
|
-
const query = useQuery([requestPath, {}], () => new Promise((res, rej) => {
|
|
270
|
-
return sendRequest(res, rej);
|
|
271
|
-
}), {
|
|
268
|
+
const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
|
|
272
269
|
enabled: load,
|
|
273
270
|
...options,
|
|
274
271
|
});
|
|
@@ -348,82 +345,82 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
348
345
|
};
|
|
349
346
|
};
|
|
350
347
|
|
|
351
|
-
const usePatchRequest = ({ path }) => {
|
|
348
|
+
const usePatchRequest = ({ path, baseUrl, headers, }) => {
|
|
352
349
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
353
350
|
const { getHeaders } = useQueryHeaders();
|
|
351
|
+
const sendRequest = async (res, rej, data) => {
|
|
352
|
+
// get request headers
|
|
353
|
+
const globalHeaders = getHeaders();
|
|
354
|
+
makeRequest({
|
|
355
|
+
path: path,
|
|
356
|
+
body: data,
|
|
357
|
+
method: HttpMethod.PATCH,
|
|
358
|
+
headers: { ...globalHeaders, ...headers },
|
|
359
|
+
baseURL: baseUrl ?? API_URL,
|
|
360
|
+
timeout: TIMEOUT,
|
|
361
|
+
}).then((postResponse) => {
|
|
362
|
+
if (postResponse.status) {
|
|
363
|
+
// scroll to top after success
|
|
364
|
+
scrollToTop();
|
|
365
|
+
res(postResponse);
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
// scroll to top after error
|
|
369
|
+
window.scrollTo({
|
|
370
|
+
top: 0,
|
|
371
|
+
behavior: 'smooth',
|
|
372
|
+
});
|
|
373
|
+
rej(postResponse);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
};
|
|
354
377
|
// register post mutation
|
|
355
|
-
const mutation = useMutation((
|
|
356
|
-
return (
|
|
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
|
-
})();
|
|
378
|
+
const mutation = useMutation((dataData) => new Promise((res, rej) => {
|
|
379
|
+
return sendRequest(res, rej, dataData);
|
|
382
380
|
}));
|
|
383
|
-
const patch = async (
|
|
384
|
-
return mutation.mutateAsync(
|
|
381
|
+
const patch = async (data, options) => {
|
|
382
|
+
return mutation.mutateAsync(data, options);
|
|
385
383
|
};
|
|
386
384
|
return { patch, ...mutation };
|
|
387
385
|
};
|
|
388
386
|
|
|
389
|
-
const usePostRequest = ({ path, isFormData = false, }) => {
|
|
387
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
390
388
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
391
389
|
const { getHeaders } = useQueryHeaders();
|
|
390
|
+
const sendRequest = async (res, rej, postData) => {
|
|
391
|
+
// get request headers
|
|
392
|
+
const globalHeaders = getHeaders();
|
|
393
|
+
makeRequest({
|
|
394
|
+
path: path,
|
|
395
|
+
body: postData,
|
|
396
|
+
method: HttpMethod.POST,
|
|
397
|
+
isFormData,
|
|
398
|
+
headers: { ...globalHeaders, ...headers },
|
|
399
|
+
baseURL: baseUrl ?? API_URL,
|
|
400
|
+
timeout: TIMEOUT,
|
|
401
|
+
}).then((postResponse) => {
|
|
402
|
+
if (postResponse.status) {
|
|
403
|
+
// scroll to top after success
|
|
404
|
+
window.scrollTo({
|
|
405
|
+
top: 0,
|
|
406
|
+
behavior: 'smooth',
|
|
407
|
+
});
|
|
408
|
+
res(postResponse);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
// scroll to top after error
|
|
412
|
+
window.scrollTo({
|
|
413
|
+
top: 0,
|
|
414
|
+
behavior: 'smooth',
|
|
415
|
+
});
|
|
416
|
+
rej(postResponse);
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
};
|
|
392
420
|
// register post mutation
|
|
393
|
-
const mutation = useMutation(async (postData) => new Promise((res, rej) =>
|
|
394
|
-
|
|
395
|
-
|
|
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);
|
|
421
|
+
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
|
|
422
|
+
const post = async (data, options) => {
|
|
423
|
+
return mutation.mutateAsync(data, options);
|
|
427
424
|
};
|
|
428
425
|
return { post, ...mutation };
|
|
429
426
|
};
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -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
|
-
|
|
3
|
+
import type { DefaultRequestOptions } from './queries.interface';
|
|
4
|
+
export declare const useDeleteRequest: <TResponse>({ baseUrl, headers, }: DefaultRequestOptions) => {
|
|
4
5
|
data: IRequestSuccess<TResponse>;
|
|
5
6
|
error: any;
|
|
6
7
|
isError: true;
|
|
@@ -8,30 +8,29 @@ 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 = ({ baseUrl, headers, }) => {
|
|
12
12
|
const [requestPath, updateDeletePath] = react.useState('');
|
|
13
13
|
const [options, setOptions] = react.useState();
|
|
14
14
|
const { API_URL, TIMEOUT } = useEnvironmentVariables.useEnvironmentVariables();
|
|
15
15
|
const { getHeaders } = useQueryHeaders.useQueryHeaders();
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}), { enabled: false, ...options });
|
|
16
|
+
const sendRequest = async (res, rej) => {
|
|
17
|
+
// get request headers
|
|
18
|
+
const globalHeaders = getHeaders();
|
|
19
|
+
const postResponse = await makeRequest.makeRequest({
|
|
20
|
+
path: requestPath,
|
|
21
|
+
headers: { ...globalHeaders, ...headers },
|
|
22
|
+
method: request_enum.HttpMethod.DELETE,
|
|
23
|
+
baseURL: baseUrl ?? API_URL,
|
|
24
|
+
timeout: TIMEOUT,
|
|
25
|
+
});
|
|
26
|
+
if (postResponse.status) {
|
|
27
|
+
res(postResponse);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
rej(postResponse);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const query = reactQuery.useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), { enabled: false, ...options });
|
|
35
34
|
const updatedPathAsync = async (link) => {
|
|
36
35
|
return updateDeletePath(link);
|
|
37
36
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -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
|
|
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
|
-
|
|
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: (
|
|
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: (
|
|
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: (
|
|
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: (
|
|
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((
|
|
16
|
-
return (
|
|
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 (
|
|
44
|
-
return mutation.mutateAsync(
|
|
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
|
-
|
|
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: (
|
|
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: (
|
|
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: (
|
|
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: (
|
|
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
|
-
|
|
16
|
-
|
|
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,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
|
+
baseUrl,
|
|
12
|
+
headers,
|
|
13
|
+
}: DefaultRequestOptions) => {
|
|
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
|
-
|
|
22
|
-
|
|
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 {
|
|
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
|
|
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
|
-
|
|
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>({
|
|
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
|
-
(
|
|
55
|
+
(dataData: any) =>
|
|
21
56
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
22
|
-
return (
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
25
|
-
|
|
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
|
-
|
|
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(
|
|
71
|
+
return mutation.mutateAsync(data, options);
|
|
63
72
|
};
|
|
64
73
|
|
|
65
74
|
return { post, ...mutation };
|