api-core-lib 12.0.91 → 12.0.93
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/{apiModule.types-CZX-M2Ub.d.cts → apiModule.types-0YqoHN1K.d.cts} +10 -5
- package/dist/{apiModule.types-CZX-M2Ub.d.ts → apiModule.types-0YqoHN1K.d.ts} +10 -5
- package/dist/client.cjs +92 -76
- package/dist/client.d.cts +11 -21
- package/dist/client.d.ts +11 -21
- package/dist/client.js +84 -68
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/{useApiRecord.types-Cq9AlZX2.d.cts → useApiRecord.types-CdbyfLNG.d.cts} +1 -1
- package/dist/{useApiRecord.types-Cye9a7zh.d.ts → useApiRecord.types-Q5Ux9P1x.d.ts} +1 -1
- package/package.json +1 -1
|
@@ -205,6 +205,8 @@ interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
|
205
205
|
autoFetch?: boolean;
|
|
206
206
|
cacheResponse?: boolean;
|
|
207
207
|
invalidates?: string[];
|
|
208
|
+
requiresAuth?: boolean;
|
|
209
|
+
pathParams?: string[];
|
|
208
210
|
_input?: TInput;
|
|
209
211
|
_output?: TOutput;
|
|
210
212
|
}
|
|
@@ -261,12 +263,15 @@ type ActionsWithQuery<TActions extends Record<string, any>> = {
|
|
|
261
263
|
hasQuery: true;
|
|
262
264
|
} ? K : never;
|
|
263
265
|
}[keyof TActions];
|
|
264
|
-
type
|
|
266
|
+
type ModuleQueries<TActions extends Record<string, ActionConfigModule<any, any>>> = Partial<{
|
|
267
|
+
[K in keyof TActions as TActions[K] extends {
|
|
268
|
+
hasQuery: true;
|
|
269
|
+
} ? K : never]: UseApiQuery;
|
|
270
|
+
}>;
|
|
271
|
+
type UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}> = {
|
|
265
272
|
states: ModuleStates<TActions>;
|
|
266
273
|
actions: ModuleActions<TActions>;
|
|
267
|
-
queries:
|
|
268
|
-
[K in ActionsWithQuery<TActions>]: UseApiQuery;
|
|
269
|
-
};
|
|
274
|
+
queries: ModuleQueries<TActions>;
|
|
270
275
|
dehydrate: () => string;
|
|
271
276
|
} & TExtra;
|
|
272
277
|
/**
|
|
@@ -287,4 +292,4 @@ type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
|
287
292
|
}) => void;
|
|
288
293
|
};
|
|
289
294
|
|
|
290
|
-
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e,
|
|
295
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, ApiError as i, TokenManager as j, MiddlewareContext as k, Middleware as l, RefreshTokenConfig as m, ActionConfig as n, UseApiState as o, ActionState as p, ExecuteOptions as q, ActionsWithQuery as r, ModuleQueries as s, t, ActionMethods as u };
|
|
@@ -205,6 +205,8 @@ interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
|
205
205
|
autoFetch?: boolean;
|
|
206
206
|
cacheResponse?: boolean;
|
|
207
207
|
invalidates?: string[];
|
|
208
|
+
requiresAuth?: boolean;
|
|
209
|
+
pathParams?: string[];
|
|
208
210
|
_input?: TInput;
|
|
209
211
|
_output?: TOutput;
|
|
210
212
|
}
|
|
@@ -261,12 +263,15 @@ type ActionsWithQuery<TActions extends Record<string, any>> = {
|
|
|
261
263
|
hasQuery: true;
|
|
262
264
|
} ? K : never;
|
|
263
265
|
}[keyof TActions];
|
|
264
|
-
type
|
|
266
|
+
type ModuleQueries<TActions extends Record<string, ActionConfigModule<any, any>>> = Partial<{
|
|
267
|
+
[K in keyof TActions as TActions[K] extends {
|
|
268
|
+
hasQuery: true;
|
|
269
|
+
} ? K : never]: UseApiQuery;
|
|
270
|
+
}>;
|
|
271
|
+
type UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}> = {
|
|
265
272
|
states: ModuleStates<TActions>;
|
|
266
273
|
actions: ModuleActions<TActions>;
|
|
267
|
-
queries:
|
|
268
|
-
[K in ActionsWithQuery<TActions>]: UseApiQuery;
|
|
269
|
-
};
|
|
274
|
+
queries: ModuleQueries<TActions>;
|
|
270
275
|
dehydrate: () => string;
|
|
271
276
|
} & TExtra;
|
|
272
277
|
/**
|
|
@@ -287,4 +292,4 @@ type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
|
287
292
|
}) => void;
|
|
288
293
|
};
|
|
289
294
|
|
|
290
|
-
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e,
|
|
295
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, UseApiQuery as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, ApiError as i, TokenManager as j, MiddlewareContext as k, Middleware as l, RefreshTokenConfig as m, ActionConfig as n, UseApiState as o, ActionState as p, ExecuteOptions as q, ActionsWithQuery as r, ModuleQueries as s, t, ActionMethods as u };
|
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,38 +287,36 @@ 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
|
-
function useModuleContext() {
|
|
320
|
-
const context = _react.useContext.call(void 0, ApiModuleContext);
|
|
321
|
-
if (!context) {
|
|
322
|
-
throw new Error("useModuleContext must be used within an ApiModuleProvider");
|
|
323
|
-
}
|
|
324
|
-
return context;
|
|
325
|
-
}
|
|
326
320
|
function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
327
321
|
const {
|
|
328
322
|
refetchOnWindowFocus = true,
|
|
@@ -339,7 +333,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
339
333
|
_react.useEffect.call(void 0, () => {
|
|
340
334
|
savedCallbacks.current = { onSuccess, onError };
|
|
341
335
|
}, [onSuccess, onError]);
|
|
336
|
+
_react.useMemo.call(void 0, () => {
|
|
337
|
+
if (hydratedState) {
|
|
338
|
+
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
339
|
+
}
|
|
340
|
+
}, [hydratedState]);
|
|
342
341
|
_react.useEffect.call(void 0, () => {
|
|
342
|
+
savedCallbacks.current = { onSuccess, onError };
|
|
343
|
+
}, [onSuccess, onError]);
|
|
344
|
+
_react.useMemo.call(void 0, () => {
|
|
343
345
|
if (hydratedState) {
|
|
344
346
|
_chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
|
|
345
347
|
}
|
|
@@ -349,7 +351,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
349
351
|
const actionConfig = moduleConfig.actions[actionName];
|
|
350
352
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
351
353
|
const execute = async (input, options2 = {}) => {
|
|
352
|
-
const finalPathParams = { ...
|
|
354
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
353
355
|
const cacheKey = shouldCache ? _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
354
356
|
let mutationContext;
|
|
355
357
|
try {
|
|
@@ -364,41 +366,48 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
364
366
|
body: input,
|
|
365
367
|
config: options2.config
|
|
366
368
|
});
|
|
369
|
+
console.log("[API Result execute]", result);
|
|
367
370
|
if (shouldCache) {
|
|
368
371
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({
|
|
369
372
|
...prev,
|
|
373
|
+
// احتفظ بالخصائص القديمة مثل 'called'
|
|
370
374
|
loading: false,
|
|
371
375
|
success: result.success,
|
|
376
|
+
// تحديث صريح
|
|
372
377
|
error: result.success ? null : result.error || prev.error,
|
|
373
|
-
|
|
374
|
-
|
|
378
|
+
// تحديث صريح
|
|
379
|
+
data: result.data,
|
|
380
|
+
// تحديث صريح للبيانات
|
|
375
381
|
meta: result.meta,
|
|
382
|
+
// تحديث صريح
|
|
376
383
|
links: result.links,
|
|
384
|
+
// تحديث صريح
|
|
377
385
|
message: result.message,
|
|
378
|
-
validationErrors: result.validationErrors,
|
|
379
|
-
rawResponse: result.rawResponse
|
|
380
|
-
|
|
386
|
+
validationErrors: result.validationErrors || [],
|
|
387
|
+
rawResponse: result.rawResponse
|
|
388
|
+
// isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
|
|
381
389
|
}));
|
|
382
390
|
}
|
|
383
391
|
if (result.success) {
|
|
384
|
-
_optionalChain([savedCallbacks, 'access',
|
|
385
|
-
_optionalChain([options2, 'access',
|
|
386
|
-
_optionalChain([actionConfig, 'access',
|
|
392
|
+
_optionalChain([savedCallbacks, 'access', _6 => _6.current, 'access', _7 => _7.onSuccess, 'optionalCall', _8 => _8(actionName, result.message || "Action successful", result.data)]);
|
|
393
|
+
_optionalChain([options2, 'access', _9 => _9.onSuccess, 'optionalCall', _10 => _10(result.data, mutationContext)]);
|
|
394
|
+
_optionalChain([actionConfig, 'access', _11 => _11.invalidates, 'optionalAccess', _12 => _12.forEach, 'call', _13 => _13((keyToInvalidate) => {
|
|
387
395
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
396
|
+
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
388
397
|
_chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
|
|
389
398
|
})]);
|
|
390
399
|
} else {
|
|
391
|
-
_optionalChain([savedCallbacks, 'access',
|
|
392
|
-
_optionalChain([options2, 'access',
|
|
400
|
+
_optionalChain([savedCallbacks, 'access', _14 => _14.current, 'access', _15 => _15.onError, 'optionalCall', _16 => _16(actionName, result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)))]);
|
|
401
|
+
_optionalChain([options2, 'access', _17 => _17.onError, 'optionalCall', _18 => _18(result.error, mutationContext)]);
|
|
393
402
|
}
|
|
394
403
|
return result;
|
|
395
404
|
} catch (error) {
|
|
396
|
-
const apiError = _optionalChain([error, 'access',
|
|
405
|
+
const apiError = _optionalChain([error, 'access', _19 => _19.response, 'optionalAccess', _20 => _20.data]) || { status: 500, message: error.message };
|
|
397
406
|
if (shouldCache) {
|
|
398
407
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
399
408
|
}
|
|
400
|
-
_optionalChain([savedCallbacks, 'access',
|
|
401
|
-
_optionalChain([options2, 'access',
|
|
409
|
+
_optionalChain([savedCallbacks, 'access', _21 => _21.current, 'access', _22 => _22.onError, 'optionalCall', _23 => _23(actionName, apiError.message, apiError)]);
|
|
410
|
+
_optionalChain([options2, 'access', _24 => _24.onError, 'optionalCall', _25 => _25(apiError, mutationContext)]);
|
|
402
411
|
throw error;
|
|
403
412
|
} finally {
|
|
404
413
|
if (options2.onSettled) {
|
|
@@ -408,7 +417,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
408
417
|
};
|
|
409
418
|
const reset = (input, options2 = {}) => {
|
|
410
419
|
if (shouldCache) {
|
|
411
|
-
const finalPathParams = { ...
|
|
420
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
412
421
|
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
413
422
|
_chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => createInitialState());
|
|
414
423
|
}
|
|
@@ -416,11 +425,11 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
416
425
|
acc[actionName] = { execute, reset };
|
|
417
426
|
return acc;
|
|
418
427
|
}, {});
|
|
419
|
-
}, [axiosInstance, moduleConfig,
|
|
428
|
+
}, [axiosInstance, moduleConfig, pathParamsString]);
|
|
420
429
|
const queries = _react.useMemo.call(void 0, () => {
|
|
421
430
|
const builtQueries = {};
|
|
422
431
|
for (const actionName in moduleConfig.actions) {
|
|
423
|
-
if (_optionalChain([moduleConfig, 'access',
|
|
432
|
+
if (_optionalChain([moduleConfig, 'access', _26 => _26.actions, 'access', _27 => _27[actionName], 'optionalAccess', _28 => _28.hasQuery])) {
|
|
424
433
|
const setActionQueryOptions = (updater) => {
|
|
425
434
|
setQueryOptions((prev) => ({ ...prev, [actionName]: typeof updater === "function" ? updater(prev[actionName] || {}) : updater }));
|
|
426
435
|
};
|
|
@@ -432,42 +441,50 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
432
441
|
setSearchTerm: (search) => setActionQueryOptions((p) => ({ ...p, search, page: 1 })),
|
|
433
442
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
434
443
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
435
|
-
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ?
|
|
444
|
+
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
436
445
|
reset: () => setActionQueryOptions({})
|
|
437
446
|
};
|
|
438
447
|
}
|
|
439
448
|
}
|
|
440
449
|
return builtQueries;
|
|
441
450
|
}, [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();
|
|
451
|
+
const states = {};
|
|
452
|
+
function isActionWithQuery(key, actions2) {
|
|
453
|
+
return _optionalChain([actions2, 'access', _29 => _29[key], 'optionalAccess', _30 => _30.hasQuery]) === true;
|
|
454
|
+
}
|
|
455
|
+
for (const actionName in moduleConfig.actions) {
|
|
456
|
+
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
457
|
+
const actionConfig = moduleConfig.actions[actionName];
|
|
458
|
+
if (actionConfig.cacheResponse !== false) {
|
|
459
|
+
let queryOptions2;
|
|
460
|
+
if (isActionWithQuery(actionName, moduleConfig.actions)) {
|
|
461
|
+
queryOptions2 = _optionalChain([queries, 'access', _31 => _31[actionName], 'optionalAccess', _32 => _32.options]);
|
|
460
462
|
}
|
|
463
|
+
const input = queryOptions2;
|
|
464
|
+
const pathParams = JSON.parse(pathParamsString);
|
|
465
|
+
const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0,
|
|
466
|
+
moduleConfig.baseEndpoint,
|
|
467
|
+
actionName,
|
|
468
|
+
input,
|
|
469
|
+
{ pathParams }
|
|
470
|
+
);
|
|
471
|
+
states[actionName] = useApiActionState(
|
|
472
|
+
actionConfig,
|
|
473
|
+
cacheKey,
|
|
474
|
+
actions[actionName].execute,
|
|
475
|
+
input,
|
|
476
|
+
enabled
|
|
477
|
+
);
|
|
478
|
+
} else {
|
|
479
|
+
states[actionName] = createInitialState();
|
|
461
480
|
}
|
|
462
481
|
}
|
|
463
|
-
|
|
464
|
-
}, [moduleConfig, queries, actions, modulePathParams, enabled]);
|
|
482
|
+
}
|
|
465
483
|
const lastBlurTimestamp = _react.useRef.call(void 0, Date.now());
|
|
466
484
|
_react.useEffect.call(void 0, () => {
|
|
467
485
|
if (!enabled || !refetchOnWindowFocus) return;
|
|
468
486
|
const onFocus = () => {
|
|
469
|
-
if (Date.now() - lastBlurTimestamp.current >
|
|
470
|
-
console.log("[Refetch on Focus] Invalidating all called queries for this module.");
|
|
487
|
+
if (Date.now() - lastBlurTimestamp.current > 1e4) {
|
|
471
488
|
const actionKeys = Object.keys(moduleConfig.actions);
|
|
472
489
|
for (const actionName of actionKeys) {
|
|
473
490
|
const state = states[actionName];
|
|
@@ -488,22 +505,22 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
488
505
|
window.removeEventListener("blur", onBlur);
|
|
489
506
|
};
|
|
490
507
|
}, [enabled, refetchOnWindowFocus, moduleConfig, states]);
|
|
491
|
-
const dehydrate = _react.
|
|
492
|
-
return _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
508
|
+
const dehydrate = _react.useMemo.call(void 0, () => {
|
|
509
|
+
return () => _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
|
|
493
510
|
}, []);
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
queries,
|
|
498
|
-
dehydrate,
|
|
511
|
+
const baseApiReturn = { actions, states, queries, dehydrate };
|
|
512
|
+
const finalApiReturn = _react.useMemo.call(void 0, () => ({
|
|
513
|
+
...baseApiReturn,
|
|
499
514
|
...extraContextData || {}
|
|
500
|
-
}), [
|
|
515
|
+
}), [baseApiReturn, extraContextData]);
|
|
516
|
+
return finalApiReturn;
|
|
501
517
|
}
|
|
502
518
|
|
|
503
519
|
// src/hooks/useApiModule/apiModuleContext.ts
|
|
504
520
|
|
|
505
521
|
function createApiModuleContext() {
|
|
506
522
|
const Context = _react.createContext.call(void 0, null);
|
|
523
|
+
Context.displayName = "ApiModuleContext";
|
|
507
524
|
const Provider = ({ value, children }) => {
|
|
508
525
|
return _react.createElement.call(void 0, Context.Provider, { value }, children);
|
|
509
526
|
};
|
|
@@ -527,5 +544,4 @@ function createApiModuleContext() {
|
|
|
527
544
|
|
|
528
545
|
|
|
529
546
|
|
|
530
|
-
|
|
531
|
-
exports.ApiModuleProvider = ApiModuleProvider; exports.createApiModuleContext = createApiModuleContext; exports.useApi = useApi; exports.useApiModule = useApiModule; exports.useApiRecord = useApiRecord; exports.useDeepCompareEffect = useDeepCompareEffect; exports.useModuleContext = useModuleContext;
|
|
547
|
+
exports.ApiModuleProvider = ApiModuleProvider; exports.createApiModuleContext = createApiModuleContext; exports.useApi = useApi; exports.useApiModule = useApiModule; exports.useApiRecord = useApiRecord; exports.useDeepCompareEffect = useDeepCompareEffect;
|
package/dist/client.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery } from './apiModule.types-0YqoHN1K.cjs';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-CdbyfLNG.cjs';
|
|
4
4
|
import * as react from 'react';
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
6
|
|
|
@@ -25,12 +25,11 @@ declare function useDeepCompareEffect(callback: EffectCallback, dependencies: De
|
|
|
25
25
|
declare const ApiModuleProvider: react.Provider<{
|
|
26
26
|
states: ModuleStates<any>;
|
|
27
27
|
actions: ModuleActions<any>;
|
|
28
|
-
queries: {
|
|
28
|
+
queries: Partial<{
|
|
29
29
|
[x: string]: UseApiQuery;
|
|
30
|
-
}
|
|
30
|
+
}>;
|
|
31
31
|
dehydrate: () => string;
|
|
32
32
|
} | null>;
|
|
33
|
-
declare function useModuleContext<TModule extends ApiModuleConfig<any>, TExtra = {}>(): UseApiModuleReturn<TModule['actions'], TExtra>;
|
|
34
33
|
declare function useApiModule<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}>(axiosInstance: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>, options?: UseApiModuleOptions<TExtra>): UseApiModuleReturn<TActions, TExtra>;
|
|
35
34
|
|
|
36
35
|
/**
|
|
@@ -38,25 +37,16 @@ declare function useApiModule<TActions extends Record<string, ActionConfigModule
|
|
|
38
37
|
* This pattern avoids the need for manual type assertions in consuming components.
|
|
39
38
|
*
|
|
40
39
|
* @template TActions - The shape of the actions in the API module.
|
|
40
|
+
* @template TExtra - The shape of any extra data passed via `extraContextData`.
|
|
41
41
|
* @returns An object containing the typed Context, Provider, and a `useContext` hook.
|
|
42
42
|
*/
|
|
43
|
-
declare function createApiModuleContext<TActions extends Record<string, any
|
|
44
|
-
Context: react.Context<
|
|
45
|
-
states: ModuleStates<TActions>;
|
|
46
|
-
actions: ModuleActions<TActions>;
|
|
47
|
-
queries: { [K in ActionsWithQuery<TActions>]: UseApiQuery; };
|
|
48
|
-
dehydrate: () => string;
|
|
49
|
-
} | null>;
|
|
43
|
+
declare function createApiModuleContext<TActions extends Record<string, any>, TExtra extends object = {}>(): {
|
|
44
|
+
Context: react.Context<UseApiModuleReturn<TActions, TExtra> | null>;
|
|
50
45
|
Provider: ({ value, children }: {
|
|
51
|
-
value: UseApiModuleReturn<TActions>;
|
|
46
|
+
value: UseApiModuleReturn<TActions, TExtra>;
|
|
52
47
|
children: ReactNode;
|
|
53
|
-
}) => react.FunctionComponentElement<react.ProviderProps<
|
|
54
|
-
|
|
55
|
-
actions: ModuleActions<TActions>;
|
|
56
|
-
queries: { [K in ActionsWithQuery<TActions>]: UseApiQuery; };
|
|
57
|
-
dehydrate: () => string;
|
|
58
|
-
} | null>>;
|
|
59
|
-
useContext: () => UseApiModuleReturn<TActions>;
|
|
48
|
+
}) => react.FunctionComponentElement<react.ProviderProps<UseApiModuleReturn<TActions, TExtra> | null>>;
|
|
49
|
+
useContext: () => UseApiModuleReturn<TActions, TExtra>;
|
|
60
50
|
};
|
|
61
51
|
|
|
62
|
-
export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect
|
|
52
|
+
export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, M as ModuleStates, d as ModuleActions, e as UseApiQuery } from './apiModule.types-0YqoHN1K.js';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-Q5Ux9P1x.js';
|
|
4
4
|
import * as react from 'react';
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
6
|
|
|
@@ -25,12 +25,11 @@ declare function useDeepCompareEffect(callback: EffectCallback, dependencies: De
|
|
|
25
25
|
declare const ApiModuleProvider: react.Provider<{
|
|
26
26
|
states: ModuleStates<any>;
|
|
27
27
|
actions: ModuleActions<any>;
|
|
28
|
-
queries: {
|
|
28
|
+
queries: Partial<{
|
|
29
29
|
[x: string]: UseApiQuery;
|
|
30
|
-
}
|
|
30
|
+
}>;
|
|
31
31
|
dehydrate: () => string;
|
|
32
32
|
} | null>;
|
|
33
|
-
declare function useModuleContext<TModule extends ApiModuleConfig<any>, TExtra = {}>(): UseApiModuleReturn<TModule['actions'], TExtra>;
|
|
34
33
|
declare function useApiModule<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}>(axiosInstance: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>, options?: UseApiModuleOptions<TExtra>): UseApiModuleReturn<TActions, TExtra>;
|
|
35
34
|
|
|
36
35
|
/**
|
|
@@ -38,25 +37,16 @@ declare function useApiModule<TActions extends Record<string, ActionConfigModule
|
|
|
38
37
|
* This pattern avoids the need for manual type assertions in consuming components.
|
|
39
38
|
*
|
|
40
39
|
* @template TActions - The shape of the actions in the API module.
|
|
40
|
+
* @template TExtra - The shape of any extra data passed via `extraContextData`.
|
|
41
41
|
* @returns An object containing the typed Context, Provider, and a `useContext` hook.
|
|
42
42
|
*/
|
|
43
|
-
declare function createApiModuleContext<TActions extends Record<string, any
|
|
44
|
-
Context: react.Context<
|
|
45
|
-
states: ModuleStates<TActions>;
|
|
46
|
-
actions: ModuleActions<TActions>;
|
|
47
|
-
queries: { [K in ActionsWithQuery<TActions>]: UseApiQuery; };
|
|
48
|
-
dehydrate: () => string;
|
|
49
|
-
} | null>;
|
|
43
|
+
declare function createApiModuleContext<TActions extends Record<string, any>, TExtra extends object = {}>(): {
|
|
44
|
+
Context: react.Context<UseApiModuleReturn<TActions, TExtra> | null>;
|
|
50
45
|
Provider: ({ value, children }: {
|
|
51
|
-
value: UseApiModuleReturn<TActions>;
|
|
46
|
+
value: UseApiModuleReturn<TActions, TExtra>;
|
|
52
47
|
children: ReactNode;
|
|
53
|
-
}) => react.FunctionComponentElement<react.ProviderProps<
|
|
54
|
-
|
|
55
|
-
actions: ModuleActions<TActions>;
|
|
56
|
-
queries: { [K in ActionsWithQuery<TActions>]: UseApiQuery; };
|
|
57
|
-
dehydrate: () => string;
|
|
58
|
-
} | null>>;
|
|
59
|
-
useContext: () => UseApiModuleReturn<TActions>;
|
|
48
|
+
}) => react.FunctionComponentElement<react.ProviderProps<UseApiModuleReturn<TActions, TExtra> | null>>;
|
|
49
|
+
useContext: () => UseApiModuleReturn<TActions, TExtra>;
|
|
60
50
|
};
|
|
61
51
|
|
|
62
|
-
export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect
|
|
52
|
+
export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect };
|
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.
|
|
274
|
-
import { createContext, useCallback as useCallback3,
|
|
273
|
+
// src/hooks/useApiModule/useApiModule.ts
|
|
274
|
+
import { createContext, useCallback as useCallback3, 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,38 +287,36 @@ 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
|
-
function useModuleContext() {
|
|
320
|
-
const context = useContext(ApiModuleContext);
|
|
321
|
-
if (!context) {
|
|
322
|
-
throw new Error("useModuleContext must be used within an ApiModuleProvider");
|
|
323
|
-
}
|
|
324
|
-
return context;
|
|
325
|
-
}
|
|
326
320
|
function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
327
321
|
const {
|
|
328
322
|
refetchOnWindowFocus = true,
|
|
@@ -339,7 +333,15 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
339
333
|
useEffect4(() => {
|
|
340
334
|
savedCallbacks.current = { onSuccess, onError };
|
|
341
335
|
}, [onSuccess, onError]);
|
|
336
|
+
useMemo3(() => {
|
|
337
|
+
if (hydratedState) {
|
|
338
|
+
globalStateManager.rehydrate(hydratedState);
|
|
339
|
+
}
|
|
340
|
+
}, [hydratedState]);
|
|
342
341
|
useEffect4(() => {
|
|
342
|
+
savedCallbacks.current = { onSuccess, onError };
|
|
343
|
+
}, [onSuccess, onError]);
|
|
344
|
+
useMemo3(() => {
|
|
343
345
|
if (hydratedState) {
|
|
344
346
|
globalStateManager.rehydrate(hydratedState);
|
|
345
347
|
}
|
|
@@ -349,7 +351,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
349
351
|
const actionConfig = moduleConfig.actions[actionName];
|
|
350
352
|
const shouldCache = actionConfig.cacheResponse !== false;
|
|
351
353
|
const execute = async (input, options2 = {}) => {
|
|
352
|
-
const finalPathParams = { ...
|
|
354
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
353
355
|
const cacheKey = shouldCache ? generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
354
356
|
let mutationContext;
|
|
355
357
|
try {
|
|
@@ -364,20 +366,26 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
364
366
|
body: input,
|
|
365
367
|
config: options2.config
|
|
366
368
|
});
|
|
369
|
+
console.log("[API Result execute]", result);
|
|
367
370
|
if (shouldCache) {
|
|
368
371
|
globalStateManager.setState(cacheKey, (prev) => ({
|
|
369
372
|
...prev,
|
|
373
|
+
// احتفظ بالخصائص القديمة مثل 'called'
|
|
370
374
|
loading: false,
|
|
371
375
|
success: result.success,
|
|
376
|
+
// تحديث صريح
|
|
372
377
|
error: result.success ? null : result.error || prev.error,
|
|
373
|
-
|
|
374
|
-
|
|
378
|
+
// تحديث صريح
|
|
379
|
+
data: result.data,
|
|
380
|
+
// تحديث صريح للبيانات
|
|
375
381
|
meta: result.meta,
|
|
382
|
+
// تحديث صريح
|
|
376
383
|
links: result.links,
|
|
384
|
+
// تحديث صريح
|
|
377
385
|
message: result.message,
|
|
378
|
-
validationErrors: result.validationErrors,
|
|
379
|
-
rawResponse: result.rawResponse
|
|
380
|
-
|
|
386
|
+
validationErrors: result.validationErrors || [],
|
|
387
|
+
rawResponse: result.rawResponse
|
|
388
|
+
// isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
|
|
381
389
|
}));
|
|
382
390
|
}
|
|
383
391
|
if (result.success) {
|
|
@@ -385,6 +393,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
385
393
|
options2.onSuccess?.(result.data, mutationContext);
|
|
386
394
|
actionConfig.invalidates?.forEach((keyToInvalidate) => {
|
|
387
395
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
396
|
+
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
388
397
|
globalStateManager.invalidateByPrefix(prefix);
|
|
389
398
|
});
|
|
390
399
|
} else {
|
|
@@ -408,7 +417,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
408
417
|
};
|
|
409
418
|
const reset = (input, options2 = {}) => {
|
|
410
419
|
if (shouldCache) {
|
|
411
|
-
const finalPathParams = { ...
|
|
420
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
412
421
|
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
413
422
|
globalStateManager.setState(cacheKey, () => createInitialState());
|
|
414
423
|
}
|
|
@@ -416,7 +425,7 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
416
425
|
acc[actionName] = { execute, reset };
|
|
417
426
|
return acc;
|
|
418
427
|
}, {});
|
|
419
|
-
}, [axiosInstance, moduleConfig,
|
|
428
|
+
}, [axiosInstance, moduleConfig, pathParamsString]);
|
|
420
429
|
const queries = useMemo3(() => {
|
|
421
430
|
const builtQueries = {};
|
|
422
431
|
for (const actionName in moduleConfig.actions) {
|
|
@@ -432,42 +441,50 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
432
441
|
setSearchTerm: (search) => setActionQueryOptions((p) => ({ ...p, search, page: 1 })),
|
|
433
442
|
setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
|
|
434
443
|
setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
|
|
435
|
-
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ?
|
|
444
|
+
setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
|
|
436
445
|
reset: () => setActionQueryOptions({})
|
|
437
446
|
};
|
|
438
447
|
}
|
|
439
448
|
}
|
|
440
449
|
return builtQueries;
|
|
441
450
|
}, [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();
|
|
451
|
+
const states = {};
|
|
452
|
+
function isActionWithQuery(key, actions2) {
|
|
453
|
+
return actions2[key]?.hasQuery === true;
|
|
454
|
+
}
|
|
455
|
+
for (const actionName in moduleConfig.actions) {
|
|
456
|
+
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
457
|
+
const actionConfig = moduleConfig.actions[actionName];
|
|
458
|
+
if (actionConfig.cacheResponse !== false) {
|
|
459
|
+
let queryOptions2;
|
|
460
|
+
if (isActionWithQuery(actionName, moduleConfig.actions)) {
|
|
461
|
+
queryOptions2 = queries[actionName]?.options;
|
|
460
462
|
}
|
|
463
|
+
const input = queryOptions2;
|
|
464
|
+
const pathParams = JSON.parse(pathParamsString);
|
|
465
|
+
const cacheKey = generateCacheKey(
|
|
466
|
+
moduleConfig.baseEndpoint,
|
|
467
|
+
actionName,
|
|
468
|
+
input,
|
|
469
|
+
{ pathParams }
|
|
470
|
+
);
|
|
471
|
+
states[actionName] = useApiActionState(
|
|
472
|
+
actionConfig,
|
|
473
|
+
cacheKey,
|
|
474
|
+
actions[actionName].execute,
|
|
475
|
+
input,
|
|
476
|
+
enabled
|
|
477
|
+
);
|
|
478
|
+
} else {
|
|
479
|
+
states[actionName] = createInitialState();
|
|
461
480
|
}
|
|
462
481
|
}
|
|
463
|
-
|
|
464
|
-
}, [moduleConfig, queries, actions, modulePathParams, enabled]);
|
|
482
|
+
}
|
|
465
483
|
const lastBlurTimestamp = useRef4(Date.now());
|
|
466
484
|
useEffect4(() => {
|
|
467
485
|
if (!enabled || !refetchOnWindowFocus) return;
|
|
468
486
|
const onFocus = () => {
|
|
469
|
-
if (Date.now() - lastBlurTimestamp.current >
|
|
470
|
-
console.log("[Refetch on Focus] Invalidating all called queries for this module.");
|
|
487
|
+
if (Date.now() - lastBlurTimestamp.current > 1e4) {
|
|
471
488
|
const actionKeys = Object.keys(moduleConfig.actions);
|
|
472
489
|
for (const actionName of actionKeys) {
|
|
473
490
|
const state = states[actionName];
|
|
@@ -488,22 +505,22 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
488
505
|
window.removeEventListener("blur", onBlur);
|
|
489
506
|
};
|
|
490
507
|
}, [enabled, refetchOnWindowFocus, moduleConfig, states]);
|
|
491
|
-
const dehydrate =
|
|
492
|
-
return globalStateManager.dehydrate();
|
|
508
|
+
const dehydrate = useMemo3(() => {
|
|
509
|
+
return () => globalStateManager.dehydrate();
|
|
493
510
|
}, []);
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
queries,
|
|
498
|
-
dehydrate,
|
|
511
|
+
const baseApiReturn = { actions, states, queries, dehydrate };
|
|
512
|
+
const finalApiReturn = useMemo3(() => ({
|
|
513
|
+
...baseApiReturn,
|
|
499
514
|
...extraContextData || {}
|
|
500
|
-
}), [
|
|
515
|
+
}), [baseApiReturn, extraContextData]);
|
|
516
|
+
return finalApiReturn;
|
|
501
517
|
}
|
|
502
518
|
|
|
503
519
|
// src/hooks/useApiModule/apiModuleContext.ts
|
|
504
520
|
import { createContext as createContext2, useContext as useContext2, createElement } from "react";
|
|
505
521
|
function createApiModuleContext() {
|
|
506
522
|
const Context = createContext2(null);
|
|
523
|
+
Context.displayName = "ApiModuleContext";
|
|
507
524
|
const Provider = ({ value, children }) => {
|
|
508
525
|
return createElement(Context.Provider, { value }, children);
|
|
509
526
|
};
|
|
@@ -526,6 +543,5 @@ export {
|
|
|
526
543
|
useApi,
|
|
527
544
|
useApiModule,
|
|
528
545
|
useApiRecord,
|
|
529
|
-
useDeepCompareEffect
|
|
530
|
-
useModuleContext
|
|
546
|
+
useDeepCompareEffect
|
|
531
547
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, Method, AxiosResponse, AxiosError } from 'axios';
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
4
|
-
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-
|
|
2
|
+
import { f as ApiClientConfig, A as ActionConfigModule, S as StandardResponse, g as ActionOptions, R as RequestConfig, h as ActionStateModule, Q as QueryOptions, e as UseApiQuery, i as ApiError, L as LogLevel } from './apiModule.types-0YqoHN1K.cjs';
|
|
3
|
+
export { n as ActionConfig, u as ActionMethods, p as ActionState, r as ActionsWithQuery, a as ApiModuleConfig, E as ExecutableAction, q as ExecuteOptions, I as InputOf, l as Middleware, k as MiddlewareContext, d as ModuleActions, s as ModuleQueries, M as ModuleStates, O as OutputOf, P as PaginationMeta, m as RefreshTokenConfig, j as TokenManager, T as Tokens, U as UseApiConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, o as UseApiState, V as ValidationError, t } from './apiModule.types-0YqoHN1K.cjs';
|
|
4
|
+
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-CdbyfLNG.cjs';
|
|
5
5
|
import 'react';
|
|
6
6
|
|
|
7
7
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, Method, AxiosResponse, AxiosError } from 'axios';
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
4
|
-
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-
|
|
2
|
+
import { f as ApiClientConfig, A as ActionConfigModule, S as StandardResponse, g as ActionOptions, R as RequestConfig, h as ActionStateModule, Q as QueryOptions, e as UseApiQuery, i as ApiError, L as LogLevel } from './apiModule.types-0YqoHN1K.js';
|
|
3
|
+
export { n as ActionConfig, u as ActionMethods, p as ActionState, r as ActionsWithQuery, a as ApiModuleConfig, E as ExecutableAction, q as ExecuteOptions, I as InputOf, l as Middleware, k as MiddlewareContext, d as ModuleActions, s as ModuleQueries, M as ModuleStates, O as OutputOf, P as PaginationMeta, m as RefreshTokenConfig, j as TokenManager, T as Tokens, U as UseApiConfig, b as UseApiModuleOptions, c as UseApiModuleReturn, o as UseApiState, V as ValidationError, t } from './apiModule.types-0YqoHN1K.js';
|
|
4
|
+
export { c as UseApiRecordActions, U as UseApiRecordConfig, a as UseApiRecordReturn, b as UseApiRecordState } from './useApiRecord.types-Q5Ux9P1x.js';
|
|
5
5
|
import 'react';
|
|
6
6
|
|
|
7
7
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
package/dist/server.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-
|
|
2
|
+
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-0YqoHN1K.cjs';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
|
-
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-
|
|
2
|
+
import { A as ActionConfigModule, a as ApiModuleConfig, I as InputOf } from './apiModule.types-0YqoHN1K.js';
|
|
3
3
|
import 'react';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import { R as RequestConfig,
|
|
2
|
+
import { R as RequestConfig, i as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-0YqoHN1K.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Represents the internal state of the `useApiRecord` hook.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import { R as RequestConfig,
|
|
2
|
+
import { R as RequestConfig, i as ApiError, S as StandardResponse, g as ActionOptions } from './apiModule.types-0YqoHN1K.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Represents the internal state of the `useApiRecord` hook.
|