api-core-lib 12.12.4 → 12.12.100
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/README.md +1 -0
- package/bin/cli-launcher.cjs +14 -0
- package/dist/apiModule.types-dbmnauAt.d.cts +294 -0
- package/dist/apiModule.types-dbmnauAt.d.ts +294 -0
- package/dist/chunk-25UFVV4F.cjs +381 -0
- package/dist/chunk-KPZ7BF52.js +381 -0
- package/dist/cli.cjs +346 -0
- package/dist/client.cjs +547 -0
- package/dist/client.d.cts +50 -0
- package/dist/client.d.ts +50 -0
- package/dist/client.js +547 -0
- package/dist/index.cjs +298 -0
- package/dist/index.d.cts +241 -0
- package/dist/index.d.ts +54 -423
- package/dist/index.js +298 -1
- package/dist/server.cjs +56 -0
- package/dist/server.d.cts +25 -0
- package/dist/server.d.ts +25 -0
- package/dist/server.js +56 -0
- package/dist/useApiRecord.types-C06B5p4U.d.ts +90 -0
- package/dist/useApiRecord.types-HrabvzEQ.d.cts +90 -0
- package/package.json +50 -20
- package/dist/index.d.mts +0 -610
- package/dist/index.mjs +0 -1
package/README.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"build": "tsup && node scripts/obfuscate.js",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// bin/cli-launcher.js
|
|
4
|
+
// هذا الملف هو مجرد مشغّل بسيط بصيغة CommonJS.
|
|
5
|
+
// وظيفته الوحيدة هي تحميل وتشغيل منطق الـ CLI الفعلي من مجلد dist.
|
|
6
|
+
|
|
7
|
+
try {
|
|
8
|
+
// استدعاء المنطق الرئيسي للـ CLI الذي تم بناؤه بواسطة tsup
|
|
9
|
+
require('../dist/cli.cjs');
|
|
10
|
+
} catch (error) {
|
|
11
|
+
console.error('Failed to start the API Core generator.');
|
|
12
|
+
console.error(error);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosProgressEvent, InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import react__default from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* يمثل معلومات الترقيم (Pagination) التي قد تعود من ה-API.
|
|
6
|
+
*/
|
|
7
|
+
interface PaginationMeta {
|
|
8
|
+
itemsPerPage: number;
|
|
9
|
+
totalItems: number;
|
|
10
|
+
currentPage: number;
|
|
11
|
+
totalPages: number;
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* يمثل خطأ تحقق واحد لحقل معين.
|
|
16
|
+
*/
|
|
17
|
+
interface ValidationError {
|
|
18
|
+
field: string;
|
|
19
|
+
message: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* يمثل كائن الخطأ الموحد الذي تنتجه المكتبة.
|
|
23
|
+
*/
|
|
24
|
+
interface ApiError {
|
|
25
|
+
message: string;
|
|
26
|
+
status: number;
|
|
27
|
+
code?: string;
|
|
28
|
+
errors?: ValidationError[];
|
|
29
|
+
requestId?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* الكائن القياسي والموحد الذي تعيده جميع دوال المكتبة.
|
|
33
|
+
* هذا هو النوع الأساسي الذي سيتعامل معه المطورون.
|
|
34
|
+
* @template T نوع البيانات الأساسية.
|
|
35
|
+
*/
|
|
36
|
+
interface StandardResponse<T> {
|
|
37
|
+
data: T | null;
|
|
38
|
+
links?: Record<string, string | null>;
|
|
39
|
+
meta?: PaginationMeta | Record<string, any>;
|
|
40
|
+
rawResponse: any;
|
|
41
|
+
error: ApiError | null;
|
|
42
|
+
loading: boolean;
|
|
43
|
+
success: boolean;
|
|
44
|
+
message?: string;
|
|
45
|
+
validationErrors?: ValidationError[];
|
|
46
|
+
}
|
|
47
|
+
interface Tokens {
|
|
48
|
+
accessToken: string | null;
|
|
49
|
+
refreshToken: string | null;
|
|
50
|
+
expiresAt?: number;
|
|
51
|
+
tokenType?: string;
|
|
52
|
+
}
|
|
53
|
+
interface TokenManager {
|
|
54
|
+
getTokens(): Promise<Tokens>;
|
|
55
|
+
setTokens(tokens: Tokens): Promise<void>;
|
|
56
|
+
clearTokens(): Promise<void>;
|
|
57
|
+
isHttpOnly(): boolean;
|
|
58
|
+
}
|
|
59
|
+
interface MiddlewareContext {
|
|
60
|
+
req: InternalAxiosRequestConfig;
|
|
61
|
+
res?: AxiosResponse;
|
|
62
|
+
error?: any;
|
|
63
|
+
custom?: Record<string, any>;
|
|
64
|
+
}
|
|
65
|
+
type Middleware = (context: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
66
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
67
|
+
interface RefreshTokenConfig {
|
|
68
|
+
path: string;
|
|
69
|
+
buildRequestBody?: (refreshToken: string) => Record<string, any>;
|
|
70
|
+
buildRequestHeaders?: (currentTokens: Tokens) => Record<string, string>;
|
|
71
|
+
extractTokens: (responseData: any) => {
|
|
72
|
+
accessToken: string;
|
|
73
|
+
refreshToken?: string;
|
|
74
|
+
expiresIn: number;
|
|
75
|
+
tokenType?: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
interface ApiClientConfig {
|
|
79
|
+
baseURL?: string;
|
|
80
|
+
tokenManager: TokenManager;
|
|
81
|
+
timeout?: number;
|
|
82
|
+
headers?: Record<string, string>;
|
|
83
|
+
withCredentials?: boolean;
|
|
84
|
+
refreshTokenConfig?: RefreshTokenConfig;
|
|
85
|
+
onRefreshError?: (error: any) => void;
|
|
86
|
+
middleware?: Middleware[];
|
|
87
|
+
logLevel?: LogLevel;
|
|
88
|
+
defaultIsPublic?: boolean;
|
|
89
|
+
maxTokenRefreshRetries?: number;
|
|
90
|
+
maxQueueSize?: number;
|
|
91
|
+
}
|
|
92
|
+
interface RequestConfig extends AxiosRequestConfig {
|
|
93
|
+
isPublic?: boolean;
|
|
94
|
+
cancelTokenKey?: string;
|
|
95
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
96
|
+
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
97
|
+
}
|
|
98
|
+
interface QueryOptions {
|
|
99
|
+
page?: number;
|
|
100
|
+
limit?: number;
|
|
101
|
+
search?: string;
|
|
102
|
+
sortBy?: {
|
|
103
|
+
key: string;
|
|
104
|
+
direction: 'asc' | 'desc';
|
|
105
|
+
}[];
|
|
106
|
+
filter?: Record<string, any>;
|
|
107
|
+
[key: string]: any;
|
|
108
|
+
}
|
|
109
|
+
interface ActionOptions extends RequestConfig {
|
|
110
|
+
endpoint?: string;
|
|
111
|
+
refetch?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* يمثل إعدادات إجراء واحد داخل الموديول.
|
|
115
|
+
*/
|
|
116
|
+
interface ActionConfig<TInput = any, TOutput = any> {
|
|
117
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
118
|
+
path: string;
|
|
119
|
+
description?: string;
|
|
120
|
+
isList?: boolean;
|
|
121
|
+
invalidates?: string[];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* [مُحدَّث] يمثل الحالة التفاعلية الكاملة لإجراء واحد.
|
|
125
|
+
* هذا هو النوع الذي يتم إرجاعه في `states` من الهوك.
|
|
126
|
+
*/
|
|
127
|
+
interface ActionStateModule<TOutput> {
|
|
128
|
+
data: TOutput | null;
|
|
129
|
+
links?: Record<string, string | null>;
|
|
130
|
+
meta?: PaginationMeta | Record<string, any>;
|
|
131
|
+
error: ApiError | null;
|
|
132
|
+
loading: boolean;
|
|
133
|
+
success: boolean;
|
|
134
|
+
called: boolean;
|
|
135
|
+
message?: string;
|
|
136
|
+
validationErrors?: ValidationError[];
|
|
137
|
+
rawResponse: any | null;
|
|
138
|
+
isStale?: boolean;
|
|
139
|
+
lastSuccessAt?: number;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* الكائن الموحد الذي يتم إرجاعه لكل إجراء، ويحتوي على حالته ومنفذه وأدوات التحكم.
|
|
143
|
+
*/
|
|
144
|
+
interface ExecutableAction<TInput, TOutput> {
|
|
145
|
+
state: ActionStateModule<TOutput>;
|
|
146
|
+
execute: (input?: TInput, options?: {
|
|
147
|
+
pathParams?: Record<string, any>;
|
|
148
|
+
config?: AxiosRequestConfig;
|
|
149
|
+
query?: QueryOptions;
|
|
150
|
+
}) => Promise<StandardResponse<TOutput>>;
|
|
151
|
+
reset: () => void;
|
|
152
|
+
query?: UseApiQuery;
|
|
153
|
+
}
|
|
154
|
+
type UseApiState<T> = StandardResponse<T>;
|
|
155
|
+
interface UseApiQuery {
|
|
156
|
+
/** The current query options state. */
|
|
157
|
+
options: QueryOptions;
|
|
158
|
+
/** A function to set the entire query options object at once. */
|
|
159
|
+
setOptions: react__default.Dispatch<react__default.SetStateAction<QueryOptions>>;
|
|
160
|
+
/** Sets the current page number. */
|
|
161
|
+
setPage: (page: number) => void;
|
|
162
|
+
/** Sets the number of items per page and resets to the first page. */
|
|
163
|
+
setLimit: (limit: number) => void;
|
|
164
|
+
/** Sets the search term and resets to the first page. */
|
|
165
|
+
setSearchTerm: (search: string) => void;
|
|
166
|
+
/** Sets the sorting configuration. */
|
|
167
|
+
setSorting: (sortBy: {
|
|
168
|
+
key: string;
|
|
169
|
+
direction: 'asc' | 'desc';
|
|
170
|
+
}[]) => void;
|
|
171
|
+
/** Sets the filter object and resets to the first page. */
|
|
172
|
+
setFilters: (filter: Record<string, any>) => void;
|
|
173
|
+
/** Sets a single, custom query parameter. */
|
|
174
|
+
setQueryParam: (key: string, value: any) => void;
|
|
175
|
+
/** Resets the query options to their initial state. */
|
|
176
|
+
reset: () => void;
|
|
177
|
+
}
|
|
178
|
+
interface UseApiConfig<T> {
|
|
179
|
+
endpoint: string;
|
|
180
|
+
initialData?: T | null;
|
|
181
|
+
initialQuery?: QueryOptions;
|
|
182
|
+
enabled?: boolean;
|
|
183
|
+
refetchAfterChange?: boolean;
|
|
184
|
+
requestConfig?: RequestConfig;
|
|
185
|
+
pathParams?: Record<string, string | number>;
|
|
186
|
+
encodeQuery?: boolean;
|
|
187
|
+
onSuccess?: (message: string, data?: T) => void;
|
|
188
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
189
|
+
}
|
|
190
|
+
interface t {
|
|
191
|
+
type: string;
|
|
192
|
+
payload?: any;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
196
|
+
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
197
|
+
isStale?: boolean;
|
|
198
|
+
}
|
|
199
|
+
/** @description Defines the configuration for a single API action within a module. */
|
|
200
|
+
interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
201
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
202
|
+
path: string;
|
|
203
|
+
description?: string;
|
|
204
|
+
hasQuery?: boolean;
|
|
205
|
+
autoFetch?: boolean;
|
|
206
|
+
cacheResponse?: boolean;
|
|
207
|
+
invalidates?: string[];
|
|
208
|
+
requiresAuth?: boolean;
|
|
209
|
+
pathParams?: string[];
|
|
210
|
+
_input?: TInput;
|
|
211
|
+
_output?: TOutput;
|
|
212
|
+
}
|
|
213
|
+
/** @description Defines the complete structure of an API module. */
|
|
214
|
+
/**
|
|
215
|
+
* يصف إعدادات موديول API كامل.
|
|
216
|
+
* @template TActions كائن يصف جميع الإجراءات المتاحة في الموديول.
|
|
217
|
+
*/
|
|
218
|
+
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
219
|
+
baseEndpoint: string;
|
|
220
|
+
actions: TActions;
|
|
221
|
+
}
|
|
222
|
+
/** @description Configuration options passed directly to the `useApiModule` hook. */
|
|
223
|
+
interface UseApiModuleOptions<TExtra = {}> {
|
|
224
|
+
onSuccess?: (actionName: string, message: string, data: unknown) => void;
|
|
225
|
+
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
226
|
+
refetchOnWindowFocus?: boolean;
|
|
227
|
+
pathParams?: Record<string, any>;
|
|
228
|
+
enabled?: boolean;
|
|
229
|
+
hydratedState?: string;
|
|
230
|
+
extraContextData?: TExtra;
|
|
231
|
+
}
|
|
232
|
+
/** A utility type to infer the Input type (`TInput`) from an ActionConfigModule. */
|
|
233
|
+
type InputOf<TActionConfig> = TActionConfig extends ActionConfigModule<infer TInput, any> ? TInput : never;
|
|
234
|
+
/** A utility type to infer the Output type (`TOutput`) from an ActionConfigModule. */
|
|
235
|
+
type OutputOf<TActionConfig> = TActionConfig extends ActionConfigModule<any, infer TOutput> ? TOutput : never;
|
|
236
|
+
/** @description Defines the options for the `execute` function, enabling optimistic updates. */
|
|
237
|
+
interface ExecuteOptions<TInput, TOutput, TContext = unknown> {
|
|
238
|
+
pathParams?: Record<string, any>;
|
|
239
|
+
config?: AxiosRequestConfig;
|
|
240
|
+
onMutate?: (variables: TInput) => TContext | Promise<TContext>;
|
|
241
|
+
onSuccess?: (data: TOutput, context?: TContext) => void;
|
|
242
|
+
onError?: (error: ApiError, context?: TContext) => void;
|
|
243
|
+
onSettled?: (data?: TOutput, error?: ApiError, context?: TContext) => void;
|
|
244
|
+
}
|
|
245
|
+
/** @description A fully-typed object representing the callable actions for a module. */
|
|
246
|
+
type ModuleActions<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
247
|
+
[K in keyof TActions]: {
|
|
248
|
+
execute: <TContext = unknown>(input?: InputOf<TActions[K]>, options?: ExecuteOptions<InputOf<TActions[K]>, OutputOf<TActions[K]>, TContext>) => Promise<StandardResponse<OutputOf<TActions[K]>>>;
|
|
249
|
+
reset: (input?: InputOf<TActions[K]>, options?: {
|
|
250
|
+
pathParams?: Record<string, any>;
|
|
251
|
+
}) => void;
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
/** @description A fully-typed object representing the reactive states for each action in a module. */
|
|
255
|
+
type ModuleStates<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
256
|
+
[K in keyof TActions]: ActionState<OutputOf<TActions[K]>>;
|
|
257
|
+
};
|
|
258
|
+
type ActionsWithQuery<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
259
|
+
[K in keyof TActions]: TActions[K] extends ActionConfigModule<QueryOptions, any> ? K : never;
|
|
260
|
+
}[keyof TActions];
|
|
261
|
+
/**
|
|
262
|
+
* يُنشئ نوعًا لكائن `queries` يحتوي فقط على الإجراءات التي تقبل الكويري.
|
|
263
|
+
*/
|
|
264
|
+
type ModuleQueries<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
265
|
+
[K in ActionsWithQuery<TActions>]: UseApiQuery;
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* @description The complete, fully-typed return object of the `useApiModule` hook.
|
|
269
|
+
*/
|
|
270
|
+
type UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}> = {
|
|
271
|
+
states: ModuleStates<TActions>;
|
|
272
|
+
actions: ModuleActions<TActions>;
|
|
273
|
+
queries: ModuleQueries<TActions>;
|
|
274
|
+
dehydrate: () => string;
|
|
275
|
+
} & TExtra;
|
|
276
|
+
/**
|
|
277
|
+
* يصف دوال `execute` و `reset` لإجراء واحد.
|
|
278
|
+
* هذا النوع عام (generic) ليتناسب مع أنواع الإدخال والإخراج المختلفة لكل إجراء.
|
|
279
|
+
* @template TAction يمثل إعدادات الإجراء الواحد.
|
|
280
|
+
*/
|
|
281
|
+
type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
282
|
+
/**
|
|
283
|
+
* الدالة الأساسية لتنفيذ طلب ה-API.
|
|
284
|
+
*/
|
|
285
|
+
execute: <TContext = unknown>(input?: InputOf<TAction>, options?: ExecuteOptions<InputOf<TAction>, OutputOf<TAction>, TContext>) => Promise<StandardResponse<OutputOf<TAction>>>;
|
|
286
|
+
/**
|
|
287
|
+
* دالة لإعادة تعيين حالة إجراء معين في ذاكرة التخزين المؤقت إلى حالته الأولية.
|
|
288
|
+
*/
|
|
289
|
+
reset: (input?: InputOf<TAction>, options?: {
|
|
290
|
+
pathParams?: Record<string, any>;
|
|
291
|
+
}) => void;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, ModuleQueries as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, UseApiQuery as i, ApiError as j, TokenManager as k, MiddlewareContext as l, Middleware as m, RefreshTokenConfig as n, ActionConfig as o, UseApiState as p, ActionState as q, ExecuteOptions as r, ActionsWithQuery as s, t, ActionMethods as u };
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { AxiosRequestConfig, AxiosProgressEvent, InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import react__default from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* يمثل معلومات الترقيم (Pagination) التي قد تعود من ה-API.
|
|
6
|
+
*/
|
|
7
|
+
interface PaginationMeta {
|
|
8
|
+
itemsPerPage: number;
|
|
9
|
+
totalItems: number;
|
|
10
|
+
currentPage: number;
|
|
11
|
+
totalPages: number;
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* يمثل خطأ تحقق واحد لحقل معين.
|
|
16
|
+
*/
|
|
17
|
+
interface ValidationError {
|
|
18
|
+
field: string;
|
|
19
|
+
message: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* يمثل كائن الخطأ الموحد الذي تنتجه المكتبة.
|
|
23
|
+
*/
|
|
24
|
+
interface ApiError {
|
|
25
|
+
message: string;
|
|
26
|
+
status: number;
|
|
27
|
+
code?: string;
|
|
28
|
+
errors?: ValidationError[];
|
|
29
|
+
requestId?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* الكائن القياسي والموحد الذي تعيده جميع دوال المكتبة.
|
|
33
|
+
* هذا هو النوع الأساسي الذي سيتعامل معه المطورون.
|
|
34
|
+
* @template T نوع البيانات الأساسية.
|
|
35
|
+
*/
|
|
36
|
+
interface StandardResponse<T> {
|
|
37
|
+
data: T | null;
|
|
38
|
+
links?: Record<string, string | null>;
|
|
39
|
+
meta?: PaginationMeta | Record<string, any>;
|
|
40
|
+
rawResponse: any;
|
|
41
|
+
error: ApiError | null;
|
|
42
|
+
loading: boolean;
|
|
43
|
+
success: boolean;
|
|
44
|
+
message?: string;
|
|
45
|
+
validationErrors?: ValidationError[];
|
|
46
|
+
}
|
|
47
|
+
interface Tokens {
|
|
48
|
+
accessToken: string | null;
|
|
49
|
+
refreshToken: string | null;
|
|
50
|
+
expiresAt?: number;
|
|
51
|
+
tokenType?: string;
|
|
52
|
+
}
|
|
53
|
+
interface TokenManager {
|
|
54
|
+
getTokens(): Promise<Tokens>;
|
|
55
|
+
setTokens(tokens: Tokens): Promise<void>;
|
|
56
|
+
clearTokens(): Promise<void>;
|
|
57
|
+
isHttpOnly(): boolean;
|
|
58
|
+
}
|
|
59
|
+
interface MiddlewareContext {
|
|
60
|
+
req: InternalAxiosRequestConfig;
|
|
61
|
+
res?: AxiosResponse;
|
|
62
|
+
error?: any;
|
|
63
|
+
custom?: Record<string, any>;
|
|
64
|
+
}
|
|
65
|
+
type Middleware = (context: MiddlewareContext, next: () => Promise<void>) => Promise<void>;
|
|
66
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
67
|
+
interface RefreshTokenConfig {
|
|
68
|
+
path: string;
|
|
69
|
+
buildRequestBody?: (refreshToken: string) => Record<string, any>;
|
|
70
|
+
buildRequestHeaders?: (currentTokens: Tokens) => Record<string, string>;
|
|
71
|
+
extractTokens: (responseData: any) => {
|
|
72
|
+
accessToken: string;
|
|
73
|
+
refreshToken?: string;
|
|
74
|
+
expiresIn: number;
|
|
75
|
+
tokenType?: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
interface ApiClientConfig {
|
|
79
|
+
baseURL?: string;
|
|
80
|
+
tokenManager: TokenManager;
|
|
81
|
+
timeout?: number;
|
|
82
|
+
headers?: Record<string, string>;
|
|
83
|
+
withCredentials?: boolean;
|
|
84
|
+
refreshTokenConfig?: RefreshTokenConfig;
|
|
85
|
+
onRefreshError?: (error: any) => void;
|
|
86
|
+
middleware?: Middleware[];
|
|
87
|
+
logLevel?: LogLevel;
|
|
88
|
+
defaultIsPublic?: boolean;
|
|
89
|
+
maxTokenRefreshRetries?: number;
|
|
90
|
+
maxQueueSize?: number;
|
|
91
|
+
}
|
|
92
|
+
interface RequestConfig extends AxiosRequestConfig {
|
|
93
|
+
isPublic?: boolean;
|
|
94
|
+
cancelTokenKey?: string;
|
|
95
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
96
|
+
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
97
|
+
}
|
|
98
|
+
interface QueryOptions {
|
|
99
|
+
page?: number;
|
|
100
|
+
limit?: number;
|
|
101
|
+
search?: string;
|
|
102
|
+
sortBy?: {
|
|
103
|
+
key: string;
|
|
104
|
+
direction: 'asc' | 'desc';
|
|
105
|
+
}[];
|
|
106
|
+
filter?: Record<string, any>;
|
|
107
|
+
[key: string]: any;
|
|
108
|
+
}
|
|
109
|
+
interface ActionOptions extends RequestConfig {
|
|
110
|
+
endpoint?: string;
|
|
111
|
+
refetch?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* يمثل إعدادات إجراء واحد داخل الموديول.
|
|
115
|
+
*/
|
|
116
|
+
interface ActionConfig<TInput = any, TOutput = any> {
|
|
117
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
118
|
+
path: string;
|
|
119
|
+
description?: string;
|
|
120
|
+
isList?: boolean;
|
|
121
|
+
invalidates?: string[];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* [مُحدَّث] يمثل الحالة التفاعلية الكاملة لإجراء واحد.
|
|
125
|
+
* هذا هو النوع الذي يتم إرجاعه في `states` من الهوك.
|
|
126
|
+
*/
|
|
127
|
+
interface ActionStateModule<TOutput> {
|
|
128
|
+
data: TOutput | null;
|
|
129
|
+
links?: Record<string, string | null>;
|
|
130
|
+
meta?: PaginationMeta | Record<string, any>;
|
|
131
|
+
error: ApiError | null;
|
|
132
|
+
loading: boolean;
|
|
133
|
+
success: boolean;
|
|
134
|
+
called: boolean;
|
|
135
|
+
message?: string;
|
|
136
|
+
validationErrors?: ValidationError[];
|
|
137
|
+
rawResponse: any | null;
|
|
138
|
+
isStale?: boolean;
|
|
139
|
+
lastSuccessAt?: number;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* الكائن الموحد الذي يتم إرجاعه لكل إجراء، ويحتوي على حالته ومنفذه وأدوات التحكم.
|
|
143
|
+
*/
|
|
144
|
+
interface ExecutableAction<TInput, TOutput> {
|
|
145
|
+
state: ActionStateModule<TOutput>;
|
|
146
|
+
execute: (input?: TInput, options?: {
|
|
147
|
+
pathParams?: Record<string, any>;
|
|
148
|
+
config?: AxiosRequestConfig;
|
|
149
|
+
query?: QueryOptions;
|
|
150
|
+
}) => Promise<StandardResponse<TOutput>>;
|
|
151
|
+
reset: () => void;
|
|
152
|
+
query?: UseApiQuery;
|
|
153
|
+
}
|
|
154
|
+
type UseApiState<T> = StandardResponse<T>;
|
|
155
|
+
interface UseApiQuery {
|
|
156
|
+
/** The current query options state. */
|
|
157
|
+
options: QueryOptions;
|
|
158
|
+
/** A function to set the entire query options object at once. */
|
|
159
|
+
setOptions: react__default.Dispatch<react__default.SetStateAction<QueryOptions>>;
|
|
160
|
+
/** Sets the current page number. */
|
|
161
|
+
setPage: (page: number) => void;
|
|
162
|
+
/** Sets the number of items per page and resets to the first page. */
|
|
163
|
+
setLimit: (limit: number) => void;
|
|
164
|
+
/** Sets the search term and resets to the first page. */
|
|
165
|
+
setSearchTerm: (search: string) => void;
|
|
166
|
+
/** Sets the sorting configuration. */
|
|
167
|
+
setSorting: (sortBy: {
|
|
168
|
+
key: string;
|
|
169
|
+
direction: 'asc' | 'desc';
|
|
170
|
+
}[]) => void;
|
|
171
|
+
/** Sets the filter object and resets to the first page. */
|
|
172
|
+
setFilters: (filter: Record<string, any>) => void;
|
|
173
|
+
/** Sets a single, custom query parameter. */
|
|
174
|
+
setQueryParam: (key: string, value: any) => void;
|
|
175
|
+
/** Resets the query options to their initial state. */
|
|
176
|
+
reset: () => void;
|
|
177
|
+
}
|
|
178
|
+
interface UseApiConfig<T> {
|
|
179
|
+
endpoint: string;
|
|
180
|
+
initialData?: T | null;
|
|
181
|
+
initialQuery?: QueryOptions;
|
|
182
|
+
enabled?: boolean;
|
|
183
|
+
refetchAfterChange?: boolean;
|
|
184
|
+
requestConfig?: RequestConfig;
|
|
185
|
+
pathParams?: Record<string, string | number>;
|
|
186
|
+
encodeQuery?: boolean;
|
|
187
|
+
onSuccess?: (message: string, data?: T) => void;
|
|
188
|
+
onError?: (message: string, error?: ApiError) => void;
|
|
189
|
+
}
|
|
190
|
+
interface t {
|
|
191
|
+
type: string;
|
|
192
|
+
payload?: any;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
196
|
+
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
197
|
+
isStale?: boolean;
|
|
198
|
+
}
|
|
199
|
+
/** @description Defines the configuration for a single API action within a module. */
|
|
200
|
+
interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
201
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
202
|
+
path: string;
|
|
203
|
+
description?: string;
|
|
204
|
+
hasQuery?: boolean;
|
|
205
|
+
autoFetch?: boolean;
|
|
206
|
+
cacheResponse?: boolean;
|
|
207
|
+
invalidates?: string[];
|
|
208
|
+
requiresAuth?: boolean;
|
|
209
|
+
pathParams?: string[];
|
|
210
|
+
_input?: TInput;
|
|
211
|
+
_output?: TOutput;
|
|
212
|
+
}
|
|
213
|
+
/** @description Defines the complete structure of an API module. */
|
|
214
|
+
/**
|
|
215
|
+
* يصف إعدادات موديول API كامل.
|
|
216
|
+
* @template TActions كائن يصف جميع الإجراءات المتاحة في الموديول.
|
|
217
|
+
*/
|
|
218
|
+
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
219
|
+
baseEndpoint: string;
|
|
220
|
+
actions: TActions;
|
|
221
|
+
}
|
|
222
|
+
/** @description Configuration options passed directly to the `useApiModule` hook. */
|
|
223
|
+
interface UseApiModuleOptions<TExtra = {}> {
|
|
224
|
+
onSuccess?: (actionName: string, message: string, data: unknown) => void;
|
|
225
|
+
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
226
|
+
refetchOnWindowFocus?: boolean;
|
|
227
|
+
pathParams?: Record<string, any>;
|
|
228
|
+
enabled?: boolean;
|
|
229
|
+
hydratedState?: string;
|
|
230
|
+
extraContextData?: TExtra;
|
|
231
|
+
}
|
|
232
|
+
/** A utility type to infer the Input type (`TInput`) from an ActionConfigModule. */
|
|
233
|
+
type InputOf<TActionConfig> = TActionConfig extends ActionConfigModule<infer TInput, any> ? TInput : never;
|
|
234
|
+
/** A utility type to infer the Output type (`TOutput`) from an ActionConfigModule. */
|
|
235
|
+
type OutputOf<TActionConfig> = TActionConfig extends ActionConfigModule<any, infer TOutput> ? TOutput : never;
|
|
236
|
+
/** @description Defines the options for the `execute` function, enabling optimistic updates. */
|
|
237
|
+
interface ExecuteOptions<TInput, TOutput, TContext = unknown> {
|
|
238
|
+
pathParams?: Record<string, any>;
|
|
239
|
+
config?: AxiosRequestConfig;
|
|
240
|
+
onMutate?: (variables: TInput) => TContext | Promise<TContext>;
|
|
241
|
+
onSuccess?: (data: TOutput, context?: TContext) => void;
|
|
242
|
+
onError?: (error: ApiError, context?: TContext) => void;
|
|
243
|
+
onSettled?: (data?: TOutput, error?: ApiError, context?: TContext) => void;
|
|
244
|
+
}
|
|
245
|
+
/** @description A fully-typed object representing the callable actions for a module. */
|
|
246
|
+
type ModuleActions<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
247
|
+
[K in keyof TActions]: {
|
|
248
|
+
execute: <TContext = unknown>(input?: InputOf<TActions[K]>, options?: ExecuteOptions<InputOf<TActions[K]>, OutputOf<TActions[K]>, TContext>) => Promise<StandardResponse<OutputOf<TActions[K]>>>;
|
|
249
|
+
reset: (input?: InputOf<TActions[K]>, options?: {
|
|
250
|
+
pathParams?: Record<string, any>;
|
|
251
|
+
}) => void;
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
/** @description A fully-typed object representing the reactive states for each action in a module. */
|
|
255
|
+
type ModuleStates<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
256
|
+
[K in keyof TActions]: ActionState<OutputOf<TActions[K]>>;
|
|
257
|
+
};
|
|
258
|
+
type ActionsWithQuery<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
259
|
+
[K in keyof TActions]: TActions[K] extends ActionConfigModule<QueryOptions, any> ? K : never;
|
|
260
|
+
}[keyof TActions];
|
|
261
|
+
/**
|
|
262
|
+
* يُنشئ نوعًا لكائن `queries` يحتوي فقط على الإجراءات التي تقبل الكويري.
|
|
263
|
+
*/
|
|
264
|
+
type ModuleQueries<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
265
|
+
[K in ActionsWithQuery<TActions>]: UseApiQuery;
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* @description The complete, fully-typed return object of the `useApiModule` hook.
|
|
269
|
+
*/
|
|
270
|
+
type UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>, TExtra extends object = {}> = {
|
|
271
|
+
states: ModuleStates<TActions>;
|
|
272
|
+
actions: ModuleActions<TActions>;
|
|
273
|
+
queries: ModuleQueries<TActions>;
|
|
274
|
+
dehydrate: () => string;
|
|
275
|
+
} & TExtra;
|
|
276
|
+
/**
|
|
277
|
+
* يصف دوال `execute` و `reset` لإجراء واحد.
|
|
278
|
+
* هذا النوع عام (generic) ليتناسب مع أنواع الإدخال والإخراج المختلفة لكل إجراء.
|
|
279
|
+
* @template TAction يمثل إعدادات الإجراء الواحد.
|
|
280
|
+
*/
|
|
281
|
+
type ActionMethods<TAction extends ActionConfigModule<any, any>> = {
|
|
282
|
+
/**
|
|
283
|
+
* الدالة الأساسية لتنفيذ طلب ה-API.
|
|
284
|
+
*/
|
|
285
|
+
execute: <TContext = unknown>(input?: InputOf<TAction>, options?: ExecuteOptions<InputOf<TAction>, OutputOf<TAction>, TContext>) => Promise<StandardResponse<OutputOf<TAction>>>;
|
|
286
|
+
/**
|
|
287
|
+
* دالة لإعادة تعيين حالة إجراء معين في ذاكرة التخزين المؤقت إلى حالته الأولية.
|
|
288
|
+
*/
|
|
289
|
+
reset: (input?: InputOf<TAction>, options?: {
|
|
290
|
+
pathParams?: Record<string, any>;
|
|
291
|
+
}) => void;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export type { ActionConfigModule as A, ExecutableAction as E, InputOf as I, LogLevel as L, ModuleStates as M, OutputOf as O, PaginationMeta as P, QueryOptions as Q, RequestConfig as R, StandardResponse as S, Tokens as T, UseApiConfig as U, ValidationError as V, ApiModuleConfig as a, UseApiModuleOptions as b, UseApiModuleReturn as c, ModuleActions as d, ModuleQueries as e, ApiClientConfig as f, ActionOptions as g, ActionStateModule as h, UseApiQuery as i, ApiError as j, TokenManager as k, MiddlewareContext as l, Middleware as m, RefreshTokenConfig as n, ActionConfig as o, UseApiState as p, ActionState as q, ExecuteOptions as r, ActionsWithQuery as s, t, ActionMethods as u };
|