api-core-lib 12.0.14 → 12.0.16
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-Cvp9QdAc.d.cts → apiModule.types-D0LSWRW8.d.cts} +27 -1
- package/dist/{apiModule.types-Cvp9QdAc.d.ts → apiModule.types-D0LSWRW8.d.ts} +27 -1
- package/dist/client.cjs +101 -129
- package/dist/client.d.cts +2 -3
- package/dist/client.d.ts +2 -3
- package/dist/client.js +102 -128
- package/dist/index.cjs +6 -6
- package/dist/index.d.cts +54 -4
- package/dist/index.d.ts +54 -4
- package/dist/index.js +6 -6
- package/dist/server.cjs +6 -82
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +6 -82
- package/dist/{cacheKey-BJQaehcQ.d.ts → useApiRecord.types-C7T0gsX_.d.ts} +2 -52
- package/dist/{cacheKey-BNQ1ii6y.d.cts → useApiRecord.types-DXv1-ly6.d.cts} +2 -52
- package/package.json +1 -1
|
@@ -187,6 +187,10 @@ interface UseApiConfig<T> {
|
|
|
187
187
|
onSuccess?: (message: string, data?: T) => void;
|
|
188
188
|
onError?: (message: string, error?: ApiError) => void;
|
|
189
189
|
}
|
|
190
|
+
interface t {
|
|
191
|
+
type: string;
|
|
192
|
+
payload?: any;
|
|
193
|
+
}
|
|
190
194
|
|
|
191
195
|
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
192
196
|
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
@@ -199,11 +203,16 @@ interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
|
199
203
|
description?: string;
|
|
200
204
|
hasQuery?: boolean;
|
|
201
205
|
autoFetch?: boolean;
|
|
206
|
+
cacheResponse?: boolean;
|
|
202
207
|
invalidates?: string[];
|
|
203
208
|
_input?: TInput;
|
|
204
209
|
_output?: TOutput;
|
|
205
210
|
}
|
|
206
211
|
/** @description Defines the complete structure of an API module. */
|
|
212
|
+
/**
|
|
213
|
+
* يصف إعدادات موديول API كامل.
|
|
214
|
+
* @template TActions كائن يصف جميع الإجراءات المتاحة في الموديول.
|
|
215
|
+
*/
|
|
207
216
|
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
208
217
|
baseEndpoint: string;
|
|
209
218
|
actions: TActions;
|
|
@@ -254,5 +263,22 @@ interface UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<
|
|
|
254
263
|
};
|
|
255
264
|
dehydrate: () => string;
|
|
256
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* يصف دوال `execute` و `reset` لإجراء واحد.
|
|
268
|
+
* هذا النوع عام (generic) ليتناسب مع أنواع الإدخال والإخراج المختلفة لكل إجراء.
|
|
269
|
+
* @template TAction يمثل إعدادات الإجراء الواحد.
|
|
270
|
+
*/
|
|
271
|
+
type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
272
|
+
/**
|
|
273
|
+
* الدالة الأساسية لتنفيذ طلب ה-API.
|
|
274
|
+
*/
|
|
275
|
+
execute: <TContext = unknown>(input?: InputOf<TAction>, options?: ExecuteOptions<InputOf<TAction>, OutputOf<TAction>, TContext>) => Promise<StandardResponse<OutputOf<TAction>>>;
|
|
276
|
+
/**
|
|
277
|
+
* دالة لإعادة تعيين حالة إجراء معين في ذاكرة التخزين المؤقت إلى حالته الأولية.
|
|
278
|
+
*/
|
|
279
|
+
reset: (input?: InputOf<TAction>, options?: {
|
|
280
|
+
pathParams?: Record<string, any>;
|
|
281
|
+
}) => void;
|
|
282
|
+
};
|
|
257
283
|
|
|
258
|
-
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ApiClientConfig as d, ActionOptions as e,
|
|
284
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ApiClientConfig as d, ActionOptions as e, ActionStateModule as f, UseApiQuery as g, ApiError as h, TokenManager as i, Middleware as j, RefreshTokenConfig as k, ActionConfig as l, UseApiState as m, ActionState as n, ExecuteOptions as o, ModuleActions as p, ModuleStates as q, ActionMethods as r, t };
|
|
@@ -187,6 +187,10 @@ interface UseApiConfig<T> {
|
|
|
187
187
|
onSuccess?: (message: string, data?: T) => void;
|
|
188
188
|
onError?: (message: string, error?: ApiError) => void;
|
|
189
189
|
}
|
|
190
|
+
interface t {
|
|
191
|
+
type: string;
|
|
192
|
+
payload?: any;
|
|
193
|
+
}
|
|
190
194
|
|
|
191
195
|
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
192
196
|
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
@@ -199,11 +203,16 @@ interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
|
199
203
|
description?: string;
|
|
200
204
|
hasQuery?: boolean;
|
|
201
205
|
autoFetch?: boolean;
|
|
206
|
+
cacheResponse?: boolean;
|
|
202
207
|
invalidates?: string[];
|
|
203
208
|
_input?: TInput;
|
|
204
209
|
_output?: TOutput;
|
|
205
210
|
}
|
|
206
211
|
/** @description Defines the complete structure of an API module. */
|
|
212
|
+
/**
|
|
213
|
+
* يصف إعدادات موديول API كامل.
|
|
214
|
+
* @template TActions كائن يصف جميع الإجراءات المتاحة في الموديول.
|
|
215
|
+
*/
|
|
207
216
|
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
208
217
|
baseEndpoint: string;
|
|
209
218
|
actions: TActions;
|
|
@@ -254,5 +263,22 @@ interface UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<
|
|
|
254
263
|
};
|
|
255
264
|
dehydrate: () => string;
|
|
256
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* يصف دوال `execute` و `reset` لإجراء واحد.
|
|
268
|
+
* هذا النوع عام (generic) ليتناسب مع أنواع الإدخال والإخراج المختلفة لكل إجراء.
|
|
269
|
+
* @template TAction يمثل إعدادات الإجراء الواحد.
|
|
270
|
+
*/
|
|
271
|
+
type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
272
|
+
/**
|
|
273
|
+
* الدالة الأساسية لتنفيذ طلب ה-API.
|
|
274
|
+
*/
|
|
275
|
+
execute: <TContext = unknown>(input?: InputOf<TAction>, options?: ExecuteOptions<InputOf<TAction>, OutputOf<TAction>, TContext>) => Promise<StandardResponse<OutputOf<TAction>>>;
|
|
276
|
+
/**
|
|
277
|
+
* دالة لإعادة تعيين حالة إجراء معين في ذاكرة التخزين المؤقت إلى حالته الأولية.
|
|
278
|
+
*/
|
|
279
|
+
reset: (input?: InputOf<TAction>, options?: {
|
|
280
|
+
pathParams?: Record<string, any>;
|
|
281
|
+
}) => void;
|
|
282
|
+
};
|
|
257
283
|
|
|
258
|
-
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ApiClientConfig as d, ActionOptions as e,
|
|
284
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ApiClientConfig as d, ActionOptions as e, ActionStateModule as f, UseApiQuery as g, ApiError as h, TokenManager as i, Middleware as j, RefreshTokenConfig as k, ActionConfig as l, UseApiState as m, ActionState as n, ExecuteOptions as o, ModuleActions as p, ModuleStates as q, ActionMethods as r, t };
|
package/dist/client.cjs
CHANGED
|
@@ -34,8 +34,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
34
34
|
var client_exports = {};
|
|
35
35
|
__export(client_exports, {
|
|
36
36
|
ApiModuleProvider: () => ApiModuleProvider,
|
|
37
|
-
generateCacheKey: () => generateCacheKey,
|
|
38
|
-
globalStateManager: () => globalStateManager,
|
|
39
37
|
useApi: () => useApi,
|
|
40
38
|
useApiModule: () => useApiModule,
|
|
41
39
|
useApiRecord: () => useApiRecord,
|
|
@@ -614,14 +612,14 @@ var GlobalStateManager = class {
|
|
|
614
612
|
* يحدّث الحالة لمفتاح معين ويقوم بإعلام جميع المشتركين.
|
|
615
613
|
*/
|
|
616
614
|
setState(key, updater) {
|
|
617
|
-
const
|
|
615
|
+
const item = this.store.get(key);
|
|
616
|
+
const currentState = item.state;
|
|
618
617
|
const newState = updater(currentState);
|
|
619
|
-
if (
|
|
620
|
-
|
|
621
|
-
} else {
|
|
622
|
-
this.store.get(key).state = newState;
|
|
618
|
+
if (Object.is(currentState, newState)) {
|
|
619
|
+
return;
|
|
623
620
|
}
|
|
624
|
-
|
|
621
|
+
item.state = newState;
|
|
622
|
+
item.listeners.forEach((listener) => listener());
|
|
625
623
|
}
|
|
626
624
|
/**
|
|
627
625
|
* يجعل البيانات المرتبطة بمفتاح معين "قديمة" (stale).
|
|
@@ -679,27 +677,54 @@ var GlobalStateManager = class {
|
|
|
679
677
|
};
|
|
680
678
|
var globalStateManager = new GlobalStateManager();
|
|
681
679
|
|
|
680
|
+
// src/core/cacheKey.ts
|
|
681
|
+
var generateCacheKey = (moduleName, actionName, input, callOptions = {}) => {
|
|
682
|
+
const params = { path: callOptions.pathParams, body: input };
|
|
683
|
+
try {
|
|
684
|
+
return `${moduleName}/${actionName}::${JSON.stringify(params)}`;
|
|
685
|
+
} catch (error) {
|
|
686
|
+
console.warn("Could not stringify cache key params, falling back to timestamp.", { moduleName, actionName, error });
|
|
687
|
+
return `${moduleName}/${actionName}::${Date.now()}`;
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
682
691
|
// src/hooks/useApiModule/useApiModule.ts
|
|
683
692
|
var ApiModuleContext = (0, import_react4.createContext)(null);
|
|
684
693
|
var ApiModuleProvider = ApiModuleContext.Provider;
|
|
685
|
-
|
|
694
|
+
var createInitialState2 = () => ({
|
|
695
|
+
data: null,
|
|
696
|
+
lastSuccessAt: void 0,
|
|
697
|
+
meta: [],
|
|
698
|
+
validationErrors: [],
|
|
699
|
+
error: null,
|
|
700
|
+
loading: false,
|
|
701
|
+
success: false,
|
|
702
|
+
called: false,
|
|
703
|
+
isStale: false,
|
|
704
|
+
rawResponse: null
|
|
705
|
+
});
|
|
706
|
+
function useApiActionState(actionConfig, cacheKey, execute, input, enabled) {
|
|
686
707
|
const getClientSnapshot = () => globalStateManager.getSnapshot(cacheKey);
|
|
687
708
|
const getServerSnapshot = () => globalStateManager.getSnapshot(cacheKey);
|
|
688
709
|
const state = (0, import_react4.useSyncExternalStore)(
|
|
689
710
|
(callback) => globalStateManager.subscribe(cacheKey, callback),
|
|
690
711
|
getClientSnapshot,
|
|
691
|
-
// لقطة العميل
|
|
692
712
|
getServerSnapshot
|
|
693
|
-
// لقطة الخادم
|
|
694
713
|
);
|
|
695
|
-
const
|
|
714
|
+
const refetch = (0, import_react4.useCallback)(() => {
|
|
715
|
+
execute(input);
|
|
716
|
+
}, [execute, JSON.stringify(input)]);
|
|
717
|
+
const refetchRef = (0, import_react4.useRef)(refetch);
|
|
718
|
+
(0, import_react4.useEffect)(() => {
|
|
719
|
+
refetchRef.current = refetch;
|
|
720
|
+
}, [refetch]);
|
|
696
721
|
(0, import_react4.useEffect)(() => {
|
|
697
722
|
if (enabled && actionConfig.autoFetch && !state.called && !state.loading) {
|
|
698
|
-
|
|
723
|
+
refetchRef.current();
|
|
699
724
|
} else if (enabled && state.isStale && !state.loading) {
|
|
700
|
-
|
|
725
|
+
refetchRef.current();
|
|
701
726
|
}
|
|
702
|
-
}, [enabled, state.isStale, state.loading, state.called, actionConfig.autoFetch
|
|
727
|
+
}, [enabled, state.isStale, state.loading, state.called, actionConfig.autoFetch]);
|
|
703
728
|
return state;
|
|
704
729
|
}
|
|
705
730
|
function useModuleContext() {
|
|
@@ -722,29 +747,62 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
722
747
|
globalStateManager.rehydrate(hydratedState);
|
|
723
748
|
}
|
|
724
749
|
}, [hydratedState]);
|
|
750
|
+
(0, import_react4.useEffect)(() => {
|
|
751
|
+
savedCallbacks.current = { onSuccess, onError };
|
|
752
|
+
}, [onSuccess, onError]);
|
|
753
|
+
(0, import_react4.useMemo)(() => {
|
|
754
|
+
if (hydratedState) {
|
|
755
|
+
globalStateManager.rehydrate(hydratedState);
|
|
756
|
+
}
|
|
757
|
+
}, [hydratedState]);
|
|
725
758
|
const actions = (0, import_react4.useMemo)(() => {
|
|
726
759
|
return Object.keys(moduleConfig.actions).reduce((acc, actionName) => {
|
|
727
760
|
const actionConfig = moduleConfig.actions[actionName];
|
|
761
|
+
const shouldCache = actionConfig.cacheResponse !== false;
|
|
728
762
|
const execute = async (input, options2 = {}) => {
|
|
729
763
|
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
730
|
-
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
764
|
+
const cacheKey = shouldCache ? generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
|
|
731
765
|
let mutationContext;
|
|
732
766
|
try {
|
|
733
|
-
if (options2.onMutate) {
|
|
767
|
+
if (options2.onMutate && shouldCache) {
|
|
734
768
|
mutationContext = await options2.onMutate(input);
|
|
735
769
|
}
|
|
736
|
-
|
|
770
|
+
if (shouldCache) {
|
|
771
|
+
globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
|
|
772
|
+
}
|
|
737
773
|
const result = await callDynamicApi(axiosInstance, moduleConfig.baseEndpoint, actionConfig, {
|
|
738
774
|
pathParams: finalPathParams,
|
|
739
775
|
body: input,
|
|
740
776
|
config: options2.config
|
|
741
777
|
});
|
|
742
|
-
|
|
778
|
+
console.log("[API Result]", result);
|
|
779
|
+
if (shouldCache) {
|
|
780
|
+
globalStateManager.setState(cacheKey, (prev) => ({
|
|
781
|
+
...prev,
|
|
782
|
+
// احتفظ بالخصائص القديمة مثل 'called'
|
|
783
|
+
loading: false,
|
|
784
|
+
success: result.success,
|
|
785
|
+
// تحديث صريح
|
|
786
|
+
error: result.success ? null : result.error || prev.error,
|
|
787
|
+
// تحديث صريح
|
|
788
|
+
data: result.data,
|
|
789
|
+
// تحديث صريح للبيانات
|
|
790
|
+
meta: result.meta,
|
|
791
|
+
// تحديث صريح
|
|
792
|
+
links: result.links,
|
|
793
|
+
// تحديث صريح
|
|
794
|
+
message: result.message,
|
|
795
|
+
validationErrors: result.validationErrors || [],
|
|
796
|
+
rawResponse: result.rawResponse
|
|
797
|
+
// isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
|
|
798
|
+
}));
|
|
799
|
+
}
|
|
743
800
|
if (result.success) {
|
|
744
801
|
savedCallbacks.current.onSuccess?.(actionName, result.message || "Action successful", result.data);
|
|
745
802
|
options2.onSuccess?.(result.data, mutationContext);
|
|
746
803
|
actionConfig.invalidates?.forEach((keyToInvalidate) => {
|
|
747
804
|
const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
|
|
805
|
+
console.log(`[Invalidating] by prefix: ${prefix}`);
|
|
748
806
|
globalStateManager.invalidateByPrefix(prefix);
|
|
749
807
|
});
|
|
750
808
|
} else {
|
|
@@ -753,12 +811,13 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
753
811
|
}
|
|
754
812
|
return result;
|
|
755
813
|
} catch (error) {
|
|
756
|
-
const apiError = { status: 500, message: error.message };
|
|
757
|
-
|
|
758
|
-
|
|
814
|
+
const apiError = error.response?.data || { status: 500, message: error.message };
|
|
815
|
+
if (shouldCache) {
|
|
816
|
+
globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
|
|
817
|
+
}
|
|
759
818
|
savedCallbacks.current.onError?.(actionName, apiError.message, apiError);
|
|
760
819
|
options2.onError?.(apiError, mutationContext);
|
|
761
|
-
|
|
820
|
+
throw error;
|
|
762
821
|
} finally {
|
|
763
822
|
if (options2.onSettled) {
|
|
764
823
|
options2.onSettled();
|
|
@@ -766,17 +825,11 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
766
825
|
}
|
|
767
826
|
};
|
|
768
827
|
const reset = (input, options2 = {}) => {
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
loading: false,
|
|
775
|
-
success: false,
|
|
776
|
-
called: false,
|
|
777
|
-
isStale: false,
|
|
778
|
-
rawResponse: null
|
|
779
|
-
}));
|
|
828
|
+
if (shouldCache) {
|
|
829
|
+
const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
|
|
830
|
+
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
|
|
831
|
+
globalStateManager.setState(cacheKey, () => createInitialState2());
|
|
832
|
+
}
|
|
780
833
|
};
|
|
781
834
|
acc[actionName] = { execute, reset };
|
|
782
835
|
return acc;
|
|
@@ -808,31 +861,24 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
808
861
|
for (const actionName in moduleConfig.actions) {
|
|
809
862
|
if (Object.prototype.hasOwnProperty.call(moduleConfig.actions, actionName)) {
|
|
810
863
|
const actionConfig = moduleConfig.actions[actionName];
|
|
811
|
-
|
|
812
|
-
const input = actionConfig.hasQuery ? query : void 0;
|
|
813
|
-
const pathParams = JSON.parse(pathParamsString);
|
|
814
|
-
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams });
|
|
815
|
-
states[actionName] = useApiActionState(
|
|
816
|
-
actionConfig,
|
|
817
|
-
cacheKey,
|
|
818
|
-
actions[actionName].execute,
|
|
819
|
-
enabled
|
|
820
|
-
);
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
(0, import_react4.useEffect)(() => {
|
|
824
|
-
if (!enabled) return;
|
|
825
|
-
const actionKeys = Object.keys(moduleConfig.actions);
|
|
826
|
-
for (const actionName of actionKeys) {
|
|
827
|
-
const state = states[actionName];
|
|
828
|
-
const actionConfig = moduleConfig.actions[actionName];
|
|
829
|
-
if (actionConfig.autoFetch && state && !state.called && !state.loading) {
|
|
864
|
+
if (actionConfig.cacheResponse !== false) {
|
|
830
865
|
const query = queries[actionName]?.options;
|
|
831
866
|
const input = actionConfig.hasQuery ? query : void 0;
|
|
832
|
-
|
|
867
|
+
const pathParams = JSON.parse(pathParamsString);
|
|
868
|
+
const cacheKey = generateCacheKey(moduleConfig.baseEndpoint, actionName, input, { pathParams });
|
|
869
|
+
states[actionName] = useApiActionState(
|
|
870
|
+
actionConfig,
|
|
871
|
+
cacheKey,
|
|
872
|
+
// [تصحيح] مرر `execute` مباشرة
|
|
873
|
+
actions[actionName].execute,
|
|
874
|
+
input,
|
|
875
|
+
enabled
|
|
876
|
+
);
|
|
877
|
+
} else {
|
|
878
|
+
states[actionName] = createInitialState2();
|
|
833
879
|
}
|
|
834
880
|
}
|
|
835
|
-
}
|
|
881
|
+
}
|
|
836
882
|
const lastBlurTimestamp = (0, import_react4.useRef)(Date.now());
|
|
837
883
|
(0, import_react4.useEffect)(() => {
|
|
838
884
|
if (!enabled || !refetchOnWindowFocus) return;
|
|
@@ -863,83 +909,9 @@ function useApiModule(axiosInstance, moduleConfig, options = {}) {
|
|
|
863
909
|
}, []);
|
|
864
910
|
return { actions, states, queries, dehydrate };
|
|
865
911
|
}
|
|
866
|
-
|
|
867
|
-
// src/core/client.ts
|
|
868
|
-
var import_axios3 = __toESM(require("axios"), 1);
|
|
869
|
-
var import_uuid = require("uuid");
|
|
870
|
-
|
|
871
|
-
// src/core/cache.ts
|
|
872
|
-
var CacheManager = class {
|
|
873
|
-
constructor() {
|
|
874
|
-
__publicField(this, "cache", /* @__PURE__ */ new Map());
|
|
875
|
-
__publicField(this, "defaultDuration", 15 * 60 * 1e3);
|
|
876
|
-
}
|
|
877
|
-
// 15 minutes
|
|
878
|
-
set(key, data, duration) {
|
|
879
|
-
this.cache.set(key, {
|
|
880
|
-
data,
|
|
881
|
-
timestamp: Date.now(),
|
|
882
|
-
duration: duration || this.defaultDuration
|
|
883
|
-
});
|
|
884
|
-
}
|
|
885
|
-
get(key) {
|
|
886
|
-
const item = this.cache.get(key);
|
|
887
|
-
if (!item) return null;
|
|
888
|
-
const isExpired = Date.now() - item.timestamp > item.duration;
|
|
889
|
-
if (isExpired) {
|
|
890
|
-
this.cache.delete(key);
|
|
891
|
-
return null;
|
|
892
|
-
}
|
|
893
|
-
return item.data;
|
|
894
|
-
}
|
|
895
|
-
/**
|
|
896
|
-
* [FIX] تم تحويلها إلى دالة عامة (generic) لتعيد النوع الصحيح.
|
|
897
|
-
* الآن بدلًا من إرجاع CacheItem<unknown>، ستُرجع CacheItem<T>.
|
|
898
|
-
*/
|
|
899
|
-
getWithMeta(key) {
|
|
900
|
-
const item = this.cache.get(key);
|
|
901
|
-
if (!item) return null;
|
|
902
|
-
const isExpired = Date.now() - item.timestamp > item.duration;
|
|
903
|
-
if (isExpired) {
|
|
904
|
-
this.cache.delete(key);
|
|
905
|
-
return null;
|
|
906
|
-
}
|
|
907
|
-
return item;
|
|
908
|
-
}
|
|
909
|
-
delete(key) {
|
|
910
|
-
this.cache.delete(key);
|
|
911
|
-
}
|
|
912
|
-
clear() {
|
|
913
|
-
this.cache.clear();
|
|
914
|
-
}
|
|
915
|
-
invalidateByPrefix(prefix) {
|
|
916
|
-
const keysToDelete = [];
|
|
917
|
-
for (const key of this.cache.keys()) {
|
|
918
|
-
if (key.startsWith(prefix)) {
|
|
919
|
-
keysToDelete.push(key);
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
keysToDelete.forEach((key) => this.cache.delete(key));
|
|
923
|
-
console.log(`Invalidated ${keysToDelete.length} cache entries with prefix: ${prefix}`);
|
|
924
|
-
}
|
|
925
|
-
};
|
|
926
|
-
var cacheManager = new CacheManager();
|
|
927
|
-
|
|
928
|
-
// src/core/cacheKey.ts
|
|
929
|
-
var generateCacheKey = (moduleName, actionName, input, callOptions = {}) => {
|
|
930
|
-
const params = { path: callOptions.pathParams, body: input };
|
|
931
|
-
try {
|
|
932
|
-
return `${moduleName}/${actionName}::${JSON.stringify(params)}`;
|
|
933
|
-
} catch (error) {
|
|
934
|
-
console.warn("Could not stringify cache key params, falling back to timestamp.", { moduleName, actionName, error });
|
|
935
|
-
return `${moduleName}/${actionName}::${Date.now()}`;
|
|
936
|
-
}
|
|
937
|
-
};
|
|
938
912
|
// Annotate the CommonJS export names for ESM import in node:
|
|
939
913
|
0 && (module.exports = {
|
|
940
914
|
ApiModuleProvider,
|
|
941
|
-
generateCacheKey,
|
|
942
|
-
globalStateManager,
|
|
943
915
|
useApi,
|
|
944
916
|
useApiModule,
|
|
945
917
|
useApiRecord,
|
package/dist/client.d.cts
CHANGED
|
@@ -1,7 +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 } from './apiModule.types-
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './
|
|
4
|
-
export { b as generateCacheKey, g as globalStateManager } from './cacheKey-BNQ1ii6y.cjs';
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn } from './apiModule.types-D0LSWRW8.cjs';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-DXv1-ly6.cjs';
|
|
5
4
|
import * as React from 'react';
|
|
6
5
|
|
|
7
6
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +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 } from './apiModule.types-
|
|
3
|
-
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './
|
|
4
|
-
export { b as generateCacheKey, g as globalStateManager } from './cacheKey-BJQaehcQ.js';
|
|
2
|
+
import { U as UseApiConfig, A as ActionConfigModule, a as ApiModuleConfig, b as UseApiModuleOptions, c as UseApiModuleReturn } from './apiModule.types-D0LSWRW8.js';
|
|
3
|
+
import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-C7T0gsX_.js';
|
|
5
4
|
import * as React from 'react';
|
|
6
5
|
|
|
7
6
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
|