api-core-lib 5.5.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 +136 -143
- package/dist/index.d.ts +136 -143
- package/dist/index.js +1 -599
- package/dist/index.mjs +1 -556
- package/package.json +7 -4
package/dist/index.d.mts
CHANGED
|
@@ -3,14 +3,13 @@ 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;
|
|
@@ -35,34 +44,46 @@ interface ApiError {
|
|
|
35
44
|
errors?: ValidationError[];
|
|
36
45
|
requestId?: string;
|
|
37
46
|
}
|
|
47
|
+
/**
|
|
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.
|
|
63
|
+
*/
|
|
38
64
|
interface Tokens {
|
|
39
65
|
accessToken: string | null;
|
|
40
66
|
refreshToken: string | null;
|
|
41
67
|
expiresAt?: number;
|
|
42
68
|
tokenType?: string;
|
|
43
69
|
}
|
|
70
|
+
/**
|
|
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).
|
|
73
|
+
*/
|
|
44
74
|
interface TokenManager {
|
|
45
75
|
getTokens(): Promise<Tokens>;
|
|
46
76
|
setTokens(tokens: Tokens): Promise<void>;
|
|
47
77
|
clearTokens(): Promise<void>;
|
|
48
78
|
/**
|
|
49
|
-
*
|
|
50
|
-
* true
|
|
51
|
-
* false
|
|
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).
|
|
52
82
|
*/
|
|
53
83
|
isHttpOnly(): boolean;
|
|
54
84
|
}
|
|
55
85
|
/**
|
|
56
|
-
*
|
|
57
|
-
*/
|
|
58
|
-
interface Logger {
|
|
59
|
-
log(message?: any, ...optionalParams: any[]): void;
|
|
60
|
-
info(message?: any, ...optionalParams: any[]): void;
|
|
61
|
-
warn(message?: any, ...optionalParams: any[]): void;
|
|
62
|
-
error(message?: any, ...optionalParams: any[]): void;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* سياق البرمجيات الوسيطة الذي يتم تمريره لكل دالة.
|
|
86
|
+
* The context object passed to each middleware function in the pipeline.
|
|
66
87
|
*/
|
|
67
88
|
interface MiddlewareContext {
|
|
68
89
|
req: InternalAxiosRequestConfig;
|
|
@@ -72,22 +93,28 @@ interface MiddlewareContext {
|
|
|
72
93
|
custom?: Record<string, any>;
|
|
73
94
|
}
|
|
74
95
|
/**
|
|
75
|
-
*
|
|
96
|
+
* Defines the signature for a middleware function.
|
|
76
97
|
*/
|
|
77
98
|
type Middleware = (context: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Extends the default Axios request configuration with custom properties for the library.
|
|
101
|
+
*/
|
|
78
102
|
interface RequestConfig extends AxiosRequestConfig {
|
|
103
|
+
/** If true, the request will bypass the token injection logic. */
|
|
79
104
|
isPublic?: boolean;
|
|
80
|
-
|
|
81
|
-
interface RequestConfig extends AxiosRequestConfig {
|
|
105
|
+
/** A key for managing request cancellation. */
|
|
82
106
|
cancelTokenKey?: string;
|
|
107
|
+
/** Callback for upload progress events. */
|
|
83
108
|
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
109
|
+
/** Callback for download progress events. */
|
|
84
110
|
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
85
|
-
isPublic?: boolean;
|
|
86
111
|
}
|
|
87
112
|
/**
|
|
88
|
-
*
|
|
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' }
|
|
89
116
|
*/
|
|
90
|
-
interface
|
|
117
|
+
interface QueryOptions {
|
|
91
118
|
page?: number;
|
|
92
119
|
limit?: number;
|
|
93
120
|
search?: string;
|
|
@@ -96,35 +123,58 @@ interface PaginateQueryOptions {
|
|
|
96
123
|
direction: 'asc' | 'desc';
|
|
97
124
|
}[];
|
|
98
125
|
filter?: Record<string, any>;
|
|
126
|
+
[key: string]: any;
|
|
99
127
|
}
|
|
100
128
|
/**
|
|
101
|
-
*
|
|
129
|
+
* Defines additional options for action methods like create, update, or delete.
|
|
102
130
|
*/
|
|
103
|
-
interface
|
|
131
|
+
interface ActionOptions {
|
|
104
132
|
/**
|
|
105
|
-
*
|
|
106
|
-
* @
|
|
133
|
+
* Overrides the default endpoint for a specific action. Useful for specialized routes.
|
|
134
|
+
* @example update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
|
|
107
135
|
*/
|
|
108
|
-
|
|
136
|
+
endpoint?: string;
|
|
109
137
|
/**
|
|
110
|
-
*
|
|
111
|
-
* @param refreshToken - التوكن المستخدم للتجديد.
|
|
112
|
-
* @returns كائن يمثل جسم الطلب.
|
|
113
|
-
* @default (refreshToken) => ({ refresh_token: refreshToken })
|
|
138
|
+
* Overrides the `refetchAfterChange` setting for a single action.
|
|
114
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. */
|
|
115
171
|
buildRequestBody?: (refreshToken: string) => Record<string, any>;
|
|
116
|
-
/**
|
|
117
|
-
* دالة لتحديد كيفية بناء الهيدرز (headers) الخاصة بطلب التجديد.
|
|
118
|
-
* @param currentTokens - التوكنات الحالية.
|
|
119
|
-
* @returns كائن يمثل الهيدرز الإضافية.
|
|
120
|
-
* @default () => ({})
|
|
121
|
-
*/
|
|
172
|
+
/** A function to build any custom headers for the refresh call. */
|
|
122
173
|
buildRequestHeaders?: (currentTokens: Tokens) => Record<string, string>;
|
|
123
174
|
/**
|
|
124
|
-
*
|
|
125
|
-
* @param responseData
|
|
126
|
-
* @returns
|
|
127
|
-
* @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.
|
|
128
178
|
*/
|
|
129
179
|
extractTokens: (responseData: any) => {
|
|
130
180
|
accessToken: string;
|
|
@@ -134,106 +184,59 @@ interface RefreshTokenConfig {
|
|
|
134
184
|
};
|
|
135
185
|
}
|
|
136
186
|
/**
|
|
137
|
-
*
|
|
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.
|
|
138
215
|
*/
|
|
139
216
|
interface ApiClientConfig {
|
|
217
|
+
/** The base URL for all API requests. */
|
|
140
218
|
baseURL?: string;
|
|
219
|
+
/** The token manager instance responsible for handling token storage. */
|
|
141
220
|
tokenManager: TokenManager;
|
|
221
|
+
/** The request timeout in milliseconds. */
|
|
142
222
|
timeout?: number;
|
|
223
|
+
/** Default headers to be sent with every request. */
|
|
143
224
|
headers?: Record<string, string>;
|
|
225
|
+
/** If true, cookies will be sent with cross-origin requests. */
|
|
144
226
|
withCredentials?: boolean;
|
|
227
|
+
/** Configuration for the automatic token refresh mechanism. */
|
|
145
228
|
refreshTokenConfig?: RefreshTokenConfig;
|
|
229
|
+
/** A callback function executed if the token refresh process fails. */
|
|
146
230
|
onRefreshError?: (error: any) => void;
|
|
147
|
-
/**
|
|
148
|
-
* ✨ NEW: Logger مخصص. الافتراضي هو `console`.
|
|
149
|
-
*/
|
|
231
|
+
/** A custom logger instance. Defaults to the browser `console`. */
|
|
150
232
|
logger?: Logger;
|
|
151
|
-
/**
|
|
152
|
-
* ✨ NEW: مصفوفة من البرمجيات الوسيطة (Middleware) لتشغيلها مع كل طلب.
|
|
153
|
-
*/
|
|
233
|
+
/** An array of middleware functions to be executed with every request. */
|
|
154
234
|
middleware?: Middleware[];
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* يمثل بنية الاستجابة القياسية من الـ API.
|
|
158
|
-
* @template T نوع البيانات الأساسية في الاستجابة.
|
|
159
|
-
*/
|
|
160
|
-
interface ApiResponse<T = any> {
|
|
161
|
-
data: T;
|
|
162
|
-
message?: string;
|
|
163
|
-
meta?: PaginationMeta;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* يمثل بنية الاستجابة القياسية المغلفة التي قد تأتي من بعض نقاط الـ API.
|
|
167
|
-
* @template T نوع البيانات الأساسية في الاستجابة.
|
|
168
|
-
*/
|
|
169
|
-
interface ApiResponse<T = any> {
|
|
170
|
-
data: T;
|
|
171
|
-
message?: string;
|
|
172
|
-
meta?: PaginationMeta;
|
|
173
|
-
success?: boolean;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* يمثل كائن الاستجابة الموحد والنهائي الذي يرجعه كل طلب.
|
|
177
|
-
* هذا هو النوع الذي ستتعامل معه دائمًا.
|
|
178
|
-
* @template T نوع البيانات النهائية التي تريد الوصول إليها.
|
|
179
|
-
*/
|
|
180
|
-
interface StandardResponse<T> {
|
|
181
|
-
data: T | null;
|
|
182
|
-
rawResponse: any;
|
|
183
|
-
error: ApiError | null;
|
|
184
|
-
loading: boolean;
|
|
185
|
-
success: boolean;
|
|
186
|
-
message?: string;
|
|
187
|
-
validationErrors?: ValidationError[];
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* NEW: واجهة استعلام مرنة تسمح بالبارامترات القياسية (للفلترة والترقيم)
|
|
191
|
-
* بالإضافة إلى أي بارامترات مخصصة أخرى عبر الـ index signature.
|
|
192
|
-
* @example
|
|
193
|
-
* { page: 1, limit: 10, search: 'term', status: 'published', authorId: 123 }
|
|
194
|
-
*/
|
|
195
|
-
interface QueryOptions {
|
|
196
|
-
page?: number;
|
|
197
|
-
limit?: number;
|
|
198
|
-
search?: string;
|
|
199
|
-
sortBy?: {
|
|
200
|
-
key: string;
|
|
201
|
-
direction: 'asc' | 'desc';
|
|
202
|
-
}[];
|
|
203
|
-
filter?: Record<string, any>;
|
|
204
|
-
[key: string]: any;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* NEW: واجهة لتمرير خيارات إضافية لدوال الأكشن (create, update, remove).
|
|
208
|
-
*/
|
|
209
|
-
interface ActionOptions {
|
|
210
235
|
/**
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* // لتحديث عنصر وتغيير حالته عبر مسار مختلف
|
|
215
|
-
* update('123', { status: 'active' }, { endpoint: '/items/123/activate' })
|
|
236
|
+
* Sets the verbosity of the internal logger.
|
|
237
|
+
* @default 'info'
|
|
216
238
|
*/
|
|
217
|
-
|
|
218
|
-
refetch?: boolean;
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* واجهة لتهيئة الهوك `useApi`.
|
|
222
|
-
* @template T نوع البيانات التي يتعامل معها الهوك.
|
|
223
|
-
*/
|
|
224
|
-
interface UseApiConfig<T> {
|
|
225
|
-
endpoint: string;
|
|
226
|
-
initialData?: T | T[];
|
|
227
|
-
initialQuery?: QueryOptions;
|
|
228
|
-
enabled?: boolean;
|
|
229
|
-
refetchAfterChange?: boolean;
|
|
230
|
-
onSuccess?: (message: string, data?: T) => void;
|
|
231
|
-
onError?: (message: string, error?: ApiError) => void;
|
|
232
|
-
/**
|
|
233
|
-
* إعدادات Axios/Request افتراضية يتم تطبيقها على جميع طلبات الجلب (GET).
|
|
234
|
-
* مفيدة لتمرير إعدادات مثل isPublic.
|
|
235
|
-
*/
|
|
236
|
-
requestConfig?: RequestConfig;
|
|
239
|
+
logLevel?: LogLevel;
|
|
237
240
|
}
|
|
238
241
|
|
|
239
242
|
/**
|
|
@@ -246,16 +249,6 @@ interface UseApiConfig<T> {
|
|
|
246
249
|
|
|
247
250
|
/**
|
|
248
251
|
* Creates and configures a new Axios instance with advanced features.
|
|
249
|
-
*
|
|
250
|
-
* This factory function is the central entry point of the library. It sets up
|
|
251
|
-
* request and response interceptors to handle:
|
|
252
|
-
* - Secure token management (supporting httpOnly and client-side storage).
|
|
253
|
-
* - A flexible middleware pipeline for custom logic.
|
|
254
|
-
* - Centralized logging for observability.
|
|
255
|
-
* - Automatic token refresh for client-side scenarios.
|
|
256
|
-
*
|
|
257
|
-
* @param {ApiClientConfig} config - The configuration object for the API client.
|
|
258
|
-
* @returns {AxiosInstance} A fully configured Axios instance ready for use.
|
|
259
252
|
*/
|
|
260
253
|
declare function createApiClient(config: ApiClientConfig): AxiosInstance;
|
|
261
254
|
|
|
@@ -376,4 +369,4 @@ declare function useApi<T extends {
|
|
|
376
369
|
};
|
|
377
370
|
};
|
|
378
371
|
|
|
379
|
-
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 };
|