api-core-lib 4.4.4 → 5.5.5
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 +157 -126
- package/dist/index.d.ts +157 -126
- package/dist/index.js +1 -575
- package/dist/index.mjs +1 -532
- package/package.json +43 -40
package/dist/index.d.mts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method } from 'axios';
|
|
2
2
|
import * as react from 'react';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @file src/types.ts
|
|
6
|
-
* @description
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* في جميع أنحاء المشروع ويوفر واجهة واضحة للمستخدم النهائي.
|
|
6
|
+
* @description This file serves as the central source for all data types and interfaces
|
|
7
|
+
* used throughout the library. Defining types here ensures consistency, type safety,
|
|
8
|
+
* and provides a clear API contract for the end-user.
|
|
10
9
|
*/
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* Defines the structure for pagination metadata returned by the API.
|
|
14
13
|
*/
|
|
15
14
|
interface PaginationMeta {
|
|
16
15
|
itemsPerPage: number;
|
|
@@ -19,14 +18,24 @@ interface PaginationMeta {
|
|
|
19
18
|
totalPages: number;
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
22
|
-
*
|
|
21
|
+
* Represents the standard wrapper that an API might use for its responses.
|
|
22
|
+
* @template T The type of the core data payload.
|
|
23
|
+
*/
|
|
24
|
+
interface ApiResponse<T = any> {
|
|
25
|
+
success?: boolean;
|
|
26
|
+
data: T;
|
|
27
|
+
message?: string;
|
|
28
|
+
meta?: PaginationMeta;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Defines the structure for a single field validation error.
|
|
23
32
|
*/
|
|
24
33
|
interface ValidationError {
|
|
25
34
|
field: string;
|
|
26
35
|
message: string;
|
|
27
36
|
}
|
|
28
37
|
/**
|
|
29
|
-
*
|
|
38
|
+
* Represents the unified error object produced by the library for any failed request.
|
|
30
39
|
*/
|
|
31
40
|
interface ApiError {
|
|
32
41
|
message: string;
|
|
@@ -36,7 +45,21 @@ interface ApiError {
|
|
|
36
45
|
requestId?: string;
|
|
37
46
|
}
|
|
38
47
|
/**
|
|
39
|
-
*
|
|
48
|
+
* The standardized, final response object that every library function returns.
|
|
49
|
+
* This is the primary type developers will interact with.
|
|
50
|
+
* @template T The type of the final, unwrapped data.
|
|
51
|
+
*/
|
|
52
|
+
interface StandardResponse<T> {
|
|
53
|
+
data: T | null;
|
|
54
|
+
rawResponse: any;
|
|
55
|
+
error: ApiError | null;
|
|
56
|
+
loading: boolean;
|
|
57
|
+
success: boolean;
|
|
58
|
+
message?: string;
|
|
59
|
+
validationErrors?: ValidationError[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Represents the set of authentication tokens managed by the library.
|
|
40
63
|
*/
|
|
41
64
|
interface Tokens {
|
|
42
65
|
accessToken: string | null;
|
|
@@ -45,28 +68,53 @@ interface Tokens {
|
|
|
45
68
|
tokenType?: string;
|
|
46
69
|
}
|
|
47
70
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
71
|
+
* An interface for a token manager, allowing the logic for token storage to be decoupled.
|
|
72
|
+
* Consumers of the library can provide their own implementation (e.g., LocalStorage, Cookies).
|
|
50
73
|
*/
|
|
51
74
|
interface TokenManager {
|
|
52
75
|
getTokens(): Promise<Tokens>;
|
|
53
76
|
setTokens(tokens: Tokens): Promise<void>;
|
|
54
77
|
clearTokens(): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Determines the operating context.
|
|
80
|
+
* @returns `true` if tokens are in secure httpOnly cookies (server-side context).
|
|
81
|
+
* @returns `false` if tokens are accessible from the client (e.g., localStorage).
|
|
82
|
+
*/
|
|
55
83
|
isHttpOnly(): boolean;
|
|
56
84
|
}
|
|
57
85
|
/**
|
|
58
|
-
*
|
|
86
|
+
* The context object passed to each middleware function in the pipeline.
|
|
87
|
+
*/
|
|
88
|
+
interface MiddlewareContext {
|
|
89
|
+
req: InternalAxiosRequestConfig;
|
|
90
|
+
res?: AxiosResponse;
|
|
91
|
+
error?: any;
|
|
92
|
+
logger: Logger;
|
|
93
|
+
custom?: Record<string, any>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Defines the signature for a middleware function.
|
|
97
|
+
*/
|
|
98
|
+
type Middleware = (context: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Extends the default Axios request configuration with custom properties for the library.
|
|
59
101
|
*/
|
|
60
102
|
interface RequestConfig extends AxiosRequestConfig {
|
|
103
|
+
/** If true, the request will bypass the token injection logic. */
|
|
104
|
+
isPublic?: boolean;
|
|
105
|
+
/** A key for managing request cancellation. */
|
|
61
106
|
cancelTokenKey?: string;
|
|
107
|
+
/** Callback for upload progress events. */
|
|
62
108
|
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
109
|
+
/** Callback for download progress events. */
|
|
63
110
|
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
64
|
-
isPublic?: boolean;
|
|
65
111
|
}
|
|
66
112
|
/**
|
|
67
|
-
*
|
|
113
|
+
* Defines a flexible query options interface for pagination, sorting, filtering, and searching.
|
|
114
|
+
* Supports standard keys as well as any custom top-level query parameters.
|
|
115
|
+
* @example { page: 1, limit: 10, search: 'term', status: 'published' }
|
|
68
116
|
*/
|
|
69
|
-
interface
|
|
117
|
+
interface QueryOptions {
|
|
70
118
|
page?: number;
|
|
71
119
|
limit?: number;
|
|
72
120
|
search?: string;
|
|
@@ -75,35 +123,58 @@ interface PaginateQueryOptions {
|
|
|
75
123
|
direction: 'asc' | 'desc';
|
|
76
124
|
}[];
|
|
77
125
|
filter?: Record<string, any>;
|
|
126
|
+
[key: string]: any;
|
|
78
127
|
}
|
|
79
128
|
/**
|
|
80
|
-
*
|
|
129
|
+
* Defines additional options for action methods like create, update, or delete.
|
|
81
130
|
*/
|
|
82
|
-
interface
|
|
131
|
+
interface ActionOptions {
|
|
83
132
|
/**
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
133
|
+
* Overrides the default endpoint for a specific action. Useful for specialized routes.
|
|
134
|
+
* @example update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
|
|
86
135
|
*/
|
|
87
|
-
|
|
136
|
+
endpoint?: string;
|
|
88
137
|
/**
|
|
89
|
-
*
|
|
90
|
-
* @param refreshToken - التوكن المستخدم للتجديد.
|
|
91
|
-
* @returns كائن يمثل جسم الطلب.
|
|
92
|
-
* @default (refreshToken) => ({ refresh_token: refreshToken })
|
|
138
|
+
* Overrides the `refetchAfterChange` setting for a single action.
|
|
93
139
|
*/
|
|
140
|
+
refetch?: boolean;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The main configuration object for the `useApi` hook.
|
|
144
|
+
* @template T The data type the hook will manage.
|
|
145
|
+
*/
|
|
146
|
+
interface UseApiConfig<T> {
|
|
147
|
+
/** The base API endpoint for the resource (e.g., '/users'). */
|
|
148
|
+
endpoint: string;
|
|
149
|
+
/** Initial data to populate the state before the first fetch. */
|
|
150
|
+
initialData?: T | T[];
|
|
151
|
+
/** Default query options to use for the initial fetch. */
|
|
152
|
+
initialQuery?: QueryOptions;
|
|
153
|
+
/** If false, the hook will not fetch data automatically on mount. */
|
|
154
|
+
enabled?: boolean;
|
|
155
|
+
/** If true, data will be refetched automatically after a successful create, update, or delete action. */
|
|
156
|
+
refetchAfterChange?: boolean;
|
|
157
|
+
/** A default `RequestConfig` to apply to all GET requests made by the hook. */
|
|
158
|
+
requestConfig?: RequestConfig;
|
|
159
|
+
/** Callback function executed on a successful action. */
|
|
160
|
+
onSuccess?: (message: string, data?: T) => void;
|
|
161
|
+
/** Callback function executed on a failed action. */
|
|
162
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Defines the configuration for the automatic token refresh mechanism.
|
|
166
|
+
*/
|
|
167
|
+
interface RefreshTokenConfig {
|
|
168
|
+
/** The API path for the token refresh endpoint (e.g., '/auth/refresh'). */
|
|
169
|
+
path: string;
|
|
170
|
+
/** A function to build the request body for the refresh call. */
|
|
94
171
|
buildRequestBody?: (refreshToken: string) => Record<string, any>;
|
|
95
|
-
/**
|
|
96
|
-
* دالة لتحديد كيفية بناء الهيدرز (headers) الخاصة بطلب التجديد.
|
|
97
|
-
* @param currentTokens - التوكنات الحالية.
|
|
98
|
-
* @returns كائن يمثل الهيدرز الإضافية.
|
|
99
|
-
* @default () => ({})
|
|
100
|
-
*/
|
|
172
|
+
/** A function to build any custom headers for the refresh call. */
|
|
101
173
|
buildRequestHeaders?: (currentTokens: Tokens) => Record<string, string>;
|
|
102
174
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param responseData
|
|
105
|
-
* @returns
|
|
106
|
-
* @default (data) => ({ accessToken: data.access_token, ... })
|
|
175
|
+
* A function to extract the new tokens from the refresh API's response data.
|
|
176
|
+
* @param responseData The raw data from the refresh API response.
|
|
177
|
+
* @returns An object containing the new token details.
|
|
107
178
|
*/
|
|
108
179
|
extractTokens: (responseData: any) => {
|
|
109
180
|
accessToken: string;
|
|
@@ -113,113 +184,72 @@ interface RefreshTokenConfig {
|
|
|
113
184
|
};
|
|
114
185
|
}
|
|
115
186
|
/**
|
|
116
|
-
*
|
|
187
|
+
* Defines the available levels for logging.
|
|
188
|
+
* 'debug': Logs everything.
|
|
189
|
+
* 'info': Logs standard requests and responses.
|
|
190
|
+
* 'warn': Logs warnings and errors.
|
|
191
|
+
* 'error': Logs only critical errors.
|
|
192
|
+
* 'silent': Disables all logging.
|
|
193
|
+
*/
|
|
194
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
195
|
+
/**
|
|
196
|
+
* An interface for a custom logger, compatible with the standard `console` object.
|
|
197
|
+
* It now includes a `debug` method for more granular logging.
|
|
198
|
+
*/
|
|
199
|
+
interface Logger {
|
|
200
|
+
/** Logs a standard message. In our wrapper, this is often an alias for `info` or `debug`. */
|
|
201
|
+
log(message?: any, ...optionalParams: any[]): void;
|
|
202
|
+
/** Logs an informational message. */
|
|
203
|
+
info(message?: any, ...optionalParams: any[]): void;
|
|
204
|
+
/** Logs a warning message. */
|
|
205
|
+
warn(message?: any, ...optionalParams: any[]): void;
|
|
206
|
+
/** Logs an error message. */
|
|
207
|
+
error(message?: any, ...optionalParams: any[]): void;
|
|
208
|
+
/**
|
|
209
|
+
* Logs a debug message, typically for verbose, development-only output.
|
|
210
|
+
*/
|
|
211
|
+
debug(message?: any, ...optionalParams: any[]): void;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The main configuration object for the `createApiClient` factory function.
|
|
117
215
|
*/
|
|
118
216
|
interface ApiClientConfig {
|
|
217
|
+
/** The base URL for all API requests. */
|
|
119
218
|
baseURL?: string;
|
|
219
|
+
/** The token manager instance responsible for handling token storage. */
|
|
120
220
|
tokenManager: TokenManager;
|
|
221
|
+
/** The request timeout in milliseconds. */
|
|
121
222
|
timeout?: number;
|
|
223
|
+
/** Default headers to be sent with every request. */
|
|
122
224
|
headers?: Record<string, string>;
|
|
225
|
+
/** If true, cookies will be sent with cross-origin requests. */
|
|
123
226
|
withCredentials?: boolean;
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* إعدادات مخصصة لعملية تجديد التوكن.
|
|
127
|
-
* إذا لم يتم توفيرها، ستتوقف محاولات التجديد التلقائي.
|
|
128
|
-
*/
|
|
227
|
+
/** Configuration for the automatic token refresh mechanism. */
|
|
129
228
|
refreshTokenConfig?: RefreshTokenConfig;
|
|
229
|
+
/** A callback function executed if the token refresh process fails. */
|
|
130
230
|
onRefreshError?: (error: any) => void;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
*/
|
|
136
|
-
interface ApiResponse<T = any> {
|
|
137
|
-
data: T;
|
|
138
|
-
message?: string;
|
|
139
|
-
meta?: PaginationMeta;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* يمثل بنية الاستجابة القياسية المغلفة التي قد تأتي من بعض نقاط الـ API.
|
|
143
|
-
* @template T نوع البيانات الأساسية في الاستجابة.
|
|
144
|
-
*/
|
|
145
|
-
interface ApiResponse<T = any> {
|
|
146
|
-
data: T;
|
|
147
|
-
message?: string;
|
|
148
|
-
meta?: PaginationMeta;
|
|
149
|
-
success?: boolean;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* يمثل كائن الاستجابة الموحد والنهائي الذي يرجعه كل طلب.
|
|
153
|
-
* هذا هو النوع الذي ستتعامل معه دائمًا.
|
|
154
|
-
* @template T نوع البيانات النهائية التي تريد الوصول إليها.
|
|
155
|
-
*/
|
|
156
|
-
interface StandardResponse<T> {
|
|
157
|
-
data: T | null;
|
|
158
|
-
rawResponse: any;
|
|
159
|
-
error: ApiError | null;
|
|
160
|
-
loading: boolean;
|
|
161
|
-
success: boolean;
|
|
162
|
-
message?: string;
|
|
163
|
-
validationErrors?: ValidationError[];
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* NEW: واجهة استعلام مرنة تسمح بالبارامترات القياسية (للفلترة والترقيم)
|
|
167
|
-
* بالإضافة إلى أي بارامترات مخصصة أخرى عبر الـ index signature.
|
|
168
|
-
* @example
|
|
169
|
-
* { page: 1, limit: 10, search: 'term', status: 'published', authorId: 123 }
|
|
170
|
-
*/
|
|
171
|
-
interface QueryOptions {
|
|
172
|
-
page?: number;
|
|
173
|
-
limit?: number;
|
|
174
|
-
search?: string;
|
|
175
|
-
sortBy?: {
|
|
176
|
-
key: string;
|
|
177
|
-
direction: 'asc' | 'desc';
|
|
178
|
-
}[];
|
|
179
|
-
filter?: Record<string, any>;
|
|
180
|
-
[key: string]: any;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* NEW: واجهة لتمرير خيارات إضافية لدوال الأكشن (create, update, remove).
|
|
184
|
-
*/
|
|
185
|
-
interface ActionOptions {
|
|
231
|
+
/** A custom logger instance. Defaults to the browser `console`. */
|
|
232
|
+
logger?: Logger;
|
|
233
|
+
/** An array of middleware functions to be executed with every request. */
|
|
234
|
+
middleware?: Middleware[];
|
|
186
235
|
/**
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* // لتحديث عنصر وتغيير حالته عبر مسار مختلف
|
|
191
|
-
* update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
|
|
236
|
+
* Sets the verbosity of the internal logger.
|
|
237
|
+
* @default 'info'
|
|
192
238
|
*/
|
|
193
|
-
|
|
194
|
-
refetch?: boolean;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* واجهة لتهيئة الهوك `useApi`.
|
|
198
|
-
* @template T نوع البيانات التي يتعامل معها الهوك.
|
|
199
|
-
*/
|
|
200
|
-
interface UseApiConfig<T> {
|
|
201
|
-
endpoint: string;
|
|
202
|
-
initialData?: T | T[];
|
|
203
|
-
initialQuery?: QueryOptions;
|
|
204
|
-
enabled?: boolean;
|
|
205
|
-
refetchAfterChange?: boolean;
|
|
206
|
-
onSuccess?: (message: string, data?: T) => void;
|
|
207
|
-
onError?: (message: string, error?: ApiError) => void;
|
|
208
|
-
/**
|
|
209
|
-
* إعدادات Axios/Request افتراضية يتم تطبيقها على جميع طلبات الجلب (GET).
|
|
210
|
-
* مفيدة لتمرير إعدادات مثل isPublic.
|
|
211
|
-
*/
|
|
212
|
-
requestConfig?: RequestConfig;
|
|
239
|
+
logLevel?: LogLevel;
|
|
213
240
|
}
|
|
214
241
|
|
|
215
242
|
/**
|
|
216
243
|
* @file src/core/client.ts
|
|
217
|
-
* @description
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
244
|
+
* @description This is the heart of the API client library.
|
|
245
|
+
* The `createApiClient` function constructs and configures a sophisticated Axios instance.
|
|
246
|
+
* It features intelligent, security-first token management, a flexible middleware system,
|
|
247
|
+
* and customizable logging, all designed to work seamlessly in modern web frameworks like Next.js.
|
|
221
248
|
*/
|
|
222
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Creates and configures a new Axios instance with advanced features.
|
|
252
|
+
*/
|
|
223
253
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
|
224
254
|
|
|
225
255
|
type CrudRequestConfig = RequestConfig & ActionOptions;
|
|
@@ -283,6 +313,7 @@ declare function createApiActions<TActions extends Record<string, {
|
|
|
283
313
|
endpoint: string;
|
|
284
314
|
requestType: any;
|
|
285
315
|
responseType: any;
|
|
316
|
+
log?: boolean;
|
|
286
317
|
}>>(axiosInstance: AxiosInstance, actionsConfig: TActions): {
|
|
287
318
|
[K in keyof TActions]: ApiAction<TActions[K]['requestType'], TActions[K]['responseType']>;
|
|
288
319
|
};
|
|
@@ -306,7 +337,7 @@ declare const cacheManager: CacheManager;
|
|
|
306
337
|
* @param responseOrError The raw Axios response or a pre-processed ApiError.
|
|
307
338
|
* @returns A standardized `StandardResponse` object with direct access to `.data`.
|
|
308
339
|
*/
|
|
309
|
-
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError) => StandardResponse<T>;
|
|
340
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError, log?: boolean) => StandardResponse<T>;
|
|
310
341
|
|
|
311
342
|
declare function useApi<T extends {
|
|
312
343
|
id?: string | number;
|
|
@@ -338,4 +369,4 @@ declare function useApi<T extends {
|
|
|
338
369
|
};
|
|
339
370
|
};
|
|
340
371
|
|
|
341
|
-
export { type ActionOptions, type ApiClientConfig, type ApiError, type ApiResponse, type
|
|
372
|
+
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 UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi };
|