api-core-lib 12.0.91 → 12.0.92
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 +90 -67
- package/dist/client.js +81 -58
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -270,19 +270,15 @@ function useDeepCompareEffect(callback, dependencies) {
|
|
|
270
270
|
_react.useEffect.call(void 0, callback, [currentDependenciesRef.current]);
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
-
// src/hooks/useApiModule/useApiModule.
|
|
273
|
+
// src/hooks/useApiModule/useApiModule.ts
|
|
274
274
|
|
|
275
275
|
var ApiModuleContext = _react.createContext.call(void 0, null);
|
|
276
276
|
var ApiModuleProvider = ApiModuleContext.Provider;
|
|
277
277
|
var createInitialState = () => ({
|
|
278
278
|
data: null,
|
|
279
279
|
lastSuccessAt: void 0,
|
|
280
|
-
meta:
|
|
281
|
-
|
|
282
|
-
links: void 0,
|
|
283
|
-
// links هو كائن، وليس مصفوفة
|
|
284
|
-
validationErrors: void 0,
|
|
285
|
-
// validationErrors هو مصفوفة، ولكن يمكن أن يكون غير موجود
|
|
280
|
+
meta: [],
|
|
281
|
+
validationErrors: [],
|
|
286
282
|
error: null,
|
|
287
283
|
loading: false,
|
|
288
284
|
success: false,
|
|
@@ -291,29 +287,34 @@ var createInitialState = () => ({
|
|
|
291
287
|
rawResponse: null
|
|
292
288
|
});
|
|
293
289
|
function useApiActionState(actionConfig, cacheKey, execute, input, enabled) {
|
|
294
|
-
const getClientSnapshot =
|
|
295
|
-
const getServerSnapshot =
|
|
290
|
+
const getClientSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
|
|
291
|
+
const getServerSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
|
|
296
292
|
const state = _react.useSyncExternalStore.call(void 0,
|
|
297
293
|
(callback) => _chunk25UFVV4Fcjs.globalStateManager.subscribe(cacheKey, callback),
|
|
298
294
|
getClientSnapshot,
|
|
299
295
|
getServerSnapshot
|
|
300
296
|
);
|
|
297
|
+
const inputRef = _react.useRef.call(void 0, input);
|
|
298
|
+
_react.useEffect.call(void 0, () => {
|
|
299
|
+
inputRef.current = input;
|
|
300
|
+
}, [input]);
|
|
301
301
|
const refetch = _react.useCallback.call(void 0, () => {
|
|
302
|
-
execute(
|
|
303
|
-
}, [execute
|
|
304
|
-
const
|
|
302
|
+
execute(inputRef.current);
|
|
303
|
+
}, [execute]);
|
|
304
|
+
const prevCacheKeyRef = _react.useRef.call(void 0, cacheKey);
|
|
305
305
|
_react.useEffect.call(void 0, () => {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (enabled && actionConfig.autoFetch && !currentState.called && !currentState.loading) {
|
|
306
|
+
if (prevCacheKeyRef.current !== cacheKey && enabled && state.called) {
|
|
307
|
+
console.log(`[Cache Key Changed] from ${prevCacheKeyRef.current} to ${cacheKey}. Refetching...`);
|
|
309
308
|
refetch();
|
|
310
|
-
} else if (enabled &&
|
|
309
|
+
} else if (enabled && actionConfig.autoFetch && !state.called && !state.loading) {
|
|
310
|
+
console.log(`[Auto Fetch] for ${cacheKey}. Fetching...`);
|
|
311
311
|
refetch();
|
|
312
|
-
} else if (enabled &&
|
|
312
|
+
} else if (enabled && state.isStale && !state.loading) {
|
|
313
|
+
console.log(`[Stale State] for ${cacheKey}. Refetching...`);
|
|
313
314
|
refetch();
|
|
314
315
|
}
|
|
315
|
-
|
|
316
|
-
}, [cacheKey, enabled, state, actionConfig.autoFetch, refetch]);
|
|
316
|
+
prevCacheKeyRef.current = cacheKey;
|
|
317
|
+
}, [cacheKey, enabled, state.isStale, state.loading, state.called, actionConfig.autoFetch, refetch]);
|
|
317
318
|
return state;
|
|
318
319
|
}
|
|
319
320
|
function useModuleContext() {
|
|
@@ -339,7 +340,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
339
340
|
_react.useEffect.call(void 0, () => {
|
|
340
341
|
savedCallbacks.current = { onSuccess, onError };
|
|
341
342
|
}, [onSuccess, onError]);
|
|
343
|
+
_react.useMemo.call(void 0, () => {
|
|
344
|
+
if (hydratedState) {
|
|
345
|
+
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
346
|
+
}
|
|
347
|
+
}, [hydratedState]);
|
|
342
348
|
_react.useEffect.call(void 0, () => {
|
|
349
|
+
savedCallbacks.current = { onSuccess, onError };
|
|
350
|
+
}, [onSuccess, onError]);
|
|
351
|
+
_react.useMemo.call(void 0, () => {
|
|
343
352
|
if (hydratedState) {
|
|
344
353
|
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
345
354
|
}
|
|
@@ -349,7 +358,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
349
358
|
const actionConfig = moduleConfig.actions[actionName];
|
|
350
359
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
351
360
|
const execute = async (input, options2 = {}) => {
|
|
352
|
-
const finalPathParams = { ...
|
|
361
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
353
362
|
const cacheKey = shouldCache ? _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
354
363
|
let mutationContext;
|
|
355
364
|
try {
|
|
@@ -364,41 +373,48 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
364
373
|
body: input,
|
|
365
374
|
config: options2.config
|
|
366
375
|
});
|
|
376
|
+
console.log("[API Result execute]", result);
|
|
367
377
|
if (shouldCache) {
|
|
368
378
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({
|
|
369
379
|
...prev,
|
|
380
|
+
// احتفظ بالخصائص القديمة مثل 'called'
|
|
370
381
|
loading: false,
|
|
371
382
|
success: result.success,
|
|
383
|
+
// تحديث صريح
|
|
372
384
|
error: result.success ? null : result.error || prev.error,
|
|
373
|
-
|
|
374
|
-
|
|
385
|
+
// تحديث صريح
|
|
386
|
+
data: result.data,
|
|
387
|
+
// تحديث صريح للبيانات
|
|
375
388
|
meta: result.meta,
|
|
389
|
+
// تحديث صريح
|
|
376
390
|
links: result.links,
|
|
391
|
+
// تحديث صريح
|
|
377
392
|
message: result.message,
|
|
378
|
-
validationErrors: result.validationErrors,
|
|
379
|
-
rawResponse: result.rawResponse
|
|
380
|
-
|
|
393
|
+
validationErrors: result.validationErrors || [],
|
|
394
|
+
rawResponse: result.rawResponse
|
|
395
|
+
// isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
|
|
381
396
|
}));
|
|
382
397
|
}
|
|
383
398
|
if (result.success) {
|
|
384
|
-
_optionalChain([savedCallbacks, 'access',
|
|
385
|
-
_optionalChain([options2, 'access',
|
|
386
|
-
_optionalChain([actionConfig, 'access',
|
|
399
|
+
_optionalChain([savedCallbacks, 'access', _6 => _6.current, 'access', _7 => _7.onSuccess, 'optionalCall', _8 => _8(actionName, result.message || "Action successful", result.data)]);
|
|
400
|
+
_optionalChain([options2, 'access', _9 => _9.onSuccess, 'optionalCall', _10 => _10(result.data, mutationContext)]);
|
|
401
|
+
_optionalChain([actionConfig, 'access', _11 => _11.invalidates, 'optionalAccess', _12 => _12.forEach, 'call', _13 => _13((keyToInvalidate) => {
|
|
387
402
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
403
|
+
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
388
404
|
_chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
|
|
389
405
|
})]);
|
|
390
406
|
} else {
|
|
391
|
-
_optionalChain([savedCallbacks, 'access',
|
|
392
|
-
_optionalChain([options2, 'access',
|
|
407
|
+
_optionalChain([savedCallbacks, 'access', _14 => _14.current, 'access', _15 => _15.onError, 'optionalCall', _16 => _16(actionName, result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)))]);
|
|
408
|
+
_optionalChain([options2, 'access', _17 => _17.onError, 'optionalCall', _18 => _18(result.error, mutationContext)]);
|
|
393
409
|
}
|
|
394
410
|
return result;
|
|
395
411
|
} catch (error) {
|
|
396
|
-
const apiError = _optionalChain([error, 'access',
|
|
412
|
+
const apiError = _optionalChain([error, 'access', _19 => _19.response, 'optionalAccess', _20 => _20.data]) || { status: 500, message: error.message };
|
|
397
413
|
if (shouldCache) {
|
|
398
414
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
399
415
|
}
|
|
400
|
-
_optionalChain([savedCallbacks, 'access',
|
|
401
|
-
_optionalChain([options2, 'access',
|
|
416
|
+
_optionalChain([savedCallbacks, 'access', _21 => _21.current, 'access', _22 => _22.onError, 'optionalCall', _23 => _23(actionName, apiError.message, apiError)]);
|
|
417
|
+
_optionalChain([options2, 'access', _24 => _24.onError, 'optionalCall', _25 => _25(apiError, mutationContext)]);
|
|
402
418
|
throw error;
|
|
403
419
|
} finally {
|
|
404
420
|
if (options2.onSettled) {
|
|
@@ -408,7 +424,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
408
424
|
};
|
|
409
425
|
const reset = (input, options2 = {}) => {
|
|
410
426
|
if (shouldCache) {
|
|
411
|
-
const finalPathParams = { ...
|
|
427
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
412
428
|
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
413
429
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => createInitialState());
|
|
414
430
|
}
|
|
@@ -416,11 +432,11 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
416
432
|
acc[actionName] = { execute, reset };
|
|
417
433
|
return acc;
|
|
418
434
|
}, {});
|
|
419
|
-
}, [axiosInstance, moduleConfig,
|
|
435
|
+
}, [axiosInstance, moduleConfig, pathParamsString]);
|
|
420
436
|
const queries = _react.useMemo.call(void 0, () => {
|
|
421
437
|
const builtQueries = {};
|
|
422
438
|
for (const actionName in moduleConfig.actions) {
|
|
423
|
-
if (_optionalChain([moduleConfig, 'access',
|
|
439
|
+
if (_optionalChain([moduleConfig, 'access', _26 => _26.actions, 'access', _27 => _27[actionName], 'optionalAccess', _28 => _28.hasQuery])) {
|
|
424
440
|
const setActionQueryOptions = (updater) => {
|
|
425
441
|
setQueryOptions((prev) => ({ ...prev, [actionName]: typeof updater === "function" ? updater(prev[actionName] || {}) : updater }));
|
|
426
442
|
};
|
|
@@ -432,42 +448,50 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
432
448
|
setSearchTerm: (search) => setActionQueryOptions((p) => ({ ...p, search, page: 1 })),
|
|
433
449
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
434
450
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
435
|
-
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ?
|
|
451
|
+
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
436
452
|
reset: () => setActionQueryOptions({})
|
|
437
453
|
};
|
|
438
454
|
}
|
|
439
455
|
}
|
|
440
456
|
return builtQueries;
|
|
441
457
|
}, [queryOptions, moduleConfig.actions]);
|
|
442
|
-
const states =
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
cacheKey,
|
|
454
|
-
actions[actionName].execute,
|
|
455
|
-
input,
|
|
456
|
-
enabled
|
|
457
|
-
);
|
|
458
|
-
} else {
|
|
459
|
-
builtStates[actionName] = createInitialState();
|
|
458
|
+
const states = {};
|
|
459
|
+
function isActionWithQuery(key, actions2) {
|
|
460
|
+
return _optionalChain([actions2, 'access', _29 => _29[key], 'optionalAccess', _30 => _30.hasQuery]) === true;
|
|
461
|
+
}
|
|
462
|
+
for (const actionName in moduleConfig.actions) {
|
|
463
|
+
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
464
|
+
const actionConfig = moduleConfig.actions[actionName];
|
|
465
|
+
if (actionConfig.cacheResponse !== false) {
|
|
466
|
+
let queryOptions2;
|
|
467
|
+
if (isActionWithQuery(actionName, moduleConfig.actions)) {
|
|
468
|
+
queryOptions2 = _optionalChain([queries, 'access', _31 => _31[actionName], 'optionalAccess', _32 => _32.options]);
|
|
460
469
|
}
|
|
470
|
+
const input = queryOptions2;
|
|
471
|
+
const pathParams = JSON.parse(pathParamsString);
|
|
472
|
+
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0,
|
|
473
|
+
moduleConfig.baseEndpoint,
|
|
474
|
+
actionName,
|
|
475
|
+
input,
|
|
476
|
+
{ pathParams }
|
|
477
|
+
);
|
|
478
|
+
states[actionName] = useApiActionState(
|
|
479
|
+
actionConfig,
|
|
480
|
+
cacheKey,
|
|
481
|
+
actions[actionName].execute,
|
|
482
|
+
input,
|
|
483
|
+
enabled
|
|
484
|
+
);
|
|
485
|
+
} else {
|
|
486
|
+
states[actionName] = createInitialState();
|
|
461
487
|
}
|
|
462
488
|
}
|
|
463
|
-
|
|
464
|
-
}, [moduleConfig, queries, actions, modulePathParams, enabled]);
|
|
489
|
+
}
|
|
465
490
|
const lastBlurTimestamp = _react.useRef.call(void 0, Date.now());
|
|
466
491
|
_react.useEffect.call(void 0, () => {
|
|
467
492
|
if (!enabled || !refetchOnWindowFocus) return;
|
|
468
493
|
const onFocus = () => {
|
|
469
|
-
if (Date.now() - lastBlurTimestamp.current >
|
|
470
|
-
console.log("[Refetch on Focus] Invalidating all called queries for this module.");
|
|
494
|
+
if (Date.now() - lastBlurTimestamp.current > 1e4) {
|
|
471
495
|
const actionKeys = Object.keys(moduleConfig.actions);
|
|
472
496
|
for (const actionName of actionKeys) {
|
|
473
497
|
const state = states[actionName];
|
|
@@ -488,16 +512,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
488
512
|
window.removeEventListener("blur", onBlur);
|
|
489
513
|
};
|
|
490
514
|
}, [enabled, refetchOnWindowFocus, moduleConfig, states]);
|
|
491
|
-
const dehydrate = _react.
|
|
492
|
-
return _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
515
|
+
const dehydrate = _react.useMemo.call(void 0, () => {
|
|
516
|
+
return () => _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
493
517
|
}, []);
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
queries,
|
|
498
|
-
dehydrate,
|
|
518
|
+
const baseApiReturn = { actions, states, queries, dehydrate };
|
|
519
|
+
const finalApiReturn = _react.useMemo.call(void 0, () => ({
|
|
520
|
+
...baseApiReturn,
|
|
499
521
|
...extraContextData || {}
|
|
500
|
-
}), [
|
|
522
|
+
}), [baseApiReturn, extraContextData]);
|
|
523
|
+
return finalApiReturn;
|
|
501
524
|
}
|
|
502
525
|
|
|
503
526
|
// src/hooks/useApiModule/apiModuleContext.ts
|
package/dist/client.js
CHANGED
|
@@ -270,19 +270,15 @@ function useDeepCompareEffect(callback, dependencies) {
|
|
|
270
270
|
useEffect3(callback, [currentDependenciesRef.current]);
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
-
// src/hooks/useApiModule/useApiModule.
|
|
273
|
+
// src/hooks/useApiModule/useApiModule.ts
|
|
274
274
|
import { createContext, useCallback as useCallback3, useContext, useEffect as useEffect4, useMemo as useMemo3, useRef as useRef4, useState as useState3, useSyncExternalStore } from "react";
|
|
275
275
|
var ApiModuleContext = createContext(null);
|
|
276
276
|
var ApiModuleProvider = ApiModuleContext.Provider;
|
|
277
277
|
var createInitialState = () => ({
|
|
278
278
|
data: null,
|
|
279
279
|
lastSuccessAt: void 0,
|
|
280
|
-
meta:
|
|
281
|
-
|
|
282
|
-
links: void 0,
|
|
283
|
-
// links هو كائن، وليس مصفوفة
|
|
284
|
-
validationErrors: void 0,
|
|
285
|
-
// validationErrors هو مصفوفة، ولكن يمكن أن يكون غير موجود
|
|
280
|
+
meta: [],
|
|
281
|
+
validationErrors: [],
|
|
286
282
|
error: null,
|
|
287
283
|
loading: false,
|
|
288
284
|
success: false,
|
|
@@ -291,29 +287,34 @@ var createInitialState = () => ({
|
|
|
291
287
|
rawResponse: null
|
|
292
288
|
});
|
|
293
289
|
function useApiActionState(actionConfig, cacheKey, execute, input, enabled) {
|
|
294
|
-
const getClientSnapshot =
|
|
295
|
-
const getServerSnapshot =
|
|
290
|
+
const getClientSnapshot = () => globalStateManager.getSnapshot(cacheKey);
|
|
291
|
+
const getServerSnapshot = () => globalStateManager.getSnapshot(cacheKey);
|
|
296
292
|
const state = useSyncExternalStore(
|
|
297
293
|
(callback) => globalStateManager.subscribe(cacheKey, callback),
|
|
298
294
|
getClientSnapshot,
|
|
299
295
|
getServerSnapshot
|
|
300
296
|
);
|
|
297
|
+
const inputRef = useRef4(input);
|
|
298
|
+
useEffect4(() => {
|
|
299
|
+
inputRef.current = input;
|
|
300
|
+
}, [input]);
|
|
301
301
|
const refetch = useCallback3(() => {
|
|
302
|
-
execute(
|
|
303
|
-
}, [execute
|
|
304
|
-
const
|
|
302
|
+
execute(inputRef.current);
|
|
303
|
+
}, [execute]);
|
|
304
|
+
const prevCacheKeyRef = useRef4(cacheKey);
|
|
305
305
|
useEffect4(() => {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (enabled && actionConfig.autoFetch && !currentState.called && !currentState.loading) {
|
|
306
|
+
if (prevCacheKeyRef.current !== cacheKey && enabled && state.called) {
|
|
307
|
+
console.log(`[Cache Key Changed] from ${prevCacheKeyRef.current} to ${cacheKey}. Refetching...`);
|
|
309
308
|
refetch();
|
|
310
|
-
} else if (enabled &&
|
|
309
|
+
} else if (enabled && actionConfig.autoFetch && !state.called && !state.loading) {
|
|
310
|
+
console.log(`[Auto Fetch] for ${cacheKey}. Fetching...`);
|
|
311
311
|
refetch();
|
|
312
|
-
} else if (enabled &&
|
|
312
|
+
} else if (enabled && state.isStale && !state.loading) {
|
|
313
|
+
console.log(`[Stale State] for ${cacheKey}. Refetching...`);
|
|
313
314
|
refetch();
|
|
314
315
|
}
|
|
315
|
-
|
|
316
|
-
}, [cacheKey, enabled, state, actionConfig.autoFetch, refetch]);
|
|
316
|
+
prevCacheKeyRef.current = cacheKey;
|
|
317
|
+
}, [cacheKey, enabled, state.isStale, state.loading, state.called, actionConfig.autoFetch, refetch]);
|
|
317
318
|
return state;
|
|
318
319
|
}
|
|
319
320
|
function useModuleContext() {
|
|
@@ -339,7 +340,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
339
340
|
useEffect4(() => {
|
|
340
341
|
savedCallbacks.current = { onSuccess, onError };
|
|
341
342
|
}, [onSuccess, onError]);
|
|
343
|
+
useMemo3(() => {
|
|
344
|
+
if (hydratedState) {
|
|
345
|
+
globalStateManager.rehydrate(hydratedState);
|
|
346
|
+
}
|
|
347
|
+
}, [hydratedState]);
|
|
342
348
|
useEffect4(() => {
|
|
349
|
+
savedCallbacks.current = { onSuccess, onError };
|
|
350
|
+
}, [onSuccess, onError]);
|
|
351
|
+
useMemo3(() => {
|
|
343
352
|
if (hydratedState) {
|
|
344
353
|
globalStateManager.rehydrate(hydratedState);
|
|
345
354
|
}
|
|
@@ -349,7 +358,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
349
358
|
const actionConfig = moduleConfig.actions[actionName];
|
|
350
359
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
351
360
|
const execute = async (input, options2 = {}) => {
|
|
352
|
-
const finalPathParams = { ...
|
|
361
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
353
362
|
const cacheKey = shouldCache ? generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
354
363
|
let mutationContext;
|
|
355
364
|
try {
|
|
@@ -364,20 +373,26 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
364
373
|
body: input,
|
|
365
374
|
config: options2.config
|
|
366
375
|
});
|
|
376
|
+
console.log("[API Result execute]", result);
|
|
367
377
|
if (shouldCache) {
|
|
368
378
|
globalStateManager.setState(cacheKey, (prev) => ({
|
|
369
379
|
...prev,
|
|
380
|
+
// احتفظ بالخصائص القديمة مثل 'called'
|
|
370
381
|
loading: false,
|
|
371
382
|
success: result.success,
|
|
383
|
+
// تحديث صريح
|
|
372
384
|
error: result.success ? null : result.error || prev.error,
|
|
373
|
-
|
|
374
|
-
|
|
385
|
+
// تحديث صريح
|
|
386
|
+
data: result.data,
|
|
387
|
+
// تحديث صريح للبيانات
|
|
375
388
|
meta: result.meta,
|
|
389
|
+
// تحديث صريح
|
|
376
390
|
links: result.links,
|
|
391
|
+
// تحديث صريح
|
|
377
392
|
message: result.message,
|
|
378
|
-
validationErrors: result.validationErrors,
|
|
379
|
-
rawResponse: result.rawResponse
|
|
380
|
-
|
|
393
|
+
validationErrors: result.validationErrors || [],
|
|
394
|
+
rawResponse: result.rawResponse
|
|
395
|
+
// isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
|
|
381
396
|
}));
|
|
382
397
|
}
|
|
383
398
|
if (result.success) {
|
|
@@ -385,6 +400,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
385
400
|
options2.onSuccess?.(result.data, mutationContext);
|
|
386
401
|
actionConfig.invalidates?.forEach((keyToInvalidate) => {
|
|
387
402
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
403
|
+
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
388
404
|
globalStateManager.invalidateByPrefix(prefix);
|
|
389
405
|
});
|
|
390
406
|
} else {
|
|
@@ -408,7 +424,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
408
424
|
};
|
|
409
425
|
const reset = (input, options2 = {}) => {
|
|
410
426
|
if (shouldCache) {
|
|
411
|
-
const finalPathParams = { ...
|
|
427
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
412
428
|
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
413
429
|
globalStateManager.setState(cacheKey, () => createInitialState());
|
|
414
430
|
}
|
|
@@ -416,7 +432,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
416
432
|
acc[actionName] = { execute, reset };
|
|
417
433
|
return acc;
|
|
418
434
|
}, {});
|
|
419
|
-
}, [axiosInstance, moduleConfig,
|
|
435
|
+
}, [axiosInstance, moduleConfig, pathParamsString]);
|
|
420
436
|
const queries = useMemo3(() => {
|
|
421
437
|
const builtQueries = {};
|
|
422
438
|
for (const actionName in moduleConfig.actions) {
|
|
@@ -432,42 +448,50 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
432
448
|
setSearchTerm: (search) => setActionQueryOptions((p) => ({ ...p, search, page: 1 })),
|
|
433
449
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
434
450
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
435
|
-
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ?
|
|
451
|
+
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
436
452
|
reset: () => setActionQueryOptions({})
|
|
437
453
|
};
|
|
438
454
|
}
|
|
439
455
|
}
|
|
440
456
|
return builtQueries;
|
|
441
457
|
}, [queryOptions, moduleConfig.actions]);
|
|
442
|
-
const states =
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
cacheKey,
|
|
454
|
-
actions[actionName].execute,
|
|
455
|
-
input,
|
|
456
|
-
enabled
|
|
457
|
-
);
|
|
458
|
-
} else {
|
|
459
|
-
builtStates[actionName] = createInitialState();
|
|
458
|
+
const states = {};
|
|
459
|
+
function isActionWithQuery(key, actions2) {
|
|
460
|
+
return actions2[key]?.hasQuery === true;
|
|
461
|
+
}
|
|
462
|
+
for (const actionName in moduleConfig.actions) {
|
|
463
|
+
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
464
|
+
const actionConfig = moduleConfig.actions[actionName];
|
|
465
|
+
if (actionConfig.cacheResponse !== false) {
|
|
466
|
+
let queryOptions2;
|
|
467
|
+
if (isActionWithQuery(actionName, moduleConfig.actions)) {
|
|
468
|
+
queryOptions2 = queries[actionName]?.options;
|
|
460
469
|
}
|
|
470
|
+
const input = queryOptions2;
|
|
471
|
+
const pathParams = JSON.parse(pathParamsString);
|
|
472
|
+
const cacheKey = generateCacheKey(
|
|
473
|
+
moduleConfig.baseEndpoint,
|
|
474
|
+
actionName,
|
|
475
|
+
input,
|
|
476
|
+
{ pathParams }
|
|
477
|
+
);
|
|
478
|
+
states[actionName] = useApiActionState(
|
|
479
|
+
actionConfig,
|
|
480
|
+
cacheKey,
|
|
481
|
+
actions[actionName].execute,
|
|
482
|
+
input,
|
|
483
|
+
enabled
|
|
484
|
+
);
|
|
485
|
+
} else {
|
|
486
|
+
states[actionName] = createInitialState();
|
|
461
487
|
}
|
|
462
488
|
}
|
|
463
|
-
|
|
464
|
-
}, [moduleConfig, queries, actions, modulePathParams, enabled]);
|
|
489
|
+
}
|
|
465
490
|
const lastBlurTimestamp = useRef4(Date.now());
|
|
466
491
|
useEffect4(() => {
|
|
467
492
|
if (!enabled || !refetchOnWindowFocus) return;
|
|
468
493
|
const onFocus = () => {
|
|
469
|
-
if (Date.now() - lastBlurTimestamp.current >
|
|
470
|
-
console.log("[Refetch on Focus] Invalidating all called queries for this module.");
|
|
494
|
+
if (Date.now() - lastBlurTimestamp.current > 1e4) {
|
|
471
495
|
const actionKeys = Object.keys(moduleConfig.actions);
|
|
472
496
|
for (const actionName of actionKeys) {
|
|
473
497
|
const state = states[actionName];
|
|
@@ -488,16 +512,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
488
512
|
window.removeEventListener("blur", onBlur);
|
|
489
513
|
};
|
|
490
514
|
}, [enabled, refetchOnWindowFocus, moduleConfig, states]);
|
|
491
|
-
const dehydrate =
|
|
492
|
-
return globalStateManager.dehydrate();
|
|
515
|
+
const dehydrate = useMemo3(() => {
|
|
516
|
+
return () => globalStateManager.dehydrate();
|
|
493
517
|
}, []);
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
queries,
|
|
498
|
-
dehydrate,
|
|
518
|
+
const baseApiReturn = { actions, states, queries, dehydrate };
|
|
519
|
+
const finalApiReturn = useMemo3(() => ({
|
|
520
|
+
...baseApiReturn,
|
|
499
521
|
...extraContextData || {}
|
|
500
|
-
}), [
|
|
522
|
+
}), [baseApiReturn, extraContextData]);
|
|
523
|
+
return finalApiReturn;
|
|
501
524
|
}
|
|
502
525
|
|
|
503
526
|
// src/hooks/useApiModule/apiModuleContext.ts
|