api-core-lib 16.12.133 → 16.12.134
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/client.cjs +45 -61
- package/dist/client.js +39 -55
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -330,32 +330,29 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
330
330
|
defaultQueryOptions,
|
|
331
331
|
initialActionsToExecute
|
|
332
332
|
} = options;
|
|
333
|
-
const pathParamsString = _react.useMemo.call(void 0, () => JSON.stringify(globalPathParams || {}), [globalPathParams]);
|
|
334
333
|
const [queryOptions, setQueryOptions] = _react.useState.call(void 0, () => {
|
|
335
334
|
const initialOptions = {};
|
|
336
335
|
for (const actionName in moduleConfig.actions) {
|
|
337
336
|
const actionConfig = moduleConfig.actions[actionName];
|
|
338
|
-
if (actionConfig.
|
|
339
|
-
initialOptions[actionName] = { ...
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (defaultQueryOptions) {
|
|
343
|
-
for (const actionName in defaultQueryOptions) {
|
|
344
|
-
initialOptions[actionName] = {
|
|
345
|
-
...initialOptions[actionName],
|
|
346
|
-
...defaultQueryOptions[actionName]
|
|
347
|
-
};
|
|
337
|
+
if (actionConfig.hasQuery) {
|
|
338
|
+
initialOptions[actionName] = { ..._optionalChain([defaultQueryOptions, 'optionalAccess', _6 => _6[actionName]]) || {} };
|
|
348
339
|
}
|
|
349
340
|
}
|
|
350
341
|
return initialOptions;
|
|
351
342
|
});
|
|
352
|
-
const
|
|
353
|
-
const
|
|
354
|
-
const
|
|
355
|
-
const savedCallbacks = _react.useRef.call(void 0, { onSuccess, onError });
|
|
343
|
+
const queryOptionsRef = _react.useRef.call(void 0, queryOptions);
|
|
344
|
+
const pathParamsRef = _react.useRef.call(void 0, globalPathParams);
|
|
345
|
+
const savedCallbacksRef = _react.useRef.call(void 0, { onSuccess, onError });
|
|
356
346
|
_react.useEffect.call(void 0, () => {
|
|
357
|
-
|
|
347
|
+
queryOptionsRef.current = queryOptions;
|
|
348
|
+
}, [queryOptions]);
|
|
349
|
+
_react.useEffect.call(void 0, () => {
|
|
350
|
+
pathParamsRef.current = globalPathParams;
|
|
351
|
+
}, [globalPathParams]);
|
|
352
|
+
_react.useEffect.call(void 0, () => {
|
|
353
|
+
savedCallbacksRef.current = { onSuccess, onError };
|
|
358
354
|
}, [onSuccess, onError]);
|
|
355
|
+
const stableActionsRef = _react.useRef.call(void 0, {});
|
|
359
356
|
_react.useEffect.call(void 0, () => {
|
|
360
357
|
if (hydratedState) {
|
|
361
358
|
_chunkJAMEOM7Tcjs.globalStateManager.rehydrate(hydratedState);
|
|
@@ -386,7 +383,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
386
383
|
return newState;
|
|
387
384
|
});
|
|
388
385
|
}
|
|
389
|
-
}, [urlSync]);
|
|
386
|
+
}, [_optionalChain([urlSync, 'optionalAccess', _7 => _7.searchParams]), _optionalChain([urlSync, 'optionalAccess', _8 => _8.actionsToSync]), _optionalChain([urlSync, 'optionalAccess', _9 => _9.defaultSyncAction])]);
|
|
390
387
|
_react.useEffect.call(void 0, () => {
|
|
391
388
|
if (!urlSync) return;
|
|
392
389
|
const { searchParams, updateUrl, actionsToSync, defaultSyncAction } = urlSync;
|
|
@@ -411,19 +408,17 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
411
408
|
if (hasChanged) {
|
|
412
409
|
updateUrl(currentParams.toString());
|
|
413
410
|
}
|
|
414
|
-
}, [queryOptions, urlSync]);
|
|
411
|
+
}, [queryOptions, _optionalChain([urlSync, 'optionalAccess', _10 => _10.updateUrl]), _optionalChain([urlSync, 'optionalAccess', _11 => _11.searchParams]), _optionalChain([urlSync, 'optionalAccess', _12 => _12.actionsToSync]), _optionalChain([urlSync, 'optionalAccess', _13 => _13.defaultSyncAction])]);
|
|
415
412
|
_react.useMemo.call(void 0, () => {
|
|
416
413
|
for (const actionName in moduleConfig.actions) {
|
|
417
414
|
const actionConfig = moduleConfig.actions[actionName];
|
|
418
415
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
419
416
|
const execute = async (input, options2 = {}) => {
|
|
420
|
-
|
|
421
|
-
const
|
|
417
|
+
const finalPathParams = { ...pathParamsRef.current, ...options2.pathParams };
|
|
418
|
+
const currentQueryOptions = queryOptionsRef.current[actionName];
|
|
422
419
|
let bodyForCall = input;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const currentQueryOptions = queryOptions[actionName];
|
|
426
|
-
bodyForCall = { ...actionConfig._input, ...currentQueryOptions, ..._optionalChain([options2, 'access', _6 => _6.config, 'optionalAccess', _7 => _7.params]), ...input };
|
|
420
|
+
if (actionConfig.method === "GET" && actionConfig.hasQuery) {
|
|
421
|
+
bodyForCall = { ...currentQueryOptions, ..._optionalChain([options2, 'access', _14 => _14.config, 'optionalAccess', _15 => _15.params]), ...input };
|
|
427
422
|
} else if (actionConfig.upload && input) {
|
|
428
423
|
bodyForCall = new FormData();
|
|
429
424
|
for (const key in input) {
|
|
@@ -431,18 +426,14 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
431
426
|
}
|
|
432
427
|
}
|
|
433
428
|
const cacheKey = shouldCache ? _chunkJAMEOM7Tcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, bodyForCall, { pathParams: finalPathParams }) : "";
|
|
434
|
-
|
|
429
|
+
if (shouldCache) {
|
|
430
|
+
_chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
431
|
+
}
|
|
435
432
|
try {
|
|
436
|
-
if (options2.onMutate && shouldCache) {
|
|
437
|
-
mutationContext = await options2.onMutate(input);
|
|
438
|
-
}
|
|
439
|
-
if (shouldCache) {
|
|
440
|
-
_chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
441
|
-
}
|
|
442
433
|
const result = await _chunkJAMEOM7Tcjs.callDynamicApi.call(void 0, axiosInstance, moduleConfig.baseEndpoint, actionConfig, {
|
|
443
434
|
pathParams: finalPathParams,
|
|
444
435
|
body: bodyForCall,
|
|
445
|
-
config:
|
|
436
|
+
config: options2.config
|
|
446
437
|
});
|
|
447
438
|
if (shouldCache) {
|
|
448
439
|
_chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, (prev) => ({
|
|
@@ -460,42 +451,39 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
460
451
|
}));
|
|
461
452
|
}
|
|
462
453
|
if (result.success) {
|
|
463
|
-
_optionalChain([
|
|
464
|
-
_optionalChain([options2, 'access',
|
|
465
|
-
_optionalChain([actionConfig, 'access',
|
|
454
|
+
_optionalChain([savedCallbacksRef, 'access', _16 => _16.current, 'access', _17 => _17.onSuccess, 'optionalCall', _18 => _18(actionName, result.message || "Action successful", result.data)]);
|
|
455
|
+
_optionalChain([options2, 'access', _19 => _19.onSuccess, 'optionalCall', _20 => _20(result.data, void 0)]);
|
|
456
|
+
_optionalChain([actionConfig, 'access', _21 => _21.invalidates, 'optionalAccess', _22 => _22.forEach, 'call', _23 => _23((keyToInvalidate) => {
|
|
466
457
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
467
458
|
_chunkJAMEOM7Tcjs.globalStateManager.invalidateByPrefix(prefix);
|
|
468
459
|
})]);
|
|
469
460
|
} else {
|
|
470
|
-
_optionalChain([
|
|
471
|
-
_optionalChain([options2, 'access',
|
|
461
|
+
_optionalChain([savedCallbacksRef, 'access', _24 => _24.current, 'access', _25 => _25.onError, 'optionalCall', _26 => _26(actionName, result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)))]);
|
|
462
|
+
_optionalChain([options2, 'access', _27 => _27.onError, 'optionalCall', _28 => _28(result.error, void 0)]);
|
|
472
463
|
}
|
|
473
464
|
return result;
|
|
474
465
|
} catch (error) {
|
|
475
|
-
const apiError = _optionalChain([error, 'access',
|
|
466
|
+
const apiError = _optionalChain([error, 'access', _29 => _29.response, 'optionalAccess', _30 => _30.data]) || { status: 500, message: error.message };
|
|
476
467
|
if (shouldCache) {
|
|
477
468
|
_chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
478
469
|
}
|
|
479
|
-
_optionalChain([
|
|
480
|
-
_optionalChain([options2, 'access',
|
|
470
|
+
_optionalChain([savedCallbacksRef, 'access', _31 => _31.current, 'access', _32 => _32.onError, 'optionalCall', _33 => _33(actionName, apiError.message, apiError)]);
|
|
471
|
+
_optionalChain([options2, 'access', _34 => _34.onError, 'optionalCall', _35 => _35(apiError, void 0)]);
|
|
481
472
|
throw error;
|
|
482
|
-
} finally {
|
|
483
|
-
if (options2.onSettled) {
|
|
484
|
-
options2.onSettled();
|
|
485
|
-
}
|
|
486
473
|
}
|
|
487
474
|
};
|
|
488
475
|
const reset = (input, options2 = {}) => {
|
|
489
476
|
if (shouldCache) {
|
|
490
|
-
const finalPathParams = { ...
|
|
477
|
+
const finalPathParams = { ...pathParamsRef.current, ...options2.pathParams };
|
|
491
478
|
const cacheKey = _chunkJAMEOM7Tcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
492
479
|
_chunkJAMEOM7Tcjs.globalStateManager.setState(cacheKey, () => createInitialState());
|
|
493
480
|
}
|
|
494
481
|
};
|
|
495
482
|
stableActionsRef.current[actionName] = { execute, reset };
|
|
496
483
|
}
|
|
497
|
-
}, [axiosInstance, moduleConfig
|
|
484
|
+
}, [axiosInstance, moduleConfig]);
|
|
498
485
|
const actions = _react.useMemo.call(void 0, () => stableActionsRef.current, []);
|
|
486
|
+
const initialFetchRef = _react.useRef.call(void 0, false);
|
|
499
487
|
_react.useEffect.call(void 0, () => {
|
|
500
488
|
if (typeof window === "undefined" || initialFetchRef.current || !enabled) return;
|
|
501
489
|
const actionsToRun = initialActionsToExecute ? initialActionsToExecute.map(String) : Object.keys(moduleConfig.actions).filter(
|
|
@@ -503,23 +491,17 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
503
491
|
);
|
|
504
492
|
actionsToRun.forEach((actionName) => {
|
|
505
493
|
const key = actionName;
|
|
506
|
-
const
|
|
507
|
-
if (_optionalChain([hydratedData, 'access', _28 => _28[actionName], 'optionalAccess', _29 => _29.data])) {
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
const actionConfig = moduleConfig.actions[key];
|
|
511
|
-
const executeAction = _optionalChain([actions, 'access', _30 => _30[key], 'optionalAccess', _31 => _31.execute]);
|
|
494
|
+
const executeAction = _optionalChain([actions, 'access', _36 => _36[key], 'optionalAccess', _37 => _37.execute]);
|
|
512
495
|
if (typeof executeAction === "function") {
|
|
513
|
-
|
|
514
|
-
executeAction(input);
|
|
496
|
+
executeAction();
|
|
515
497
|
}
|
|
516
498
|
});
|
|
517
499
|
initialFetchRef.current = true;
|
|
518
|
-
}, [actions, initialActionsToExecute, moduleConfig.actions,
|
|
500
|
+
}, [actions, initialActionsToExecute, moduleConfig.actions, enabled]);
|
|
519
501
|
const queries = _react.useMemo.call(void 0, () => {
|
|
520
502
|
return Object.keys(moduleConfig.actions).reduce((acc, actionName) => {
|
|
521
503
|
const actionConfig = moduleConfig.actions[actionName];
|
|
522
|
-
if (_optionalChain([actionConfig, 'optionalAccess',
|
|
504
|
+
if (_optionalChain([actionConfig, 'optionalAccess', _38 => _38.hasQuery])) {
|
|
523
505
|
const setActionQueryOptions = (updater) => {
|
|
524
506
|
const key = actionName;
|
|
525
507
|
setQueryOptions((prev) => ({ ...prev, [key]: typeof updater === "function" ? updater(prev[key] || {}) : updater }));
|
|
@@ -538,15 +520,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
538
520
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
539
521
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
540
522
|
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
541
|
-
reset: () => setActionQueryOptions(
|
|
523
|
+
reset: () => setActionQueryOptions(_optionalChain([defaultQueryOptions, 'optionalAccess', _39 => _39[actionName]]) || {})
|
|
542
524
|
};
|
|
543
525
|
}
|
|
544
526
|
return acc;
|
|
545
527
|
}, {});
|
|
546
|
-
}, [actions, queryOptions, moduleConfig.actions]);
|
|
528
|
+
}, [actions, queryOptions, moduleConfig.actions, defaultQueryOptions]);
|
|
547
529
|
const states = {};
|
|
548
530
|
function isActionWithQuery(key, actions2) {
|
|
549
|
-
return _optionalChain([actions2, 'access',
|
|
531
|
+
return _optionalChain([actions2, 'access', _40 => _40[key], 'optionalAccess', _41 => _41.hasQuery]) === true;
|
|
550
532
|
}
|
|
551
533
|
for (const actionName in moduleConfig.actions) {
|
|
552
534
|
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
@@ -555,10 +537,10 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
555
537
|
if (actionConfig.cacheResponse !== false) {
|
|
556
538
|
let currentQueryOptions;
|
|
557
539
|
if (isActionWithQuery(key, moduleConfig.actions)) {
|
|
558
|
-
currentQueryOptions = _optionalChain([queries, 'access',
|
|
540
|
+
currentQueryOptions = _optionalChain([queries, 'access', _42 => _42[key], 'optionalAccess', _43 => _43.options]);
|
|
559
541
|
}
|
|
560
542
|
const input = currentQueryOptions;
|
|
561
|
-
const pathParams =
|
|
543
|
+
const pathParams = globalPathParams || {};
|
|
562
544
|
const cacheKey = _chunkJAMEOM7Tcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams });
|
|
563
545
|
states[actionName] = useApiActionState(actionConfig, cacheKey, actions[key].execute, input, enabled);
|
|
564
546
|
} else {
|
|
@@ -596,6 +578,8 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
596
578
|
queries,
|
|
597
579
|
dehydrate,
|
|
598
580
|
...extraContextData || {}
|
|
581
|
+
// [REFACTOR] `states` and `queries` are now dependencies because they are recalculated on re-renders.
|
|
582
|
+
// `actions` is stable and doesn't need to be a dependency.
|
|
599
583
|
}), [actions, states, queries, dehydrate, extraContextData]);
|
|
600
584
|
return finalApiReturn;
|
|
601
585
|
}
|
package/dist/client.js
CHANGED
|
@@ -330,32 +330,29 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
330
330
|
defaultQueryOptions,
|
|
331
331
|
initialActionsToExecute
|
|
332
332
|
} = options;
|
|
333
|
-
const pathParamsString = useMemo3(() => JSON.stringify(globalPathParams || {}), [globalPathParams]);
|
|
334
333
|
const [queryOptions, setQueryOptions] = useState3(() => {
|
|
335
334
|
const initialOptions = {};
|
|
336
335
|
for (const actionName in moduleConfig.actions) {
|
|
337
336
|
const actionConfig = moduleConfig.actions[actionName];
|
|
338
|
-
if (actionConfig.
|
|
339
|
-
initialOptions[actionName] = { ...
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
if (defaultQueryOptions) {
|
|
343
|
-
for (const actionName in defaultQueryOptions) {
|
|
344
|
-
initialOptions[actionName] = {
|
|
345
|
-
...initialOptions[actionName],
|
|
346
|
-
...defaultQueryOptions[actionName]
|
|
347
|
-
};
|
|
337
|
+
if (actionConfig.hasQuery) {
|
|
338
|
+
initialOptions[actionName] = { ...defaultQueryOptions?.[actionName] || {} };
|
|
348
339
|
}
|
|
349
340
|
}
|
|
350
341
|
return initialOptions;
|
|
351
342
|
});
|
|
352
|
-
const
|
|
353
|
-
const
|
|
354
|
-
const
|
|
355
|
-
const savedCallbacks = useRef4({ onSuccess, onError });
|
|
343
|
+
const queryOptionsRef = useRef4(queryOptions);
|
|
344
|
+
const pathParamsRef = useRef4(globalPathParams);
|
|
345
|
+
const savedCallbacksRef = useRef4({ onSuccess, onError });
|
|
356
346
|
useEffect4(() => {
|
|
357
|
-
|
|
347
|
+
queryOptionsRef.current = queryOptions;
|
|
348
|
+
}, [queryOptions]);
|
|
349
|
+
useEffect4(() => {
|
|
350
|
+
pathParamsRef.current = globalPathParams;
|
|
351
|
+
}, [globalPathParams]);
|
|
352
|
+
useEffect4(() => {
|
|
353
|
+
savedCallbacksRef.current = { onSuccess, onError };
|
|
358
354
|
}, [onSuccess, onError]);
|
|
355
|
+
const stableActionsRef = useRef4({});
|
|
359
356
|
useEffect4(() => {
|
|
360
357
|
if (hydratedState) {
|
|
361
358
|
globalStateManager.rehydrate(hydratedState);
|
|
@@ -386,7 +383,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
386
383
|
return newState;
|
|
387
384
|
});
|
|
388
385
|
}
|
|
389
|
-
}, [urlSync]);
|
|
386
|
+
}, [urlSync?.searchParams, urlSync?.actionsToSync, urlSync?.defaultSyncAction]);
|
|
390
387
|
useEffect4(() => {
|
|
391
388
|
if (!urlSync) return;
|
|
392
389
|
const { searchParams, updateUrl, actionsToSync, defaultSyncAction } = urlSync;
|
|
@@ -411,19 +408,17 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
411
408
|
if (hasChanged) {
|
|
412
409
|
updateUrl(currentParams.toString());
|
|
413
410
|
}
|
|
414
|
-
}, [queryOptions, urlSync]);
|
|
411
|
+
}, [queryOptions, urlSync?.updateUrl, urlSync?.searchParams, urlSync?.actionsToSync, urlSync?.defaultSyncAction]);
|
|
415
412
|
useMemo3(() => {
|
|
416
413
|
for (const actionName in moduleConfig.actions) {
|
|
417
414
|
const actionConfig = moduleConfig.actions[actionName];
|
|
418
415
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
419
416
|
const execute = async (input, options2 = {}) => {
|
|
420
|
-
|
|
421
|
-
const
|
|
417
|
+
const finalPathParams = { ...pathParamsRef.current, ...options2.pathParams };
|
|
418
|
+
const currentQueryOptions = queryOptionsRef.current[actionName];
|
|
422
419
|
let bodyForCall = input;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const currentQueryOptions = queryOptions[actionName];
|
|
426
|
-
bodyForCall = { ...actionConfig._input, ...currentQueryOptions, ...options2.config?.params, ...input };
|
|
420
|
+
if (actionConfig.method === "GET" && actionConfig.hasQuery) {
|
|
421
|
+
bodyForCall = { ...currentQueryOptions, ...options2.config?.params, ...input };
|
|
427
422
|
} else if (actionConfig.upload && input) {
|
|
428
423
|
bodyForCall = new FormData();
|
|
429
424
|
for (const key in input) {
|
|
@@ -431,18 +426,14 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
431
426
|
}
|
|
432
427
|
}
|
|
433
428
|
const cacheKey = shouldCache ? generateCacheKey(moduleConfig.baseEndpoint, actionName, bodyForCall, { pathParams: finalPathParams }) : "";
|
|
434
|
-
|
|
429
|
+
if (shouldCache) {
|
|
430
|
+
globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
431
|
+
}
|
|
435
432
|
try {
|
|
436
|
-
if (options2.onMutate && shouldCache) {
|
|
437
|
-
mutationContext = await options2.onMutate(input);
|
|
438
|
-
}
|
|
439
|
-
if (shouldCache) {
|
|
440
|
-
globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
441
|
-
}
|
|
442
433
|
const result = await callDynamicApi(axiosInstance, moduleConfig.baseEndpoint, actionConfig, {
|
|
443
434
|
pathParams: finalPathParams,
|
|
444
435
|
body: bodyForCall,
|
|
445
|
-
config:
|
|
436
|
+
config: options2.config
|
|
446
437
|
});
|
|
447
438
|
if (shouldCache) {
|
|
448
439
|
globalStateManager.setState(cacheKey, (prev) => ({
|
|
@@ -460,15 +451,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
460
451
|
}));
|
|
461
452
|
}
|
|
462
453
|
if (result.success) {
|
|
463
|
-
|
|
464
|
-
options2.onSuccess?.(result.data,
|
|
454
|
+
savedCallbacksRef.current.onSuccess?.(actionName, result.message || "Action successful", result.data);
|
|
455
|
+
options2.onSuccess?.(result.data, void 0);
|
|
465
456
|
actionConfig.invalidates?.forEach((keyToInvalidate) => {
|
|
466
457
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
467
458
|
globalStateManager.invalidateByPrefix(prefix);
|
|
468
459
|
});
|
|
469
460
|
} else {
|
|
470
|
-
|
|
471
|
-
options2.onError?.(result.error,
|
|
461
|
+
savedCallbacksRef.current.onError?.(actionName, result.message || "Action failed", result.error ?? void 0);
|
|
462
|
+
options2.onError?.(result.error, void 0);
|
|
472
463
|
}
|
|
473
464
|
return result;
|
|
474
465
|
} catch (error) {
|
|
@@ -476,26 +467,23 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
476
467
|
if (shouldCache) {
|
|
477
468
|
globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
478
469
|
}
|
|
479
|
-
|
|
480
|
-
options2.onError?.(apiError,
|
|
470
|
+
savedCallbacksRef.current.onError?.(actionName, apiError.message, apiError);
|
|
471
|
+
options2.onError?.(apiError, void 0);
|
|
481
472
|
throw error;
|
|
482
|
-
} finally {
|
|
483
|
-
if (options2.onSettled) {
|
|
484
|
-
options2.onSettled();
|
|
485
|
-
}
|
|
486
473
|
}
|
|
487
474
|
};
|
|
488
475
|
const reset = (input, options2 = {}) => {
|
|
489
476
|
if (shouldCache) {
|
|
490
|
-
const finalPathParams = { ...
|
|
477
|
+
const finalPathParams = { ...pathParamsRef.current, ...options2.pathParams };
|
|
491
478
|
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
492
479
|
globalStateManager.setState(cacheKey, () => createInitialState());
|
|
493
480
|
}
|
|
494
481
|
};
|
|
495
482
|
stableActionsRef.current[actionName] = { execute, reset };
|
|
496
483
|
}
|
|
497
|
-
}, [axiosInstance, moduleConfig
|
|
484
|
+
}, [axiosInstance, moduleConfig]);
|
|
498
485
|
const actions = useMemo3(() => stableActionsRef.current, []);
|
|
486
|
+
const initialFetchRef = useRef4(false);
|
|
499
487
|
useEffect4(() => {
|
|
500
488
|
if (typeof window === "undefined" || initialFetchRef.current || !enabled) return;
|
|
501
489
|
const actionsToRun = initialActionsToExecute ? initialActionsToExecute.map(String) : Object.keys(moduleConfig.actions).filter(
|
|
@@ -503,19 +491,13 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
503
491
|
);
|
|
504
492
|
actionsToRun.forEach((actionName) => {
|
|
505
493
|
const key = actionName;
|
|
506
|
-
const hydratedData = hydratedState ? JSON.parse(hydratedState) : {};
|
|
507
|
-
if (hydratedData[actionName]?.data) {
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
const actionConfig = moduleConfig.actions[key];
|
|
511
494
|
const executeAction = actions[key]?.execute;
|
|
512
495
|
if (typeof executeAction === "function") {
|
|
513
|
-
|
|
514
|
-
executeAction(input);
|
|
496
|
+
executeAction();
|
|
515
497
|
}
|
|
516
498
|
});
|
|
517
499
|
initialFetchRef.current = true;
|
|
518
|
-
}, [actions, initialActionsToExecute, moduleConfig.actions,
|
|
500
|
+
}, [actions, initialActionsToExecute, moduleConfig.actions, enabled]);
|
|
519
501
|
const queries = useMemo3(() => {
|
|
520
502
|
return Object.keys(moduleConfig.actions).reduce((acc, actionName) => {
|
|
521
503
|
const actionConfig = moduleConfig.actions[actionName];
|
|
@@ -538,12 +520,12 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
538
520
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
539
521
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
540
522
|
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
541
|
-
reset: () => setActionQueryOptions(
|
|
523
|
+
reset: () => setActionQueryOptions(defaultQueryOptions?.[actionName] || {})
|
|
542
524
|
};
|
|
543
525
|
}
|
|
544
526
|
return acc;
|
|
545
527
|
}, {});
|
|
546
|
-
}, [actions, queryOptions, moduleConfig.actions]);
|
|
528
|
+
}, [actions, queryOptions, moduleConfig.actions, defaultQueryOptions]);
|
|
547
529
|
const states = {};
|
|
548
530
|
function isActionWithQuery(key, actions2) {
|
|
549
531
|
return actions2[key]?.hasQuery === true;
|
|
@@ -558,7 +540,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
558
540
|
currentQueryOptions = queries[key]?.options;
|
|
559
541
|
}
|
|
560
542
|
const input = currentQueryOptions;
|
|
561
|
-
const pathParams =
|
|
543
|
+
const pathParams = globalPathParams || {};
|
|
562
544
|
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams });
|
|
563
545
|
states[actionName] = useApiActionState(actionConfig, cacheKey, actions[key].execute, input, enabled);
|
|
564
546
|
} else {
|
|
@@ -596,6 +578,8 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
596
578
|
queries,
|
|
597
579
|
dehydrate,
|
|
598
580
|
...extraContextData || {}
|
|
581
|
+
// [REFACTOR] `states` and `queries` are now dependencies because they are recalculated on re-renders.
|
|
582
|
+
// `actions` is stable and doesn't need to be a dependency.
|
|
599
583
|
}), [actions, states, queries, dehydrate, extraContextData]);
|
|
600
584
|
return finalApiReturn;
|
|
601
585
|
}
|