@ventlio/tanstack-query 0.2.63-beta.3 → 0.2.63-beta.5
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 +46 -7
- package/dist/index.mjs.map +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 +40 -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/usePostRequest.ts +8 -2
- package/src/request/make-request.ts +41 -5
- package/src/request/request.interface.ts +5 -0
- package/src/types/index.ts +2 -2
package/dist/index.mjs
CHANGED
|
@@ -199,20 +199,41 @@ 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
|
+
const currentFile = appFiles[fileKey];
|
|
219
|
+
if (Array.isArray(currentFile)) {
|
|
220
|
+
for (const innerFile of currentFile) {
|
|
221
|
+
body.append(fileKey, innerFile);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
body.append(fileKey, currentFile);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
delete headers['Content-Type'];
|
|
231
|
+
}
|
|
211
232
|
}
|
|
212
233
|
try {
|
|
213
|
-
const
|
|
234
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
214
235
|
// send request
|
|
215
|
-
const resp = await
|
|
236
|
+
const resp = await axiosRequest({
|
|
216
237
|
url: path,
|
|
217
238
|
method,
|
|
218
239
|
data: body,
|
|
@@ -240,6 +261,19 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
240
261
|
...errorData,
|
|
241
262
|
});
|
|
242
263
|
}
|
|
264
|
+
}
|
|
265
|
+
function getAppFiles(body, fileSelectors = []) {
|
|
266
|
+
const files = {};
|
|
267
|
+
if (body) {
|
|
268
|
+
if (fileSelectors.length > 0) {
|
|
269
|
+
//
|
|
270
|
+
for (const fileKey of fileSelectors) {
|
|
271
|
+
files[fileKey] = body[fileKey];
|
|
272
|
+
delete body[fileKey];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return files;
|
|
243
277
|
}
|
|
244
278
|
|
|
245
279
|
const useDeleteRequest = (deleteOptions) => {
|
|
@@ -420,10 +454,11 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
420
454
|
return { patch, ...mutation };
|
|
421
455
|
};
|
|
422
456
|
|
|
423
|
-
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
457
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
424
458
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
425
459
|
const queryClient = useQueryClient();
|
|
426
460
|
const { getHeaders } = useQueryHeaders();
|
|
461
|
+
const { isApp } = useReactNativeEnv();
|
|
427
462
|
const sendRequest = async (res, rej, postData) => {
|
|
428
463
|
// get request headers
|
|
429
464
|
const globalHeaders = getHeaders();
|
|
@@ -436,6 +471,10 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, }) => {
|
|
|
436
471
|
headers: { ...globalHeaders, ...headers },
|
|
437
472
|
baseURL: baseUrl ?? API_URL,
|
|
438
473
|
timeout: TIMEOUT,
|
|
474
|
+
appFileConfig: {
|
|
475
|
+
isApp,
|
|
476
|
+
fileSelectors,
|
|
477
|
+
},
|
|
439
478
|
});
|
|
440
479
|
if (postResponse.status) {
|
|
441
480
|
// 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -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,41 @@ 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
|
+
const currentFile = appFiles[fileKey];
|
|
23
|
+
if (Array.isArray(currentFile)) {
|
|
24
|
+
for (const innerFile of currentFile) {
|
|
25
|
+
body.append(fileKey, innerFile);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
body.append(fileKey, currentFile);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
delete headers['Content-Type'];
|
|
35
|
+
}
|
|
15
36
|
}
|
|
16
37
|
try {
|
|
17
|
-
const
|
|
38
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
18
39
|
// send request
|
|
19
|
-
const resp = await
|
|
40
|
+
const resp = await axiosRequest({
|
|
20
41
|
url: path,
|
|
21
42
|
method,
|
|
22
43
|
data: body,
|
|
@@ -44,6 +65,19 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
44
65
|
...errorData,
|
|
45
66
|
});
|
|
46
67
|
}
|
|
68
|
+
}
|
|
69
|
+
function getAppFiles(body, fileSelectors = []) {
|
|
70
|
+
const files = {};
|
|
71
|
+
if (body) {
|
|
72
|
+
if (fileSelectors.length > 0) {
|
|
73
|
+
//
|
|
74
|
+
for (const fileKey of fileSelectors) {
|
|
75
|
+
files[fileKey] = body[fileKey];
|
|
76
|
+
delete body[fileKey];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return files;
|
|
47
81
|
}
|
|
48
82
|
|
|
49
83
|
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
|
@@ -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,43 @@ 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
|
+
const currentFile = appFiles[fileKey];
|
|
35
|
+
if (Array.isArray(currentFile)) {
|
|
36
|
+
for (const innerFile of currentFile) {
|
|
37
|
+
body.append(fileKey, innerFile);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
body.append(fileKey, currentFile);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
delete headers['Content-Type'];
|
|
45
|
+
}
|
|
25
46
|
}
|
|
26
47
|
|
|
27
48
|
try {
|
|
28
|
-
const
|
|
49
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
29
50
|
|
|
30
51
|
// send request
|
|
31
|
-
const resp = await
|
|
52
|
+
const resp = await axiosRequest({
|
|
32
53
|
url: path,
|
|
33
54
|
method,
|
|
34
55
|
data: body,
|
|
@@ -60,3 +81,18 @@ export async function makeRequest<TResponse>({
|
|
|
60
81
|
});
|
|
61
82
|
}
|
|
62
83
|
}
|
|
84
|
+
function getAppFiles(body: any, fileSelectors: string[] = []) {
|
|
85
|
+
const files: Record<string, string> = {};
|
|
86
|
+
|
|
87
|
+
if (body) {
|
|
88
|
+
if (fileSelectors.length > 0) {
|
|
89
|
+
//
|
|
90
|
+
for (const fileKey of fileSelectors) {
|
|
91
|
+
files[fileKey] = body[fileKey];
|
|
92
|
+
delete body[fileKey];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return files;
|
|
98
|
+
}
|
|
@@ -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;
|