api-core-lib 12.12.4 → 12.12.101

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.
@@ -0,0 +1,547 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }"use client";
2
+
3
+
4
+
5
+
6
+
7
+
8
+ var _chunk25UFVV4Fcjs = require('./chunk-25UFVV4F.cjs');
9
+
10
+ // src/hooks/useApi.ts
11
+ var _react = require('react');
12
+ function buildDynamicPath(template, params) {
13
+ if (!params) return template;
14
+ let path = template;
15
+ for (const key in params) {
16
+ path = path.replace(new RegExp(`{${key}}`, "g"), String(params[key]));
17
+ }
18
+ return path;
19
+ }
20
+ function useApi(axiosInstance, config) {
21
+ const {
22
+ endpoint,
23
+ pathParams,
24
+ // [REMOVE] لم نعد بحاجة لهذا الخيار
25
+ // encodeQuery = true,
26
+ initialData,
27
+ initialQuery = {},
28
+ enabled = true,
29
+ refetchAfterChange = true,
30
+ requestConfig,
31
+ onSuccess,
32
+ onError
33
+ } = config;
34
+ const finalEndpoint = _react.useMemo.call(void 0,
35
+ () => buildDynamicPath(endpoint, pathParams),
36
+ [endpoint, pathParams]
37
+ );
38
+ const [state, setState] = _react.useState.call(void 0, {
39
+ data: _nullishCoalesce(initialData, () => ( null)),
40
+ rawResponse: null,
41
+ loading: enabled,
42
+ error: null,
43
+ success: false,
44
+ message: void 0,
45
+ validationErrors: void 0
46
+ });
47
+ const [queryOptions, setQueryOptions] = _react.useState.call(void 0, initialQuery);
48
+ const apiServices = _react.useMemo.call(void 0,
49
+ () => _chunk25UFVV4Fcjs.createApiServices.call(void 0, axiosInstance, finalEndpoint),
50
+ [axiosInstance, finalEndpoint]
51
+ );
52
+ const savedOnSuccess = _react.useRef.call(void 0, onSuccess);
53
+ const savedOnError = _react.useRef.call(void 0, onError);
54
+ _react.useEffect.call(void 0, () => {
55
+ savedOnSuccess.current = onSuccess;
56
+ savedOnError.current = onError;
57
+ }, [onSuccess, onError]);
58
+ const fetchData = _react.useCallback.call(void 0, async (currentQueryOptions) => {
59
+ setState((prev) => ({ ...prev, loading: true, error: null }));
60
+ try {
61
+ const finalQuery = currentQueryOptions || queryOptions;
62
+ const hasQueryParams = Object.keys(finalQuery).length > 0;
63
+ const queryString = _chunk25UFVV4Fcjs.buildPaginateQuery.call(void 0, finalQuery);
64
+ const result = hasQueryParams ? await apiServices.getWithQuery(queryString, requestConfig) : await apiServices.get(void 0, requestConfig);
65
+ setState(result);
66
+ if (result.success && savedOnSuccess.current) {
67
+ savedOnSuccess.current(result.message || "Fetch successful!", _nullishCoalesce(result.data, () => ( void 0)));
68
+ } else if (!result.success && savedOnError.current) {
69
+ savedOnError.current(result.message || "Fetch failed", _nullishCoalesce(result.error, () => ( void 0)));
70
+ }
71
+ } catch (e) {
72
+ const apiError = { status: 500, message: e.message || "An unexpected client-side error occurred." };
73
+ setState((prev) => ({ ...prev, loading: false, error: apiError, success: false }));
74
+ if (savedOnError.current) {
75
+ savedOnError.current(apiError.message, apiError);
76
+ }
77
+ }
78
+ }, [apiServices, queryOptions, requestConfig]);
79
+ _react.useEffect.call(void 0, () => {
80
+ if (enabled) {
81
+ fetchData();
82
+ }
83
+ }, [enabled, fetchData]);
84
+ const handleActionResult = _react.useCallback.call(void 0, async (actionPromise) => {
85
+ setState((prev) => ({ ...prev, loading: true }));
86
+ try {
87
+ const result = await actionPromise;
88
+ if (result.success) {
89
+ if (savedOnSuccess.current) {
90
+ savedOnSuccess.current(result.message || "Action successful!", _nullishCoalesce(result.data, () => ( void 0)));
91
+ }
92
+ if (refetchAfterChange) {
93
+ await fetchData(queryOptions);
94
+ } else {
95
+ setState((prev) => ({ ...prev, ...result, loading: false }));
96
+ }
97
+ } else {
98
+ if (savedOnError.current) {
99
+ savedOnError.current(result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)));
100
+ }
101
+ setState((prev) => ({ ...prev, loading: false, error: result.error, success: false }));
102
+ }
103
+ return result;
104
+ } catch (e) {
105
+ const apiError = { status: 500, message: e.message || "An unexpected error occurred during action." };
106
+ setState((prev) => ({ ...prev, loading: false, error: apiError, success: false }));
107
+ if (savedOnError.current) {
108
+ savedOnError.current(apiError.message, apiError);
109
+ }
110
+ return { data: null, error: apiError, loading: false, success: false, rawResponse: e };
111
+ }
112
+ }, [refetchAfterChange, fetchData, queryOptions]);
113
+ const actions = {
114
+ fetch: fetchData,
115
+ create: (newItem, options) => handleActionResult(apiServices.post(newItem, options)),
116
+ put: (id, item, options) => handleActionResult(apiServices.put(id, item, options)),
117
+ update: (id, updatedItem, options) => handleActionResult(apiServices.patch(id, updatedItem, options)),
118
+ remove: (id, options) => handleActionResult(apiServices.remove(id, options)),
119
+ bulkRemove: (ids, options) => handleActionResult(apiServices.bulkDelete(ids, options))
120
+ };
121
+ const query = {
122
+ options: queryOptions,
123
+ setOptions: setQueryOptions,
124
+ setPage: (page) => setQueryOptions((prev) => ({ ...prev, page })),
125
+ setLimit: (limit) => setQueryOptions((prev) => ({ ...prev, page: 1, limit })),
126
+ setSearchTerm: (search) => setQueryOptions((prev) => ({ ...prev, page: 1, search })),
127
+ setSorting: (sortBy) => setQueryOptions((prev) => ({ ...prev, sortBy })),
128
+ setFilters: (filter) => setQueryOptions((prev) => ({ ...prev, page: 1, filter })),
129
+ setQueryParam: (key, value) => setQueryOptions((prev) => ({ ...prev, [key]: value, page: key !== "page" ? 1 : value })),
130
+ reset: () => setQueryOptions(initialQuery)
131
+ };
132
+ return { state, setState, actions, query };
133
+ }
134
+
135
+ // src/hooks/useApiRecord/index.ts
136
+
137
+ function buildDynamicPath2(template, params) {
138
+ if (!params) return template;
139
+ let path = template;
140
+ for (const key in params) {
141
+ path = path.replace(new RegExp(`{${key}}`, "g"), String(params[key]));
142
+ }
143
+ return path;
144
+ }
145
+ function useApiRecord(axiosInstance, config) {
146
+ const {
147
+ endpoint,
148
+ pathParams,
149
+ recordId,
150
+ initialData,
151
+ enabled = true,
152
+ refetchAfterChange = true,
153
+ requestConfig,
154
+ onSuccess,
155
+ onError
156
+ } = config;
157
+ const finalEndpoint = _react.useMemo.call(void 0,
158
+ () => buildDynamicPath2(endpoint, pathParams),
159
+ [endpoint, pathParams]
160
+ );
161
+ const initialState = _react.useMemo.call(void 0, () => ({
162
+ data: _nullishCoalesce(initialData, () => ( null)),
163
+ rawResponse: null,
164
+ error: null,
165
+ loading: enabled && !!recordId,
166
+ success: false,
167
+ message: void 0,
168
+ validationErrors: void 0
169
+ }), [initialData, enabled, recordId]);
170
+ const [state, setState] = _react.useState.call(void 0, initialState);
171
+ const savedOnSuccess = _react.useRef.call(void 0, onSuccess);
172
+ const savedOnError = _react.useRef.call(void 0, onError);
173
+ _react.useEffect.call(void 0, () => {
174
+ savedOnSuccess.current = onSuccess;
175
+ savedOnError.current = onError;
176
+ }, [onSuccess, onError]);
177
+ const apiServices = _react.useMemo.call(void 0,
178
+ () => _chunk25UFVV4Fcjs.createApiServices.call(void 0, axiosInstance, finalEndpoint),
179
+ [axiosInstance, finalEndpoint]
180
+ );
181
+ const fetchRecord = _react.useCallback.call(void 0, async () => {
182
+ if (!recordId) {
183
+ setState(initialState);
184
+ return;
185
+ }
186
+ setState((prev) => ({ ...prev, loading: true, error: null, success: false }));
187
+ try {
188
+ const result = await apiServices.get(recordId, requestConfig);
189
+ setState(result);
190
+ if (result.success && savedOnSuccess.current) {
191
+ savedOnSuccess.current(result.message || "Record fetched successfully!", _nullishCoalesce(result.data, () => ( void 0)));
192
+ } else if (!result.success && savedOnError.current) {
193
+ savedOnError.current(result.message || "Fetch failed", _nullishCoalesce(result.error, () => ( void 0)));
194
+ }
195
+ } catch (e) {
196
+ console.error("[useApiRecord Fetch Error]", e);
197
+ const apiError = {
198
+ status: _optionalChain([e, 'access', _ => _.response, 'optionalAccess', _2 => _2.status]) || 500,
199
+ message: e.message || "An unexpected client-side error occurred.",
200
+ code: e.code
201
+ };
202
+ setState((prev) => ({ ...prev, loading: false, success: false, error: apiError }));
203
+ if (savedOnError.current) {
204
+ savedOnError.current(apiError.message, apiError);
205
+ }
206
+ }
207
+ }, [apiServices, recordId, requestConfig, initialState]);
208
+ _react.useEffect.call(void 0, () => {
209
+ if (enabled) {
210
+ fetchRecord();
211
+ }
212
+ }, [enabled, fetchRecord]);
213
+ const handleActionResult = _react.useCallback.call(void 0,
214
+ async (actionPromise, options) => {
215
+ setState((prev) => ({ ...prev, loading: true }));
216
+ try {
217
+ const result = await actionPromise;
218
+ if (result.success) {
219
+ if (savedOnSuccess.current) {
220
+ savedOnSuccess.current(result.message || "Action successful!", result.data);
221
+ }
222
+ const shouldRefetch = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.refetch]), () => ( refetchAfterChange));
223
+ if (shouldRefetch) {
224
+ await fetchRecord();
225
+ } else {
226
+ setState({ ...result, loading: false, data: result.data });
227
+ }
228
+ } else {
229
+ if (savedOnError.current) {
230
+ savedOnError.current(result.message || "Action failed", _nullishCoalesce(result.error, () => ( void 0)));
231
+ }
232
+ setState((prev) => ({ ...prev, ...result, loading: false }));
233
+ }
234
+ return result;
235
+ } catch (e) {
236
+ console.error("[useApiRecord Action Error]", e);
237
+ const apiError = {
238
+ status: _optionalChain([e, 'access', _4 => _4.response, 'optionalAccess', _5 => _5.status]) || 500,
239
+ message: e.message || "An unexpected error occurred during the action.",
240
+ code: e.code
241
+ };
242
+ const errorResponse = { ...initialState, loading: false, success: false, error: apiError, rawResponse: e, data: null };
243
+ setState(errorResponse);
244
+ if (savedOnError.current) {
245
+ savedOnError.current(apiError.message, apiError);
246
+ }
247
+ return errorResponse;
248
+ }
249
+ },
250
+ [refetchAfterChange, fetchRecord, initialState]
251
+ );
252
+ const actions = {
253
+ fetch: fetchRecord,
254
+ update: (updatedItem, options) => handleActionResult(apiServices.patch(recordId, updatedItem, { ...requestConfig, ...options }), options),
255
+ put: (item, options) => handleActionResult(apiServices.put(recordId, item, { ...requestConfig, ...options }), options),
256
+ remove: (options) => handleActionResult(apiServices.remove(recordId, { ...requestConfig, ...options }), options),
257
+ resetState: () => setState(initialState)
258
+ };
259
+ return { state, setState, actions };
260
+ }
261
+
262
+ // src/hooks/useDeepCompareEffect/index.ts
263
+
264
+ var _fastdeepequal = require('fast-deep-equal'); var _fastdeepequal2 = _interopRequireDefault(_fastdeepequal);
265
+ function useDeepCompareEffect(callback, dependencies) {
266
+ const currentDependenciesRef = _react.useRef.call(void 0, );
267
+ if (!_fastdeepequal2.default.call(void 0, currentDependenciesRef.current, dependencies)) {
268
+ currentDependenciesRef.current = dependencies;
269
+ }
270
+ _react.useEffect.call(void 0, callback, [currentDependenciesRef.current]);
271
+ }
272
+
273
+ // src/hooks/useApiModule/useApiModule.ts
274
+
275
+ var ApiModuleContext = _react.createContext.call(void 0, null);
276
+ var ApiModuleProvider = ApiModuleContext.Provider;
277
+ var createInitialState = () => ({
278
+ data: null,
279
+ lastSuccessAt: void 0,
280
+ meta: [],
281
+ validationErrors: [],
282
+ error: null,
283
+ loading: false,
284
+ success: false,
285
+ called: false,
286
+ isStale: false,
287
+ rawResponse: null
288
+ });
289
+ function useApiActionState(actionConfig, cacheKey, execute, input, enabled) {
290
+ const getClientSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
291
+ const getServerSnapshot = () => _chunk25UFVV4Fcjs.globalStateManager.getSnapshot(cacheKey);
292
+ const state = _react.useSyncExternalStore.call(void 0,
293
+ (callback) => _chunk25UFVV4Fcjs.globalStateManager.subscribe(cacheKey, callback),
294
+ getClientSnapshot,
295
+ getServerSnapshot
296
+ );
297
+ const inputRef = _react.useRef.call(void 0, input);
298
+ _react.useEffect.call(void 0, () => {
299
+ inputRef.current = input;
300
+ }, [input]);
301
+ const refetch = _react.useCallback.call(void 0, () => {
302
+ execute(inputRef.current);
303
+ }, [execute]);
304
+ const prevCacheKeyRef = _react.useRef.call(void 0, cacheKey);
305
+ _react.useEffect.call(void 0, () => {
306
+ if (prevCacheKeyRef.current !== cacheKey && enabled && state.called) {
307
+ console.log(`[Cache Key Changed] from ${prevCacheKeyRef.current} to ${cacheKey}. Refetching...`);
308
+ refetch();
309
+ } else if (enabled && actionConfig.autoFetch && !state.called && !state.loading) {
310
+ console.log(`[Auto Fetch] for ${cacheKey}. Fetching...`);
311
+ refetch();
312
+ } else if (enabled && state.isStale && !state.loading) {
313
+ console.log(`[Stale State] for ${cacheKey}. Refetching...`);
314
+ refetch();
315
+ }
316
+ prevCacheKeyRef.current = cacheKey;
317
+ }, [cacheKey, enabled, state.isStale, state.loading, state.called, actionConfig.autoFetch, refetch]);
318
+ return state;
319
+ }
320
+ function useApiModule(axiosInstance, moduleConfig, options = {}) {
321
+ const {
322
+ refetchOnWindowFocus = true,
323
+ onSuccess,
324
+ onError,
325
+ pathParams: modulePathParams,
326
+ enabled = true,
327
+ hydratedState,
328
+ extraContextData
329
+ } = options;
330
+ const pathParamsString = _react.useMemo.call(void 0, () => JSON.stringify(modulePathParams || {}), [modulePathParams]);
331
+ const [queryOptions, setQueryOptions] = _react.useState.call(void 0, {});
332
+ const savedCallbacks = _react.useRef.call(void 0, { onSuccess, onError });
333
+ _react.useEffect.call(void 0, () => {
334
+ savedCallbacks.current = { onSuccess, onError };
335
+ }, [onSuccess, onError]);
336
+ _react.useMemo.call(void 0, () => {
337
+ if (hydratedState) {
338
+ _chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
339
+ }
340
+ }, [hydratedState]);
341
+ _react.useEffect.call(void 0, () => {
342
+ savedCallbacks.current = { onSuccess, onError };
343
+ }, [onSuccess, onError]);
344
+ _react.useMemo.call(void 0, () => {
345
+ if (hydratedState) {
346
+ _chunk25UFVV4Fcjs.globalStateManager.rehydrate(hydratedState);
347
+ }
348
+ }, [hydratedState]);
349
+ const actions = _react.useMemo.call(void 0, () => {
350
+ return Object.keys(moduleConfig.actions).reduce((acc, actionName) => {
351
+ const actionConfig = moduleConfig.actions[actionName];
352
+ const shouldCache = actionConfig.cacheResponse !== false;
353
+ const execute = async (input, options2 = {}) => {
354
+ const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
355
+ const cacheKey = shouldCache ? _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams }) : "";
356
+ let mutationContext;
357
+ try {
358
+ if (options2.onMutate && shouldCache) {
359
+ mutationContext = await options2.onMutate(input);
360
+ }
361
+ if (shouldCache) {
362
+ _chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, loading: true, called: true, error: null, isStale: false }));
363
+ }
364
+ const result = await _chunk25UFVV4Fcjs.callDynamicApi.call(void 0, axiosInstance, moduleConfig.baseEndpoint, actionConfig, {
365
+ pathParams: finalPathParams,
366
+ body: input,
367
+ config: options2.config
368
+ });
369
+ console.log("[API Result execute]", result);
370
+ if (shouldCache) {
371
+ _chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({
372
+ ...prev,
373
+ // احتفظ بالخصائص القديمة مثل 'called'
374
+ loading: false,
375
+ success: result.success,
376
+ // تحديث صريح
377
+ error: result.success ? null : result.error || prev.error,
378
+ // تحديث صريح
379
+ data: result.data,
380
+ // تحديث صريح للبيانات
381
+ meta: result.meta,
382
+ // تحديث صريح
383
+ links: result.links,
384
+ // تحديث صريح
385
+ message: result.message,
386
+ validationErrors: result.validationErrors || [],
387
+ rawResponse: result.rawResponse
388
+ // isStale تم تعيينه إلى false في بداية execute، وسيظل كذلك
389
+ }));
390
+ }
391
+ if (result.success) {
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) => {
395
+ const prefix = `${moduleConfig.baseEndpoint}/${keyToInvalidate}::`;
396
+ console.log(`[Invalidating] by prefix: ${prefix}`);
397
+ _chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
398
+ })]);
399
+ } else {
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)]);
402
+ }
403
+ return result;
404
+ } catch (error) {
405
+ const apiError = _optionalChain([error, 'access', _19 => _19.response, 'optionalAccess', _20 => _20.data]) || { status: 500, message: error.message };
406
+ if (shouldCache) {
407
+ _chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, (prev) => ({ ...prev, error: apiError, loading: false, success: false }));
408
+ }
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)]);
411
+ throw error;
412
+ } finally {
413
+ if (options2.onSettled) {
414
+ options2.onSettled();
415
+ }
416
+ }
417
+ };
418
+ const reset = (input, options2 = {}) => {
419
+ if (shouldCache) {
420
+ const finalPathParams = { ...JSON.parse(pathParamsString), ...options2.pathParams };
421
+ const cacheKey = _chunk25UFVV4Fcjs.generateCacheKey.call(void 0, moduleConfig.baseEndpoint, actionName, input, { pathParams: finalPathParams });
422
+ _chunk25UFVV4Fcjs.globalStateManager.setState(cacheKey, () => createInitialState());
423
+ }
424
+ };
425
+ acc[actionName] = { execute, reset };
426
+ return acc;
427
+ }, {});
428
+ }, [axiosInstance, moduleConfig, pathParamsString]);
429
+ const queries = _react.useMemo.call(void 0, () => {
430
+ const builtQueries = {};
431
+ for (const actionName in moduleConfig.actions) {
432
+ if (_optionalChain([moduleConfig, 'access', _26 => _26.actions, 'access', _27 => _27[actionName], 'optionalAccess', _28 => _28.hasQuery])) {
433
+ const setActionQueryOptions = (updater) => {
434
+ setQueryOptions((prev) => ({ ...prev, [actionName]: typeof updater === "function" ? updater(prev[actionName] || {}) : updater }));
435
+ };
436
+ builtQueries[actionName] = {
437
+ options: queryOptions[actionName] || {},
438
+ setOptions: setActionQueryOptions,
439
+ setPage: (page) => setActionQueryOptions((p) => ({ ...p, page })),
440
+ setLimit: (limit) => setActionQueryOptions((p) => ({ ...p, limit, page: 1 })),
441
+ setSearchTerm: (search) => setActionQueryOptions((p) => ({ ...p, search, page: 1 })),
442
+ setFilters: (filter) => setActionQueryOptions((p) => ({ ...p, filter, page: 1 })),
443
+ setSorting: (sortBy) => setActionQueryOptions((p) => ({ ...p, sortBy })),
444
+ setQueryParam: (key, value) => setActionQueryOptions((p) => ({ ...p, [key]: value, page: key !== "page" ? value : p.page })),
445
+ reset: () => setActionQueryOptions({})
446
+ };
447
+ }
448
+ }
449
+ return builtQueries;
450
+ }, [queryOptions, moduleConfig.actions]);
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]);
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();
480
+ }
481
+ }
482
+ }
483
+ const lastBlurTimestamp = _react.useRef.call(void 0, Date.now());
484
+ _react.useEffect.call(void 0, () => {
485
+ if (!enabled || !refetchOnWindowFocus) return;
486
+ const onFocus = () => {
487
+ if (Date.now() - lastBlurTimestamp.current > 1e4) {
488
+ const actionKeys = Object.keys(moduleConfig.actions);
489
+ for (const actionName of actionKeys) {
490
+ const state = states[actionName];
491
+ if (state && state.called && !state.loading) {
492
+ const prefix = `${moduleConfig.baseEndpoint}/${actionName}::`;
493
+ _chunk25UFVV4Fcjs.globalStateManager.invalidateByPrefix(prefix);
494
+ }
495
+ }
496
+ }
497
+ };
498
+ const onBlur = () => {
499
+ lastBlurTimestamp.current = Date.now();
500
+ };
501
+ window.addEventListener("focus", onFocus);
502
+ window.addEventListener("blur", onBlur);
503
+ return () => {
504
+ window.removeEventListener("focus", onFocus);
505
+ window.removeEventListener("blur", onBlur);
506
+ };
507
+ }, [enabled, refetchOnWindowFocus, moduleConfig, states]);
508
+ const dehydrate = _react.useMemo.call(void 0, () => {
509
+ return () => _chunk25UFVV4Fcjs.globalStateManager.dehydrate();
510
+ }, []);
511
+ const baseApiReturn = { actions, states, queries, dehydrate };
512
+ const finalApiReturn = _react.useMemo.call(void 0, () => ({
513
+ ...baseApiReturn,
514
+ ...extraContextData || {}
515
+ }), [baseApiReturn, extraContextData]);
516
+ return finalApiReturn;
517
+ }
518
+
519
+ // src/hooks/useApiModule/apiModuleContext.ts
520
+
521
+ function createApiModuleContext() {
522
+ const Context = _react.createContext.call(void 0, null);
523
+ Context.displayName = "ApiModuleContext";
524
+ const Provider = ({ value, children }) => {
525
+ return _react.createElement.call(void 0, Context.Provider, { value }, children);
526
+ };
527
+ const useConsumer = () => {
528
+ const context = _react.useContext.call(void 0, Context);
529
+ if (!context) {
530
+ throw new Error("This component must be used within its corresponding ApiModuleProvider.");
531
+ }
532
+ return context;
533
+ };
534
+ return {
535
+ Context,
536
+ Provider,
537
+ useContext: useConsumer
538
+ };
539
+ }
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+ exports.ApiModuleProvider = ApiModuleProvider; exports.createApiModuleContext = createApiModuleContext; exports.useApi = useApi; exports.useApiModule = useApiModule; exports.useApiRecord = useApiRecord; exports.useDeepCompareEffect = useDeepCompareEffect;
@@ -0,0 +1,50 @@
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 ModuleQueries } from './apiModule.types-dbmnauAt.cjs';
3
+ import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-HrabvzEQ.cjs';
4
+ import * as react from 'react';
5
+ import { ReactNode } from 'react';
6
+
7
+ declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
8
+
9
+ /**
10
+ * A React hook for managing the lifecycle of a single API resource (a record).
11
+ * It handles fetching, updating, replacing, and deleting a record, while managing
12
+ * loading, error, and data states. It supports dynamic path parameters for flexible API routing.
13
+ *
14
+ * @template T The data type of the record being managed.
15
+ * @param {AxiosInstance} axiosInstance - The configured Axios instance for making API calls.
16
+ * @param {UseApiRecordConfig<T>} config - Configuration options for the hook.
17
+ * @returns {UseApiRecordReturn<T>} An object containing the state, actions, and setState function.
18
+ */
19
+ declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
20
+
21
+ type EffectCallback = () => (void | (() => void));
22
+ type DependencyList = readonly any[];
23
+ declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
24
+
25
+ declare const ApiModuleProvider: react.Provider<{
26
+ states: ModuleStates<any>;
27
+ actions: ModuleActions<any>;
28
+ queries: ModuleQueries<any>;
29
+ dehydrate: () => string;
30
+ } | null>;
31
+ declare function useApiModule<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}>(axiosInstance: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>, options?: UseApiModuleOptions<TExtra>): UseApiModuleReturn<TActions, TExtra>;
32
+
33
+ /**
34
+ * Creates a strongly-typed React Context, Provider, and consumer hook for a specific API module.
35
+ * This pattern avoids the need for manual type assertions in consuming components.
36
+ *
37
+ * @template TActions - The shape of the actions in the API module.
38
+ * @template TExtra - The shape of any extra data passed via `extraContextData`.
39
+ * @returns An object containing the typed Context, Provider, and a `useContext` hook.
40
+ */
41
+ declare function createApiModuleContext<TActions extends Record<string, any>, TExtra extends object = {}>(): {
42
+ Context: react.Context<UseApiModuleReturn<TActions, TExtra> | null>;
43
+ Provider: ({ value, children }: {
44
+ value: UseApiModuleReturn<TActions, TExtra>;
45
+ children: ReactNode;
46
+ }) => react.FunctionComponentElement<react.ProviderProps<UseApiModuleReturn<TActions, TExtra> | null>>;
47
+ useContext: () => UseApiModuleReturn<TActions, TExtra>;
48
+ };
49
+
50
+ export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect };
@@ -0,0 +1,50 @@
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 ModuleQueries } from './apiModule.types-dbmnauAt.js';
3
+ import { U as UseApiRecordConfig, a as UseApiRecordReturn } from './useApiRecord.types-C06B5p4U.js';
4
+ import * as react from 'react';
5
+ import { ReactNode } from 'react';
6
+
7
+ declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): any;
8
+
9
+ /**
10
+ * A React hook for managing the lifecycle of a single API resource (a record).
11
+ * It handles fetching, updating, replacing, and deleting a record, while managing
12
+ * loading, error, and data states. It supports dynamic path parameters for flexible API routing.
13
+ *
14
+ * @template T The data type of the record being managed.
15
+ * @param {AxiosInstance} axiosInstance - The configured Axios instance for making API calls.
16
+ * @param {UseApiRecordConfig<T>} config - Configuration options for the hook.
17
+ * @returns {UseApiRecordReturn<T>} An object containing the state, actions, and setState function.
18
+ */
19
+ declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
20
+
21
+ type EffectCallback = () => (void | (() => void));
22
+ type DependencyList = readonly any[];
23
+ declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
24
+
25
+ declare const ApiModuleProvider: react.Provider<{
26
+ states: ModuleStates<any>;
27
+ actions: ModuleActions<any>;
28
+ queries: ModuleQueries<any>;
29
+ dehydrate: () => string;
30
+ } | null>;
31
+ declare function useApiModule<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}>(axiosInstance: AxiosInstance, moduleConfig: ApiModuleConfig<TActions>, options?: UseApiModuleOptions<TExtra>): UseApiModuleReturn<TActions, TExtra>;
32
+
33
+ /**
34
+ * Creates a strongly-typed React Context, Provider, and consumer hook for a specific API module.
35
+ * This pattern avoids the need for manual type assertions in consuming components.
36
+ *
37
+ * @template TActions - The shape of the actions in the API module.
38
+ * @template TExtra - The shape of any extra data passed via `extraContextData`.
39
+ * @returns An object containing the typed Context, Provider, and a `useContext` hook.
40
+ */
41
+ declare function createApiModuleContext<TActions extends Record<string, any>, TExtra extends object = {}>(): {
42
+ Context: react.Context<UseApiModuleReturn<TActions, TExtra> | null>;
43
+ Provider: ({ value, children }: {
44
+ value: UseApiModuleReturn<TActions, TExtra>;
45
+ children: ReactNode;
46
+ }) => react.FunctionComponentElement<react.ProviderProps<UseApiModuleReturn<TActions, TExtra> | null>>;
47
+ useContext: () => UseApiModuleReturn<TActions, TExtra>;
48
+ };
49
+
50
+ export { ApiModuleProvider, createApiModuleContext, useApi, useApiModule, useApiRecord, useDeepCompareEffect };