@ventlio/tanstack-query 0.5.6 → 0.5.8
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/_virtual/index.js +8 -0
- package/dist/{index.js.map → _virtual/index.js.map} +1 -1
- package/dist/_virtual/use-sync-external-store-shim.development.js +4 -0
- package/dist/_virtual/use-sync-external-store-shim.development.js.map +1 -0
- package/dist/_virtual/use-sync-external-store-shim.production.js +4 -0
- package/dist/_virtual/use-sync-external-store-shim.production.js.map +1 -0
- package/dist/_virtual/with-selector.development.js +4 -0
- package/dist/_virtual/with-selector.development.js.map +1 -0
- package/dist/_virtual/with-selector.js +8 -0
- package/dist/_virtual/with-selector.js.map +1 -0
- package/dist/_virtual/with-selector.production.js +4 -0
- package/dist/_virtual/with-selector.production.js.map +1 -0
- package/dist/config/bootStore.d.ts +3 -0
- package/dist/config/bootstrapQueryRequest.d.ts +1 -1
- package/dist/config/index.d.ts +0 -1
- package/dist/index.mjs +724 -49
- package/dist/index.mjs.map +1 -1
- package/dist/node_modules/@tanstack/react-store/dist/esm/index.js +48 -0
- package/dist/node_modules/@tanstack/react-store/dist/esm/index.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js +110 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.development.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.js +98 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim/with-selector.production.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js +107 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js +78 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/shim/index.js +22 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/shim/index.js.map +1 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/shim/with-selector.js +13 -0
- package/dist/node_modules/@tanstack/react-store/node_modules/use-sync-external-store/shim/with-selector.js.map +1 -0
- package/dist/node_modules/@tanstack/store/dist/esm/derived.js +119 -0
- package/dist/node_modules/@tanstack/store/dist/esm/derived.js.map +1 -0
- package/dist/node_modules/@tanstack/store/dist/esm/scheduler.js +88 -0
- package/dist/node_modules/@tanstack/store/dist/esm/scheduler.js.map +1 -0
- package/dist/node_modules/@tanstack/store/dist/esm/store.js +29 -0
- package/dist/node_modules/@tanstack/store/dist/esm/store.js.map +1 -0
- package/dist/src/config/bootStore.js +16 -0
- package/dist/src/config/bootStore.js.map +1 -0
- package/dist/src/config/bootstrapQueryRequest.js +11 -0
- package/dist/{config → src/config}/bootstrapQueryRequest.js.map +1 -1
- package/dist/src/config/useReactNativeEnv.js +13 -0
- package/dist/{config → src/config}/useReactNativeEnv.js.map +1 -1
- package/dist/{index.js → src/index.js} +0 -1
- package/dist/src/index.js.map +1 -0
- package/dist/{model → src/model}/useQueryModel.js +4 -7
- package/dist/{model → src/model}/useQueryModel.js.map +1 -1
- package/dist/{queries → src/queries}/useDeleteRequest.js +5 -4
- package/dist/{queries → src/queries}/useDeleteRequest.js.map +1 -1
- package/dist/{queries → src/queries}/useGetInfiniteRequest.js +5 -4
- package/dist/{queries → src/queries}/useGetInfiniteRequest.js.map +1 -1
- package/dist/{queries → src/queries}/useGetRequest.js +5 -4
- package/dist/{queries → src/queries}/useGetRequest.js.map +1 -1
- package/dist/{queries → src/queries}/usePatchRequest.js +7 -6
- package/dist/{queries → src/queries}/usePatchRequest.js.map +1 -1
- package/dist/{queries → src/queries}/usePostRequest.js +7 -6
- package/dist/{queries → src/queries}/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +14 -3
- package/package.json +2 -1
- package/src/config/bootStore.ts +14 -0
- package/src/config/bootstrapQueryRequest.ts +4 -15
- package/src/config/index.ts +0 -1
- package/src/config/useReactNativeEnv.ts +6 -5
- package/src/model/useQueryModel.ts +4 -4
- package/src/queries/useDeleteRequest.ts +16 -7
- package/src/queries/useGetInfiniteRequest.ts +16 -7
- package/src/queries/useGetRequest.ts +16 -7
- package/src/queries/usePatchRequest.ts +9 -8
- package/src/queries/usePostRequest.ts +15 -10
- package/src/queries/usePutRequest.ts +15 -10
- package/src/types/index.ts +15 -3
- package/dist/config/bootstrapQueryRequest.js +0 -21
- package/dist/config/useQueryConfig.d.ts +0 -2
- package/dist/config/useQueryConfig.js +0 -11
- package/dist/config/useQueryConfig.js.map +0 -1
- package/dist/config/useReactNativeEnv.js +0 -12
- package/src/config/useQueryConfig.ts +0 -11
- /package/dist/{config → src/config}/useEnvironmentVariables.js +0 -0
- /package/dist/{config → src/config}/useEnvironmentVariables.js.map +0 -0
- /package/dist/{config → src/config}/useQueryHeaders.js +0 -0
- /package/dist/{config → src/config}/useQueryHeaders.js.map +0 -0
- /package/dist/{helpers → src/helpers}/scrollToTop.js +0 -0
- /package/dist/{helpers → src/helpers}/scrollToTop.js.map +0 -0
- /package/dist/{helpers → src/helpers}/timeFuncs.js +0 -0
- /package/dist/{helpers → src/helpers}/timeFuncs.js.map +0 -0
- /package/dist/{hooks → src/hooks}/useUploadProgress.js +0 -0
- /package/dist/{hooks → src/hooks}/useUploadProgress.js.map +0 -0
- /package/dist/{model → src/model}/useKeyTrackerModel.js +0 -0
- /package/dist/{model → src/model}/useKeyTrackerModel.js.map +0 -0
- /package/dist/{model → src/model}/useRefetchQuery.js +0 -0
- /package/dist/{model → src/model}/useRefetchQuery.js.map +0 -0
- /package/dist/{request → src/request}/axios-instance.js +0 -0
- /package/dist/{request → src/request}/axios-instance.js.map +0 -0
- /package/dist/{request → src/request}/buildFormData.js +0 -0
- /package/dist/{request → src/request}/buildFormData.js.map +0 -0
- /package/dist/{request → src/request}/make-request.js +0 -0
- /package/dist/{request → src/request}/make-request.js.map +0 -0
- /package/dist/{request → src/request}/request.enum.js +0 -0
- /package/dist/{request → src/request}/request.enum.js.map +0 -0
- /package/dist/{request → src/request}/transformer.js +0 -0
- /package/dist/{request → src/request}/transformer.js.map +0 -0
- /package/dist/{stores → src/stores}/useHeaderStore.js +0 -0
- /package/dist/{stores → src/stores}/useHeaderStore.js.map +0 -0
- /package/dist/{stores → src/stores}/usePauseFutureRequests.js +0 -0
- /package/dist/{stores → src/stores}/usePauseFutureRequests.js.map +0 -0
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
2
|
-
import type { IRequestError, IRequestSuccess } from '../request';
|
|
1
|
+
import type { AxiosProgressEvent, RawAxiosRequestHeaders } from 'axios';
|
|
2
|
+
import type { AppFileConfig, HttpMethod, IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
3
3
|
export interface BootstrapConfig {
|
|
4
4
|
environments?: {
|
|
5
5
|
appBaseUrl: string;
|
|
@@ -7,12 +7,23 @@ export interface BootstrapConfig {
|
|
|
7
7
|
};
|
|
8
8
|
context?: ContextType;
|
|
9
9
|
modelConfig?: BootstrapModelConfig;
|
|
10
|
-
middleware?: <T = any>(next: () => Promise<IRequestSuccess<T> | IRequestError>, configs?: {
|
|
10
|
+
middleware?: <T = any>(next: (options?: Partial<NextOptions>) => Promise<IRequestSuccess<T> | IRequestError>, configs?: {
|
|
11
11
|
baseUrl: string;
|
|
12
12
|
path: string;
|
|
13
13
|
body?: Record<string, any>;
|
|
14
14
|
}) => Promise<IRequestError | IRequestSuccess<T>>;
|
|
15
15
|
}
|
|
16
|
+
export interface NextOptions extends Partial<IMakeRequest> {
|
|
17
|
+
baseURL: string;
|
|
18
|
+
timeout: number;
|
|
19
|
+
path: string;
|
|
20
|
+
body: any;
|
|
21
|
+
method: HttpMethod;
|
|
22
|
+
isFormData: boolean;
|
|
23
|
+
headers: RawAxiosRequestHeaders;
|
|
24
|
+
appFileConfig: AppFileConfig;
|
|
25
|
+
onUploadProgress: (progressEvent: AxiosProgressEvent) => void;
|
|
26
|
+
}
|
|
16
27
|
export interface BootstrapModelConfig {
|
|
17
28
|
idColumn: string;
|
|
18
29
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ventlio/tanstack-query",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"contributors": [
|
|
@@ -87,6 +87,7 @@
|
|
|
87
87
|
"src"
|
|
88
88
|
],
|
|
89
89
|
"dependencies": {
|
|
90
|
+
"@tanstack/react-store": "^0.7.0",
|
|
90
91
|
"lodash.result": "^4.5.2",
|
|
91
92
|
"lodash.set": "^4.3.2",
|
|
92
93
|
"url-search-params-polyfill": "^8.2.5",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Store } from '@tanstack/react-store';
|
|
2
|
+
import { BootstrapConfig } from '../types';
|
|
3
|
+
|
|
4
|
+
export const bootStore = new Store<BootstrapConfig>({
|
|
5
|
+
context: 'web',
|
|
6
|
+
environments: {
|
|
7
|
+
appBaseUrl: undefined as any,
|
|
8
|
+
appTimeout: 30000,
|
|
9
|
+
},
|
|
10
|
+
middleware: (next) => {
|
|
11
|
+
return next();
|
|
12
|
+
},
|
|
13
|
+
modelConfig: undefined,
|
|
14
|
+
});
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
import type { QueryClient } from '@tanstack/react-query';
|
|
2
2
|
import 'url-search-params-polyfill';
|
|
3
3
|
import type { BootstrapConfig } from '../types';
|
|
4
|
+
import { bootStore } from './bootStore';
|
|
4
5
|
|
|
5
|
-
export const bootstrapQueryRequest = (queryClient: QueryClient, options
|
|
6
|
-
// make query config doesn't expire
|
|
7
|
-
|
|
6
|
+
export const bootstrapQueryRequest = async (queryClient: QueryClient, options: BootstrapConfig): Promise<void> => {
|
|
8
7
|
// set default query config
|
|
9
|
-
|
|
10
|
-
headers: {
|
|
11
|
-
Authorization: ``,
|
|
12
|
-
},
|
|
13
|
-
options,
|
|
14
|
-
};
|
|
8
|
+
await queryClient.resumePausedMutations();
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
queries: {
|
|
18
|
-
meta: defaultMeta,
|
|
19
|
-
},
|
|
20
|
-
mutations: { meta: defaultMeta },
|
|
21
|
-
});
|
|
10
|
+
bootStore.setState(() => options);
|
|
22
11
|
};
|
package/src/config/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useStore } from '@tanstack/react-store';
|
|
2
|
+
import { bootStore } from './bootStore';
|
|
2
3
|
|
|
3
4
|
export const useReactNativeEnv = () => {
|
|
4
|
-
const
|
|
5
|
+
const { environments, context } = useStore(bootStore);
|
|
5
6
|
|
|
6
|
-
const appUrl: string | undefined =
|
|
7
|
-
const appTimeout: number | undefined =
|
|
8
|
-
const isApp =
|
|
7
|
+
const appUrl: string | undefined = environments?.appBaseUrl;
|
|
8
|
+
const appTimeout: number | undefined = environments?.appTimeout;
|
|
9
|
+
const isApp = context === 'app';
|
|
9
10
|
|
|
10
11
|
return { appUrl, appTimeout, isApp };
|
|
11
12
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useStore } from '@tanstack/react-store';
|
|
2
3
|
import result from 'lodash.result';
|
|
3
4
|
import { default as lodashSet } from 'lodash.set';
|
|
4
|
-
import {
|
|
5
|
+
import { bootStore } from '../config/bootStore';
|
|
5
6
|
import type { QueryModelAddPosition, QueryModelBuilder } from './model.interface';
|
|
6
7
|
import { useKeyTrackerModel } from './useKeyTrackerModel';
|
|
7
8
|
|
|
@@ -9,7 +10,7 @@ export const useQueryModel = <T>(keyTracker: string): QueryModelBuilder<T> => {
|
|
|
9
10
|
const queryClient = useQueryClient();
|
|
10
11
|
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
11
12
|
const queryKey = getQueryKey() as any[];
|
|
12
|
-
const config =
|
|
13
|
+
const config = useStore(bootStore);
|
|
13
14
|
|
|
14
15
|
const add = (data: T, position?: QueryModelAddPosition, path?: string): T | undefined => {
|
|
15
16
|
let records = (findAll(path) ?? []) as T[];
|
|
@@ -79,8 +80,7 @@ export const useQueryModel = <T>(keyTracker: string): QueryModelBuilder<T> => {
|
|
|
79
80
|
};
|
|
80
81
|
|
|
81
82
|
const getModelConfig = () => {
|
|
82
|
-
const {
|
|
83
|
-
const { modelConfig } = options ?? {};
|
|
83
|
+
const { modelConfig } = config;
|
|
84
84
|
|
|
85
85
|
return modelConfig;
|
|
86
86
|
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useQuery } from '@tanstack/react-query';
|
|
3
|
+
import { useStore } from '@tanstack/react-store';
|
|
3
4
|
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useEnvironmentVariables
|
|
5
|
+
import { useEnvironmentVariables } from '../config';
|
|
6
|
+
import { bootStore } from '../config/bootStore';
|
|
5
7
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
6
8
|
import { HttpMethod, makeRequest } from '../request';
|
|
7
9
|
import { useHeaderStore, usePauseFutureRequests } from '../stores';
|
|
@@ -12,7 +14,8 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
12
14
|
const [requestPath, setRequestPath] = useState<string>('');
|
|
13
15
|
const [options, setOptions] = useState<any>();
|
|
14
16
|
|
|
15
|
-
const {
|
|
17
|
+
const { middleware } = useStore(bootStore);
|
|
18
|
+
|
|
16
19
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
17
20
|
|
|
18
21
|
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
@@ -34,12 +37,18 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
34
37
|
};
|
|
35
38
|
|
|
36
39
|
let deleteResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
37
|
-
if (
|
|
40
|
+
if (middleware) {
|
|
38
41
|
// perform global middleware
|
|
39
|
-
deleteResponse = await
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
deleteResponse = await middleware(
|
|
43
|
+
async (middlewareOptions) =>
|
|
44
|
+
await makeRequest<TResponse>(
|
|
45
|
+
middlewareOptions ? { ...requestOptions, ...middlewareOptions } : requestOptions
|
|
46
|
+
),
|
|
47
|
+
{
|
|
48
|
+
path: requestUrl,
|
|
49
|
+
baseUrl: baseUrl ?? API_URL,
|
|
50
|
+
}
|
|
51
|
+
);
|
|
43
52
|
} else {
|
|
44
53
|
deleteResponse = await makeRequest<TResponse>(requestOptions);
|
|
45
54
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { InfiniteData, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
4
|
-
import { useEnvironmentVariables
|
|
4
|
+
import { useEnvironmentVariables } from '../config';
|
|
5
5
|
|
|
6
|
+
import { useStore } from '@tanstack/react-store';
|
|
7
|
+
import { bootStore } from '../config/bootStore';
|
|
6
8
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
7
9
|
import { makeRequest } from '../request';
|
|
8
10
|
import { useHeaderStore, usePauseFutureRequests } from '../stores';
|
|
@@ -35,7 +37,8 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
35
37
|
const [requestPath, setRequestPath] = useState<string>(path);
|
|
36
38
|
|
|
37
39
|
const [options, setOptions] = useState<any>(queryOptions);
|
|
38
|
-
const {
|
|
40
|
+
const { middleware } = useStore(bootStore);
|
|
41
|
+
|
|
39
42
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
40
43
|
|
|
41
44
|
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
@@ -66,12 +69,18 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
66
69
|
};
|
|
67
70
|
|
|
68
71
|
let getResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
69
|
-
if (
|
|
72
|
+
if (middleware) {
|
|
70
73
|
// perform global middleware
|
|
71
|
-
getResponse = await
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
getResponse = await middleware(
|
|
75
|
+
async (middlewareOptions) =>
|
|
76
|
+
await makeRequest<TResponse>(
|
|
77
|
+
middlewareOptions ? { ...requestOptions, ...middlewareOptions } : requestOptions
|
|
78
|
+
),
|
|
79
|
+
{
|
|
80
|
+
path,
|
|
81
|
+
baseUrl: baseUrl ?? API_URL,
|
|
82
|
+
}
|
|
83
|
+
);
|
|
75
84
|
} else {
|
|
76
85
|
getResponse = await makeRequest<TResponse>(requestOptions);
|
|
77
86
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { QueryKey, useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
3
|
-
import { useEnvironmentVariables
|
|
3
|
+
import { useEnvironmentVariables } from '../config';
|
|
4
4
|
|
|
5
|
+
import { useStore } from '@tanstack/react-store';
|
|
6
|
+
import { bootStore } from '../config/bootStore';
|
|
5
7
|
import { IRequestError, IRequestSuccess, makeRequest } from '../request';
|
|
6
8
|
import { useHeaderStore, usePauseFutureRequests } from '../stores';
|
|
7
9
|
import { DefaultRequestOptions, IPagination, TanstackQueryOption } from './queries.interface';
|
|
@@ -24,9 +26,10 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
24
26
|
const [page, setPage] = useState<number>(1);
|
|
25
27
|
|
|
26
28
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
29
|
+
const { middleware } = useStore(bootStore);
|
|
30
|
+
|
|
27
31
|
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
28
32
|
|
|
29
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
30
33
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
31
34
|
|
|
32
35
|
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
@@ -54,12 +57,18 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
54
57
|
};
|
|
55
58
|
|
|
56
59
|
let getResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
57
|
-
if (
|
|
60
|
+
if (middleware) {
|
|
58
61
|
// perform global middleware
|
|
59
|
-
getResponse = await
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
getResponse = await middleware(
|
|
63
|
+
async (middlewareOptions) =>
|
|
64
|
+
await makeRequest<TResponse>(
|
|
65
|
+
middlewareOptions ? { ...requestOptions, ...middlewareOptions } : requestOptions
|
|
66
|
+
),
|
|
67
|
+
{
|
|
68
|
+
path,
|
|
69
|
+
baseUrl: baseUrl ?? API_URL,
|
|
70
|
+
}
|
|
71
|
+
);
|
|
63
72
|
} else {
|
|
64
73
|
getResponse = await makeRequest<TResponse>(requestOptions);
|
|
65
74
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
+
import { useStore } from '@tanstack/react-store';
|
|
3
4
|
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useEnvironmentVariables
|
|
5
|
+
import { useEnvironmentVariables } from '../config';
|
|
6
|
+
import { bootStore } from '../config/bootStore';
|
|
5
7
|
import { scrollToTop } from '../helpers';
|
|
6
8
|
import { useUploadProgress } from '../hooks';
|
|
7
9
|
import { HttpMethod, makeRequest } from '../request';
|
|
@@ -17,8 +19,7 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
17
19
|
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
18
20
|
|
|
19
21
|
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
20
|
-
|
|
21
|
-
const config = useQueryConfig();
|
|
22
|
+
const { middleware, context } = useStore(bootStore);
|
|
22
23
|
|
|
23
24
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
24
25
|
// get request headers
|
|
@@ -34,10 +35,10 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
let patchResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
37
|
-
if (
|
|
38
|
+
if (middleware) {
|
|
38
39
|
// perform global middleware
|
|
39
|
-
const middlewareResponse = await
|
|
40
|
-
async () => await makeRequest<TResponse>(requestOptions),
|
|
40
|
+
const middlewareResponse = await middleware(
|
|
41
|
+
async (options) => await makeRequest<TResponse>(options ? { ...requestOptions, ...options } : requestOptions),
|
|
41
42
|
{
|
|
42
43
|
path,
|
|
43
44
|
baseUrl: baseUrl ?? API_URL,
|
|
@@ -51,13 +52,13 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
51
52
|
}
|
|
52
53
|
if (patchResponse.status) {
|
|
53
54
|
// scroll to top after success
|
|
54
|
-
if (
|
|
55
|
+
if (context !== 'app') {
|
|
55
56
|
scrollToTop();
|
|
56
57
|
}
|
|
57
58
|
res(patchResponse as IRequestSuccess<TResponse>);
|
|
58
59
|
} else {
|
|
59
60
|
// scroll to top after error
|
|
60
|
-
if (
|
|
61
|
+
if (context !== 'app') {
|
|
61
62
|
scrollToTop();
|
|
62
63
|
}
|
|
63
64
|
rej(patchResponse);
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
-
import { useEnvironmentVariables,
|
|
3
|
+
import { useEnvironmentVariables, useReactNativeEnv } from '../config';
|
|
4
4
|
|
|
5
|
+
import { useStore } from '@tanstack/react-store';
|
|
5
6
|
import { useEffect, useState } from 'react';
|
|
7
|
+
import { bootStore } from '../config/bootStore';
|
|
6
8
|
import { scrollToTop } from '../helpers';
|
|
7
9
|
import { useUploadProgress } from '../hooks';
|
|
8
10
|
import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
@@ -23,7 +25,7 @@ export const usePostRequest = <TResponse>({
|
|
|
23
25
|
} & DefaultRequestOptions) => {
|
|
24
26
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
25
27
|
|
|
26
|
-
const
|
|
28
|
+
const { middleware, context } = useStore(bootStore);
|
|
27
29
|
|
|
28
30
|
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
29
31
|
const { isApp } = useReactNativeEnv();
|
|
@@ -60,13 +62,16 @@ export const usePostRequest = <TResponse>({
|
|
|
60
62
|
};
|
|
61
63
|
|
|
62
64
|
let postResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
63
|
-
if (
|
|
65
|
+
if (middleware) {
|
|
64
66
|
// perform global middleware
|
|
65
|
-
postResponse = await
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
postResponse = await middleware(
|
|
68
|
+
async (options) => await makeRequest<TResponse>(options ? { ...requestOptions, ...options } : requestOptions),
|
|
69
|
+
{
|
|
70
|
+
path,
|
|
71
|
+
baseUrl: baseUrl ?? API_URL,
|
|
72
|
+
body: data,
|
|
73
|
+
}
|
|
74
|
+
);
|
|
70
75
|
} else {
|
|
71
76
|
postResponse = await makeRequest<TResponse>(requestOptions);
|
|
72
77
|
}
|
|
@@ -74,13 +79,13 @@ export const usePostRequest = <TResponse>({
|
|
|
74
79
|
if (postResponse.status) {
|
|
75
80
|
// scroll to top after success
|
|
76
81
|
|
|
77
|
-
if (
|
|
82
|
+
if (context !== 'app') {
|
|
78
83
|
scrollToTop();
|
|
79
84
|
}
|
|
80
85
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
81
86
|
} else {
|
|
82
87
|
// scroll to top after error
|
|
83
|
-
if (
|
|
88
|
+
if (context !== 'app') {
|
|
84
89
|
scrollToTop();
|
|
85
90
|
}
|
|
86
91
|
rej(postResponse);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
+
import { useStore } from '@tanstack/react-store';
|
|
3
4
|
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useEnvironmentVariables
|
|
5
|
+
import { useEnvironmentVariables } from '../config';
|
|
6
|
+
import { bootStore } from '../config/bootStore';
|
|
5
7
|
import { scrollToTop } from '../helpers';
|
|
6
8
|
import { useUploadProgress } from '../hooks';
|
|
7
9
|
import { HttpMethod, makeRequest } from '../request';
|
|
@@ -18,7 +20,7 @@ export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: str
|
|
|
18
20
|
|
|
19
21
|
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
20
22
|
|
|
21
|
-
const
|
|
23
|
+
const { middleware, context } = useStore(bootStore);
|
|
22
24
|
|
|
23
25
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
24
26
|
// get request headers
|
|
@@ -34,25 +36,28 @@ export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: str
|
|
|
34
36
|
};
|
|
35
37
|
|
|
36
38
|
let putResponse: IRequestError | IRequestSuccess<TResponse>;
|
|
37
|
-
if (
|
|
39
|
+
if (middleware) {
|
|
38
40
|
// perform global middleware
|
|
39
|
-
putResponse = await
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
putResponse = await middleware(
|
|
42
|
+
async (options) => await makeRequest<TResponse>(options ? { ...requestOptions, ...options } : requestOptions),
|
|
43
|
+
{
|
|
44
|
+
path,
|
|
45
|
+
baseUrl: baseUrl ?? API_URL,
|
|
46
|
+
body: data,
|
|
47
|
+
}
|
|
48
|
+
);
|
|
44
49
|
} else {
|
|
45
50
|
putResponse = await makeRequest<TResponse>(requestOptions);
|
|
46
51
|
}
|
|
47
52
|
if (putResponse.status) {
|
|
48
53
|
// scroll to top after success
|
|
49
|
-
if (
|
|
54
|
+
if (context !== 'app') {
|
|
50
55
|
scrollToTop();
|
|
51
56
|
}
|
|
52
57
|
res(putResponse as IRequestSuccess<TResponse>);
|
|
53
58
|
} else {
|
|
54
59
|
// scroll to top after error
|
|
55
|
-
if (
|
|
60
|
+
if (context !== 'app') {
|
|
56
61
|
scrollToTop();
|
|
57
62
|
}
|
|
58
63
|
rej(putResponse);
|
package/src/types/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
2
|
-
import type { IRequestError, IRequestSuccess } from '../request';
|
|
1
|
+
import type { AxiosProgressEvent, RawAxiosRequestHeaders } from 'axios';
|
|
2
|
+
import type { AppFileConfig, HttpMethod, IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
3
3
|
|
|
4
4
|
export interface BootstrapConfig {
|
|
5
5
|
environments?: {
|
|
@@ -9,11 +9,23 @@ export interface BootstrapConfig {
|
|
|
9
9
|
context?: ContextType;
|
|
10
10
|
modelConfig?: BootstrapModelConfig;
|
|
11
11
|
middleware?: <T = any>(
|
|
12
|
-
next: () => Promise<IRequestSuccess<T> | IRequestError>,
|
|
12
|
+
next: (options?: Partial<NextOptions>) => Promise<IRequestSuccess<T> | IRequestError>,
|
|
13
13
|
configs?: { baseUrl: string; path: string; body?: Record<string, any> }
|
|
14
14
|
) => Promise<IRequestError | IRequestSuccess<T>>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export interface NextOptions extends Partial<IMakeRequest> {
|
|
18
|
+
baseURL: string;
|
|
19
|
+
timeout: number;
|
|
20
|
+
path: string;
|
|
21
|
+
body: any;
|
|
22
|
+
method: HttpMethod;
|
|
23
|
+
isFormData: boolean;
|
|
24
|
+
headers: RawAxiosRequestHeaders;
|
|
25
|
+
appFileConfig: AppFileConfig;
|
|
26
|
+
onUploadProgress: (progressEvent: AxiosProgressEvent) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
export interface BootstrapModelConfig {
|
|
18
30
|
idColumn: string;
|
|
19
31
|
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import 'url-search-params-polyfill';
|
|
2
|
-
|
|
3
|
-
const bootstrapQueryRequest = (queryClient, options) => {
|
|
4
|
-
// make query config doesn't expire
|
|
5
|
-
// set default query config
|
|
6
|
-
const defaultMeta = {
|
|
7
|
-
headers: {
|
|
8
|
-
Authorization: ``,
|
|
9
|
-
},
|
|
10
|
-
options,
|
|
11
|
-
};
|
|
12
|
-
queryClient.setDefaultOptions({
|
|
13
|
-
queries: {
|
|
14
|
-
meta: defaultMeta,
|
|
15
|
-
},
|
|
16
|
-
mutations: { meta: defaultMeta },
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export { bootstrapQueryRequest };
|
|
21
|
-
//# sourceMappingURL=bootstrapQueryRequest.js.map
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
-
|
|
3
|
-
const useQueryConfig = () => {
|
|
4
|
-
const queryClient = useQueryClient();
|
|
5
|
-
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
|
|
6
|
-
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
|
|
7
|
-
return { ...queryMeta, ...mutationMeta };
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export { useQueryConfig };
|
|
11
|
-
//# sourceMappingURL=useQueryConfig.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useQueryConfig.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useQueryConfig } from './useQueryConfig.js';
|
|
2
|
-
|
|
3
|
-
const useReactNativeEnv = () => {
|
|
4
|
-
const config = useQueryConfig();
|
|
5
|
-
const appUrl = config.options?.environments?.appBaseUrl;
|
|
6
|
-
const appTimeout = config.options?.environments?.appTimeout;
|
|
7
|
-
const isApp = config.options?.context === 'app';
|
|
8
|
-
return { appUrl, appTimeout, isApp };
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { useReactNativeEnv };
|
|
12
|
-
//# sourceMappingURL=useReactNativeEnv.js.map
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
-
import type { TanstackQueryConfig } from '../types';
|
|
3
|
-
|
|
4
|
-
export const useQueryConfig = (): TanstackQueryConfig => {
|
|
5
|
-
const queryClient = useQueryClient();
|
|
6
|
-
|
|
7
|
-
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
8
|
-
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
9
|
-
|
|
10
|
-
return { ...queryMeta, ...mutationMeta };
|
|
11
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|