api-core-lib 11.11.10 → 12.1.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 +118 -10
- package/dist/index.d.ts +118 -10
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -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
|
|
@@ -154,8 +155,11 @@ interface UseApiConfig<T> {
|
|
|
154
155
|
refetchAfterChange?: boolean;
|
|
155
156
|
/** A default `RequestConfig` to apply to all GET requests made by the hook. */
|
|
156
157
|
requestConfig?: RequestConfig;
|
|
157
|
-
|
|
158
|
+
/**
|
|
159
|
+
* [NEW] كائن يحتوي على قيم المتغيرات في قالب المسار.
|
|
160
|
+
*/
|
|
158
161
|
pathParams?: Record<string, string | number>;
|
|
162
|
+
encodeQuery?: boolean;
|
|
159
163
|
/** Callback function executed on a successful action. */
|
|
160
164
|
onSuccess?: (message: string, data?: T) => void;
|
|
161
165
|
/** Callback function executed on a failed action. */
|
|
@@ -377,13 +381,16 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, baseEndpoint
|
|
|
377
381
|
* بالإضافة إلى دوال التحقق من الأنواع (type guards).
|
|
378
382
|
*/
|
|
379
383
|
|
|
384
|
+
interface BuildQueryOptions {
|
|
385
|
+
encode?: boolean;
|
|
386
|
+
}
|
|
380
387
|
/**
|
|
381
|
-
* يبني سلسلة استعلام (query string) من كائن
|
|
382
|
-
*
|
|
383
|
-
* @param
|
|
388
|
+
* يبني سلسلة استعلام (query string) من كائن الخيارات.
|
|
389
|
+
* @param options - كائن خيارات الاستعلام (فلترة, ترتيب, ...).
|
|
390
|
+
* @param config - إعدادات إضافية مثل التحكم في التشفير.
|
|
384
391
|
* @returns سلسلة استعلام جاهزة للإضافة للرابط.
|
|
385
392
|
*/
|
|
386
|
-
declare function buildPaginateQuery(
|
|
393
|
+
declare function buildPaginateQuery(options: QueryOptions, config?: BuildQueryOptions): string;
|
|
387
394
|
|
|
388
395
|
/**
|
|
389
396
|
* Defines a single custom API action.
|
|
@@ -449,11 +456,112 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
|
|
|
449
456
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
450
457
|
|
|
451
458
|
/**
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
|
|
459
|
+
* هوك متقدم يقوم ببناء مجموعة من الإجراءات القابلة للتنفيذ من إعدادات موديول API،
|
|
460
|
+
* مع فصل تام بين الدوال المستقرة والحالات المتغيرة لمنع الحلقات اللانهائية.
|
|
461
|
+
*/
|
|
462
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): any;
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
466
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
467
|
+
* used across the library.
|
|
468
|
+
* @template T The data type of the record.
|
|
469
|
+
*/
|
|
470
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
471
|
+
/**
|
|
472
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
473
|
+
* a single API resource.
|
|
474
|
+
* @template T The data type of the record.
|
|
455
475
|
*/
|
|
456
|
-
|
|
476
|
+
interface UseApiRecordActions<T> {
|
|
477
|
+
/**
|
|
478
|
+
* Manually fetches or refetches the record from the API.
|
|
479
|
+
*/
|
|
480
|
+
fetch: () => Promise<void>;
|
|
481
|
+
/**
|
|
482
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
483
|
+
* @param updatedItem An object containing the fields to update.
|
|
484
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
485
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
486
|
+
*/
|
|
487
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
488
|
+
/**
|
|
489
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
490
|
+
* @param item The complete new record object.
|
|
491
|
+
* @param options Additional request options.
|
|
492
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
493
|
+
*/
|
|
494
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
495
|
+
/**
|
|
496
|
+
* Deletes the record using an HTTP DELETE request.
|
|
497
|
+
* @param options Additional request options.
|
|
498
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
499
|
+
*/
|
|
500
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
501
|
+
/**
|
|
502
|
+
* Resets the hook's state to its initial value.
|
|
503
|
+
*/
|
|
504
|
+
resetState: () => void;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
508
|
+
* @template T The data type of the record.
|
|
509
|
+
*/
|
|
510
|
+
interface UseApiRecordConfig<T> {
|
|
511
|
+
/**
|
|
512
|
+
* The base API endpoint template for the resource.
|
|
513
|
+
* Can contain placeholders like `{endpointName}`.
|
|
514
|
+
* @example 'v1/dynamic/{endpointName}'
|
|
515
|
+
*/
|
|
516
|
+
endpoint: string;
|
|
517
|
+
/**
|
|
518
|
+
* An object containing key-value pairs to replace placeholders in the endpoint template.
|
|
519
|
+
* @example { endpointName: 'products' }
|
|
520
|
+
*/
|
|
521
|
+
pathParams?: Record<string, string | number>;
|
|
522
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
523
|
+
recordId?: string | number | null;
|
|
524
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
525
|
+
initialData?: T | null;
|
|
526
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
527
|
+
enabled?: boolean;
|
|
528
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
529
|
+
refetchAfterChange?: boolean;
|
|
530
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
531
|
+
requestConfig?: RequestConfig;
|
|
532
|
+
/** A callback function executed on a successful API action. */
|
|
533
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
534
|
+
/** A callback function executed on a failed API action. */
|
|
535
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* The return value of the `useApiRecord` hook.
|
|
539
|
+
* @template T The data type of the record.
|
|
540
|
+
*/
|
|
541
|
+
interface UseApiRecordReturn<T> {
|
|
542
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
543
|
+
state: UseApiRecordState<T>;
|
|
544
|
+
/** Action methods to manipulate the record. */
|
|
545
|
+
actions: UseApiRecordActions<T>;
|
|
546
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
547
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* A React hook for managing the lifecycle of a single API resource (a record).
|
|
552
|
+
* It handles fetching, updating, replacing, and deleting a record, while managing
|
|
553
|
+
* loading, error, and data states. It supports dynamic path parameters for flexible API routing.
|
|
554
|
+
*
|
|
555
|
+
* @template T The data type of the record being managed.
|
|
556
|
+
* @param {AxiosInstance} axiosInstance - The configured Axios instance for making API calls.
|
|
557
|
+
* @param {UseApiRecordConfig<T>} config - Configuration options for the hook.
|
|
558
|
+
* @returns {UseApiRecordReturn<T>} An object containing the state, actions, and setState function.
|
|
559
|
+
*/
|
|
560
|
+
declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
|
|
561
|
+
|
|
562
|
+
type EffectCallback = () => (void | (() => void));
|
|
563
|
+
type DependencyList = readonly any[];
|
|
564
|
+
declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
|
|
457
565
|
|
|
458
566
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
459
567
|
interface UseApiResourceState<T> {
|
|
@@ -501,4 +609,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
501
609
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
502
610
|
}
|
|
503
611
|
|
|
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 };
|
|
612
|
+
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
|
|
@@ -154,8 +155,11 @@ interface UseApiConfig<T> {
|
|
|
154
155
|
refetchAfterChange?: boolean;
|
|
155
156
|
/** A default `RequestConfig` to apply to all GET requests made by the hook. */
|
|
156
157
|
requestConfig?: RequestConfig;
|
|
157
|
-
|
|
158
|
+
/**
|
|
159
|
+
* [NEW] كائن يحتوي على قيم المتغيرات في قالب المسار.
|
|
160
|
+
*/
|
|
158
161
|
pathParams?: Record<string, string | number>;
|
|
162
|
+
encodeQuery?: boolean;
|
|
159
163
|
/** Callback function executed on a successful action. */
|
|
160
164
|
onSuccess?: (message: string, data?: T) => void;
|
|
161
165
|
/** Callback function executed on a failed action. */
|
|
@@ -377,13 +381,16 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, baseEndpoint
|
|
|
377
381
|
* بالإضافة إلى دوال التحقق من الأنواع (type guards).
|
|
378
382
|
*/
|
|
379
383
|
|
|
384
|
+
interface BuildQueryOptions {
|
|
385
|
+
encode?: boolean;
|
|
386
|
+
}
|
|
380
387
|
/**
|
|
381
|
-
* يبني سلسلة استعلام (query string) من كائن
|
|
382
|
-
*
|
|
383
|
-
* @param
|
|
388
|
+
* يبني سلسلة استعلام (query string) من كائن الخيارات.
|
|
389
|
+
* @param options - كائن خيارات الاستعلام (فلترة, ترتيب, ...).
|
|
390
|
+
* @param config - إعدادات إضافية مثل التحكم في التشفير.
|
|
384
391
|
* @returns سلسلة استعلام جاهزة للإضافة للرابط.
|
|
385
392
|
*/
|
|
386
|
-
declare function buildPaginateQuery(
|
|
393
|
+
declare function buildPaginateQuery(options: QueryOptions, config?: BuildQueryOptions): string;
|
|
387
394
|
|
|
388
395
|
/**
|
|
389
396
|
* Defines a single custom API action.
|
|
@@ -449,11 +456,112 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
|
|
|
449
456
|
declare function useApi<T>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
|
|
450
457
|
|
|
451
458
|
/**
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
|
|
459
|
+
* هوك متقدم يقوم ببناء مجموعة من الإجراءات القابلة للتنفيذ من إعدادات موديول API،
|
|
460
|
+
* مع فصل تام بين الدوال المستقرة والحالات المتغيرة لمنع الحلقات اللانهائية.
|
|
461
|
+
*/
|
|
462
|
+
declare function useApiModule<TModule extends ApiModuleConfig>(axiosInstance: AxiosInstance, moduleConfig: TModule, options?: UseApiModuleOptions): any;
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Represents the internal state of the `useApiRecord` hook.
|
|
466
|
+
* It mirrors the `StandardResponse` structure, which is the unified response format
|
|
467
|
+
* used across the library.
|
|
468
|
+
* @template T The data type of the record.
|
|
469
|
+
*/
|
|
470
|
+
type UseApiRecordState<T> = StandardResponse<T>;
|
|
471
|
+
/**
|
|
472
|
+
* Defines the action methods provided by the `useApiRecord` hook to interact with
|
|
473
|
+
* a single API resource.
|
|
474
|
+
* @template T The data type of the record.
|
|
455
475
|
*/
|
|
456
|
-
|
|
476
|
+
interface UseApiRecordActions<T> {
|
|
477
|
+
/**
|
|
478
|
+
* Manually fetches or refetches the record from the API.
|
|
479
|
+
*/
|
|
480
|
+
fetch: () => Promise<void>;
|
|
481
|
+
/**
|
|
482
|
+
* Partially updates the record using an HTTP PATCH request.
|
|
483
|
+
* @param updatedItem An object containing the fields to update.
|
|
484
|
+
* @param options Additional request options, allowing overrides for this specific action.
|
|
485
|
+
* @returns A promise that resolves to the standard response object for the updated record.
|
|
486
|
+
*/
|
|
487
|
+
update: (updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
488
|
+
/**
|
|
489
|
+
* Replaces the entire record with a new one using an HTTP PUT request.
|
|
490
|
+
* @param item The complete new record object.
|
|
491
|
+
* @param options Additional request options.
|
|
492
|
+
* @returns A promise that resolves to the standard response object for the replaced record.
|
|
493
|
+
*/
|
|
494
|
+
put: (item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
|
|
495
|
+
/**
|
|
496
|
+
* Deletes the record using an HTTP DELETE request.
|
|
497
|
+
* @param options Additional request options.
|
|
498
|
+
* @returns A promise that resolves to a standard response object with a null data payload.
|
|
499
|
+
*/
|
|
500
|
+
remove: (options?: ActionOptions) => Promise<StandardResponse<null>>;
|
|
501
|
+
/**
|
|
502
|
+
* Resets the hook's state to its initial value.
|
|
503
|
+
*/
|
|
504
|
+
resetState: () => void;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Defines the configuration options for the `useApiRecord` hook.
|
|
508
|
+
* @template T The data type of the record.
|
|
509
|
+
*/
|
|
510
|
+
interface UseApiRecordConfig<T> {
|
|
511
|
+
/**
|
|
512
|
+
* The base API endpoint template for the resource.
|
|
513
|
+
* Can contain placeholders like `{endpointName}`.
|
|
514
|
+
* @example 'v1/dynamic/{endpointName}'
|
|
515
|
+
*/
|
|
516
|
+
endpoint: string;
|
|
517
|
+
/**
|
|
518
|
+
* An object containing key-value pairs to replace placeholders in the endpoint template.
|
|
519
|
+
* @example { endpointName: 'products' }
|
|
520
|
+
*/
|
|
521
|
+
pathParams?: Record<string, string | number>;
|
|
522
|
+
/** The unique identifier of the record to fetch or modify. */
|
|
523
|
+
recordId?: string | number | null;
|
|
524
|
+
/** Optional initial data to populate the state before the first fetch is complete. */
|
|
525
|
+
initialData?: T | null;
|
|
526
|
+
/** If `false`, the hook will not automatically fetch data on mount. Defaults to `true`. */
|
|
527
|
+
enabled?: boolean;
|
|
528
|
+
/** If `true`, the record will be refetched after a successful `update`, `put`, or `remove` action. Defaults to `true`. */
|
|
529
|
+
refetchAfterChange?: boolean;
|
|
530
|
+
/** Default `RequestConfig` to apply to the initial GET request made by the hook. */
|
|
531
|
+
requestConfig?: RequestConfig;
|
|
532
|
+
/** A callback function executed on a successful API action. */
|
|
533
|
+
onSuccess?: (message: string, data?: any) => void;
|
|
534
|
+
/** A callback function executed on a failed API action. */
|
|
535
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* The return value of the `useApiRecord` hook.
|
|
539
|
+
* @template T The data type of the record.
|
|
540
|
+
*/
|
|
541
|
+
interface UseApiRecordReturn<T> {
|
|
542
|
+
/** The current state of the API request, including data, loading, and error status. */
|
|
543
|
+
state: UseApiRecordState<T>;
|
|
544
|
+
/** Action methods to manipulate the record. */
|
|
545
|
+
actions: UseApiRecordActions<T>;
|
|
546
|
+
/** A React dispatch function to manually set the hook's state. Use with caution. */
|
|
547
|
+
setState: Dispatch<SetStateAction<UseApiRecordState<T>>>;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* A React hook for managing the lifecycle of a single API resource (a record).
|
|
552
|
+
* It handles fetching, updating, replacing, and deleting a record, while managing
|
|
553
|
+
* loading, error, and data states. It supports dynamic path parameters for flexible API routing.
|
|
554
|
+
*
|
|
555
|
+
* @template T The data type of the record being managed.
|
|
556
|
+
* @param {AxiosInstance} axiosInstance - The configured Axios instance for making API calls.
|
|
557
|
+
* @param {UseApiRecordConfig<T>} config - Configuration options for the hook.
|
|
558
|
+
* @returns {UseApiRecordReturn<T>} An object containing the state, actions, and setState function.
|
|
559
|
+
*/
|
|
560
|
+
declare function useApiRecord<T>(axiosInstance: AxiosInstance, config: UseApiRecordConfig<T>): UseApiRecordReturn<T>;
|
|
561
|
+
|
|
562
|
+
type EffectCallback = () => (void | (() => void));
|
|
563
|
+
type DependencyList = readonly any[];
|
|
564
|
+
declare function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList): void;
|
|
457
565
|
|
|
458
566
|
type ApiResourceStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
459
567
|
interface UseApiResourceState<T> {
|
|
@@ -501,4 +609,4 @@ interface UseApiResourceReturn<T, TCreate, TUpdate, TPathParams> {
|
|
|
501
609
|
setQuery: React.Dispatch<React.SetStateAction<QueryOptions>>;
|
|
502
610
|
}
|
|
503
611
|
|
|
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 };
|
|
612
|
+
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 };
|