api-core-lib 7.7.6 → 8.7.7

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 CHANGED
@@ -127,7 +127,7 @@ interface QueryOptions {
127
127
  /**
128
128
  * Defines additional options for action methods like create, update, or delete.
129
129
  */
130
- interface ActionOptions {
130
+ interface ActionOptions extends RequestConfig {
131
131
  /**
132
132
  * Overrides the default endpoint for a specific action. Useful for specialized routes.
133
133
  * @example update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
@@ -146,7 +146,7 @@ interface UseApiConfig<T> {
146
146
  /** The base API endpoint for the resource (e.g., '/users'). */
147
147
  endpoint: string;
148
148
  /** Initial data to populate the state before the first fetch. */
149
- initialData?: T | T[];
149
+ initialData?: T | null;
150
150
  /** Default query options to use for the initial fetch. */
151
151
  initialQuery?: QueryOptions;
152
152
  /** If false, the hook will not fetch data automatically on mount. */
@@ -259,20 +259,20 @@ interface ApiClientConfig {
259
259
  */
260
260
  declare function createApiClient(config: ApiClientConfig): AxiosInstance;
261
261
 
262
- type CrudRequestConfig = RequestConfig & ActionOptions;
263
- /**
264
- * دالة مصنع (Factory Function) لإنشاء مجموعة خدمات API قابلة لإعادة الاستخدام لنقطة نهاية (endpoint) محددة.
265
- * توفر عمليات CRUD كاملة بالإضافة إلى ميزات متقدمة مثل الحذف الجماعي ورفع الملفات.
266
- */
267
- declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: string): {
268
- get: (id?: string, config?: RequestConfig) => Promise<StandardResponse<T | T[]>>;
269
- getWithQuery: (query: string, config?: RequestConfig) => Promise<StandardResponse<T[]>>;
270
- post: (data: Partial<T>, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
271
- put: (id: string, data: T, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
272
- patch: (id: string, data: Partial<T>, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
273
- remove: (id: string, config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
274
- bulkDelete: (ids: string[], config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
275
- upload: (file: File, additionalData?: Record<string, any>, config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
262
+ /**
263
+ * A factory function to create a reusable set of API services for a specific endpoint.
264
+ * It provides full CRUD operations plus advanced features like bulk deletion and file uploads,
265
+ * with intelligent, dynamic URL building.
266
+ */
267
+ declare function createApiServices<T>(axiosInstance: AxiosInstance, baseEndpoint: string): {
268
+ get: (id?: string | number, config?: ActionOptions) => Promise<StandardResponse<T>>;
269
+ getWithQuery: (query: string, config?: RequestConfig) => Promise<StandardResponse<T>>;
270
+ post: (data: Partial<T>, config?: ActionOptions) => Promise<StandardResponse<T>>;
271
+ put: (id: string | number, data: T, config?: ActionOptions) => Promise<StandardResponse<T>>;
272
+ patch: (id: string | number, data: Partial<T>, config?: ActionOptions) => Promise<StandardResponse<T>>;
273
+ remove: (id: string | number, config?: ActionOptions) => Promise<StandardResponse<any>>;
274
+ bulkDelete: (ids: Array<string | number>, config?: ActionOptions) => Promise<StandardResponse<any>>;
275
+ upload: (file: File, additionalData?: Record<string, any>, config?: ActionOptions) => Promise<StandardResponse<any>>;
276
276
  };
277
277
 
278
278
  /**
@@ -361,38 +361,19 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
361
361
  * It contains the fetched data, loading status, and any potential errors.
362
362
  * @template T The type of the data entity being managed.
363
363
  */
364
- type UseApiState<T> = StandardResponse<T | T[]>;
364
+ type UseApiState<T> = StandardResponse<T>;
365
365
  /**
366
366
  * A collection of callable functions for performing CRUD and other operations.
367
367
  * These actions automatically handle state updates like loading and refetching.
368
368
  * @template T The type of the data entity being managed.
369
369
  */
370
- interface UseApiActions<T> {
371
- /**
372
- * Manually triggers a refetch of the hook's primary data using the current query options.
373
- * This is useful for manual refresh buttons.
374
- */
375
- /**
376
- * Fetches data from an arbitrary path, independent of the hook's main state.
377
- * This is useful for one-off requests that shouldn't affect the primary data.
378
- * @param path The relative or absolute path to fetch from.
379
- * @param pathParams Optional parameters to fill dynamic parts of the path.
380
- * @param options Optional Axios request configuration.
381
- * @returns A promise that resolves with the standard API response.
382
- */
383
- fetch: (options?: QueryOptions) => Promise<void>;
384
- /** Creates a new item. */
385
- create: (newItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
386
- /** Replaces an entire item. */
387
- put: (id: string | number, item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
388
- /** Partially updates an item. */
389
- update: (id: string | number, updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
390
- /** Deletes an item. */
370
+ interface UseApiActions<T, TListItem = T extends (infer U)[] ? U : T> {
371
+ fetch: () => Promise<void>;
372
+ create: (newItem: Partial<TListItem>, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
373
+ put: (id: string | number, item: TListItem, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
374
+ update: (id: string | number, updatedItem: Partial<TListItem>, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
391
375
  remove: (id: string | number, options?: ActionOptions) => Promise<StandardResponse<any>>;
392
- /** Deletes multiple items in a single request. */
393
376
  bulkRemove: (ids: Array<string | number>, options?: ActionOptions) => Promise<StandardResponse<any>>;
394
- /** Uploads a file, optionally with additional data. */
395
- upload: (file: File, additionalData?: Record<string, any>, options?: ActionOptions) => Promise<StandardResponse<any>>;
396
377
  }
397
378
  /**
398
379
  * A collection of functions and properties for controlling the query parameters
@@ -427,18 +408,13 @@ interface UseApiQuery {
427
408
  * @template T The type of the data entity being managed.
428
409
  */
429
410
  interface UseApiReturn<T> {
430
- /** The current state of the API request (data, loading, error). */
431
411
  state: UseApiState<T>;
432
- /** A function to manually set the state. Use with caution. */
433
412
  setState: React.Dispatch<React.SetStateAction<UseApiState<T>>>;
434
- /** An object containing all available data manipulation actions (create, update, delete, etc.). */
435
413
  actions: UseApiActions<T>;
436
- /** An object for controlling the query parameters for data fetching. */
437
414
  query: UseApiQuery;
438
415
  }
439
416
 
440
- declare function useApi<T extends {
441
- id?: string | number;
442
- }>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
417
+ declare function useApi<T>(// مرن تمامًا الآن
418
+ axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
443
419
 
444
420
  export { type ActionOptions, type ApiClientConfig, type ApiError, type ApiResponse, type LogLevel, type Logger, type Middleware, type MiddlewareContext, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiQuery, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi };
package/dist/index.d.ts CHANGED
@@ -127,7 +127,7 @@ interface QueryOptions {
127
127
  /**
128
128
  * Defines additional options for action methods like create, update, or delete.
129
129
  */
130
- interface ActionOptions {
130
+ interface ActionOptions extends RequestConfig {
131
131
  /**
132
132
  * Overrides the default endpoint for a specific action. Useful for specialized routes.
133
133
  * @example update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
@@ -146,7 +146,7 @@ interface UseApiConfig<T> {
146
146
  /** The base API endpoint for the resource (e.g., '/users'). */
147
147
  endpoint: string;
148
148
  /** Initial data to populate the state before the first fetch. */
149
- initialData?: T | T[];
149
+ initialData?: T | null;
150
150
  /** Default query options to use for the initial fetch. */
151
151
  initialQuery?: QueryOptions;
152
152
  /** If false, the hook will not fetch data automatically on mount. */
@@ -259,20 +259,20 @@ interface ApiClientConfig {
259
259
  */
260
260
  declare function createApiClient(config: ApiClientConfig): AxiosInstance;
261
261
 
262
- type CrudRequestConfig = RequestConfig & ActionOptions;
263
- /**
264
- * دالة مصنع (Factory Function) لإنشاء مجموعة خدمات API قابلة لإعادة الاستخدام لنقطة نهاية (endpoint) محددة.
265
- * توفر عمليات CRUD كاملة بالإضافة إلى ميزات متقدمة مثل الحذف الجماعي ورفع الملفات.
266
- */
267
- declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: string): {
268
- get: (id?: string, config?: RequestConfig) => Promise<StandardResponse<T | T[]>>;
269
- getWithQuery: (query: string, config?: RequestConfig) => Promise<StandardResponse<T[]>>;
270
- post: (data: Partial<T>, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
271
- put: (id: string, data: T, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
272
- patch: (id: string, data: Partial<T>, config?: CrudRequestConfig) => Promise<StandardResponse<T>>;
273
- remove: (id: string, config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
274
- bulkDelete: (ids: string[], config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
275
- upload: (file: File, additionalData?: Record<string, any>, config?: CrudRequestConfig) => Promise<StandardResponse<any>>;
262
+ /**
263
+ * A factory function to create a reusable set of API services for a specific endpoint.
264
+ * It provides full CRUD operations plus advanced features like bulk deletion and file uploads,
265
+ * with intelligent, dynamic URL building.
266
+ */
267
+ declare function createApiServices<T>(axiosInstance: AxiosInstance, baseEndpoint: string): {
268
+ get: (id?: string | number, config?: ActionOptions) => Promise<StandardResponse<T>>;
269
+ getWithQuery: (query: string, config?: RequestConfig) => Promise<StandardResponse<T>>;
270
+ post: (data: Partial<T>, config?: ActionOptions) => Promise<StandardResponse<T>>;
271
+ put: (id: string | number, data: T, config?: ActionOptions) => Promise<StandardResponse<T>>;
272
+ patch: (id: string | number, data: Partial<T>, config?: ActionOptions) => Promise<StandardResponse<T>>;
273
+ remove: (id: string | number, config?: ActionOptions) => Promise<StandardResponse<any>>;
274
+ bulkDelete: (ids: Array<string | number>, config?: ActionOptions) => Promise<StandardResponse<any>>;
275
+ upload: (file: File, additionalData?: Record<string, any>, config?: ActionOptions) => Promise<StandardResponse<any>>;
276
276
  };
277
277
 
278
278
  /**
@@ -361,38 +361,19 @@ declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiErro
361
361
  * It contains the fetched data, loading status, and any potential errors.
362
362
  * @template T The type of the data entity being managed.
363
363
  */
364
- type UseApiState<T> = StandardResponse<T | T[]>;
364
+ type UseApiState<T> = StandardResponse<T>;
365
365
  /**
366
366
  * A collection of callable functions for performing CRUD and other operations.
367
367
  * These actions automatically handle state updates like loading and refetching.
368
368
  * @template T The type of the data entity being managed.
369
369
  */
370
- interface UseApiActions<T> {
371
- /**
372
- * Manually triggers a refetch of the hook's primary data using the current query options.
373
- * This is useful for manual refresh buttons.
374
- */
375
- /**
376
- * Fetches data from an arbitrary path, independent of the hook's main state.
377
- * This is useful for one-off requests that shouldn't affect the primary data.
378
- * @param path The relative or absolute path to fetch from.
379
- * @param pathParams Optional parameters to fill dynamic parts of the path.
380
- * @param options Optional Axios request configuration.
381
- * @returns A promise that resolves with the standard API response.
382
- */
383
- fetch: (options?: QueryOptions) => Promise<void>;
384
- /** Creates a new item. */
385
- create: (newItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
386
- /** Replaces an entire item. */
387
- put: (id: string | number, item: T, options?: ActionOptions) => Promise<StandardResponse<T>>;
388
- /** Partially updates an item. */
389
- update: (id: string | number, updatedItem: Partial<T>, options?: ActionOptions) => Promise<StandardResponse<T>>;
390
- /** Deletes an item. */
370
+ interface UseApiActions<T, TListItem = T extends (infer U)[] ? U : T> {
371
+ fetch: () => Promise<void>;
372
+ create: (newItem: Partial<TListItem>, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
373
+ put: (id: string | number, item: TListItem, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
374
+ update: (id: string | number, updatedItem: Partial<TListItem>, options?: ActionOptions) => Promise<StandardResponse<TListItem>>;
391
375
  remove: (id: string | number, options?: ActionOptions) => Promise<StandardResponse<any>>;
392
- /** Deletes multiple items in a single request. */
393
376
  bulkRemove: (ids: Array<string | number>, options?: ActionOptions) => Promise<StandardResponse<any>>;
394
- /** Uploads a file, optionally with additional data. */
395
- upload: (file: File, additionalData?: Record<string, any>, options?: ActionOptions) => Promise<StandardResponse<any>>;
396
377
  }
397
378
  /**
398
379
  * A collection of functions and properties for controlling the query parameters
@@ -427,18 +408,13 @@ interface UseApiQuery {
427
408
  * @template T The type of the data entity being managed.
428
409
  */
429
410
  interface UseApiReturn<T> {
430
- /** The current state of the API request (data, loading, error). */
431
411
  state: UseApiState<T>;
432
- /** A function to manually set the state. Use with caution. */
433
412
  setState: React.Dispatch<React.SetStateAction<UseApiState<T>>>;
434
- /** An object containing all available data manipulation actions (create, update, delete, etc.). */
435
413
  actions: UseApiActions<T>;
436
- /** An object for controlling the query parameters for data fetching. */
437
414
  query: UseApiQuery;
438
415
  }
439
416
 
440
- declare function useApi<T extends {
441
- id?: string | number;
442
- }>(axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
417
+ declare function useApi<T>(// مرن تمامًا الآن
418
+ axiosInstance: AxiosInstance, config: UseApiConfig<T>): UseApiReturn<T>;
443
419
 
444
420
  export { type ActionOptions, type ApiClientConfig, type ApiError, type ApiResponse, type LogLevel, type Logger, type Middleware, type MiddlewareContext, type PaginationMeta, type QueryOptions, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiActions, type UseApiConfig, type UseApiQuery, type UseApiReturn, type UseApiState, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi };