api-core-lib 11.9.8 → 11.10.9
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/index.d.mts +14 -115
- package/dist/index.d.ts +14 -115
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +5 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method } from 'axios';
|
|
2
2
|
|
|
3
|
-
interface Logger {
|
|
4
|
-
debug: (message: string, ...args: any[]) => void;
|
|
5
|
-
info: (message: string, ...args: any[]) => void;
|
|
6
|
-
warn: (message: string, ...args: any[]) => void;
|
|
7
|
-
error: (message: string, ...args: any[]) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
3
|
/**
|
|
11
4
|
* @file src/types.ts
|
|
12
5
|
* @description This file serves as the central source for all data types and interfaces
|
|
@@ -95,7 +88,6 @@ interface MiddlewareContext {
|
|
|
95
88
|
req: InternalAxiosRequestConfig;
|
|
96
89
|
res?: AxiosResponse;
|
|
97
90
|
error?: any;
|
|
98
|
-
logger: Logger;
|
|
99
91
|
custom?: Record<string, any>;
|
|
100
92
|
}
|
|
101
93
|
/**
|
|
@@ -223,7 +215,6 @@ interface ApiClientConfig {
|
|
|
223
215
|
/** A callback function executed if the token refresh process fails. */
|
|
224
216
|
onRefreshError?: (error: any) => void;
|
|
225
217
|
/** A custom logger instance. Defaults to the browser `console`. */
|
|
226
|
-
logger?: Logger;
|
|
227
218
|
/** An array of middleware functions to be executed with every request. */
|
|
228
219
|
middleware?: Middleware[];
|
|
229
220
|
/**
|
|
@@ -311,144 +302,52 @@ interface UseApiReturn<T> {
|
|
|
311
302
|
query: UseApiQuery;
|
|
312
303
|
}
|
|
313
304
|
|
|
314
|
-
/**
|
|
315
|
-
* [إضافة جديدة]
|
|
316
|
-
* يمثل الكائن الكامل الذي يتم إرجاعه من الهوك `useApiModule`.
|
|
317
|
-
* يفصل بين الدوال المستقرة والحالات المتغيرة.
|
|
318
|
-
* @template TModule نوع إعدادات الموديول.
|
|
319
|
-
*/
|
|
320
|
-
interface UseApiModuleReturn<TModule extends ApiModuleConfig> {
|
|
321
|
-
/**
|
|
322
|
-
* قاموس من الإجراءات التنفيذية المستقرة.
|
|
323
|
-
* آمن للاستخدام في مصفوفات الاعتماديات مثل `useEffect`.
|
|
324
|
-
*/
|
|
325
|
-
actions: ModuleActions<TModule>;
|
|
326
|
-
/**
|
|
327
|
-
* قاموس من الحالات المتغيرة لجميع الإجراءات.
|
|
328
|
-
* استخدم هذا الكائن لعرض البيانات، وحالات التحميل، والأخطاء في واجهة المستخدم.
|
|
329
|
-
*/
|
|
330
|
-
states: Record<keyof TModule['actions'], ActionState<any>>;
|
|
331
|
-
/**
|
|
332
|
-
* قاموس من أدوات التحكم بالاستعلام لجميع الإجراءات التي تم تمييزها بـ `isList: true`.
|
|
333
|
-
*/
|
|
334
|
-
queries: Record<string, UseApiQuery | undefined>;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* يصف إجراءً واحدًا داخل وحدة API، مع أنواع دقيقة للإدخال والإخراج.
|
|
338
|
-
* @template TInput نوع بيانات الإدخال (الجسم أو معاملات الاستعلام).
|
|
339
|
-
* @template TOutput نوع البيانات التي يتم إرجاعها في `data` عند النجاح.
|
|
340
|
-
*/
|
|
341
305
|
interface ActionConfig<TInput = any, TOutput = any> {
|
|
342
|
-
/** نوع طلب HTTP. */
|
|
343
306
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
344
|
-
/**
|
|
345
|
-
* المسار النسبي للإجراء. يمكن أن يحتوي على متغيرات مثل `{id}` أو `{tenantId}`.
|
|
346
|
-
* مثال: '/{id}/activate'
|
|
347
|
-
*/
|
|
348
307
|
path: string;
|
|
349
|
-
/**
|
|
350
|
-
* وصف للإجراء، مفيد للتوثيق وتلميحات محرر الكود.
|
|
351
|
-
*/
|
|
352
308
|
description?: string;
|
|
353
|
-
/**
|
|
354
|
-
* إذا كان `true`، فهذا يعني أن الإجراء يجلب قائمة بيانات ويدعم معاملات الاستعلام (pagination, sorting).
|
|
355
|
-
* سيتم تزويد هذا الإجراء بأدوات التحكم `query` تلقائيًا.
|
|
356
|
-
*/
|
|
357
309
|
isList?: boolean;
|
|
358
|
-
/**
|
|
359
|
-
* قائمة بأسماء الإجراءات (من نفس الموديول) التي يجب إبطال بياناتها في الـ Cache بعد نجاح هذا الإجراء.
|
|
360
|
-
* أساسي لمزامنة البيانات تلقائيًا بعد عمليات التعديل (Mutations).
|
|
361
|
-
* مثال: إجراء `update` يجب أن يبطل `getOne` و `list`.
|
|
362
|
-
*/
|
|
363
310
|
invalidates?: string[];
|
|
364
311
|
}
|
|
365
|
-
/**
|
|
366
|
-
* يمثل الإعداد الكامل لوحدة API. هذا هو "مصدر الحقيقة" لمجموعة من نقاط النهاية.
|
|
367
|
-
*/
|
|
368
312
|
interface ApiModuleConfig {
|
|
369
|
-
/** نقطة النهاية الأساسية للموديول (مثل '/api/v1/products'). */
|
|
370
313
|
baseEndpoint: string;
|
|
371
|
-
/** قاموس يحتوي على جميع الإجراءات المتاحة في هذا الموديول. المفتاح هو اسم الإجراء. */
|
|
372
314
|
actions: Record<string, ActionConfig<any, any>>;
|
|
373
315
|
}
|
|
374
|
-
/**
|
|
375
|
-
* إعدادات اختيارية لتخصيص سلوك الهوك `useApiModule`.
|
|
376
|
-
*/
|
|
377
316
|
interface UseApiModuleOptions {
|
|
378
|
-
/**
|
|
379
|
-
* دالة callback تُنفذ عند نجاح أي إجراء.
|
|
380
|
-
* @param actionName اسم الإجراء الذي نجح.
|
|
381
|
-
* @param message رسالة النجاح من الـ API.
|
|
382
|
-
* @param data البيانات المُرجعة.
|
|
383
|
-
*/
|
|
384
317
|
onSuccess?: (actionName: string, message: string, data: any) => void;
|
|
385
|
-
/**
|
|
386
|
-
* دالة callback تُنفذ عند فشل أي إجراء.
|
|
387
|
-
* @param actionName اسم الإجراء الذي فشل.
|
|
388
|
-
* @param message رسالة الخطأ من الـ API.
|
|
389
|
-
* @param error كائن الخطأ الموحد.
|
|
390
|
-
*/
|
|
391
318
|
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
392
|
-
/**
|
|
393
|
-
* المدة التي تعتبر فيها البيانات "حية" (fresh) بالمللي ثانية.
|
|
394
|
-
* خلال هذه المدة، لن يتم إرسال طلب شبكة جديد لنفس البيانات.
|
|
395
|
-
* الافتراضي: `0` (مما يعني أنها تعتبر "قديمة" stale فورًا).
|
|
396
|
-
*/
|
|
397
319
|
staleTime?: number;
|
|
398
|
-
/**
|
|
399
|
-
* مدة بقاء البيانات في الـ Cache (بالمللي ثانية) قبل أن يتم حذفها تلقائيًا.
|
|
400
|
-
* يمرر إلى `cacheManager.set`. إذا لم يتم تحديده، يستخدم `CacheManager` قيمته الافتراضية.
|
|
401
|
-
*/
|
|
402
320
|
cacheTime?: number;
|
|
403
|
-
/**
|
|
404
|
-
* إذا كان `true`، سيتم إعادة جلب جميع الإجراءات التي تم استدعاؤها عند عودة المستخدم إلى نافذة التطبيق.
|
|
405
|
-
* الافتراضي: `true`.
|
|
406
|
-
*/
|
|
407
321
|
refetchOnWindowFocus?: boolean;
|
|
408
322
|
}
|
|
409
|
-
/**
|
|
410
|
-
* الحالة التي يديرها كل إجراء على حدة.
|
|
411
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
412
|
-
*/
|
|
413
323
|
interface ActionState<TOutput> {
|
|
414
|
-
/** البيانات التي تم جلبها بنجاح. تكون `null` في البداية أو عند حدوث خطأ. */
|
|
415
324
|
data: TOutput | null;
|
|
416
|
-
/** كائن الخطأ في حال فشل الطلب. */
|
|
417
325
|
error: ApiError | null;
|
|
418
|
-
/** `true` أثناء تنفيذ طلب الشبكة. */
|
|
419
326
|
loading: boolean;
|
|
420
|
-
/** `true` إذا كان آخر طلب قد نجح. */
|
|
421
327
|
success: boolean;
|
|
422
|
-
/** `true` إذا تم استدعاء الإجراء مرة واحدة على الأقل. مفيد للتمييز بين الحالة الأولية وحالة "لم يتم العثور على بيانات". */
|
|
423
328
|
called: boolean;
|
|
424
329
|
}
|
|
425
330
|
/**
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
331
|
+
* The unified object for a single action, containing its state, executor, and controls.
|
|
332
|
+
* This is the new, more ergonomic return structure.
|
|
429
333
|
*/
|
|
430
334
|
interface ExecutableAction<TInput, TOutput> {
|
|
431
|
-
/**
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
* @param options خيارات إضافية للطلب مثل `pathParams` و `config` Axios.
|
|
435
|
-
* @returns `Promise` يحتوي على الاستجابة الموحدة `StandardResponse`.
|
|
436
|
-
*/
|
|
335
|
+
/** The reactive state for this specific action. */
|
|
336
|
+
state: ActionState<TOutput>;
|
|
337
|
+
/** The stable function to execute the API call. */
|
|
437
338
|
execute: (input?: TInput, options?: {
|
|
438
339
|
pathParams?: Record<string, any>;
|
|
439
340
|
config?: AxiosRequestConfig;
|
|
440
341
|
query?: QueryOptions;
|
|
441
342
|
}) => Promise<StandardResponse<TOutput>>;
|
|
442
|
-
/**
|
|
443
|
-
state: ActionState<TOutput>;
|
|
444
|
-
/** دالة لإعادة حالة هذا الإجراء إلى وضعها الأولي. */
|
|
343
|
+
/** A stable function to reset the action's state to its initial value. */
|
|
445
344
|
reset: () => void;
|
|
446
|
-
/**
|
|
345
|
+
/** Query controls (pagination, sorting, etc.) available only if `isList: true`. */
|
|
447
346
|
query?: UseApiQuery;
|
|
448
347
|
}
|
|
449
348
|
/**
|
|
450
|
-
*
|
|
451
|
-
*
|
|
349
|
+
* The final, fully-typed object returned by the useApiModule hook.
|
|
350
|
+
* It's a dictionary of executable actions.
|
|
452
351
|
*/
|
|
453
352
|
type ModuleActions<TModule extends ApiModuleConfig> = {
|
|
454
353
|
[K in keyof TModule['actions']]: TModule['actions'][K] extends ActionConfig<infer TInput, infer TOutput> ? ExecutableAction<TInput, TOutput> : never;
|
|
@@ -550,11 +449,11 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
|
|
|
550
449
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
551
450
|
|
|
552
451
|
/**
|
|
553
|
-
* A
|
|
554
|
-
* It provides a
|
|
555
|
-
*
|
|
452
|
+
* A production-ready custom hook that dynamically builds a set of executable and fully-typed API actions.
|
|
453
|
+
* It provides a unified, ergonomic API for each action, collocating state and execution logic,
|
|
454
|
+
* while guaranteeing stable function references to prevent re-renders.
|
|
556
455
|
*/
|
|
557
|
-
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions):
|
|
456
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): ModuleActions<TModule>;
|
|
558
457
|
|
|
559
458
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
560
459
|
interface UseApiResourceState<T> {
|
|
@@ -602,4 +501,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
602
501
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
603
502
|
}
|
|
604
503
|
|
|
605
|
-
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type Middleware, type MiddlewareContext, type ModuleActions, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiModuleOptions, type
|
|
504
|
+
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type Middleware, type MiddlewareContext, type ModuleActions, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiModuleOptions, type UseApiQuery, type UseApiResourceActions, type UseApiResourceConfig, type UseApiResourceReturn, type UseApiResourceState, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi, useApiModule };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method } from 'axios';
|
|
2
2
|
|
|
3
|
-
interface Logger {
|
|
4
|
-
debug: (message: string, ...args: any[]) => void;
|
|
5
|
-
info: (message: string, ...args: any[]) => void;
|
|
6
|
-
warn: (message: string, ...args: any[]) => void;
|
|
7
|
-
error: (message: string, ...args: any[]) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
3
|
/**
|
|
11
4
|
* @file src/types.ts
|
|
12
5
|
* @description This file serves as the central source for all data types and interfaces
|
|
@@ -95,7 +88,6 @@ interface MiddlewareContext {
|
|
|
95
88
|
req: InternalAxiosRequestConfig;
|
|
96
89
|
res?: AxiosResponse;
|
|
97
90
|
error?: any;
|
|
98
|
-
logger: Logger;
|
|
99
91
|
custom?: Record<string, any>;
|
|
100
92
|
}
|
|
101
93
|
/**
|
|
@@ -223,7 +215,6 @@ interface ApiClientConfig {
|
|
|
223
215
|
/** A callback function executed if the token refresh process fails. */
|
|
224
216
|
onRefreshError?: (error: any) => void;
|
|
225
217
|
/** A custom logger instance. Defaults to the browser `console`. */
|
|
226
|
-
logger?: Logger;
|
|
227
218
|
/** An array of middleware functions to be executed with every request. */
|
|
228
219
|
middleware?: Middleware[];
|
|
229
220
|
/**
|
|
@@ -311,144 +302,52 @@ interface UseApiReturn<T> {
|
|
|
311
302
|
query: UseApiQuery;
|
|
312
303
|
}
|
|
313
304
|
|
|
314
|
-
/**
|
|
315
|
-
* [إضافة جديدة]
|
|
316
|
-
* يمثل الكائن الكامل الذي يتم إرجاعه من الهوك `useApiModule`.
|
|
317
|
-
* يفصل بين الدوال المستقرة والحالات المتغيرة.
|
|
318
|
-
* @template TModule نوع إعدادات الموديول.
|
|
319
|
-
*/
|
|
320
|
-
interface UseApiModuleReturn<TModule extends ApiModuleConfig> {
|
|
321
|
-
/**
|
|
322
|
-
* قاموس من الإجراءات التنفيذية المستقرة.
|
|
323
|
-
* آمن للاستخدام في مصفوفات الاعتماديات مثل `useEffect`.
|
|
324
|
-
*/
|
|
325
|
-
actions: ModuleActions<TModule>;
|
|
326
|
-
/**
|
|
327
|
-
* قاموس من الحالات المتغيرة لجميع الإجراءات.
|
|
328
|
-
* استخدم هذا الكائن لعرض البيانات، وحالات التحميل، والأخطاء في واجهة المستخدم.
|
|
329
|
-
*/
|
|
330
|
-
states: Record<keyof TModule['actions'], ActionState<any>>;
|
|
331
|
-
/**
|
|
332
|
-
* قاموس من أدوات التحكم بالاستعلام لجميع الإجراءات التي تم تمييزها بـ `isList: true`.
|
|
333
|
-
*/
|
|
334
|
-
queries: Record<string, UseApiQuery | undefined>;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* يصف إجراءً واحدًا داخل وحدة API، مع أنواع دقيقة للإدخال والإخراج.
|
|
338
|
-
* @template TInput نوع بيانات الإدخال (الجسم أو معاملات الاستعلام).
|
|
339
|
-
* @template TOutput نوع البيانات التي يتم إرجاعها في `data` عند النجاح.
|
|
340
|
-
*/
|
|
341
305
|
interface ActionConfig<TInput = any, TOutput = any> {
|
|
342
|
-
/** نوع طلب HTTP. */
|
|
343
306
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
344
|
-
/**
|
|
345
|
-
* المسار النسبي للإجراء. يمكن أن يحتوي على متغيرات مثل `{id}` أو `{tenantId}`.
|
|
346
|
-
* مثال: '/{id}/activate'
|
|
347
|
-
*/
|
|
348
307
|
path: string;
|
|
349
|
-
/**
|
|
350
|
-
* وصف للإجراء، مفيد للتوثيق وتلميحات محرر الكود.
|
|
351
|
-
*/
|
|
352
308
|
description?: string;
|
|
353
|
-
/**
|
|
354
|
-
* إذا كان `true`، فهذا يعني أن الإجراء يجلب قائمة بيانات ويدعم معاملات الاستعلام (pagination, sorting).
|
|
355
|
-
* سيتم تزويد هذا الإجراء بأدوات التحكم `query` تلقائيًا.
|
|
356
|
-
*/
|
|
357
309
|
isList?: boolean;
|
|
358
|
-
/**
|
|
359
|
-
* قائمة بأسماء الإجراءات (من نفس الموديول) التي يجب إبطال بياناتها في الـ Cache بعد نجاح هذا الإجراء.
|
|
360
|
-
* أساسي لمزامنة البيانات تلقائيًا بعد عمليات التعديل (Mutations).
|
|
361
|
-
* مثال: إجراء `update` يجب أن يبطل `getOne` و `list`.
|
|
362
|
-
*/
|
|
363
310
|
invalidates?: string[];
|
|
364
311
|
}
|
|
365
|
-
/**
|
|
366
|
-
* يمثل الإعداد الكامل لوحدة API. هذا هو "مصدر الحقيقة" لمجموعة من نقاط النهاية.
|
|
367
|
-
*/
|
|
368
312
|
interface ApiModuleConfig {
|
|
369
|
-
/** نقطة النهاية الأساسية للموديول (مثل '/api/v1/products'). */
|
|
370
313
|
baseEndpoint: string;
|
|
371
|
-
/** قاموس يحتوي على جميع الإجراءات المتاحة في هذا الموديول. المفتاح هو اسم الإجراء. */
|
|
372
314
|
actions: Record<string, ActionConfig<any, any>>;
|
|
373
315
|
}
|
|
374
|
-
/**
|
|
375
|
-
* إعدادات اختيارية لتخصيص سلوك الهوك `useApiModule`.
|
|
376
|
-
*/
|
|
377
316
|
interface UseApiModuleOptions {
|
|
378
|
-
/**
|
|
379
|
-
* دالة callback تُنفذ عند نجاح أي إجراء.
|
|
380
|
-
* @param actionName اسم الإجراء الذي نجح.
|
|
381
|
-
* @param message رسالة النجاح من الـ API.
|
|
382
|
-
* @param data البيانات المُرجعة.
|
|
383
|
-
*/
|
|
384
317
|
onSuccess?: (actionName: string, message: string, data: any) => void;
|
|
385
|
-
/**
|
|
386
|
-
* دالة callback تُنفذ عند فشل أي إجراء.
|
|
387
|
-
* @param actionName اسم الإجراء الذي فشل.
|
|
388
|
-
* @param message رسالة الخطأ من الـ API.
|
|
389
|
-
* @param error كائن الخطأ الموحد.
|
|
390
|
-
*/
|
|
391
318
|
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
392
|
-
/**
|
|
393
|
-
* المدة التي تعتبر فيها البيانات "حية" (fresh) بالمللي ثانية.
|
|
394
|
-
* خلال هذه المدة، لن يتم إرسال طلب شبكة جديد لنفس البيانات.
|
|
395
|
-
* الافتراضي: `0` (مما يعني أنها تعتبر "قديمة" stale فورًا).
|
|
396
|
-
*/
|
|
397
319
|
staleTime?: number;
|
|
398
|
-
/**
|
|
399
|
-
* مدة بقاء البيانات في الـ Cache (بالمللي ثانية) قبل أن يتم حذفها تلقائيًا.
|
|
400
|
-
* يمرر إلى `cacheManager.set`. إذا لم يتم تحديده، يستخدم `CacheManager` قيمته الافتراضية.
|
|
401
|
-
*/
|
|
402
320
|
cacheTime?: number;
|
|
403
|
-
/**
|
|
404
|
-
* إذا كان `true`، سيتم إعادة جلب جميع الإجراءات التي تم استدعاؤها عند عودة المستخدم إلى نافذة التطبيق.
|
|
405
|
-
* الافتراضي: `true`.
|
|
406
|
-
*/
|
|
407
321
|
refetchOnWindowFocus?: boolean;
|
|
408
322
|
}
|
|
409
|
-
/**
|
|
410
|
-
* الحالة التي يديرها كل إجراء على حدة.
|
|
411
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
412
|
-
*/
|
|
413
323
|
interface ActionState<TOutput> {
|
|
414
|
-
/** البيانات التي تم جلبها بنجاح. تكون `null` في البداية أو عند حدوث خطأ. */
|
|
415
324
|
data: TOutput | null;
|
|
416
|
-
/** كائن الخطأ في حال فشل الطلب. */
|
|
417
325
|
error: ApiError | null;
|
|
418
|
-
/** `true` أثناء تنفيذ طلب الشبكة. */
|
|
419
326
|
loading: boolean;
|
|
420
|
-
/** `true` إذا كان آخر طلب قد نجح. */
|
|
421
327
|
success: boolean;
|
|
422
|
-
/** `true` إذا تم استدعاء الإجراء مرة واحدة على الأقل. مفيد للتمييز بين الحالة الأولية وحالة "لم يتم العثور على بيانات". */
|
|
423
328
|
called: boolean;
|
|
424
329
|
}
|
|
425
330
|
/**
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
331
|
+
* The unified object for a single action, containing its state, executor, and controls.
|
|
332
|
+
* This is the new, more ergonomic return structure.
|
|
429
333
|
*/
|
|
430
334
|
interface ExecutableAction<TInput, TOutput> {
|
|
431
|
-
/**
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
* @param options خيارات إضافية للطلب مثل `pathParams` و `config` Axios.
|
|
435
|
-
* @returns `Promise` يحتوي على الاستجابة الموحدة `StandardResponse`.
|
|
436
|
-
*/
|
|
335
|
+
/** The reactive state for this specific action. */
|
|
336
|
+
state: ActionState<TOutput>;
|
|
337
|
+
/** The stable function to execute the API call. */
|
|
437
338
|
execute: (input?: TInput, options?: {
|
|
438
339
|
pathParams?: Record<string, any>;
|
|
439
340
|
config?: AxiosRequestConfig;
|
|
440
341
|
query?: QueryOptions;
|
|
441
342
|
}) => Promise<StandardResponse<TOutput>>;
|
|
442
|
-
/**
|
|
443
|
-
state: ActionState<TOutput>;
|
|
444
|
-
/** دالة لإعادة حالة هذا الإجراء إلى وضعها الأولي. */
|
|
343
|
+
/** A stable function to reset the action's state to its initial value. */
|
|
445
344
|
reset: () => void;
|
|
446
|
-
/**
|
|
345
|
+
/** Query controls (pagination, sorting, etc.) available only if `isList: true`. */
|
|
447
346
|
query?: UseApiQuery;
|
|
448
347
|
}
|
|
449
348
|
/**
|
|
450
|
-
*
|
|
451
|
-
*
|
|
349
|
+
* The final, fully-typed object returned by the useApiModule hook.
|
|
350
|
+
* It's a dictionary of executable actions.
|
|
452
351
|
*/
|
|
453
352
|
type ModuleActions<TModule extends ApiModuleConfig> = {
|
|
454
353
|
[K in keyof TModule['actions']]: TModule['actions'][K] extends ActionConfig<infer TInput, infer TOutput> ? ExecutableAction<TInput, TOutput> : never;
|
|
@@ -550,11 +449,11 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
|
|
|
550
449
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
551
450
|
|
|
552
451
|
/**
|
|
553
|
-
* A
|
|
554
|
-
* It provides a
|
|
555
|
-
*
|
|
452
|
+
* A production-ready custom hook that dynamically builds a set of executable and fully-typed API actions.
|
|
453
|
+
* It provides a unified, ergonomic API for each action, collocating state and execution logic,
|
|
454
|
+
* while guaranteeing stable function references to prevent re-renders.
|
|
556
455
|
*/
|
|
557
|
-
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions):
|
|
456
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): ModuleActions<TModule>;
|
|
558
457
|
|
|
559
458
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
560
459
|
interface UseApiResourceState<T> {
|
|
@@ -602,4 +501,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
602
501
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
603
502
|
}
|
|
604
503
|
|
|
605
|
-
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type Middleware, type MiddlewareContext, type ModuleActions, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiModuleOptions, type
|
|
504
|
+
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type Middleware, type MiddlewareContext, type ModuleActions, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiModuleOptions, type UseApiQuery, type UseApiResourceActions, type UseApiResourceConfig, type UseApiResourceReturn, type UseApiResourceState, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi, useApiModule };
|