api-core-lib 3.2.2 → 3.3.2
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 +76 -28
- package/dist/index.d.ts +76 -28
- package/dist/index.js +51 -18
- package/dist/index.mjs +50 -18
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, AxiosResponse } from 'axios';
|
1
|
+
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method, AxiosResponse } from 'axios';
|
2
2
|
import * as react from 'react';
|
3
3
|
|
4
4
|
/**
|
@@ -18,15 +18,6 @@ interface PaginationMeta {
|
|
18
18
|
currentPage: number;
|
19
19
|
totalPages: number;
|
20
20
|
}
|
21
|
-
/**
|
22
|
-
* يمثل بنية الاستجابة القياسية من الـ API.
|
23
|
-
* @template T نوع البيانات الأساسية في الاستجابة.
|
24
|
-
*/
|
25
|
-
interface ApiResponse<T = any> {
|
26
|
-
data: T;
|
27
|
-
message?: string;
|
28
|
-
meta?: PaginationMeta;
|
29
|
-
}
|
30
21
|
/**
|
31
22
|
* يمثل خطأ التحقق من صحة حقل معين.
|
32
23
|
*/
|
@@ -44,18 +35,6 @@ interface ApiError {
|
|
44
35
|
errors?: ValidationError[];
|
45
36
|
requestId?: string;
|
46
37
|
}
|
47
|
-
/**
|
48
|
-
* يمثل كائن الاستجابة الموحد الذي يرجعه كل طلب.
|
49
|
-
* @template T نوع البيانات المتوقعة في حالة النجاح.
|
50
|
-
*/
|
51
|
-
interface StandardResponse<T> {
|
52
|
-
response?: ApiResponse<T> | T;
|
53
|
-
error: ApiError | null;
|
54
|
-
loading: boolean;
|
55
|
-
success: boolean;
|
56
|
-
message?: string;
|
57
|
-
validationErrors?: ValidationError[];
|
58
|
-
}
|
59
38
|
/**
|
60
39
|
* يمثل مجموعة التوكنات التي يتم إدارتها.
|
61
40
|
*/
|
@@ -162,6 +141,39 @@ interface ApiClientConfig {
|
|
162
141
|
refreshTokenConfig?: RefreshTokenConfig;
|
163
142
|
onRefreshError?: (error: any) => void;
|
164
143
|
}
|
144
|
+
/**
|
145
|
+
* يمثل بنية الاستجابة القياسية من الـ API.
|
146
|
+
* @template T نوع البيانات الأساسية في الاستجابة.
|
147
|
+
*/
|
148
|
+
interface ApiResponse<T = any> {
|
149
|
+
data: T;
|
150
|
+
message?: string;
|
151
|
+
meta?: PaginationMeta;
|
152
|
+
}
|
153
|
+
/**
|
154
|
+
* يمثل بنية الاستجابة القياسية المغلفة التي قد تأتي من بعض نقاط الـ API.
|
155
|
+
* @template T نوع البيانات الأساسية في الاستجابة.
|
156
|
+
*/
|
157
|
+
interface ApiResponse<T = any> {
|
158
|
+
data: T;
|
159
|
+
message?: string;
|
160
|
+
meta?: PaginationMeta;
|
161
|
+
success?: boolean;
|
162
|
+
}
|
163
|
+
/**
|
164
|
+
* يمثل كائن الاستجابة الموحد والنهائي الذي يرجعه كل طلب.
|
165
|
+
* هذا هو النوع الذي ستتعامل معه دائمًا.
|
166
|
+
* @template T نوع البيانات النهائية التي تريد الوصول إليها.
|
167
|
+
*/
|
168
|
+
interface StandardResponse<T> {
|
169
|
+
data: T | null;
|
170
|
+
rawResponse: any;
|
171
|
+
error: ApiError | null;
|
172
|
+
loading: boolean;
|
173
|
+
success: boolean;
|
174
|
+
message?: string;
|
175
|
+
validationErrors?: ValidationError[];
|
176
|
+
}
|
165
177
|
|
166
178
|
/**
|
167
179
|
* @file src/core/client.ts
|
@@ -191,6 +203,39 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
191
203
|
|
192
204
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
193
205
|
|
206
|
+
/**
|
207
|
+
* Defines a single custom API action.
|
208
|
+
* @template TRequest - The type of the data sent in the request body/params.
|
209
|
+
* @template TResponse - The type of the data expected in the successful API response.
|
210
|
+
*/
|
211
|
+
type ApiAction<TRequest, TResponse> = (payload: TRequest, config?: RequestConfig) => Promise<StandardResponse<TResponse>>;
|
212
|
+
/**
|
213
|
+
* A factory function to create a collection of typed, custom API actions.
|
214
|
+
*
|
215
|
+
* @template TActions - An object type where keys are action names and values are objects
|
216
|
+
* defining the endpoint, method, and types for that action.
|
217
|
+
* @param axiosInstance - The configured Axios instance from `createApiClient`.
|
218
|
+
* @param actionsConfig - An object defining the configuration for each custom action.
|
219
|
+
* @returns A fully-typed object of executable API action functions.
|
220
|
+
*
|
221
|
+
* @example
|
222
|
+
* const authActions = createApiActions(apiClient, {
|
223
|
+
* login: { method: 'POST', endpoint: '/auth/login', requestType: {} as LoginCredentials, responseType: {} as AuthResponse },
|
224
|
+
* getProfile: { method: 'GET', endpoint: '/user/profile', requestType: {} as void, responseType: {} as UserProfile }
|
225
|
+
* });
|
226
|
+
*
|
227
|
+
* // Usage:
|
228
|
+
* const result = await authActions.login({ email: '..', password: '..' });
|
229
|
+
*/
|
230
|
+
declare function createApiActions<TActions extends Record<string, {
|
231
|
+
method: Method;
|
232
|
+
endpoint: string;
|
233
|
+
requestType: any;
|
234
|
+
responseType: any;
|
235
|
+
}>>(axiosInstance: AxiosInstance, actionsConfig: TActions): {
|
236
|
+
[K in keyof TActions]: ApiAction<TActions[K]['requestType'], TActions[K]['responseType']>;
|
237
|
+
};
|
238
|
+
|
194
239
|
declare class CacheManager {
|
195
240
|
private cache;
|
196
241
|
private defaultDuration;
|
@@ -202,12 +247,15 @@ declare class CacheManager {
|
|
202
247
|
declare const cacheManager: CacheManager;
|
203
248
|
|
204
249
|
/**
|
205
|
-
*
|
206
|
-
*
|
207
|
-
* ...
|
250
|
+
* A smart response processor that normalizes API responses.
|
251
|
+
* It intelligently unwraps nested data from standard API envelopes
|
252
|
+
* (like { success: true, data: {...} }) and provides direct access
|
253
|
+
* to the core data, while still handling errors consistently.
|
254
|
+
*
|
255
|
+
* @param responseOrError The raw Axios response or a pre-processed ApiError.
|
256
|
+
* @returns A standardized `StandardResponse` object with direct access to `.data`.
|
208
257
|
*/
|
209
|
-
|
210
|
-
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
258
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError) => StandardResponse<T>;
|
211
259
|
|
212
260
|
declare function useApi<T extends {
|
213
261
|
id?: string | number;
|
@@ -235,4 +283,4 @@ declare function useApi<T extends {
|
|
235
283
|
};
|
236
284
|
};
|
237
285
|
|
238
|
-
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiClient, createApiServices, processResponse, useApi };
|
286
|
+
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi };
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, AxiosResponse } from 'axios';
|
1
|
+
import { ResponseType, AxiosRequestConfig, AxiosProgressEvent, AxiosInstance, Method, AxiosResponse } from 'axios';
|
2
2
|
import * as react from 'react';
|
3
3
|
|
4
4
|
/**
|
@@ -18,15 +18,6 @@ interface PaginationMeta {
|
|
18
18
|
currentPage: number;
|
19
19
|
totalPages: number;
|
20
20
|
}
|
21
|
-
/**
|
22
|
-
* يمثل بنية الاستجابة القياسية من الـ API.
|
23
|
-
* @template T نوع البيانات الأساسية في الاستجابة.
|
24
|
-
*/
|
25
|
-
interface ApiResponse<T = any> {
|
26
|
-
data: T;
|
27
|
-
message?: string;
|
28
|
-
meta?: PaginationMeta;
|
29
|
-
}
|
30
21
|
/**
|
31
22
|
* يمثل خطأ التحقق من صحة حقل معين.
|
32
23
|
*/
|
@@ -44,18 +35,6 @@ interface ApiError {
|
|
44
35
|
errors?: ValidationError[];
|
45
36
|
requestId?: string;
|
46
37
|
}
|
47
|
-
/**
|
48
|
-
* يمثل كائن الاستجابة الموحد الذي يرجعه كل طلب.
|
49
|
-
* @template T نوع البيانات المتوقعة في حالة النجاح.
|
50
|
-
*/
|
51
|
-
interface StandardResponse<T> {
|
52
|
-
response?: ApiResponse<T> | T;
|
53
|
-
error: ApiError | null;
|
54
|
-
loading: boolean;
|
55
|
-
success: boolean;
|
56
|
-
message?: string;
|
57
|
-
validationErrors?: ValidationError[];
|
58
|
-
}
|
59
38
|
/**
|
60
39
|
* يمثل مجموعة التوكنات التي يتم إدارتها.
|
61
40
|
*/
|
@@ -162,6 +141,39 @@ interface ApiClientConfig {
|
|
162
141
|
refreshTokenConfig?: RefreshTokenConfig;
|
163
142
|
onRefreshError?: (error: any) => void;
|
164
143
|
}
|
144
|
+
/**
|
145
|
+
* يمثل بنية الاستجابة القياسية من الـ API.
|
146
|
+
* @template T نوع البيانات الأساسية في الاستجابة.
|
147
|
+
*/
|
148
|
+
interface ApiResponse<T = any> {
|
149
|
+
data: T;
|
150
|
+
message?: string;
|
151
|
+
meta?: PaginationMeta;
|
152
|
+
}
|
153
|
+
/**
|
154
|
+
* يمثل بنية الاستجابة القياسية المغلفة التي قد تأتي من بعض نقاط الـ API.
|
155
|
+
* @template T نوع البيانات الأساسية في الاستجابة.
|
156
|
+
*/
|
157
|
+
interface ApiResponse<T = any> {
|
158
|
+
data: T;
|
159
|
+
message?: string;
|
160
|
+
meta?: PaginationMeta;
|
161
|
+
success?: boolean;
|
162
|
+
}
|
163
|
+
/**
|
164
|
+
* يمثل كائن الاستجابة الموحد والنهائي الذي يرجعه كل طلب.
|
165
|
+
* هذا هو النوع الذي ستتعامل معه دائمًا.
|
166
|
+
* @template T نوع البيانات النهائية التي تريد الوصول إليها.
|
167
|
+
*/
|
168
|
+
interface StandardResponse<T> {
|
169
|
+
data: T | null;
|
170
|
+
rawResponse: any;
|
171
|
+
error: ApiError | null;
|
172
|
+
loading: boolean;
|
173
|
+
success: boolean;
|
174
|
+
message?: string;
|
175
|
+
validationErrors?: ValidationError[];
|
176
|
+
}
|
165
177
|
|
166
178
|
/**
|
167
179
|
* @file src/core/client.ts
|
@@ -191,6 +203,39 @@ declare function createApiServices<T>(axiosInstance: AxiosInstance, endpoint: st
|
|
191
203
|
|
192
204
|
declare function buildPaginateQuery(query: PaginateQueryOptions): string;
|
193
205
|
|
206
|
+
/**
|
207
|
+
* Defines a single custom API action.
|
208
|
+
* @template TRequest - The type of the data sent in the request body/params.
|
209
|
+
* @template TResponse - The type of the data expected in the successful API response.
|
210
|
+
*/
|
211
|
+
type ApiAction<TRequest, TResponse> = (payload: TRequest, config?: RequestConfig) => Promise<StandardResponse<TResponse>>;
|
212
|
+
/**
|
213
|
+
* A factory function to create a collection of typed, custom API actions.
|
214
|
+
*
|
215
|
+
* @template TActions - An object type where keys are action names and values are objects
|
216
|
+
* defining the endpoint, method, and types for that action.
|
217
|
+
* @param axiosInstance - The configured Axios instance from `createApiClient`.
|
218
|
+
* @param actionsConfig - An object defining the configuration for each custom action.
|
219
|
+
* @returns A fully-typed object of executable API action functions.
|
220
|
+
*
|
221
|
+
* @example
|
222
|
+
* const authActions = createApiActions(apiClient, {
|
223
|
+
* login: { method: 'POST', endpoint: '/auth/login', requestType: {} as LoginCredentials, responseType: {} as AuthResponse },
|
224
|
+
* getProfile: { method: 'GET', endpoint: '/user/profile', requestType: {} as void, responseType: {} as UserProfile }
|
225
|
+
* });
|
226
|
+
*
|
227
|
+
* // Usage:
|
228
|
+
* const result = await authActions.login({ email: '..', password: '..' });
|
229
|
+
*/
|
230
|
+
declare function createApiActions<TActions extends Record<string, {
|
231
|
+
method: Method;
|
232
|
+
endpoint: string;
|
233
|
+
requestType: any;
|
234
|
+
responseType: any;
|
235
|
+
}>>(axiosInstance: AxiosInstance, actionsConfig: TActions): {
|
236
|
+
[K in keyof TActions]: ApiAction<TActions[K]['requestType'], TActions[K]['responseType']>;
|
237
|
+
};
|
238
|
+
|
194
239
|
declare class CacheManager {
|
195
240
|
private cache;
|
196
241
|
private defaultDuration;
|
@@ -202,12 +247,15 @@ declare class CacheManager {
|
|
202
247
|
declare const cacheManager: CacheManager;
|
203
248
|
|
204
249
|
/**
|
205
|
-
*
|
206
|
-
*
|
207
|
-
* ...
|
250
|
+
* A smart response processor that normalizes API responses.
|
251
|
+
* It intelligently unwraps nested data from standard API envelopes
|
252
|
+
* (like { success: true, data: {...} }) and provides direct access
|
253
|
+
* to the core data, while still handling errors consistently.
|
254
|
+
*
|
255
|
+
* @param responseOrError The raw Axios response or a pre-processed ApiError.
|
256
|
+
* @returns A standardized `StandardResponse` object with direct access to `.data`.
|
208
257
|
*/
|
209
|
-
|
210
|
-
declare const processResponse: <T>(responseOrError: AxiosResponse<ApiResponse<T>> | ApiError) => StandardResponse<T>;
|
258
|
+
declare const processResponse: <T>(responseOrError: AxiosResponse<any> | ApiError) => StandardResponse<T>;
|
211
259
|
|
212
260
|
declare function useApi<T extends {
|
213
261
|
id?: string | number;
|
@@ -235,4 +283,4 @@ declare function useApi<T extends {
|
|
235
283
|
};
|
236
284
|
};
|
237
285
|
|
238
|
-
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiClient, createApiServices, processResponse, useApi };
|
286
|
+
export { type ApiClientConfig, type ApiError, type ApiResponse, type PaginateQueryOptions, type PaginationMeta, type RefreshTokenConfig, type RequestConfig, type StandardResponse, type TokenManager, type Tokens, type UseApiConfig, type ValidationError, buildPaginateQuery, cacheManager, createApiActions, createApiClient, createApiServices, processResponse, useApi };
|
package/dist/index.js
CHANGED
@@ -32,6 +32,7 @@ var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
33
33
|
buildPaginateQuery: () => buildPaginateQuery,
|
34
34
|
cacheManager: () => cacheManager,
|
35
|
+
createApiActions: () => createApiActions,
|
35
36
|
createApiClient: () => createApiClient,
|
36
37
|
createApiServices: () => createApiServices,
|
37
38
|
processResponse: () => processResponse,
|
@@ -188,37 +189,38 @@ function isAxiosResponse(obj) {
|
|
188
189
|
var processResponse = (responseOrError) => {
|
189
190
|
if (isApiError(responseOrError)) {
|
190
191
|
return {
|
191
|
-
|
192
|
+
data: null,
|
193
|
+
rawResponse: void 0,
|
192
194
|
error: responseOrError,
|
193
195
|
validationErrors: responseOrError.errors || [],
|
194
|
-
// الخطأ الآن صحيح
|
195
196
|
success: false,
|
196
197
|
loading: false,
|
197
198
|
message: responseOrError.message
|
198
199
|
};
|
199
200
|
}
|
200
201
|
if (isAxiosResponse(responseOrError)) {
|
201
|
-
const
|
202
|
+
const rawData = responseOrError.data;
|
203
|
+
const isWrappedResponse = rawData && typeof rawData.success === "boolean" && rawData.data !== void 0;
|
204
|
+
const finalData = isWrappedResponse ? rawData.data : rawData;
|
205
|
+
const message = isWrappedResponse ? rawData.message : "Request successful.";
|
202
206
|
return {
|
203
|
-
|
207
|
+
data: finalData,
|
208
|
+
// <-- وصول مباشر للبيانات النهائية!
|
209
|
+
rawResponse: rawData,
|
210
|
+
// احتفظ بالاستجابة الكاملة إذا احتجت إليها
|
204
211
|
loading: false,
|
205
212
|
success: true,
|
206
213
|
error: null,
|
207
|
-
message
|
214
|
+
message,
|
208
215
|
validationErrors: []
|
209
|
-
// <-- أضف قيمة افتراضية هنا للاتساق
|
210
216
|
};
|
211
217
|
}
|
212
218
|
return {
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
status: 500
|
217
|
-
},
|
219
|
+
data: null,
|
220
|
+
rawResponse: void 0,
|
221
|
+
error: { message: "An unknown error occurred during response processing.", status: 500 },
|
218
222
|
success: false,
|
219
|
-
loading: false
|
220
|
-
validationErrors: []
|
221
|
-
// <-- أضف قيمة افتراضية هنا أيضًا
|
223
|
+
loading: false
|
222
224
|
};
|
223
225
|
};
|
224
226
|
|
@@ -268,6 +270,34 @@ function createApiServices(axiosInstance, endpoint) {
|
|
268
270
|
return { get, getWithQuery, post, patch, remove };
|
269
271
|
}
|
270
272
|
|
273
|
+
// src/services/actions.ts
|
274
|
+
function createAction(axiosInstance, method, endpoint) {
|
275
|
+
return async (payload, config) => {
|
276
|
+
try {
|
277
|
+
const response = await axiosInstance.request({
|
278
|
+
// <--- غيرنا النوع إلى any
|
279
|
+
url: endpoint,
|
280
|
+
method,
|
281
|
+
...method.toUpperCase() === "GET" ? { params: payload } : { data: payload },
|
282
|
+
...config
|
283
|
+
});
|
284
|
+
return processResponse(response);
|
285
|
+
} catch (error) {
|
286
|
+
return processResponse(error);
|
287
|
+
}
|
288
|
+
};
|
289
|
+
}
|
290
|
+
function createApiActions(axiosInstance, actionsConfig) {
|
291
|
+
const actions = {};
|
292
|
+
for (const actionName in actionsConfig) {
|
293
|
+
if (Object.prototype.hasOwnProperty.call(actionsConfig, actionName)) {
|
294
|
+
const { method, endpoint } = actionsConfig[actionName];
|
295
|
+
actions[actionName] = createAction(axiosInstance, method, endpoint);
|
296
|
+
}
|
297
|
+
}
|
298
|
+
return actions;
|
299
|
+
}
|
300
|
+
|
271
301
|
// src/core/cache.ts
|
272
302
|
var CacheManager = class {
|
273
303
|
cache = /* @__PURE__ */ new Map();
|
@@ -312,7 +342,9 @@ function useApi(axiosInstance, config) {
|
|
312
342
|
onError
|
313
343
|
} = config;
|
314
344
|
const [state, setState] = (0, import_react.useState)({
|
315
|
-
|
345
|
+
data: initialData || null,
|
346
|
+
// <--- التغيير هنا
|
347
|
+
rawResponse: null,
|
316
348
|
loading: enabled,
|
317
349
|
error: null,
|
318
350
|
success: false
|
@@ -320,7 +352,7 @@ function useApi(axiosInstance, config) {
|
|
320
352
|
const [queryOptions, setQueryOptions] = (0, import_react.useState)(initialQuery);
|
321
353
|
const apiServices = (0, import_react.useRef)(createApiServices(axiosInstance, endpoint)).current;
|
322
354
|
const fetchData = (0, import_react.useCallback)(async () => {
|
323
|
-
setState((prev) => ({ ...prev, loading: true, error: null }));
|
355
|
+
setState((prev) => ({ ...prev, data: null, loading: true, error: null }));
|
324
356
|
const queryString = buildPaginateQuery(queryOptions);
|
325
357
|
const result = await apiServices.getWithQuery(queryString, { cancelTokenKey: endpoint });
|
326
358
|
setState(result);
|
@@ -339,7 +371,7 @@ function useApi(axiosInstance, config) {
|
|
339
371
|
if (result.success) {
|
340
372
|
if (refetchAfterChange) await fetchData();
|
341
373
|
else setState((prev) => ({ ...prev, loading: false }));
|
342
|
-
if (onSuccess) onSuccess(result.message || "Item created successfully!", result.
|
374
|
+
if (onSuccess) onSuccess(result.message || "Item created successfully!", result.data);
|
343
375
|
} else {
|
344
376
|
setState((prev) => ({ ...prev, loading: false, error: result.error }));
|
345
377
|
if (onError) onError(result.message || "Create failed", result.error || void 0);
|
@@ -352,7 +384,7 @@ function useApi(axiosInstance, config) {
|
|
352
384
|
if (result.success) {
|
353
385
|
if (refetchAfterChange) await fetchData();
|
354
386
|
else setState((prev) => ({ ...prev, loading: false }));
|
355
|
-
if (onSuccess) onSuccess(result.message || "Item updated successfully!", result.
|
387
|
+
if (onSuccess) onSuccess(result.message || "Item updated successfully!", result.data);
|
356
388
|
} else {
|
357
389
|
setState((prev) => ({ ...prev, loading: false, error: result.error }));
|
358
390
|
if (onError) onError(result.message || "Update failed", result.error || void 0);
|
@@ -404,6 +436,7 @@ function useApi(axiosInstance, config) {
|
|
404
436
|
0 && (module.exports = {
|
405
437
|
buildPaginateQuery,
|
406
438
|
cacheManager,
|
439
|
+
createApiActions,
|
407
440
|
createApiClient,
|
408
441
|
createApiServices,
|
409
442
|
processResponse,
|
package/dist/index.mjs
CHANGED
@@ -147,37 +147,38 @@ function isAxiosResponse(obj) {
|
|
147
147
|
var processResponse = (responseOrError) => {
|
148
148
|
if (isApiError(responseOrError)) {
|
149
149
|
return {
|
150
|
-
|
150
|
+
data: null,
|
151
|
+
rawResponse: void 0,
|
151
152
|
error: responseOrError,
|
152
153
|
validationErrors: responseOrError.errors || [],
|
153
|
-
// الخطأ الآن صحيح
|
154
154
|
success: false,
|
155
155
|
loading: false,
|
156
156
|
message: responseOrError.message
|
157
157
|
};
|
158
158
|
}
|
159
159
|
if (isAxiosResponse(responseOrError)) {
|
160
|
-
const
|
160
|
+
const rawData = responseOrError.data;
|
161
|
+
const isWrappedResponse = rawData && typeof rawData.success === "boolean" && rawData.data !== void 0;
|
162
|
+
const finalData = isWrappedResponse ? rawData.data : rawData;
|
163
|
+
const message = isWrappedResponse ? rawData.message : "Request successful.";
|
161
164
|
return {
|
162
|
-
|
165
|
+
data: finalData,
|
166
|
+
// <-- وصول مباشر للبيانات النهائية!
|
167
|
+
rawResponse: rawData,
|
168
|
+
// احتفظ بالاستجابة الكاملة إذا احتجت إليها
|
163
169
|
loading: false,
|
164
170
|
success: true,
|
165
171
|
error: null,
|
166
|
-
message
|
172
|
+
message,
|
167
173
|
validationErrors: []
|
168
|
-
// <-- أضف قيمة افتراضية هنا للاتساق
|
169
174
|
};
|
170
175
|
}
|
171
176
|
return {
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
status: 500
|
176
|
-
},
|
177
|
+
data: null,
|
178
|
+
rawResponse: void 0,
|
179
|
+
error: { message: "An unknown error occurred during response processing.", status: 500 },
|
177
180
|
success: false,
|
178
|
-
loading: false
|
179
|
-
validationErrors: []
|
180
|
-
// <-- أضف قيمة افتراضية هنا أيضًا
|
181
|
+
loading: false
|
181
182
|
};
|
182
183
|
};
|
183
184
|
|
@@ -227,6 +228,34 @@ function createApiServices(axiosInstance, endpoint) {
|
|
227
228
|
return { get, getWithQuery, post, patch, remove };
|
228
229
|
}
|
229
230
|
|
231
|
+
// src/services/actions.ts
|
232
|
+
function createAction(axiosInstance, method, endpoint) {
|
233
|
+
return async (payload, config) => {
|
234
|
+
try {
|
235
|
+
const response = await axiosInstance.request({
|
236
|
+
// <--- غيرنا النوع إلى any
|
237
|
+
url: endpoint,
|
238
|
+
method,
|
239
|
+
...method.toUpperCase() === "GET" ? { params: payload } : { data: payload },
|
240
|
+
...config
|
241
|
+
});
|
242
|
+
return processResponse(response);
|
243
|
+
} catch (error) {
|
244
|
+
return processResponse(error);
|
245
|
+
}
|
246
|
+
};
|
247
|
+
}
|
248
|
+
function createApiActions(axiosInstance, actionsConfig) {
|
249
|
+
const actions = {};
|
250
|
+
for (const actionName in actionsConfig) {
|
251
|
+
if (Object.prototype.hasOwnProperty.call(actionsConfig, actionName)) {
|
252
|
+
const { method, endpoint } = actionsConfig[actionName];
|
253
|
+
actions[actionName] = createAction(axiosInstance, method, endpoint);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
return actions;
|
257
|
+
}
|
258
|
+
|
230
259
|
// src/core/cache.ts
|
231
260
|
var CacheManager = class {
|
232
261
|
cache = /* @__PURE__ */ new Map();
|
@@ -271,7 +300,9 @@ function useApi(axiosInstance, config) {
|
|
271
300
|
onError
|
272
301
|
} = config;
|
273
302
|
const [state, setState] = useState({
|
274
|
-
|
303
|
+
data: initialData || null,
|
304
|
+
// <--- التغيير هنا
|
305
|
+
rawResponse: null,
|
275
306
|
loading: enabled,
|
276
307
|
error: null,
|
277
308
|
success: false
|
@@ -279,7 +310,7 @@ function useApi(axiosInstance, config) {
|
|
279
310
|
const [queryOptions, setQueryOptions] = useState(initialQuery);
|
280
311
|
const apiServices = useRef(createApiServices(axiosInstance, endpoint)).current;
|
281
312
|
const fetchData = useCallback(async () => {
|
282
|
-
setState((prev) => ({ ...prev, loading: true, error: null }));
|
313
|
+
setState((prev) => ({ ...prev, data: null, loading: true, error: null }));
|
283
314
|
const queryString = buildPaginateQuery(queryOptions);
|
284
315
|
const result = await apiServices.getWithQuery(queryString, { cancelTokenKey: endpoint });
|
285
316
|
setState(result);
|
@@ -298,7 +329,7 @@ function useApi(axiosInstance, config) {
|
|
298
329
|
if (result.success) {
|
299
330
|
if (refetchAfterChange) await fetchData();
|
300
331
|
else setState((prev) => ({ ...prev, loading: false }));
|
301
|
-
if (onSuccess) onSuccess(result.message || "Item created successfully!", result.
|
332
|
+
if (onSuccess) onSuccess(result.message || "Item created successfully!", result.data);
|
302
333
|
} else {
|
303
334
|
setState((prev) => ({ ...prev, loading: false, error: result.error }));
|
304
335
|
if (onError) onError(result.message || "Create failed", result.error || void 0);
|
@@ -311,7 +342,7 @@ function useApi(axiosInstance, config) {
|
|
311
342
|
if (result.success) {
|
312
343
|
if (refetchAfterChange) await fetchData();
|
313
344
|
else setState((prev) => ({ ...prev, loading: false }));
|
314
|
-
if (onSuccess) onSuccess(result.message || "Item updated successfully!", result.
|
345
|
+
if (onSuccess) onSuccess(result.message || "Item updated successfully!", result.data);
|
315
346
|
} else {
|
316
347
|
setState((prev) => ({ ...prev, loading: false, error: result.error }));
|
317
348
|
if (onError) onError(result.message || "Update failed", result.error || void 0);
|
@@ -362,6 +393,7 @@ function useApi(axiosInstance, config) {
|
|
362
393
|
export {
|
363
394
|
buildPaginateQuery,
|
364
395
|
cacheManager,
|
396
|
+
createApiActions,
|
365
397
|
createApiClient,
|
366
398
|
createApiServices,
|
367
399
|
processResponse,
|