api-core-lib 11.10.10 → 12.0.0
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 +104 -118
- package/dist/index.d.ts +104 -118
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +6 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method } from 'axios';
|
|
2
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* @file src/types.ts
|
|
@@ -88,7 +89,6 @@ interface MiddlewareContext {
|
|
|
88
89
|
req: InternalAxiosRequestConfig;
|
|
89
90
|
res?: AxiosResponse;
|
|
90
91
|
error?: any;
|
|
91
|
-
logger: Logger;
|
|
92
92
|
custom?: Record<string, any>;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
@@ -197,20 +197,6 @@ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
|
197
197
|
* An interface for a custom logger, compatible with the standard `console` object.
|
|
198
198
|
* It now includes a `debug` method for more granular logging.
|
|
199
199
|
*/
|
|
200
|
-
interface Logger {
|
|
201
|
-
/** Logs a standard message. In our wrapper, this is often an alias for `info` or `debug`. */
|
|
202
|
-
log(message?: any, ...optionalParams: any[]): void;
|
|
203
|
-
/** Logs an informational message. */
|
|
204
|
-
info(message?: any, ...optionalParams: any[]): void;
|
|
205
|
-
/** Logs a warning message. */
|
|
206
|
-
warn(message?: any, ...optionalParams: any[]): void;
|
|
207
|
-
/** Logs an error message. */
|
|
208
|
-
error(message?: any, ...optionalParams: any[]): void;
|
|
209
|
-
/**
|
|
210
|
-
* Logs a debug message, typically for verbose, development-only output.
|
|
211
|
-
*/
|
|
212
|
-
debug(message?: any, ...optionalParams: any[]): void;
|
|
213
|
-
}
|
|
214
200
|
/**
|
|
215
201
|
* The main configuration object for the `createApiClient` factory function.
|
|
216
202
|
*/
|
|
@@ -230,7 +216,6 @@ interface ApiClientConfig {
|
|
|
230
216
|
/** A callback function executed if the token refresh process fails. */
|
|
231
217
|
onRefreshError?: (error: any) => void;
|
|
232
218
|
/** A custom logger instance. Defaults to the browser `console`. */
|
|
233
|
-
logger?: Logger;
|
|
234
219
|
/** An array of middleware functions to be executed with every request. */
|
|
235
220
|
middleware?: Middleware[];
|
|
236
221
|
/**
|
|
@@ -244,19 +229,10 @@ interface ApiClientConfig {
|
|
|
244
229
|
* @default false
|
|
245
230
|
*/
|
|
246
231
|
defaultIsPublic?: boolean;
|
|
232
|
+
maxTokenRefreshRetries?: number;
|
|
233
|
+
maxQueueSize?: number;
|
|
247
234
|
}
|
|
248
235
|
|
|
249
|
-
/**
|
|
250
|
-
* @file src/core/client.ts
|
|
251
|
-
* @description This is the heart of the API client library.
|
|
252
|
-
* The `createApiClient` function constructs and configures a sophisticated Axios instance.
|
|
253
|
-
* It features intelligent, security-first token management, a flexible middleware system,
|
|
254
|
-
* and customizable logging, all designed to work seamlessly in modern web frameworks like Next.js.
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Creates and configures a new Axios instance with advanced features.
|
|
259
|
-
*/
|
|
260
236
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
|
261
237
|
|
|
262
238
|
/**
|
|
@@ -327,122 +303,52 @@ interface UseApiReturn<T> {
|
|
|
327
303
|
query: UseApiQuery;
|
|
328
304
|
}
|
|
329
305
|
|
|
330
|
-
/**
|
|
331
|
-
* يصف إجراءً واحدًا داخل وحدة API، مع أنواع دقيقة للإدخال والإخراج.
|
|
332
|
-
* @template TInput نوع بيانات الإدخال (الجسم أو معاملات الاستعلام).
|
|
333
|
-
* @template TOutput نوع البيانات التي يتم إرجاعها في `data` عند النجاح.
|
|
334
|
-
*/
|
|
335
306
|
interface ActionConfig<TInput = any, TOutput = any> {
|
|
336
|
-
/** نوع طلب HTTP. */
|
|
337
307
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
338
|
-
/**
|
|
339
|
-
* المسار النسبي للإجراء. يمكن أن يحتوي على متغيرات مثل `{id}` أو `{tenantId}`.
|
|
340
|
-
* مثال: '/{id}/activate'
|
|
341
|
-
*/
|
|
342
308
|
path: string;
|
|
343
|
-
/**
|
|
344
|
-
* وصف للإجراء، مفيد للتوثيق وتلميحات محرر الكود.
|
|
345
|
-
*/
|
|
346
309
|
description?: string;
|
|
347
|
-
/**
|
|
348
|
-
* إذا كان `true`، فهذا يعني أن الإجراء يجلب قائمة بيانات ويدعم معاملات الاستعلام (pagination, sorting).
|
|
349
|
-
* سيتم تزويد هذا الإجراء بأدوات التحكم `query` تلقائيًا.
|
|
350
|
-
*/
|
|
351
310
|
isList?: boolean;
|
|
352
|
-
/**
|
|
353
|
-
* قائمة بأسماء الإجراءات (من نفس الموديول) التي يجب إبطال بياناتها في الـ Cache بعد نجاح هذا الإجراء.
|
|
354
|
-
* أساسي لمزامنة البيانات تلقائيًا بعد عمليات التعديل (Mutations).
|
|
355
|
-
* مثال: إجراء `update` يجب أن يبطل `getOne` و `list`.
|
|
356
|
-
*/
|
|
357
311
|
invalidates?: string[];
|
|
358
312
|
}
|
|
359
|
-
/**
|
|
360
|
-
* يمثل الإعداد الكامل لوحدة API. هذا هو "مصدر الحقيقة" لمجموعة من نقاط النهاية.
|
|
361
|
-
*/
|
|
362
313
|
interface ApiModuleConfig {
|
|
363
|
-
/** نقطة النهاية الأساسية للموديول (مثل '/api/v1/products'). */
|
|
364
314
|
baseEndpoint: string;
|
|
365
|
-
/** قاموس يحتوي على جميع الإجراءات المتاحة في هذا الموديول. المفتاح هو اسم الإجراء. */
|
|
366
315
|
actions: Record<string, ActionConfig<any, any>>;
|
|
367
316
|
}
|
|
368
|
-
/**
|
|
369
|
-
* إعدادات اختيارية لتخصيص سلوك الهوك `useApiModule`.
|
|
370
|
-
*/
|
|
371
317
|
interface UseApiModuleOptions {
|
|
372
|
-
/**
|
|
373
|
-
* دالة callback تُنفذ عند نجاح أي إجراء.
|
|
374
|
-
* @param actionName اسم الإجراء الذي نجح.
|
|
375
|
-
* @param message رسالة النجاح من الـ API.
|
|
376
|
-
* @param data البيانات المُرجعة.
|
|
377
|
-
*/
|
|
378
318
|
onSuccess?: (actionName: string, message: string, data: any) => void;
|
|
379
|
-
/**
|
|
380
|
-
* دالة callback تُنفذ عند فشل أي إجراء.
|
|
381
|
-
* @param actionName اسم الإجراء الذي فشل.
|
|
382
|
-
* @param message رسالة الخطأ من الـ API.
|
|
383
|
-
* @param error كائن الخطأ الموحد.
|
|
384
|
-
*/
|
|
385
319
|
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
386
|
-
/**
|
|
387
|
-
* المدة التي تعتبر فيها البيانات "حية" (fresh) بالمللي ثانية.
|
|
388
|
-
* خلال هذه المدة، لن يتم إرسال طلب شبكة جديد لنفس البيانات.
|
|
389
|
-
* الافتراضي: `0` (مما يعني أنها تعتبر "قديمة" stale فورًا).
|
|
390
|
-
*/
|
|
391
320
|
staleTime?: number;
|
|
392
|
-
/**
|
|
393
|
-
* مدة بقاء البيانات في الـ Cache (بالمللي ثانية) قبل أن يتم حذفها تلقائيًا.
|
|
394
|
-
* يمرر إلى `cacheManager.set`. إذا لم يتم تحديده، يستخدم `CacheManager` قيمته الافتراضية.
|
|
395
|
-
*/
|
|
396
321
|
cacheTime?: number;
|
|
397
|
-
/**
|
|
398
|
-
* إذا كان `true`، سيتم إعادة جلب جميع الإجراءات التي تم استدعاؤها عند عودة المستخدم إلى نافذة التطبيق.
|
|
399
|
-
* الافتراضي: `true`.
|
|
400
|
-
*/
|
|
401
322
|
refetchOnWindowFocus?: boolean;
|
|
402
323
|
}
|
|
403
|
-
/**
|
|
404
|
-
* الحالة التي يديرها كل إجراء على حدة.
|
|
405
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
406
|
-
*/
|
|
407
324
|
interface ActionState<TOutput> {
|
|
408
|
-
/** البيانات التي تم جلبها بنجاح. تكون `null` في البداية أو عند حدوث خطأ. */
|
|
409
325
|
data: TOutput | null;
|
|
410
|
-
/** كائن الخطأ في حال فشل الطلب. */
|
|
411
326
|
error: ApiError | null;
|
|
412
|
-
/** `true` أثناء تنفيذ طلب الشبكة. */
|
|
413
327
|
loading: boolean;
|
|
414
|
-
/** `true` إذا كان آخر طلب قد نجح. */
|
|
415
328
|
success: boolean;
|
|
416
|
-
/** `true` إذا تم استدعاء الإجراء مرة واحدة على الأقل. مفيد للتمييز بين الحالة الأولية وحالة "لم يتم العثور على بيانات". */
|
|
417
329
|
called: boolean;
|
|
418
330
|
}
|
|
419
331
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
332
|
+
* The unified object for a single action, containing its state, executor, and controls.
|
|
333
|
+
* This is the new, more ergonomic return structure.
|
|
423
334
|
*/
|
|
424
335
|
interface ExecutableAction<TInput, TOutput> {
|
|
425
|
-
/**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
* @param options خيارات إضافية للطلب مثل `pathParams` و `config` Axios.
|
|
429
|
-
* @returns `Promise` يحتوي على الاستجابة الموحدة `StandardResponse`.
|
|
430
|
-
*/
|
|
336
|
+
/** The reactive state for this specific action. */
|
|
337
|
+
state: ActionState<TOutput>;
|
|
338
|
+
/** The stable function to execute the API call. */
|
|
431
339
|
execute: (input?: TInput, options?: {
|
|
432
340
|
pathParams?: Record<string, any>;
|
|
433
341
|
config?: AxiosRequestConfig;
|
|
434
342
|
query?: QueryOptions;
|
|
435
343
|
}) => Promise<StandardResponse<TOutput>>;
|
|
436
|
-
/**
|
|
437
|
-
state: ActionState<TOutput>;
|
|
438
|
-
/** دالة لإعادة حالة هذا الإجراء إلى وضعها الأولي. */
|
|
344
|
+
/** A stable function to reset the action's state to its initial value. */
|
|
439
345
|
reset: () => void;
|
|
440
|
-
/**
|
|
346
|
+
/** Query controls (pagination, sorting, etc.) available only if `isList: true`. */
|
|
441
347
|
query?: UseApiQuery;
|
|
442
348
|
}
|
|
443
349
|
/**
|
|
444
|
-
*
|
|
445
|
-
*
|
|
350
|
+
* The final, fully-typed object returned by the useApiModule hook.
|
|
351
|
+
* It's a dictionary of executable actions.
|
|
446
352
|
*/
|
|
447
353
|
type ModuleActions<TModule extends ApiModuleConfig> = {
|
|
448
354
|
[K in keyof TModule['actions']]: TModule['actions'][K] extends ActionConfig<infer TInput, infer TOutput> ? ExecutableAction<TInput, TOutput> : never;
|
|
@@ -539,23 +445,103 @@ declare class CacheManager {
|
|
|
539
445
|
}
|
|
540
446
|
declare const cacheManager: CacheManager;
|
|
541
447
|
|
|
448
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError) => StandardResponse<T>;
|
|
449
|
+
|
|
450
|
+
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
451
|
+
|
|
542
452
|
/**
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
* (like { success: true, data: {...} }) and provides direct access
|
|
546
|
-
* to the core data, while still handling errors consistently.
|
|
547
|
-
*
|
|
548
|
-
* @param responseOrError The raw Axios response or a pre-processed ApiError.
|
|
549
|
-
* @returns A standardized `StandardResponse` object with direct access to `.data`.
|
|
453
|
+
* هوك متقدم يقوم ببناء مجموعة من الإجراءات القابلة للتنفيذ من إعدادات موديول API،
|
|
454
|
+
* مع فصل تام بين الدوال المستقرة والحالات المتغيرة لمنع الحلقات اللانهائية.
|
|
550
455
|
*/
|
|
551
|
-
declare
|
|
456
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): any;
|
|
552
457
|
|
|
553
|
-
|
|
458
|
+
/**
|
|
459
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
460
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
461
|
+
* used across the library.
|
|
462
|
+
* @template T The data type of the record.
|
|
463
|
+
*/
|
|
464
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
465
|
+
/**
|
|
466
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
467
|
+
* a single API resource.
|
|
468
|
+
* @template T The data type of the record.
|
|
469
|
+
*/
|
|
470
|
+
interface UseApiRecordActions<T> {
|
|
471
|
+
/**
|
|
472
|
+
* Manually fetches or refetches the record from the API.
|
|
473
|
+
*/
|
|
474
|
+
fetch: () => Promise<void>;
|
|
475
|
+
/**
|
|
476
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
477
|
+
* @param updatedItem An object containing the fields to update.
|
|
478
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
479
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
480
|
+
*/
|
|
481
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
482
|
+
/**
|
|
483
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
484
|
+
* @param item The complete new record object.
|
|
485
|
+
* @param options Additional request options.
|
|
486
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
487
|
+
*/
|
|
488
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
489
|
+
/**
|
|
490
|
+
* Deletes the record using an HTTP DELETE request.
|
|
491
|
+
* @param options Additional request options.
|
|
492
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
493
|
+
*/
|
|
494
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
495
|
+
/**
|
|
496
|
+
* Resets the hook's state to its initial value.
|
|
497
|
+
*/
|
|
498
|
+
resetState: () => void;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
502
|
+
* @template T The data type of the record.
|
|
503
|
+
*/
|
|
504
|
+
interface UseApiRecordConfig<T> {
|
|
505
|
+
/** The base API endpoint for the resource (e.g., '/users'). */
|
|
506
|
+
endpoint: string;
|
|
507
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
508
|
+
recordId?: string | number | null;
|
|
509
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
510
|
+
initialData?: T | null;
|
|
511
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
512
|
+
enabled?: boolean;
|
|
513
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
514
|
+
refetchAfterChange?: boolean;
|
|
515
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
516
|
+
requestConfig?: RequestConfig;
|
|
517
|
+
/** A callback function executed on a successful API action. */
|
|
518
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
519
|
+
/** A callback function executed on a failed API action. */
|
|
520
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* The return value of the `useApiRecord` hook.
|
|
524
|
+
* @template T The data type of the record.
|
|
525
|
+
*/
|
|
526
|
+
interface UseApiRecordReturn<T> {
|
|
527
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
528
|
+
state: UseApiRecordState<T>;
|
|
529
|
+
/** Action methods to manipulate the record. */
|
|
530
|
+
actions: UseApiRecordActions<T>;
|
|
531
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
532
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
533
|
+
}
|
|
554
534
|
|
|
555
535
|
/**
|
|
556
|
-
*
|
|
536
|
+
* A React hook for managing the lifecycle of a single API resource (a record).
|
|
537
|
+
* It handles fetching, updating, replacing, and deleting a record, while managing
|
|
538
|
+
* loading, error, and data states.
|
|
557
539
|
*/
|
|
558
|
-
declare function
|
|
540
|
+
declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
|
|
541
|
+
|
|
542
|
+
type EffectCallback = () => (void | (() => void));
|
|
543
|
+
type DependencyList = readonly any[];
|
|
544
|
+
declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
|
|
559
545
|
|
|
560
546
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
561
547
|
interface UseApiResourceState<T> {
|
|
@@ -603,4 +589,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
603
589
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
604
590
|
}
|
|
605
591
|
|
|
606
|
-
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type
|
|
592
|
+
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 UseApiRecordActions, type UseApiRecordConfig, type UseApiRecordReturn, type UseApiRecordState, type UseApiResourceActions, type UseApiResourceConfig, type UseApiResourceReturn, type UseApiResourceState, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi, useApiModule, useApiRecord, useDeepCompareEffect };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method } from 'axios';
|
|
2
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* @file src/types.ts
|
|
@@ -88,7 +89,6 @@ interface MiddlewareContext {
|
|
|
88
89
|
req: InternalAxiosRequestConfig;
|
|
89
90
|
res?: AxiosResponse;
|
|
90
91
|
error?: any;
|
|
91
|
-
logger: Logger;
|
|
92
92
|
custom?: Record<string, any>;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
@@ -197,20 +197,6 @@ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
|
197
197
|
* An interface for a custom logger, compatible with the standard `console` object.
|
|
198
198
|
* It now includes a `debug` method for more granular logging.
|
|
199
199
|
*/
|
|
200
|
-
interface Logger {
|
|
201
|
-
/** Logs a standard message. In our wrapper, this is often an alias for `info` or `debug`. */
|
|
202
|
-
log(message?: any, ...optionalParams: any[]): void;
|
|
203
|
-
/** Logs an informational message. */
|
|
204
|
-
info(message?: any, ...optionalParams: any[]): void;
|
|
205
|
-
/** Logs a warning message. */
|
|
206
|
-
warn(message?: any, ...optionalParams: any[]): void;
|
|
207
|
-
/** Logs an error message. */
|
|
208
|
-
error(message?: any, ...optionalParams: any[]): void;
|
|
209
|
-
/**
|
|
210
|
-
* Logs a debug message, typically for verbose, development-only output.
|
|
211
|
-
*/
|
|
212
|
-
debug(message?: any, ...optionalParams: any[]): void;
|
|
213
|
-
}
|
|
214
200
|
/**
|
|
215
201
|
* The main configuration object for the `createApiClient` factory function.
|
|
216
202
|
*/
|
|
@@ -230,7 +216,6 @@ interface ApiClientConfig {
|
|
|
230
216
|
/** A callback function executed if the token refresh process fails. */
|
|
231
217
|
onRefreshError?: (error: any) => void;
|
|
232
218
|
/** A custom logger instance. Defaults to the browser `console`. */
|
|
233
|
-
logger?: Logger;
|
|
234
219
|
/** An array of middleware functions to be executed with every request. */
|
|
235
220
|
middleware?: Middleware[];
|
|
236
221
|
/**
|
|
@@ -244,19 +229,10 @@ interface ApiClientConfig {
|
|
|
244
229
|
* @default false
|
|
245
230
|
*/
|
|
246
231
|
defaultIsPublic?: boolean;
|
|
232
|
+
maxTokenRefreshRetries?: number;
|
|
233
|
+
maxQueueSize?: number;
|
|
247
234
|
}
|
|
248
235
|
|
|
249
|
-
/**
|
|
250
|
-
* @file src/core/client.ts
|
|
251
|
-
* @description This is the heart of the API client library.
|
|
252
|
-
* The `createApiClient` function constructs and configures a sophisticated Axios instance.
|
|
253
|
-
* It features intelligent, security-first token management, a flexible middleware system,
|
|
254
|
-
* and customizable logging, all designed to work seamlessly in modern web frameworks like Next.js.
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Creates and configures a new Axios instance with advanced features.
|
|
259
|
-
*/
|
|
260
236
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
|
261
237
|
|
|
262
238
|
/**
|
|
@@ -327,122 +303,52 @@ interface UseApiReturn<T> {
|
|
|
327
303
|
query: UseApiQuery;
|
|
328
304
|
}
|
|
329
305
|
|
|
330
|
-
/**
|
|
331
|
-
* يصف إجراءً واحدًا داخل وحدة API، مع أنواع دقيقة للإدخال والإخراج.
|
|
332
|
-
* @template TInput نوع بيانات الإدخال (الجسم أو معاملات الاستعلام).
|
|
333
|
-
* @template TOutput نوع البيانات التي يتم إرجاعها في `data` عند النجاح.
|
|
334
|
-
*/
|
|
335
306
|
interface ActionConfig<TInput = any, TOutput = any> {
|
|
336
|
-
/** نوع طلب HTTP. */
|
|
337
307
|
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
338
|
-
/**
|
|
339
|
-
* المسار النسبي للإجراء. يمكن أن يحتوي على متغيرات مثل `{id}` أو `{tenantId}`.
|
|
340
|
-
* مثال: '/{id}/activate'
|
|
341
|
-
*/
|
|
342
308
|
path: string;
|
|
343
|
-
/**
|
|
344
|
-
* وصف للإجراء، مفيد للتوثيق وتلميحات محرر الكود.
|
|
345
|
-
*/
|
|
346
309
|
description?: string;
|
|
347
|
-
/**
|
|
348
|
-
* إذا كان `true`، فهذا يعني أن الإجراء يجلب قائمة بيانات ويدعم معاملات الاستعلام (pagination, sorting).
|
|
349
|
-
* سيتم تزويد هذا الإجراء بأدوات التحكم `query` تلقائيًا.
|
|
350
|
-
*/
|
|
351
310
|
isList?: boolean;
|
|
352
|
-
/**
|
|
353
|
-
* قائمة بأسماء الإجراءات (من نفس الموديول) التي يجب إبطال بياناتها في الـ Cache بعد نجاح هذا الإجراء.
|
|
354
|
-
* أساسي لمزامنة البيانات تلقائيًا بعد عمليات التعديل (Mutations).
|
|
355
|
-
* مثال: إجراء `update` يجب أن يبطل `getOne` و `list`.
|
|
356
|
-
*/
|
|
357
311
|
invalidates?: string[];
|
|
358
312
|
}
|
|
359
|
-
/**
|
|
360
|
-
* يمثل الإعداد الكامل لوحدة API. هذا هو "مصدر الحقيقة" لمجموعة من نقاط النهاية.
|
|
361
|
-
*/
|
|
362
313
|
interface ApiModuleConfig {
|
|
363
|
-
/** نقطة النهاية الأساسية للموديول (مثل '/api/v1/products'). */
|
|
364
314
|
baseEndpoint: string;
|
|
365
|
-
/** قاموس يحتوي على جميع الإجراءات المتاحة في هذا الموديول. المفتاح هو اسم الإجراء. */
|
|
366
315
|
actions: Record<string, ActionConfig<any, any>>;
|
|
367
316
|
}
|
|
368
|
-
/**
|
|
369
|
-
* إعدادات اختيارية لتخصيص سلوك الهوك `useApiModule`.
|
|
370
|
-
*/
|
|
371
317
|
interface UseApiModuleOptions {
|
|
372
|
-
/**
|
|
373
|
-
* دالة callback تُنفذ عند نجاح أي إجراء.
|
|
374
|
-
* @param actionName اسم الإجراء الذي نجح.
|
|
375
|
-
* @param message رسالة النجاح من الـ API.
|
|
376
|
-
* @param data البيانات المُرجعة.
|
|
377
|
-
*/
|
|
378
318
|
onSuccess?: (actionName: string, message: string, data: any) => void;
|
|
379
|
-
/**
|
|
380
|
-
* دالة callback تُنفذ عند فشل أي إجراء.
|
|
381
|
-
* @param actionName اسم الإجراء الذي فشل.
|
|
382
|
-
* @param message رسالة الخطأ من الـ API.
|
|
383
|
-
* @param error كائن الخطأ الموحد.
|
|
384
|
-
*/
|
|
385
319
|
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
386
|
-
/**
|
|
387
|
-
* المدة التي تعتبر فيها البيانات "حية" (fresh) بالمللي ثانية.
|
|
388
|
-
* خلال هذه المدة، لن يتم إرسال طلب شبكة جديد لنفس البيانات.
|
|
389
|
-
* الافتراضي: `0` (مما يعني أنها تعتبر "قديمة" stale فورًا).
|
|
390
|
-
*/
|
|
391
320
|
staleTime?: number;
|
|
392
|
-
/**
|
|
393
|
-
* مدة بقاء البيانات في الـ Cache (بالمللي ثانية) قبل أن يتم حذفها تلقائيًا.
|
|
394
|
-
* يمرر إلى `cacheManager.set`. إذا لم يتم تحديده، يستخدم `CacheManager` قيمته الافتراضية.
|
|
395
|
-
*/
|
|
396
321
|
cacheTime?: number;
|
|
397
|
-
/**
|
|
398
|
-
* إذا كان `true`، سيتم إعادة جلب جميع الإجراءات التي تم استدعاؤها عند عودة المستخدم إلى نافذة التطبيق.
|
|
399
|
-
* الافتراضي: `true`.
|
|
400
|
-
*/
|
|
401
322
|
refetchOnWindowFocus?: boolean;
|
|
402
323
|
}
|
|
403
|
-
/**
|
|
404
|
-
* الحالة التي يديرها كل إجراء على حدة.
|
|
405
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
406
|
-
*/
|
|
407
324
|
interface ActionState<TOutput> {
|
|
408
|
-
/** البيانات التي تم جلبها بنجاح. تكون `null` في البداية أو عند حدوث خطأ. */
|
|
409
325
|
data: TOutput | null;
|
|
410
|
-
/** كائن الخطأ في حال فشل الطلب. */
|
|
411
326
|
error: ApiError | null;
|
|
412
|
-
/** `true` أثناء تنفيذ طلب الشبكة. */
|
|
413
327
|
loading: boolean;
|
|
414
|
-
/** `true` إذا كان آخر طلب قد نجح. */
|
|
415
328
|
success: boolean;
|
|
416
|
-
/** `true` إذا تم استدعاء الإجراء مرة واحدة على الأقل. مفيد للتمييز بين الحالة الأولية وحالة "لم يتم العثور على بيانات". */
|
|
417
329
|
called: boolean;
|
|
418
330
|
}
|
|
419
331
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* @template TOutput نوع بيانات الإخراج.
|
|
332
|
+
* The unified object for a single action, containing its state, executor, and controls.
|
|
333
|
+
* This is the new, more ergonomic return structure.
|
|
423
334
|
*/
|
|
424
335
|
interface ExecutableAction<TInput, TOutput> {
|
|
425
|
-
/**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
* @param options خيارات إضافية للطلب مثل `pathParams` و `config` Axios.
|
|
429
|
-
* @returns `Promise` يحتوي على الاستجابة الموحدة `StandardResponse`.
|
|
430
|
-
*/
|
|
336
|
+
/** The reactive state for this specific action. */
|
|
337
|
+
state: ActionState<TOutput>;
|
|
338
|
+
/** The stable function to execute the API call. */
|
|
431
339
|
execute: (input?: TInput, options?: {
|
|
432
340
|
pathParams?: Record<string, any>;
|
|
433
341
|
config?: AxiosRequestConfig;
|
|
434
342
|
query?: QueryOptions;
|
|
435
343
|
}) => Promise<StandardResponse<TOutput>>;
|
|
436
|
-
/**
|
|
437
|
-
state: ActionState<TOutput>;
|
|
438
|
-
/** دالة لإعادة حالة هذا الإجراء إلى وضعها الأولي. */
|
|
344
|
+
/** A stable function to reset the action's state to its initial value. */
|
|
439
345
|
reset: () => void;
|
|
440
|
-
/**
|
|
346
|
+
/** Query controls (pagination, sorting, etc.) available only if `isList: true`. */
|
|
441
347
|
query?: UseApiQuery;
|
|
442
348
|
}
|
|
443
349
|
/**
|
|
444
|
-
*
|
|
445
|
-
*
|
|
350
|
+
* The final, fully-typed object returned by the useApiModule hook.
|
|
351
|
+
* It's a dictionary of executable actions.
|
|
446
352
|
*/
|
|
447
353
|
type ModuleActions<TModule extends ApiModuleConfig> = {
|
|
448
354
|
[K in keyof TModule['actions']]: TModule['actions'][K] extends ActionConfig<infer TInput, infer TOutput> ? ExecutableAction<TInput, TOutput> : never;
|
|
@@ -539,23 +445,103 @@ declare class CacheManager {
|
|
|
539
445
|
}
|
|
540
446
|
declare const cacheManager: CacheManager;
|
|
541
447
|
|
|
448
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError) => StandardResponse<T>;
|
|
449
|
+
|
|
450
|
+
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
451
|
+
|
|
542
452
|
/**
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
* (like { success: true, data: {...} }) and provides direct access
|
|
546
|
-
* to the core data, while still handling errors consistently.
|
|
547
|
-
*
|
|
548
|
-
* @param responseOrError The raw Axios response or a pre-processed ApiError.
|
|
549
|
-
* @returns A standardized `StandardResponse` object with direct access to `.data`.
|
|
453
|
+
* هوك متقدم يقوم ببناء مجموعة من الإجراءات القابلة للتنفيذ من إعدادات موديول API،
|
|
454
|
+
* مع فصل تام بين الدوال المستقرة والحالات المتغيرة لمنع الحلقات اللانهائية.
|
|
550
455
|
*/
|
|
551
|
-
declare
|
|
456
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): any;
|
|
552
457
|
|
|
553
|
-
|
|
458
|
+
/**
|
|
459
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
460
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
461
|
+
* used across the library.
|
|
462
|
+
* @template T The data type of the record.
|
|
463
|
+
*/
|
|
464
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
465
|
+
/**
|
|
466
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
467
|
+
* a single API resource.
|
|
468
|
+
* @template T The data type of the record.
|
|
469
|
+
*/
|
|
470
|
+
interface UseApiRecordActions<T> {
|
|
471
|
+
/**
|
|
472
|
+
* Manually fetches or refetches the record from the API.
|
|
473
|
+
*/
|
|
474
|
+
fetch: () => Promise<void>;
|
|
475
|
+
/**
|
|
476
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
477
|
+
* @param updatedItem An object containing the fields to update.
|
|
478
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
479
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
480
|
+
*/
|
|
481
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
482
|
+
/**
|
|
483
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
484
|
+
* @param item The complete new record object.
|
|
485
|
+
* @param options Additional request options.
|
|
486
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
487
|
+
*/
|
|
488
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
489
|
+
/**
|
|
490
|
+
* Deletes the record using an HTTP DELETE request.
|
|
491
|
+
* @param options Additional request options.
|
|
492
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
493
|
+
*/
|
|
494
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
495
|
+
/**
|
|
496
|
+
* Resets the hook's state to its initial value.
|
|
497
|
+
*/
|
|
498
|
+
resetState: () => void;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
502
|
+
* @template T The data type of the record.
|
|
503
|
+
*/
|
|
504
|
+
interface UseApiRecordConfig<T> {
|
|
505
|
+
/** The base API endpoint for the resource (e.g., '/users'). */
|
|
506
|
+
endpoint: string;
|
|
507
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
508
|
+
recordId?: string | number | null;
|
|
509
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
510
|
+
initialData?: T | null;
|
|
511
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
512
|
+
enabled?: boolean;
|
|
513
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
514
|
+
refetchAfterChange?: boolean;
|
|
515
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
516
|
+
requestConfig?: RequestConfig;
|
|
517
|
+
/** A callback function executed on a successful API action. */
|
|
518
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
519
|
+
/** A callback function executed on a failed API action. */
|
|
520
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* The return value of the `useApiRecord` hook.
|
|
524
|
+
* @template T The data type of the record.
|
|
525
|
+
*/
|
|
526
|
+
interface UseApiRecordReturn<T> {
|
|
527
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
528
|
+
state: UseApiRecordState<T>;
|
|
529
|
+
/** Action methods to manipulate the record. */
|
|
530
|
+
actions: UseApiRecordActions<T>;
|
|
531
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
532
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
533
|
+
}
|
|
554
534
|
|
|
555
535
|
/**
|
|
556
|
-
*
|
|
536
|
+
* A React hook for managing the lifecycle of a single API resource (a record).
|
|
537
|
+
* It handles fetching, updating, replacing, and deleting a record, while managing
|
|
538
|
+
* loading, error, and data states.
|
|
557
539
|
*/
|
|
558
|
-
declare function
|
|
540
|
+
declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
|
|
541
|
+
|
|
542
|
+
type EffectCallback = () => (void | (() => void));
|
|
543
|
+
type DependencyList = readonly any[];
|
|
544
|
+
declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
|
|
559
545
|
|
|
560
546
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
561
547
|
interface UseApiResourceState<T> {
|
|
@@ -603,4 +589,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
603
589
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
604
590
|
}
|
|
605
591
|
|
|
606
|
-
export { type ActionConfig, type ActionOptions, type ActionState, type ApiClientConfig, type ApiError, type ApiModuleConfig, type ApiResourceStatus, type ApiResponse, type ExecutableAction, type LogLevel, type
|
|
592
|
+
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 UseApiRecordActions, type UseApiRecordConfig, type UseApiRecordReturn, type UseApiRecordState, type UseApiResourceActions, type UseApiResourceConfig, type UseApiResourceReturn, type UseApiResourceState, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi, useApiModule, useApiRecord, useDeepCompareEffect };
|