@ventlio/tanstack-query 0.2.6 → 0.2.7-2.1
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/LICENSE +21 -0
- package/README.md +532 -2
- package/dist/config/bootstrapQueryRequest.d.ts +4 -0
- package/dist/config/bootstrapQueryRequest.js +19 -0
- package/dist/config/bootstrapQueryRequest.js.map +1 -0
- package/dist/config/config.interface.d.ts +4 -0
- package/dist/config/index.d.ts +4 -2
- package/dist/config/useEnvironmentVariables.d.ts +2 -0
- package/dist/config/useEnvironmentVariables.js +14 -0
- package/dist/config/useEnvironmentVariables.js.map +1 -0
- package/dist/config/useQueryConfig.js +5 -9
- package/dist/config/useQueryConfig.js.map +1 -1
- package/dist/config/useQueryHeaders.js +19 -10
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/config/useReactNativeEnv.d.ts +5 -0
- package/dist/config/useReactNativeEnv.js +13 -0
- package/dist/config/useReactNativeEnv.js.map +1 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/scrollToTop.js +1 -3
- package/dist/helpers/scrollToTop.js.map +1 -1
- package/dist/helpers/timeFuncs.d.ts +1 -0
- package/dist/helpers/timeFuncs.js +11 -0
- package/dist/helpers/timeFuncs.js.map +1 -0
- package/dist/index.js +19 -43
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +417 -143
- package/dist/index.mjs.map +1 -1
- package/dist/model/index.d.ts +3 -0
- package/dist/model/model.interface.d.ts +11 -0
- package/dist/model/useKeyTrackerModel.d.ts +4 -0
- package/dist/model/useKeyTrackerModel.js +20 -0
- package/dist/model/useKeyTrackerModel.js.map +1 -0
- package/dist/model/useQueryModel.d.ts +2 -2
- package/dist/model/useQueryModel.js +119 -7
- package/dist/model/useQueryModel.js.map +1 -1
- package/dist/model/useRefetchQuery.d.ts +3 -0
- package/dist/model/useRefetchQuery.js +16 -0
- package/dist/model/useRefetchQuery.js.map +1 -0
- package/dist/queries/queries.interface.d.ts +5 -0
- package/dist/queries/useDeleteRequest.d.ts +14 -5
- package/dist/queries/useDeleteRequest.js +37 -37
- package/dist/queries/useGetRequest.d.ts +4 -3
- package/dist/queries/useGetRequest.js +53 -37
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +7 -7
- package/dist/queries/usePatchRequest.js +42 -34
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +8 -6
- package/dist/queries/usePostRequest.js +47 -37
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/request/axios-instance.d.ts +1 -1
- package/dist/request/axios-instance.js +3 -5
- package/dist/request/axios-instance.js.map +1 -1
- package/dist/request/buildFormData.d.ts +1 -1
- package/dist/request/buildFormData.js +36 -6
- package/dist/request/buildFormData.js.map +1 -1
- package/dist/request/make-request.d.ts +1 -1
- package/dist/request/make-request.js +55 -17
- package/dist/request/make-request.js.map +1 -1
- package/dist/request/request.enum.js +6 -6
- package/dist/request/request.interface.d.ts +5 -0
- package/dist/request/transformer.js +1 -4
- package/dist/request/transformer.js.map +1 -1
- package/dist/types/index.d.ts +14 -11
- package/package.json +29 -5
- package/src/__tests__/queries/usePostRequest.spec.ts +77 -0
- package/src/config/bootstrapQueryRequest.ts +19 -0
- package/src/config/config.interface.ts +4 -0
- package/src/config/index.ts +4 -2
- package/src/config/useEnvironmentVariables.ts +13 -0
- package/src/config/useQueryConfig.ts +2 -5
- package/src/config/useQueryHeaders.ts +23 -6
- package/src/config/useReactNativeEnv.ts +13 -0
- package/src/env.d.ts +4 -0
- package/src/helpers/index.ts +1 -0
- package/src/helpers/timeFuncs.ts +10 -0
- package/src/model/index.ts +3 -0
- package/src/model/model.interface.ts +12 -0
- package/src/model/useKeyTrackerModel.ts +22 -0
- package/src/model/useQueryModel.ts +139 -6
- package/src/model/useRefetchQuery.ts +19 -0
- package/src/queries/queries.interface.ts +6 -0
- package/src/queries/useDeleteRequest.ts +34 -30
- package/src/queries/useGetRequest.ts +55 -38
- package/src/queries/usePatchRequest.ts +45 -40
- package/src/queries/usePostRequest.ts +54 -37
- package/src/request/axios-instance.ts +1 -5
- package/src/request/buildFormData.ts +34 -4
- package/src/request/make-request.ts +47 -7
- package/src/request/request.interface.ts +5 -0
- package/src/request/transformer.ts +3 -12
- package/src/types/index.ts +16 -13
- package/dist/config/useQueryBaseURL.d.ts +0 -2
- package/dist/config/useQueryBaseURL.js +0 -19
- package/dist/config/useQueryBaseURL.js.map +0 -1
- package/dist/config/useQueryTimeout.d.ts +0 -2
- package/dist/config/useQueryTimeout.js +0 -19
- package/dist/config/useQueryTimeout.js.map +0 -1
- package/src/config/useQueryBaseURL.ts +0 -17
- package/src/config/useQueryTimeout.ts +0 -17
|
@@ -1,44 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
import { useQueryClient, useMutation } from '@tanstack/react-query';
|
|
2
|
+
import 'url-search-params-polyfill';
|
|
3
|
+
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
4
|
+
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
|
+
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
6
|
+
import 'axios';
|
|
7
|
+
import { makeRequest } from '../request/make-request.js';
|
|
8
|
+
import { HttpMethod } from '../request/request.enum.js';
|
|
2
9
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
// register post mutation
|
|
13
|
-
const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
|
|
14
|
-
makeRequest.makeRequest({
|
|
10
|
+
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
11
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
12
|
+
const { getHeaders } = useQueryHeaders();
|
|
13
|
+
const queryClient = useQueryClient();
|
|
14
|
+
const config = queryClient.getQueryData(['config']);
|
|
15
|
+
const sendRequest = async (res, rej, data) => {
|
|
16
|
+
// get request headers
|
|
17
|
+
const globalHeaders = getHeaders();
|
|
18
|
+
const patchResponse = await makeRequest({
|
|
15
19
|
path: path,
|
|
16
|
-
body:
|
|
17
|
-
method:
|
|
18
|
-
headers,
|
|
19
|
-
baseURL,
|
|
20
|
-
timeout,
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
body: data,
|
|
21
|
+
method: HttpMethod.PATCH,
|
|
22
|
+
headers: { ...globalHeaders, ...headers },
|
|
23
|
+
baseURL: baseUrl ?? API_URL,
|
|
24
|
+
timeout: TIMEOUT,
|
|
25
|
+
});
|
|
26
|
+
if (patchResponse.status) {
|
|
27
|
+
// scroll to top after success
|
|
28
|
+
if (config?.options?.context !== 'app') {
|
|
29
|
+
scrollToTop();
|
|
26
30
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
rej(postResponse);
|
|
31
|
+
res(patchResponse);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// scroll to top after error
|
|
35
|
+
if (config?.options?.context !== 'app') {
|
|
36
|
+
scrollToTop();
|
|
34
37
|
}
|
|
35
|
-
|
|
38
|
+
rej(patchResponse);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// register post mutation
|
|
42
|
+
const mutation = useMutation((dataData) => new Promise((res, rej) => {
|
|
43
|
+
return sendRequest(res, rej, dataData);
|
|
36
44
|
}));
|
|
37
|
-
const patch = async (
|
|
38
|
-
return mutation.mutateAsync(
|
|
45
|
+
const patch = async (data, options) => {
|
|
46
|
+
return mutation.mutateAsync(data, options);
|
|
39
47
|
};
|
|
40
48
|
return { patch, ...mutation };
|
|
41
49
|
};
|
|
42
50
|
|
|
43
|
-
|
|
51
|
+
export { usePatchRequest };
|
|
44
52
|
//# sourceMappingURL=usePatchRequest.js.map
|
|
@@ -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,11 @@
|
|
|
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, fileSelectors, }: {
|
|
4
5
|
path: string;
|
|
5
6
|
isFormData?: boolean | undefined;
|
|
6
|
-
|
|
7
|
+
fileSelectors?: string[] | undefined;
|
|
8
|
+
} & DefaultRequestOptions) => {
|
|
7
9
|
data: undefined;
|
|
8
10
|
error: null;
|
|
9
11
|
isError: false;
|
|
@@ -19,7 +21,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
19
21
|
isPaused: boolean;
|
|
20
22
|
variables: void | undefined;
|
|
21
23
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
22
|
-
post: (
|
|
24
|
+
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
23
25
|
} | {
|
|
24
26
|
data: undefined;
|
|
25
27
|
error: null;
|
|
@@ -36,7 +38,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
36
38
|
isPaused: boolean;
|
|
37
39
|
variables: void | undefined;
|
|
38
40
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
39
|
-
post: (
|
|
41
|
+
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
40
42
|
} | {
|
|
41
43
|
data: undefined;
|
|
42
44
|
error: IRequestError;
|
|
@@ -53,7 +55,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
53
55
|
isPaused: boolean;
|
|
54
56
|
variables: void | undefined;
|
|
55
57
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
56
|
-
post: (
|
|
58
|
+
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
57
59
|
} | {
|
|
58
60
|
data: IRequestSuccess<TResponse>;
|
|
59
61
|
error: null;
|
|
@@ -70,5 +72,5 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, }: {
|
|
|
70
72
|
isPaused: boolean;
|
|
71
73
|
variables: void | undefined;
|
|
72
74
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
73
|
-
post: (
|
|
75
|
+
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
74
76
|
};
|
|
@@ -1,47 +1,57 @@
|
|
|
1
|
-
|
|
1
|
+
import { useQueryClient, useMutation } from '@tanstack/react-query';
|
|
2
|
+
import 'url-search-params-polyfill';
|
|
3
|
+
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
4
|
+
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
|
+
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
6
|
+
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
7
|
+
import 'axios';
|
|
8
|
+
import { makeRequest } from '../request/make-request.js';
|
|
9
|
+
import { HttpMethod } from '../request/request.enum.js';
|
|
2
10
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
path: path,
|
|
11
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
12
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
13
|
+
const queryClient = useQueryClient();
|
|
14
|
+
const { getHeaders } = useQueryHeaders();
|
|
15
|
+
const { isApp } = useReactNativeEnv();
|
|
16
|
+
const sendRequest = async (res, rej, postData) => {
|
|
17
|
+
// get request headers
|
|
18
|
+
const globalHeaders = getHeaders();
|
|
19
|
+
const config = queryClient.getQueryData(['config']);
|
|
20
|
+
const postResponse = await makeRequest({
|
|
21
|
+
path,
|
|
15
22
|
body: postData,
|
|
16
|
-
method:
|
|
23
|
+
method: HttpMethod.POST,
|
|
17
24
|
isFormData,
|
|
18
|
-
headers,
|
|
19
|
-
baseURL,
|
|
20
|
-
timeout,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
headers: { ...globalHeaders, ...headers },
|
|
26
|
+
baseURL: baseUrl ?? API_URL,
|
|
27
|
+
timeout: TIMEOUT,
|
|
28
|
+
appFileConfig: {
|
|
29
|
+
isApp,
|
|
30
|
+
fileSelectors,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
if (postResponse.status) {
|
|
34
|
+
// scroll to top after success
|
|
35
|
+
if (config?.options?.context !== 'app') {
|
|
36
|
+
scrollToTop();
|
|
29
37
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
rej(postResponse);
|
|
38
|
+
res(postResponse);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// scroll to top after error
|
|
42
|
+
if (config?.options?.context !== 'app') {
|
|
43
|
+
scrollToTop();
|
|
37
44
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
rej(postResponse);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// register post mutation
|
|
49
|
+
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
|
|
50
|
+
const post = async (data, options) => {
|
|
51
|
+
return mutation.mutateAsync(data, options);
|
|
42
52
|
};
|
|
43
53
|
return { post, ...mutation };
|
|
44
54
|
};
|
|
45
55
|
|
|
46
|
-
|
|
56
|
+
export { usePostRequest };
|
|
47
57
|
//# sourceMappingURL=usePostRequest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { AxiosInstance } from 'axios';
|
|
2
2
|
import type { IMakeRequest } from './request.interface';
|
|
3
|
-
export declare const axiosInstance: ({ baseURL, timeout, headers
|
|
3
|
+
export declare const axiosInstance: ({ baseURL, timeout, headers }: Partial<IMakeRequest>) => AxiosInstance;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import axios from 'axios';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const axiosInstance = ({ baseURL, timeout, headers, }) => {
|
|
3
|
+
const axiosInstance = ({ baseURL, timeout, headers }) => {
|
|
6
4
|
return axios.create({
|
|
7
5
|
baseURL,
|
|
8
6
|
timeout,
|
|
@@ -10,5 +8,5 @@ const axiosInstance = ({ baseURL, timeout, headers, }) => {
|
|
|
10
8
|
});
|
|
11
9
|
};
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
export { axiosInstance };
|
|
14
12
|
//# sourceMappingURL=axios-instance.js.map
|
|
@@ -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 +1 @@
|
|
|
1
|
-
export declare const buildFormData: (body: Record<string, any>) =>
|
|
1
|
+
export declare const buildFormData: (body: Record<string, any>) => FormData;
|
|
@@ -1,14 +1,44 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const buildFormData = (body) => {
|
|
4
2
|
const formData = new FormData();
|
|
3
|
+
const handleArrayValue = (key, value) => {
|
|
4
|
+
for (const item of value) {
|
|
5
|
+
if (item instanceof File) {
|
|
6
|
+
formData.append(key, item);
|
|
7
|
+
}
|
|
8
|
+
else if (item instanceof Object) {
|
|
9
|
+
formData.append(key, JSON.stringify(item));
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
formData.append(key, item);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const handleObjectValue = (key, value) => {
|
|
17
|
+
if (value instanceof File) {
|
|
18
|
+
formData.append(key, value);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
formData.append(key, JSON.stringify(value));
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const handlePrimitiveValue = (key, value) => {
|
|
25
|
+
formData.append(key, value);
|
|
26
|
+
};
|
|
5
27
|
const bodyKeys = Object.keys(body);
|
|
6
28
|
bodyKeys.forEach((key) => {
|
|
7
|
-
|
|
29
|
+
const inputValue = body[key];
|
|
30
|
+
if (Array.isArray(inputValue) && inputValue.length > 0) {
|
|
31
|
+
handleArrayValue(key, inputValue);
|
|
32
|
+
}
|
|
33
|
+
else if (inputValue instanceof Object) {
|
|
34
|
+
handleObjectValue(key, inputValue);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
handlePrimitiveValue(key, inputValue);
|
|
38
|
+
}
|
|
8
39
|
});
|
|
9
|
-
|
|
10
|
-
return body;
|
|
40
|
+
return formData;
|
|
11
41
|
};
|
|
12
42
|
|
|
13
|
-
|
|
43
|
+
export { buildFormData };
|
|
14
44
|
//# sourceMappingURL=buildFormData.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildFormData.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildFormData.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>;
|
|
@@ -1,21 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { axiosInstance } from './axios-instance.js';
|
|
3
|
+
import { ContentType, HttpMethod } from './request.enum.js';
|
|
4
|
+
import { errorTransformer, successTransformer } from './transformer.js';
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
async function makeRequest({ body, method = request_enum.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) : {};
|
|
9
11
|
// configure body
|
|
10
|
-
body = isFormData ?
|
|
11
|
-
// configure request
|
|
12
|
+
body = (isFormData ? axios.toFormData(body) : body);
|
|
13
|
+
// configure request header1
|
|
12
14
|
if (!isFormData) {
|
|
13
|
-
headers['Content-Type'] =
|
|
15
|
+
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
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
|
+
}
|
|
14
36
|
}
|
|
15
37
|
try {
|
|
16
|
-
const
|
|
38
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
17
39
|
// send request
|
|
18
|
-
const resp = await
|
|
40
|
+
const resp = await axiosRequest({
|
|
19
41
|
url: path,
|
|
20
42
|
method,
|
|
21
43
|
data: body,
|
|
@@ -26,21 +48,37 @@ async function makeRequest({ body, method = request_enum.HttpMethod.GET, path, i
|
|
|
26
48
|
const responseCode = resp.status;
|
|
27
49
|
if (responseCode > 299) {
|
|
28
50
|
// server returned an error
|
|
29
|
-
return
|
|
51
|
+
return errorTransformer({ ...jsonResp, statusCode: responseCode });
|
|
30
52
|
}
|
|
31
|
-
return
|
|
32
|
-
data: jsonResp,
|
|
53
|
+
return successTransformer({
|
|
33
54
|
statusCode: responseCode,
|
|
55
|
+
...jsonResp,
|
|
56
|
+
status: resp.status,
|
|
34
57
|
});
|
|
35
58
|
}
|
|
36
59
|
catch (error) {
|
|
37
|
-
|
|
60
|
+
const errorData = error?.response?.data;
|
|
61
|
+
return errorTransformer({
|
|
38
62
|
statusCode: error.status,
|
|
39
63
|
message: error.message,
|
|
40
64
|
code: error.status || error.statusCode,
|
|
65
|
+
...errorData,
|
|
41
66
|
});
|
|
42
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;
|
|
43
81
|
}
|
|
44
82
|
|
|
45
|
-
|
|
83
|
+
export { makeRequest };
|
|
46
84
|
//# sourceMappingURL=make-request.js.map
|
|
@@ -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,16 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.HttpMethod = void 0;
|
|
1
|
+
var HttpMethod;
|
|
4
2
|
(function (HttpMethod) {
|
|
5
3
|
HttpMethod["GET"] = "GET";
|
|
6
4
|
HttpMethod["POST"] = "POST";
|
|
7
5
|
HttpMethod["PUT"] = "PUT";
|
|
8
6
|
HttpMethod["PATCH"] = "PATCH";
|
|
9
7
|
HttpMethod["DELETE"] = "DELETE";
|
|
10
|
-
})(
|
|
11
|
-
|
|
8
|
+
})(HttpMethod || (HttpMethod = {}));
|
|
9
|
+
var ContentType;
|
|
12
10
|
(function (ContentType) {
|
|
13
11
|
ContentType["APPLICATION_JSON"] = "application/json";
|
|
14
12
|
ContentType["MULTIPART_FORM_DATA"] = "multipart/form-data";
|
|
15
|
-
})(
|
|
13
|
+
})(ContentType || (ContentType = {}));
|
|
14
|
+
|
|
15
|
+
export { ContentType, HttpMethod };
|
|
16
16
|
//# sourceMappingURL=request.enum.js.map
|
|
@@ -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;
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
//
|
|
4
2
|
const errorTransformer = (data) => {
|
|
5
3
|
return {
|
|
@@ -21,6 +19,5 @@ const successTransformer = (data) => {
|
|
|
21
19
|
};
|
|
22
20
|
};
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
exports.successTransformer = successTransformer;
|
|
22
|
+
export { errorTransformer, successTransformer };
|
|
26
23
|
//# sourceMappingURL=transformer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transformer.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
2
|
+
export interface BootstrapConfig {
|
|
3
|
+
environments?: {
|
|
4
|
+
appBaseUrl: string;
|
|
5
|
+
appTimeout: number;
|
|
6
|
+
};
|
|
7
|
+
context?: ContextType;
|
|
8
|
+
modelConfig?: BootstrapModelConfig;
|
|
9
|
+
}
|
|
10
|
+
export interface BootstrapModelConfig {
|
|
11
|
+
idColumn: string;
|
|
12
|
+
}
|
|
13
|
+
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
2
14
|
export interface TanstackQueryConfig {
|
|
3
|
-
baseURL: string;
|
|
4
|
-
timeout?: number;
|
|
5
15
|
headers: RawAxiosRequestHeaders;
|
|
16
|
+
options?: BootstrapConfig;
|
|
6
17
|
}
|
|
7
18
|
export interface IUseQueryHeaders {
|
|
8
|
-
|
|
19
|
+
getHeaders: () => TanstackQueryConfig['headers'];
|
|
9
20
|
setQueryHeaders: (header: TanstackQueryConfig['headers']) => void;
|
|
10
21
|
}
|
|
11
|
-
export interface IUseQueryTimeout {
|
|
12
|
-
timeout: TanstackQueryConfig['timeout'];
|
|
13
|
-
setQueryTimeout: (timeout: TanstackQueryConfig['timeout']) => void;
|
|
14
|
-
}
|
|
15
|
-
export interface IUseQueryBaseURL {
|
|
16
|
-
baseURL: TanstackQueryConfig['baseURL'];
|
|
17
|
-
setQueryBaseUrl: (baseURL: TanstackQueryConfig['baseURL']) => void;
|
|
18
|
-
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ventlio/tanstack-query",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.72.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"contributors": [
|
|
7
|
+
{
|
|
8
|
+
"email": "soromgawide@gmail.com",
|
|
9
|
+
"name": "stancobridge",
|
|
10
|
+
"url": "https://github.com/stancobridge"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
6
13
|
"repository": {
|
|
7
14
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/ventlio-tech/
|
|
15
|
+
"url": "https://github.com/ventlio-tech/tanstack-query.git"
|
|
9
16
|
},
|
|
10
17
|
"types": "dist/index.d.ts",
|
|
11
18
|
"author": {
|
|
@@ -15,12 +22,14 @@
|
|
|
15
22
|
},
|
|
16
23
|
"scripts": {
|
|
17
24
|
"build": "rimraf dist && rollup --config rollup.config.ts --configPlugin rollup-plugin-typescript2",
|
|
18
|
-
"build:watch": "yarn build -w"
|
|
25
|
+
"build:watch": "yarn build -w",
|
|
26
|
+
"test": "jest"
|
|
19
27
|
},
|
|
20
28
|
"peerDependencies": {
|
|
21
29
|
"@tanstack/react-query": "^4.26.1",
|
|
22
30
|
"axios": "^1.3.4",
|
|
23
31
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
32
|
+
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
24
33
|
"react-native": "*"
|
|
25
34
|
},
|
|
26
35
|
"peerDependenciesMeta": {
|
|
@@ -39,12 +48,18 @@
|
|
|
39
48
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
40
49
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
41
50
|
"@tanstack/react-query": "^4.26.1",
|
|
51
|
+
"@testing-library/react-hooks": "^8.0.1",
|
|
42
52
|
"@types/axios": "^0.14.0",
|
|
53
|
+
"@types/jest": "^29.5.1",
|
|
54
|
+
"@types/lodash.result": "^4.5.7",
|
|
55
|
+
"@types/lodash.set": "^4.3.7",
|
|
43
56
|
"@types/node": "*",
|
|
44
|
-
"@types/react": "^18.0
|
|
57
|
+
"@types/react": "^18.2.0",
|
|
58
|
+
"@types/react-dom": "^18.2.0",
|
|
45
59
|
"@typescript-eslint/eslint-plugin": "^5.54.1",
|
|
46
60
|
"@typescript-eslint/parser": "^5.54.1",
|
|
47
61
|
"axios": "^1.3.4",
|
|
62
|
+
"axios-mock-adapter": "^1.21.4",
|
|
48
63
|
"eslint": "^8.36.0",
|
|
49
64
|
"eslint-config-google": "^0.14.0",
|
|
50
65
|
"eslint-config-prettier": "^8.7.0",
|
|
@@ -53,11 +68,15 @@
|
|
|
53
68
|
"eslint-plugin-prettier": "^4.2.1",
|
|
54
69
|
"eslint-plugin-react": "^7.32.2",
|
|
55
70
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
71
|
+
"jest": "^29.5.0",
|
|
72
|
+
"jest-environment-jsdom": "^29.5.0",
|
|
56
73
|
"prettier": "^2.8.4",
|
|
57
74
|
"react": "^18.2.0",
|
|
75
|
+
"react-dom": "^18.2.0",
|
|
58
76
|
"rimraf": "^4.4.0",
|
|
59
77
|
"rollup": "^3.19.1",
|
|
60
78
|
"rollup-plugin-typescript2": "^0.34.1",
|
|
79
|
+
"ts-jest": "^29.1.0",
|
|
61
80
|
"ts-node": "^10.9.1",
|
|
62
81
|
"tslib": "^2.5.0",
|
|
63
82
|
"typescript": "^4.9.5"
|
|
@@ -65,5 +84,10 @@
|
|
|
65
84
|
"files": [
|
|
66
85
|
"dist/**/*",
|
|
67
86
|
"src"
|
|
68
|
-
]
|
|
87
|
+
],
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"lodash.result": "^4.5.2",
|
|
90
|
+
"lodash.set": "^4.3.2",
|
|
91
|
+
"url-search-params-polyfill": "^8.2.5"
|
|
92
|
+
}
|
|
69
93
|
}
|