@intrig/react 1.0.10 → 1.0.11
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/package.json +1 -1
- package/src/extra/useAsNetworkState.ts +3 -3
- package/src/extra/useAsPromise.ts +2 -2
- package/src/extra/useResolvedCachedValue.ts +1 -1
- package/src/extra/useResolvedValue.ts +1 -1
- package/src/intrig-context.ts +3 -5
- package/src/intrig-provider.tsx +13 -13
- package/src/logger.ts +1 -1
- package/src/media-type-utils.ts +11 -11
package/package.json
CHANGED
|
@@ -13,10 +13,10 @@ import { useIntrigContext } from '@intrig/react/intrig-context';
|
|
|
13
13
|
* 2. A function to execute the provided asynchronous operation.
|
|
14
14
|
* 3. A function to reset the network state back to the initial state.
|
|
15
15
|
*/
|
|
16
|
-
export function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
|
|
17
|
-
|
|
16
|
+
export function useAsNetworkState<T, F extends ((...args: any) => Promise<T>)>(fn: F, key: string = 'default'): [NetworkState<T>, (...params: Parameters<F>) => void, () => void] {
|
|
17
|
+
let id = useId();
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
let context = useIntrigContext();
|
|
20
20
|
|
|
21
21
|
const networkState = useMemo(() => {
|
|
22
22
|
return context.state?.[`promiseState:${id}:${key}}`] ?? init()
|
|
@@ -82,7 +82,7 @@ export function useAsPromise<P, B, T, E>(
|
|
|
82
82
|
const resolveRef = useRef<(value: T) => void>();
|
|
83
83
|
const rejectRef = useRef<(reason?: any) => void>();
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
let [state, dispatch, clear] = hook(options as any); // Casting to `any` to match all overloads
|
|
86
86
|
|
|
87
87
|
useEffect(() => {
|
|
88
88
|
if (isSuccess(state)) {
|
|
@@ -99,7 +99,7 @@ export function useAsPromise<P, B, T, E>(
|
|
|
99
99
|
resolveRef.current = resolve;
|
|
100
100
|
rejectRef.current = reject;
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
let dispatchState = (dispatch as any)(...args);
|
|
103
103
|
if (isValidationError(dispatchState)) {
|
|
104
104
|
reject(dispatchState.error);
|
|
105
105
|
}
|
|
@@ -73,7 +73,7 @@ export function useResolvedCachedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B
|
|
|
73
73
|
export function useResolvedCachedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
|
|
74
74
|
const [cachedValue, setCachedValue] = useState<T | undefined>();
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
let [state] = hook(options as any); // Ensure compatibility with different hook types
|
|
77
77
|
|
|
78
78
|
useEffect(() => {
|
|
79
79
|
if (isSuccess(state)) {
|
|
@@ -68,7 +68,7 @@ export function useResolvedValue<P, B, T, E>(hook: BinaryFunctionHook<P, B, T, E
|
|
|
68
68
|
export function useResolvedValue<P, B, T, E>(hook: IntrigHook<P, B, T, E>, options: IntrigHookOptions<P, B>): T | undefined {
|
|
69
69
|
const [value, setValue] = useState<T | undefined>();
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
let [state] = hook(options as any); // Ensure compatibility with different hook types
|
|
72
72
|
|
|
73
73
|
useEffect(() => {
|
|
74
74
|
if (isSuccess(state)) {
|
package/src/intrig-context.ts
CHANGED
|
@@ -43,15 +43,13 @@ export interface ContextType {
|
|
|
43
43
|
*
|
|
44
44
|
* @type {ContextType}
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
let Context = createContext<ContextType>({
|
|
47
47
|
state: {},
|
|
48
48
|
filteredState: {},
|
|
49
|
-
dispatch() {
|
|
50
|
-
//noop
|
|
51
|
-
},
|
|
49
|
+
dispatch() {},
|
|
52
50
|
configs: {},
|
|
53
51
|
async execute() {
|
|
54
|
-
|
|
52
|
+
|
|
55
53
|
}
|
|
56
54
|
});
|
|
57
55
|
|
package/src/intrig-provider.tsx
CHANGED
|
@@ -83,11 +83,11 @@ export function IntrigProvider({
|
|
|
83
83
|
async function execute<T, E = unknown>(request: RequestType, dispatch: (state: NetworkState<T, E>) => void, schema: ZodSchema<T> | undefined, errorSchema: ZodSchema<E> | undefined) {
|
|
84
84
|
try {
|
|
85
85
|
dispatch(pending());
|
|
86
|
-
|
|
86
|
+
let response = await axiosInstances[request.source].request(request);
|
|
87
87
|
|
|
88
88
|
if (response.status >= 200 && response.status < 300) {
|
|
89
89
|
if (schema) {
|
|
90
|
-
|
|
90
|
+
let data = schema.safeParse(response.data);
|
|
91
91
|
if (!data.success) {
|
|
92
92
|
dispatch(
|
|
93
93
|
error(data.error.issues, response.status, request)
|
|
@@ -99,7 +99,7 @@ export function IntrigProvider({
|
|
|
99
99
|
dispatch(success(response.data));
|
|
100
100
|
}
|
|
101
101
|
} else {
|
|
102
|
-
|
|
102
|
+
let { data, error: validationError } = errorSchema?.safeParse(response.data ?? {}) ?? {};
|
|
103
103
|
//todo: handle error validation error.
|
|
104
104
|
dispatch(
|
|
105
105
|
error(data ?? response.data ?? response.statusText, response.status)
|
|
@@ -107,7 +107,7 @@ export function IntrigProvider({
|
|
|
107
107
|
}
|
|
108
108
|
} catch (e: any) {
|
|
109
109
|
if (isAxiosError(e)) {
|
|
110
|
-
|
|
110
|
+
let { data, error: validationError } = errorSchema?.safeParse(e.response?.data ?? {}) ?? {};
|
|
111
111
|
dispatch(error(data ?? e.response?.data, e.response?.status, request));
|
|
112
112
|
} else {
|
|
113
113
|
dispatch(error(e));
|
|
@@ -145,7 +145,7 @@ export function IntrigProviderStub({ children, configs = {}, stubs = () => {} }:
|
|
|
145
145
|
const [state, dispatch] = useReducer(requestReducer, {} as GlobalState);
|
|
146
146
|
|
|
147
147
|
const collectedStubs = useMemo(() => {
|
|
148
|
-
|
|
148
|
+
let fns: Record<string, (params: any, body: any, dispatch: (state: NetworkState<any>) => void) => Promise<void>> = {};
|
|
149
149
|
function stub<P, B, T>(hook: IntrigHook<P, B, T>, fn: (params: P, body: B, dispatch: (state: NetworkState<T>) => void) => Promise<void>) {
|
|
150
150
|
fns[hook.key] = fn;
|
|
151
151
|
}
|
|
@@ -156,9 +156,9 @@ export function IntrigProviderStub({ children, configs = {}, stubs = () => {} }:
|
|
|
156
156
|
const contextValue = useMemo(() => {
|
|
157
157
|
|
|
158
158
|
async function execute<T>(request: RequestType, dispatch: (state: NetworkState<T>) => void, schema: ZodSchema<T> | undefined) {
|
|
159
|
-
|
|
159
|
+
let stub = collectedStubs[request.key];
|
|
160
160
|
|
|
161
|
-
if (stub) {
|
|
161
|
+
if (!!stub) {
|
|
162
162
|
try {
|
|
163
163
|
await stub(request.params, request.data, dispatch);
|
|
164
164
|
} catch (e) {
|
|
@@ -329,10 +329,10 @@ export function useNetworkState<T, E = unknown>({
|
|
|
329
329
|
logger.info(`Executing request ${key} ${operation} ${source}`);
|
|
330
330
|
logger.debug(`=>`, request)
|
|
331
331
|
|
|
332
|
-
|
|
332
|
+
let abortController = new AbortController();
|
|
333
333
|
setAbortController(abortController);
|
|
334
334
|
|
|
335
|
-
|
|
335
|
+
let requestConfig: RequestType = {
|
|
336
336
|
...request,
|
|
337
337
|
onUploadProgress(event: AxiosProgressEvent) {
|
|
338
338
|
dispatch(
|
|
@@ -405,7 +405,7 @@ export function useCentralError() {
|
|
|
405
405
|
return Object.entries(ctx.filteredState)
|
|
406
406
|
.filter(([, state]) => isError(state))
|
|
407
407
|
.map(([k, state]) => {
|
|
408
|
-
|
|
408
|
+
let [source, operation, key] = k.split(':');
|
|
409
409
|
return {
|
|
410
410
|
...(state as ErrorState<unknown>),
|
|
411
411
|
source,
|
|
@@ -426,12 +426,12 @@ export function useCentralPendingState() {
|
|
|
426
426
|
const ctx = useContext(Context);
|
|
427
427
|
|
|
428
428
|
const result: NetworkState = useMemo(() => {
|
|
429
|
-
|
|
429
|
+
let pendingStates = Object.values(ctx.filteredState).filter(isPending);
|
|
430
430
|
if (!pendingStates.length) {
|
|
431
431
|
return init();
|
|
432
432
|
}
|
|
433
433
|
|
|
434
|
-
|
|
434
|
+
let progress = pendingStates
|
|
435
435
|
.filter((a) => a.progress)
|
|
436
436
|
.reduce(
|
|
437
437
|
(progress, current) => {
|
|
@@ -442,7 +442,7 @@ export function useCentralPendingState() {
|
|
|
442
442
|
},
|
|
443
443
|
{ total: 0, loaded: 0 } satisfies Progress
|
|
444
444
|
);
|
|
445
|
-
return pending(progress.total ? progress : undefined);
|
|
445
|
+
return pending(!!progress.total ? progress : undefined);
|
|
446
446
|
}, [ctx.filteredState]);
|
|
447
447
|
|
|
448
448
|
return result;
|
package/src/logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import log from 'loglevel';
|
|
2
2
|
|
|
3
3
|
// Set the default logging level (can be overridden via environment variables)
|
|
4
|
-
log.setLevel(process.env.LOG_LEVEL as log.LogLevelDesc || '
|
|
4
|
+
log.setLevel(process.env.LOG_LEVEL as log.LogLevelDesc || 'info');
|
|
5
5
|
|
|
6
6
|
const logWrapper = {
|
|
7
7
|
info: (msg: string, meta?: object) => meta ? log.info(msg, meta) : log.info(msg),
|
package/src/media-type-utils.ts
CHANGED
|
@@ -46,8 +46,8 @@ function appendFormData(
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
encoders['multipart/form-data'] = async (request, mediaType, schema) => {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
let _request = request as Record<string, any>;
|
|
50
|
+
let formData = new FormData();
|
|
51
51
|
Object.keys(_request).forEach((key: string) => {
|
|
52
52
|
appendFormData(formData, _request[key], key);
|
|
53
53
|
});
|
|
@@ -59,8 +59,8 @@ encoders['application/octet-stream'] = async (request, mediaType, schema) => {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
encoders['application/x-www-form-urlencoded'] = async (request, mediaType, schema) => {
|
|
62
|
-
|
|
63
|
-
for (
|
|
62
|
+
let formData = new FormData();
|
|
63
|
+
for (let key in request) {
|
|
64
64
|
const value = request[key];
|
|
65
65
|
formData.append(key, value instanceof Blob || typeof value === 'string' ? value : String(value));
|
|
66
66
|
}
|
|
@@ -93,8 +93,8 @@ transformers['application/json'] = async (request, mediaType, schema) => {
|
|
|
93
93
|
};
|
|
94
94
|
|
|
95
95
|
transformers['multipart/form-data'] = async (request, mediaType, schema) => {
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
let formData = await request.formData();
|
|
97
|
+
let content: Record<string, any> = {};
|
|
98
98
|
formData.forEach((value, key) => {
|
|
99
99
|
if (content[key]) {
|
|
100
100
|
if (!(content[key] instanceof Array)) {
|
|
@@ -125,15 +125,15 @@ transformers['application/x-www-form-urlencoded'] = async (
|
|
|
125
125
|
mediaType,
|
|
126
126
|
schema
|
|
127
127
|
) => {
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
let formData = await request.formData();
|
|
129
|
+
let content: Record<string, any> = {};
|
|
130
130
|
formData.forEach((value, key) => (content[key] = value));
|
|
131
131
|
return schema.parse(content);
|
|
132
132
|
};
|
|
133
133
|
|
|
134
134
|
transformers['application/xml'] = async (request, mediaType, schema) => {
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
let xmlParser = new XMLParser();
|
|
136
|
+
let content = await xmlParser.parse(await request.text());
|
|
137
137
|
return schema.parse(await content);
|
|
138
138
|
};
|
|
139
139
|
|
|
@@ -168,7 +168,7 @@ responseTransformers['application/json'] = async (data, mediaType, schema) => {
|
|
|
168
168
|
};
|
|
169
169
|
|
|
170
170
|
responseTransformers['application/xml'] = async (data, mediaType, schema) => {
|
|
171
|
-
|
|
171
|
+
let parsed = new XMLParser().parse(data);
|
|
172
172
|
return schema.parse(parsed);
|
|
173
173
|
}
|
|
174
174
|
|