@ventlio/tanstack-query 0.2.63-beta.2 → 0.2.63-beta.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/index.mjs +39 -8
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useDeleteRequest.js +1 -1
- package/dist/queries/usePostRequest.d.ts +2 -1
- package/dist/queries/usePostRequest.js +7 -1
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/request/make-request.d.ts +1 -1
- package/dist/request/make-request.js +32 -6
- package/dist/request/make-request.js.map +1 -1
- package/dist/request/request.interface.d.ts +5 -0
- package/dist/types/index.d.ts +2 -1
- package/package.json +1 -1
- package/src/queries/useDeleteRequest.ts +1 -1
- package/src/queries/usePostRequest.ts +8 -2
- package/src/request/make-request.ts +34 -5
- package/src/request/request.interface.ts +5 -0
- package/src/types/index.ts +2 -2
package/dist/index.mjs
CHANGED
|
@@ -199,20 +199,33 @@ const successTransformer = (data) => {
|
|
|
199
199
|
};
|
|
200
200
|
};
|
|
201
201
|
|
|
202
|
-
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, }) {
|
|
202
|
+
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, }) {
|
|
203
|
+
// check if file is included in mobile app environment and extract all file input to avoid
|
|
204
|
+
// it being formatted to object using axios formData builder
|
|
205
|
+
const isApp = appFileConfig?.isApp;
|
|
206
|
+
const appFiles = isApp ? getAppFiles(body, appFileConfig.fileSelectors) : {};
|
|
203
207
|
// configure body
|
|
204
|
-
body = isFormData ? axios.toFormData(body) : body;
|
|
205
|
-
// configure request
|
|
208
|
+
body = (isFormData ? axios.toFormData(body) : body);
|
|
209
|
+
// configure request header1
|
|
206
210
|
if (!isFormData) {
|
|
207
211
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
208
212
|
}
|
|
209
213
|
else {
|
|
210
|
-
|
|
214
|
+
if (isApp) {
|
|
215
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
216
|
+
// add the app files
|
|
217
|
+
for (const fileKey in appFiles) {
|
|
218
|
+
body.append(fileKey, appFiles[fileKey]);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
delete headers['Content-Type'];
|
|
223
|
+
}
|
|
211
224
|
}
|
|
212
225
|
try {
|
|
213
|
-
const
|
|
226
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
214
227
|
// send request
|
|
215
|
-
const resp = await
|
|
228
|
+
const resp = await axiosRequest({
|
|
216
229
|
url: path,
|
|
217
230
|
method,
|
|
218
231
|
data: body,
|
|
@@ -240,6 +253,19 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
240
253
|
...errorData,
|
|
241
254
|
});
|
|
242
255
|
}
|
|
256
|
+
}
|
|
257
|
+
function getAppFiles(body, fileSelectors = []) {
|
|
258
|
+
const files = {};
|
|
259
|
+
if (body) {
|
|
260
|
+
if (fileSelectors.length > 0) {
|
|
261
|
+
//
|
|
262
|
+
for (const fileKey of fileSelectors) {
|
|
263
|
+
files[fileKey] = body[fileKey];
|
|
264
|
+
delete body[fileKey];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return files;
|
|
243
269
|
}
|
|
244
270
|
|
|
245
271
|
const useDeleteRequest = (deleteOptions) => {
|
|
@@ -277,7 +303,7 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
277
303
|
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
278
304
|
internalDeleteOptions.enabled = true;
|
|
279
305
|
await updatedPathAsync(link);
|
|
280
|
-
await setOptionsAsync(
|
|
306
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
281
307
|
return query.data;
|
|
282
308
|
};
|
|
283
309
|
return { destroy, ...query };
|
|
@@ -420,10 +446,11 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
420
446
|
return { patch, ...mutation };
|
|
421
447
|
};
|
|
422
448
|
|
|
423
|
-
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
449
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
424
450
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
425
451
|
const queryClient = useQueryClient();
|
|
426
452
|
const { getHeaders } = useQueryHeaders();
|
|
453
|
+
const { isApp } = useReactNativeEnv();
|
|
427
454
|
const sendRequest = async (res, rej, postData) => {
|
|
428
455
|
// get request headers
|
|
429
456
|
const globalHeaders = getHeaders();
|
|
@@ -436,6 +463,10 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
|
436
463
|
headers: { ...globalHeaders, ...headers },
|
|
437
464
|
baseURL: baseUrl ?? API_URL,
|
|
438
465
|
timeout: TIMEOUT,
|
|
466
|
+
appFileConfig: {
|
|
467
|
+
isApp,
|
|
468
|
+
fileSelectors,
|
|
469
|
+
},
|
|
439
470
|
});
|
|
440
471
|
if (postResponse.status) {
|
|
441
472
|
// scroll to top after success
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -41,7 +41,7 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
41
41
|
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
42
42
|
internalDeleteOptions.enabled = true;
|
|
43
43
|
await updatedPathAsync(link);
|
|
44
|
-
await setOptionsAsync(
|
|
44
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
45
45
|
return query.data;
|
|
46
46
|
};
|
|
47
47
|
return { destroy, ...query };
|
|
@@ -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
|
+
export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, headers, fileSelectors, }: {
|
|
5
5
|
path: string;
|
|
6
6
|
isFormData?: boolean | undefined;
|
|
7
|
+
fileSelectors?: string[] | undefined;
|
|
7
8
|
} & DefaultRequestOptions) => {
|
|
8
9
|
data: undefined;
|
|
9
10
|
error: null;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { useQueryClient, useMutation } from '@tanstack/react-query';
|
|
2
2
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
3
3
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
4
|
+
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
4
5
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
5
6
|
import 'axios';
|
|
6
7
|
import { makeRequest } from '../request/make-request.js';
|
|
7
8
|
import { HttpMethod } from '../request/request.enum.js';
|
|
8
9
|
|
|
9
|
-
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
10
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
10
11
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
11
12
|
const queryClient = useQueryClient();
|
|
12
13
|
const { getHeaders } = useQueryHeaders();
|
|
14
|
+
const { isApp } = useReactNativeEnv();
|
|
13
15
|
const sendRequest = async (res, rej, postData) => {
|
|
14
16
|
// get request headers
|
|
15
17
|
const globalHeaders = getHeaders();
|
|
@@ -22,6 +24,10 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
|
22
24
|
headers: { ...globalHeaders, ...headers },
|
|
23
25
|
baseURL: baseUrl ?? API_URL,
|
|
24
26
|
timeout: TIMEOUT,
|
|
27
|
+
appFileConfig: {
|
|
28
|
+
isApp,
|
|
29
|
+
fileSelectors,
|
|
30
|
+
},
|
|
25
31
|
});
|
|
26
32
|
if (postResponse.status) {
|
|
27
33
|
// scroll to top after success
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.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, isFormData, headers, baseURL, timeout, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
|
|
2
|
+
export declare function makeRequest<TResponse>({ body, method, path, isFormData, headers, baseURL, timeout, appFileConfig, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
|
|
@@ -3,20 +3,33 @@ import { axiosInstance } from './axios-instance.js';
|
|
|
3
3
|
import { ContentType, HttpMethod } from './request.enum.js';
|
|
4
4
|
import { errorTransformer, successTransformer } from './transformer.js';
|
|
5
5
|
|
|
6
|
-
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, }) {
|
|
6
|
+
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, }) {
|
|
7
|
+
// check if file is included in mobile app environment and extract all file input to avoid
|
|
8
|
+
// it being formatted to object using axios formData builder
|
|
9
|
+
const isApp = appFileConfig?.isApp;
|
|
10
|
+
const appFiles = isApp ? getAppFiles(body, appFileConfig.fileSelectors) : {};
|
|
7
11
|
// configure body
|
|
8
|
-
body = isFormData ? axios.toFormData(body) : body;
|
|
9
|
-
// configure request
|
|
12
|
+
body = (isFormData ? axios.toFormData(body) : body);
|
|
13
|
+
// configure request header1
|
|
10
14
|
if (!isFormData) {
|
|
11
15
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
12
16
|
}
|
|
13
17
|
else {
|
|
14
|
-
|
|
18
|
+
if (isApp) {
|
|
19
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
20
|
+
// add the app files
|
|
21
|
+
for (const fileKey in appFiles) {
|
|
22
|
+
body.append(fileKey, appFiles[fileKey]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
delete headers['Content-Type'];
|
|
27
|
+
}
|
|
15
28
|
}
|
|
16
29
|
try {
|
|
17
|
-
const
|
|
30
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
18
31
|
// send request
|
|
19
|
-
const resp = await
|
|
32
|
+
const resp = await axiosRequest({
|
|
20
33
|
url: path,
|
|
21
34
|
method,
|
|
22
35
|
data: body,
|
|
@@ -44,6 +57,19 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
44
57
|
...errorData,
|
|
45
58
|
});
|
|
46
59
|
}
|
|
60
|
+
}
|
|
61
|
+
function getAppFiles(body, fileSelectors = []) {
|
|
62
|
+
const files = {};
|
|
63
|
+
if (body) {
|
|
64
|
+
if (fileSelectors.length > 0) {
|
|
65
|
+
//
|
|
66
|
+
for (const fileKey of fileSelectors) {
|
|
67
|
+
files[fileKey] = body[fileKey];
|
|
68
|
+
delete body[fileKey];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return files;
|
|
47
73
|
}
|
|
48
74
|
|
|
49
75
|
export { makeRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -8,6 +8,11 @@ export interface IMakeRequest {
|
|
|
8
8
|
method?: HttpMethod;
|
|
9
9
|
isFormData?: boolean;
|
|
10
10
|
headers: RawAxiosRequestHeaders;
|
|
11
|
+
appFileConfig?: AppFileConfig;
|
|
12
|
+
}
|
|
13
|
+
export interface AppFileConfig {
|
|
14
|
+
fileSelectors?: string[];
|
|
15
|
+
isApp: boolean;
|
|
11
16
|
}
|
|
12
17
|
export interface AxiosInstanceOption {
|
|
13
18
|
bearerToken?: string;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -4,8 +4,9 @@ export interface BootstrapQueryRequest {
|
|
|
4
4
|
appBaseUrl: string;
|
|
5
5
|
appTimeout: number;
|
|
6
6
|
};
|
|
7
|
-
context?:
|
|
7
|
+
context?: ContextType;
|
|
8
8
|
}
|
|
9
|
+
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
9
10
|
export interface TanstackQueryConfig {
|
|
10
11
|
headers: RawAxiosRequestHeaders;
|
|
11
12
|
options?: BootstrapQueryRequest;
|
package/package.json
CHANGED
|
@@ -63,7 +63,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
63
63
|
internalDeleteOptions.enabled = true;
|
|
64
64
|
|
|
65
65
|
await updatedPathAsync(link);
|
|
66
|
-
await setOptionsAsync(
|
|
66
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
67
67
|
|
|
68
68
|
return query.data;
|
|
69
69
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
3
|
-
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
3
|
+
import { useEnvironmentVariables, useQueryHeaders, useReactNativeEnv } from '../config';
|
|
4
4
|
|
|
5
5
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
6
6
|
import { scrollToTop } from '../helpers';
|
|
@@ -14,14 +14,16 @@ export const usePostRequest = <TResponse>({
|
|
|
14
14
|
isFormData = false,
|
|
15
15
|
baseUrl,
|
|
16
16
|
headers,
|
|
17
|
+
fileSelectors,
|
|
17
18
|
}: {
|
|
18
19
|
path: string;
|
|
19
20
|
isFormData?: boolean;
|
|
21
|
+
fileSelectors?: string[];
|
|
20
22
|
} & DefaultRequestOptions) => {
|
|
21
23
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
22
24
|
const queryClient = useQueryClient();
|
|
23
25
|
const { getHeaders } = useQueryHeaders();
|
|
24
|
-
|
|
26
|
+
const { isApp } = useReactNativeEnv();
|
|
25
27
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, postData: any) => {
|
|
26
28
|
// get request headers
|
|
27
29
|
const globalHeaders: RawAxiosRequestHeaders = getHeaders();
|
|
@@ -35,6 +37,10 @@ export const usePostRequest = <TResponse>({
|
|
|
35
37
|
headers: { ...globalHeaders, ...headers },
|
|
36
38
|
baseURL: baseUrl ?? API_URL,
|
|
37
39
|
timeout: TIMEOUT,
|
|
40
|
+
appFileConfig: {
|
|
41
|
+
isApp,
|
|
42
|
+
fileSelectors,
|
|
43
|
+
},
|
|
38
44
|
});
|
|
39
45
|
|
|
40
46
|
if (postResponse.status) {
|
|
@@ -13,22 +13,36 @@ export async function makeRequest<TResponse>({
|
|
|
13
13
|
headers = {},
|
|
14
14
|
baseURL,
|
|
15
15
|
timeout,
|
|
16
|
+
appFileConfig,
|
|
16
17
|
}: IMakeRequest) {
|
|
18
|
+
// check if file is included in mobile app environment and extract all file input to avoid
|
|
19
|
+
// it being formatted to object using axios formData builder
|
|
20
|
+
const isApp = appFileConfig?.isApp;
|
|
21
|
+
const appFiles: Record<string, string> = isApp ? getAppFiles(body, appFileConfig.fileSelectors) : {};
|
|
22
|
+
|
|
17
23
|
// configure body
|
|
18
|
-
body = isFormData ? axios.toFormData(body as
|
|
24
|
+
body = (isFormData ? axios.toFormData(body as FormData) : body) as FormData;
|
|
19
25
|
|
|
20
|
-
// configure request
|
|
26
|
+
// configure request header1
|
|
21
27
|
if (!isFormData) {
|
|
22
28
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
23
29
|
} else {
|
|
24
|
-
|
|
30
|
+
if (isApp) {
|
|
31
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
32
|
+
// add the app files
|
|
33
|
+
for (const fileKey in appFiles) {
|
|
34
|
+
body.append(fileKey, appFiles[fileKey]);
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
delete headers['Content-Type'];
|
|
38
|
+
}
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
try {
|
|
28
|
-
const
|
|
42
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
29
43
|
|
|
30
44
|
// send request
|
|
31
|
-
const resp = await
|
|
45
|
+
const resp = await axiosRequest({
|
|
32
46
|
url: path,
|
|
33
47
|
method,
|
|
34
48
|
data: body,
|
|
@@ -60,3 +74,18 @@ export async function makeRequest<TResponse>({
|
|
|
60
74
|
});
|
|
61
75
|
}
|
|
62
76
|
}
|
|
77
|
+
function getAppFiles(body: any, fileSelectors: string[] = []) {
|
|
78
|
+
const files: Record<string, string> = {};
|
|
79
|
+
|
|
80
|
+
if (body) {
|
|
81
|
+
if (fileSelectors.length > 0) {
|
|
82
|
+
//
|
|
83
|
+
for (const fileKey of fileSelectors) {
|
|
84
|
+
files[fileKey] = body[fileKey];
|
|
85
|
+
delete body[fileKey];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return files;
|
|
91
|
+
}
|
|
@@ -9,8 +9,13 @@ export interface IMakeRequest {
|
|
|
9
9
|
method?: HttpMethod;
|
|
10
10
|
isFormData?: boolean;
|
|
11
11
|
headers: RawAxiosRequestHeaders;
|
|
12
|
+
appFileConfig?: AppFileConfig;
|
|
12
13
|
}
|
|
13
14
|
|
|
15
|
+
export interface AppFileConfig {
|
|
16
|
+
fileSelectors?: string[];
|
|
17
|
+
isApp: boolean;
|
|
18
|
+
}
|
|
14
19
|
export interface AxiosInstanceOption {
|
|
15
20
|
bearerToken?: string;
|
|
16
21
|
contentType?: string;
|
package/src/types/index.ts
CHANGED
|
@@ -5,9 +5,9 @@ export interface BootstrapQueryRequest {
|
|
|
5
5
|
appBaseUrl: string;
|
|
6
6
|
appTimeout: number;
|
|
7
7
|
};
|
|
8
|
-
context?:
|
|
8
|
+
context?: ContextType;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
11
11
|
export interface TanstackQueryConfig {
|
|
12
12
|
headers: RawAxiosRequestHeaders;
|
|
13
13
|
options?: BootstrapQueryRequest;
|