api-core-lib 12.0.0 → 12.0.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/apiModule.types-2yhaJWN3.d.cts +257 -0
- package/dist/apiModule.types-2yhaJWN3.d.ts +257 -0
- package/dist/index.cjs +1151 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +339 -0
- package/dist/index.d.ts +131 -384
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -0
- package/dist/server.cjs +342 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +25 -0
- package/dist/server.d.ts +25 -0
- package/dist/server.js +1 -0
- package/dist/server.js.map +1 -0
- package/package.json +19 -3
- package/dist/index.d.mts +0 -592
- package/dist/index.mjs +0 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent } 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
|
+
|
|
191
|
+
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
192
|
+
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
193
|
+
isStale?: boolean;
|
|
194
|
+
}
|
|
195
|
+
/** @description Defines the configuration for a single API action within a module. */
|
|
196
|
+
interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
197
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
198
|
+
path: string;
|
|
199
|
+
description?: string;
|
|
200
|
+
hasQuery?: boolean;
|
|
201
|
+
autoFetch?: boolean;
|
|
202
|
+
invalidates?: string[];
|
|
203
|
+
_input?: TInput;
|
|
204
|
+
_output?: TOutput;
|
|
205
|
+
}
|
|
206
|
+
/** @description Defines the complete structure of an API module. */
|
|
207
|
+
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
208
|
+
baseEndpoint: string;
|
|
209
|
+
actions: TActions;
|
|
210
|
+
}
|
|
211
|
+
/** @description Configuration options passed directly to the `useApiModule` hook. */
|
|
212
|
+
interface UseApiModuleOptions {
|
|
213
|
+
onSuccess?: (actionName: string, message: string, data: unknown) => void;
|
|
214
|
+
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
215
|
+
refetchOnWindowFocus?: boolean;
|
|
216
|
+
pathParams?: Record<string, any>;
|
|
217
|
+
enabled?: boolean;
|
|
218
|
+
hydratedState?: string;
|
|
219
|
+
}
|
|
220
|
+
/** A utility type to infer the Input type (`TInput`) from an ActionConfigModule. */
|
|
221
|
+
type InputOf<TActionConfig> = TActionConfig extends ActionConfigModule<infer TInput, any> ? TInput : never;
|
|
222
|
+
/** A utility type to infer the Output type (`TOutput`) from an ActionConfigModule. */
|
|
223
|
+
type OutputOf<TActionConfig> = TActionConfig extends ActionConfigModule<any, infer TOutput> ? TOutput : never;
|
|
224
|
+
/** @description Defines the options for the `execute` function, enabling optimistic updates. */
|
|
225
|
+
interface ExecuteOptions<TInput, TOutput, TContext = unknown> {
|
|
226
|
+
pathParams?: Record<string, any>;
|
|
227
|
+
config?: AxiosRequestConfig;
|
|
228
|
+
onMutate?: (variables: TInput) => TContext | Promise<TContext>;
|
|
229
|
+
onSuccess?: (data: TOutput, context?: TContext) => void;
|
|
230
|
+
onError?: (error: ApiError, context?: TContext) => void;
|
|
231
|
+
}
|
|
232
|
+
/** @description A fully-typed object representing the callable actions for a module. */
|
|
233
|
+
type ModuleActions<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
234
|
+
[K in keyof TActions]: {
|
|
235
|
+
execute: <TContext = unknown>(input?: InputOf<TActions[K]>, options?: ExecuteOptions<InputOf<TActions[K]>, OutputOf<TActions[K]>, TContext>) => Promise<StandardResponse<OutputOf<TActions[K]>>>;
|
|
236
|
+
reset: (input?: InputOf<TActions[K]>, options?: {
|
|
237
|
+
pathParams?: Record<string, any>;
|
|
238
|
+
}) => void;
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
/** @description A fully-typed object representing the reactive states for each action in a module. */
|
|
242
|
+
type ModuleStates<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
243
|
+
[K in keyof TActions]: ActionState<OutputOf<TActions[K]>>;
|
|
244
|
+
};
|
|
245
|
+
/** @description The complete, fully-typed return object of the `useApiModule` hook. */
|
|
246
|
+
interface UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
247
|
+
states: ModuleStates<TActions>;
|
|
248
|
+
actions: ModuleActions<TActions>;
|
|
249
|
+
queries: {
|
|
250
|
+
[K in keyof TActions]?: TActions[K] extends {
|
|
251
|
+
hasQuery: true;
|
|
252
|
+
} ? UseApiQuery : never;
|
|
253
|
+
};
|
|
254
|
+
dehydrate: () => string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export type { ApiClientConfig as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ActionConfigModule as a, ActionOptions as b, ActionStateModule as c, ApiError as d, ApiModuleConfig as e, UseApiModuleOptions as f, UseApiModuleReturn as g, UseApiQuery as h, TokenManager as i, Middleware as j, RefreshTokenConfig as k, ActionConfig as l, UseApiState as m, ActionState as n, ExecuteOptions as o, ModuleActions as p, ModuleStates as q };
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosProgressEvent } 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
|
+
|
|
191
|
+
/** @description Extends the base ActionState with an `isStale` flag for automatic refetching. */
|
|
192
|
+
interface ActionState<TOutput> extends ActionStateModule<TOutput> {
|
|
193
|
+
isStale?: boolean;
|
|
194
|
+
}
|
|
195
|
+
/** @description Defines the configuration for a single API action within a module. */
|
|
196
|
+
interface ActionConfigModule<TInput = unknown, TOutput = unknown> {
|
|
197
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
198
|
+
path: string;
|
|
199
|
+
description?: string;
|
|
200
|
+
hasQuery?: boolean;
|
|
201
|
+
autoFetch?: boolean;
|
|
202
|
+
invalidates?: string[];
|
|
203
|
+
_input?: TInput;
|
|
204
|
+
_output?: TOutput;
|
|
205
|
+
}
|
|
206
|
+
/** @description Defines the complete structure of an API module. */
|
|
207
|
+
interface ApiModuleConfig<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
208
|
+
baseEndpoint: string;
|
|
209
|
+
actions: TActions;
|
|
210
|
+
}
|
|
211
|
+
/** @description Configuration options passed directly to the `useApiModule` hook. */
|
|
212
|
+
interface UseApiModuleOptions {
|
|
213
|
+
onSuccess?: (actionName: string, message: string, data: unknown) => void;
|
|
214
|
+
onError?: (actionName: string, message: string, error?: ApiError | null) => void;
|
|
215
|
+
refetchOnWindowFocus?: boolean;
|
|
216
|
+
pathParams?: Record<string, any>;
|
|
217
|
+
enabled?: boolean;
|
|
218
|
+
hydratedState?: string;
|
|
219
|
+
}
|
|
220
|
+
/** A utility type to infer the Input type (`TInput`) from an ActionConfigModule. */
|
|
221
|
+
type InputOf<TActionConfig> = TActionConfig extends ActionConfigModule<infer TInput, any> ? TInput : never;
|
|
222
|
+
/** A utility type to infer the Output type (`TOutput`) from an ActionConfigModule. */
|
|
223
|
+
type OutputOf<TActionConfig> = TActionConfig extends ActionConfigModule<any, infer TOutput> ? TOutput : never;
|
|
224
|
+
/** @description Defines the options for the `execute` function, enabling optimistic updates. */
|
|
225
|
+
interface ExecuteOptions<TInput, TOutput, TContext = unknown> {
|
|
226
|
+
pathParams?: Record<string, any>;
|
|
227
|
+
config?: AxiosRequestConfig;
|
|
228
|
+
onMutate?: (variables: TInput) => TContext | Promise<TContext>;
|
|
229
|
+
onSuccess?: (data: TOutput, context?: TContext) => void;
|
|
230
|
+
onError?: (error: ApiError, context?: TContext) => void;
|
|
231
|
+
}
|
|
232
|
+
/** @description A fully-typed object representing the callable actions for a module. */
|
|
233
|
+
type ModuleActions<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
234
|
+
[K in keyof TActions]: {
|
|
235
|
+
execute: <TContext = unknown>(input?: InputOf<TActions[K]>, options?: ExecuteOptions<InputOf<TActions[K]>, OutputOf<TActions[K]>, TContext>) => Promise<StandardResponse<OutputOf<TActions[K]>>>;
|
|
236
|
+
reset: (input?: InputOf<TActions[K]>, options?: {
|
|
237
|
+
pathParams?: Record<string, any>;
|
|
238
|
+
}) => void;
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
/** @description A fully-typed object representing the reactive states for each action in a module. */
|
|
242
|
+
type ModuleStates<TActions extends Record<string, ActionConfigModule<any, any>>> = {
|
|
243
|
+
[K in keyof TActions]: ActionState<OutputOf<TActions[K]>>;
|
|
244
|
+
};
|
|
245
|
+
/** @description The complete, fully-typed return object of the `useApiModule` hook. */
|
|
246
|
+
interface UseApiModuleReturn<TActions extends Record<string, ActionConfigModule<any, any>>> {
|
|
247
|
+
states: ModuleStates<TActions>;
|
|
248
|
+
actions: ModuleActions<TActions>;
|
|
249
|
+
queries: {
|
|
250
|
+
[K in keyof TActions]?: TActions[K] extends {
|
|
251
|
+
hasQuery: true;
|
|
252
|
+
} ? UseApiQuery : never;
|
|
253
|
+
};
|
|
254
|
+
dehydrate: () => string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export type { ApiClientConfig as A, ExecutableAction as E, InputOf as I, LogLevel as L, MiddlewareContext 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, ActionConfigModule as a, ActionOptions as b, ActionStateModule as c, ApiError as d, ApiModuleConfig as e, UseApiModuleOptions as f, UseApiModuleReturn as g, UseApiQuery as h, TokenManager as i, Middleware as j, RefreshTokenConfig as k, ActionConfig as l, UseApiState as m, ActionState as n, ExecuteOptions as o, ModuleActions as p, ModuleStates as q };
|