@qlover/fe-corekit 1.3.0 → 1.3.1
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 +3 -24
- package/dist/index.d.ts +337 -536
- package/dist/index.js +1 -0
- package/dist/index.umd.js +1 -1
- package/package.json +7 -15
- package/dist/index.es.js +0 -1
- package/dist/interface/index.d.ts +0 -947
- package/dist/interface/index.es.js +0 -1
- package/dist/interface/index.umd.js +0 -1
- package/dist/server/index.cjs.js +0 -1
- package/dist/server/index.d.ts +0 -152
- package/dist/server/index.es.js +0 -1
|
@@ -1,947 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic interface for encryption/decryption operations
|
|
3
|
-
* Provides a standard contract for implementing encryption strategies
|
|
4
|
-
*
|
|
5
|
-
* @template ValueType - Type of value to encrypt/decrypt
|
|
6
|
-
* @template EncryptResult - Type of encrypted result
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* // String encryption implementation
|
|
11
|
-
* class StringEncryptor implements Encryptor<string, string> {
|
|
12
|
-
* encrypt(value: string): string {
|
|
13
|
-
* // Encryption logic
|
|
14
|
-
* }
|
|
15
|
-
*
|
|
16
|
-
* decrypt(encryptedData: string): string {
|
|
17
|
-
* // Decryption logic
|
|
18
|
-
* }
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
interface Encryptor<ValueType, EncryptResult> {
|
|
23
|
-
/**
|
|
24
|
-
* Encrypts the provided value
|
|
25
|
-
* @param value - Value to encrypt
|
|
26
|
-
* @returns Encrypted result
|
|
27
|
-
*/
|
|
28
|
-
encrypt(value: ValueType): EncryptResult;
|
|
29
|
-
/**
|
|
30
|
-
* Decrypts the encrypted data
|
|
31
|
-
* @param encryptedData - Data to decrypt
|
|
32
|
-
* @returns Original value
|
|
33
|
-
*/
|
|
34
|
-
decrypt(encryptedData: EncryptResult): ValueType;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Custom error class for executor operations.
|
|
39
|
-
*
|
|
40
|
-
* This class provides a structured way to handle errors that occur during executor operations.
|
|
41
|
-
* It extends the standard Error class to include an error identification string, which can be used
|
|
42
|
-
* to categorize and manage errors more effectively.
|
|
43
|
-
*
|
|
44
|
-
* @category Executor
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```typescript
|
|
48
|
-
* try {
|
|
49
|
-
* // some executor operation
|
|
50
|
-
* } catch (error) {
|
|
51
|
-
* throw new ExecutorError('EXECUTOR_ERROR', error);
|
|
52
|
-
* }
|
|
53
|
-
* ```
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
*
|
|
57
|
-
* create an error with a message from a string
|
|
58
|
-
*
|
|
59
|
-
* ```typescript
|
|
60
|
-
* const error = new ExecutorError('ERROR_ID', 'This is an error message');
|
|
61
|
-
*
|
|
62
|
-
* // => error.message is 'This is an error message'
|
|
63
|
-
* // => error.id is 'ERROR_ID'
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
declare class ExecutorError extends Error {
|
|
67
|
-
id: string;
|
|
68
|
-
/**
|
|
69
|
-
* Constructs a new ExecutorError.
|
|
70
|
-
*
|
|
71
|
-
* if originalError is a string, it will be used as the error message.
|
|
72
|
-
* if originalError is an Error object, its message will be used as the error message.
|
|
73
|
-
* if originalError is not provided, the error message will be the id.
|
|
74
|
-
*
|
|
75
|
-
* @param id - A unique identifier for the error, used for categorization and tracking.
|
|
76
|
-
* @param originalError - The original error message or Error object that triggered this error.
|
|
77
|
-
* This parameter is optional.
|
|
78
|
-
*/
|
|
79
|
-
constructor(id: string, originalError?: string | Error);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Represents the context in which a task is executed.
|
|
84
|
-
*
|
|
85
|
-
* This interface is designed to encapsulate the necessary information
|
|
86
|
-
* for executing a task, including parameters, potential errors, and
|
|
87
|
-
* the return value. It allows for additional properties to be added
|
|
88
|
-
* dynamically, making it flexible for various use cases.
|
|
89
|
-
*
|
|
90
|
-
* @template Params - The type of parameters that the task accepts.
|
|
91
|
-
*
|
|
92
|
-
* @since 1.0.14
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* const context: ExecutorContextInterface<MyParams> = {
|
|
97
|
-
* parameters: { id: 1 },
|
|
98
|
-
* error: null,
|
|
99
|
-
* returnValue: 'Success'
|
|
100
|
-
* };
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
interface ExecutorContext<Params = unknown> {
|
|
104
|
-
/**
|
|
105
|
-
* The error that occurred during the execution of the task.
|
|
106
|
-
*
|
|
107
|
-
* This property is optional and will be populated if an error
|
|
108
|
-
* occurs during task execution.
|
|
109
|
-
*
|
|
110
|
-
* @type {Error | undefined}
|
|
111
|
-
*/
|
|
112
|
-
error?: Error;
|
|
113
|
-
/**
|
|
114
|
-
* The parameters passed to the task.
|
|
115
|
-
*
|
|
116
|
-
* These parameters are used to execute the task and are of a generic
|
|
117
|
-
* type, allowing for flexibility in the types of parameters that can
|
|
118
|
-
* be passed.
|
|
119
|
-
*
|
|
120
|
-
* @type {Params}
|
|
121
|
-
*/
|
|
122
|
-
parameters: Params;
|
|
123
|
-
/**
|
|
124
|
-
* The return value of the task.
|
|
125
|
-
*
|
|
126
|
-
* This property is optional and will contain the result of the task
|
|
127
|
-
* execution if it completes successfully.
|
|
128
|
-
*
|
|
129
|
-
* @type {unknown | undefined}
|
|
130
|
-
*/
|
|
131
|
-
returnValue?: unknown;
|
|
132
|
-
/**
|
|
133
|
-
* The runtime information of the task.
|
|
134
|
-
*
|
|
135
|
-
* The current runtime of each hook.
|
|
136
|
-
*
|
|
137
|
-
* This property is optional and will contain the runtime information
|
|
138
|
-
* of the task if it is executed.
|
|
139
|
-
*
|
|
140
|
-
* property is read-only
|
|
141
|
-
*
|
|
142
|
-
* will return a frozen object that will be cleared after the chain execution is complete.
|
|
143
|
-
*
|
|
144
|
-
*/
|
|
145
|
-
hooksRuntimes: HookRuntimes;
|
|
146
|
-
}
|
|
147
|
-
interface HookRuntimes {
|
|
148
|
-
hookName?: string;
|
|
149
|
-
/**
|
|
150
|
-
* The return value of the task.
|
|
151
|
-
*
|
|
152
|
-
* Readonly
|
|
153
|
-
*/
|
|
154
|
-
returnValue?: unknown;
|
|
155
|
-
/**
|
|
156
|
-
* 执行次数
|
|
157
|
-
*/
|
|
158
|
-
times?: number;
|
|
159
|
-
/**
|
|
160
|
-
* 是否中断链
|
|
161
|
-
*/
|
|
162
|
-
breakChain?: boolean;
|
|
163
|
-
/**
|
|
164
|
-
* 如果有返回值,是否中断链
|
|
165
|
-
*
|
|
166
|
-
* 一般常用于需要返回一个值时中断链,比如 onError 声明周期
|
|
167
|
-
*/
|
|
168
|
-
returnBreakChain?: boolean;
|
|
169
|
-
[key: string]: unknown;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Type definition for promise-based task
|
|
174
|
-
* @template T - Return type of the task
|
|
175
|
-
* @template D - Input data type for the task
|
|
176
|
-
* @example
|
|
177
|
-
* ```typescript
|
|
178
|
-
* const promiseTask: PromiseTask<string, number> = async (data: number) => {
|
|
179
|
-
* return `Result: ${data}`;
|
|
180
|
-
* };
|
|
181
|
-
* ```
|
|
182
|
-
* @category ExecutorPlugin
|
|
183
|
-
*/
|
|
184
|
-
type PromiseTask<Result, Params> = (context: ExecutorContext<Params>) => Promise<Result>;
|
|
185
|
-
/**
|
|
186
|
-
* Type definition for synchronous task
|
|
187
|
-
* @template Result - Return type of the task
|
|
188
|
-
* @template D - Input data type for the task
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* const syncTask: SyncTask<string, number> = (data: number) => {
|
|
192
|
-
* return `Result: ${data}`;
|
|
193
|
-
* };
|
|
194
|
-
* ```
|
|
195
|
-
* @category ExecutorPlugin
|
|
196
|
-
*/
|
|
197
|
-
type SyncTask<Result, Params> = (context: ExecutorContext<Params>) => Result;
|
|
198
|
-
/**
|
|
199
|
-
* Union type for both promise and sync tasks
|
|
200
|
-
* @template T - Return type of the task
|
|
201
|
-
* @template D - Input data type for the task
|
|
202
|
-
* @category ExecutorPlugin
|
|
203
|
-
*/
|
|
204
|
-
type Task<Result, Params> = PromiseTask<Result, Params> | SyncTask<Result, Params>;
|
|
205
|
-
/**
|
|
206
|
-
* Base plugin class for extending executor functionality.
|
|
207
|
-
*
|
|
208
|
-
* Plugins provide a way to intercept and modify the execution flow at different stages:
|
|
209
|
-
* - Before execution (onBefore)
|
|
210
|
-
* - After successful execution (onSuccess)
|
|
211
|
-
* - On error (onError)
|
|
212
|
-
* - Custom execution logic (onExec)
|
|
213
|
-
*
|
|
214
|
-
* LifeCycle:
|
|
215
|
-
*
|
|
216
|
-
* **onBefore**
|
|
217
|
-
* - onBefore can modify the input data before it reaches the task, before exec is called.
|
|
218
|
-
* - The parameter of the first plugin's onBefore is the input data of exec.
|
|
219
|
-
* - The parameter of other plugins' onBefore is the return value of previous plugin's onBefore.
|
|
220
|
-
* - Also, not return value, will use first plugin's onBefore return value or exec's input data.
|
|
221
|
-
* - The parameter of the first plugin's onBefore is the input data of exec.
|
|
222
|
-
* - If any plugin's onBefore throws an error, it immediately stops the onBefore chain and enters the onError chain.
|
|
223
|
-
*
|
|
224
|
-
* **onExec**
|
|
225
|
-
* - onExec can modify the task before it is executed.
|
|
226
|
-
* - Use first plugin's onExec return value or exec's task.
|
|
227
|
-
* - The exec execution is only allowed to be modified once, so only the first onExec lifecycle method registered in the plugins list will be used.
|
|
228
|
-
*
|
|
229
|
-
* **onSuccess**
|
|
230
|
-
* - When call exec, onSuccess will be executed after onExec.
|
|
231
|
-
* - onSuccess accept the result of previous plugin's onSuccess, and can return a new result to the next plugin's onSuccess.
|
|
232
|
-
* - That means, if any plugin's onSuccess returns a new value, the next plugin's onSuccess will accept the value of previous plugin's onSuccess as parameter,
|
|
233
|
-
* - and can continue to return a new value, until the last plugin's onSuccess. The entire chain will not stop.
|
|
234
|
-
* - The parameter of the first plugin's onSuccess is the result of exec.
|
|
235
|
-
* - If any plugin's onSuccess throws an error, it immediately stops the onSuccess chain and enters the onError chain.
|
|
236
|
-
*
|
|
237
|
-
* **onError**
|
|
238
|
-
* - When an error occurs during call exec, all plugins' onError will be ordered executed.
|
|
239
|
-
* - After exec, all errors will be wrapped with ExecutorError.
|
|
240
|
-
* - If onError of any of the plugins returns an error, the error is thrown and the entire chain is stopped, but execNoError only return the error.
|
|
241
|
-
* - If any plugin's onError throws an error, it immediately stops the entire chain and throws the error, since errors in the error chain cannot be caught. Whether exec or execNoError.
|
|
242
|
-
* - If all plugins' onError neither return nor throw an error, wrapping raw Errors with ExecutorError and throw.
|
|
243
|
-
* - If execNoError is called, the first error encountered is returned, and the entire lifecycle is terminated.
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* **execNoError returns all errors as they are.**
|
|
247
|
-
*
|
|
248
|
-
* @template T - Type of data being processed
|
|
249
|
-
* @template R - Type of result after processing
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```typescript
|
|
253
|
-
* class LoggerPlugin extends ExecutorPlugin {
|
|
254
|
-
* onBefore(data: unknown) {
|
|
255
|
-
* console.log('Before execution:', data);
|
|
256
|
-
* return data;
|
|
257
|
-
* }
|
|
258
|
-
*
|
|
259
|
-
* onSuccess(result: unknown) {
|
|
260
|
-
* console.log('Execution succeeded:', result);
|
|
261
|
-
* return result;
|
|
262
|
-
* }
|
|
263
|
-
*
|
|
264
|
-
* onError(error: Error) {
|
|
265
|
-
* console.error('Execution failed:', error);
|
|
266
|
-
* throw error;
|
|
267
|
-
* }
|
|
268
|
-
* }
|
|
269
|
-
* ```
|
|
270
|
-
* @category ExecutorPlugin
|
|
271
|
-
*/
|
|
272
|
-
interface ExecutorPlugin<T = unknown> {
|
|
273
|
-
/**
|
|
274
|
-
* The pluginName of the plugin.
|
|
275
|
-
*
|
|
276
|
-
* Plugins with the same pluginName will be merged.
|
|
277
|
-
*/
|
|
278
|
-
readonly pluginName: string;
|
|
279
|
-
/**
|
|
280
|
-
* Indicates if only one instance of this plugin should exist in the executor
|
|
281
|
-
* When true, attempting to add duplicate plugins will result in a warning
|
|
282
|
-
*/
|
|
283
|
-
readonly onlyOne?: boolean;
|
|
284
|
-
/**
|
|
285
|
-
* Controls whether the plugin is active for specific hook executions
|
|
286
|
-
* @param name - Name of the hook being executed
|
|
287
|
-
* @param args - Arguments passed to the hook
|
|
288
|
-
* @returns Boolean indicating if the plugin should be executed
|
|
289
|
-
*
|
|
290
|
-
* @example
|
|
291
|
-
* ```typescript
|
|
292
|
-
* enabled(name: keyof ExecutorPlugin, context: ExecutorContextInterface<T>) {
|
|
293
|
-
* // Only enable for error handling
|
|
294
|
-
* return name === 'onError';
|
|
295
|
-
* }
|
|
296
|
-
* ```
|
|
297
|
-
*/
|
|
298
|
-
enabled?(name: keyof ExecutorPlugin, context?: ExecutorContext<T>): boolean;
|
|
299
|
-
/**
|
|
300
|
-
* Hook executed before the main task
|
|
301
|
-
* Can modify the input data before it reaches the task
|
|
302
|
-
* @param data - Input data
|
|
303
|
-
* @returns Modified data or Promise of modified data
|
|
304
|
-
*/
|
|
305
|
-
onBefore?(context: ExecutorContext<T>): void | Promise<void>;
|
|
306
|
-
/**
|
|
307
|
-
* Error handling hook
|
|
308
|
-
* - For `exec`: returning a value or throwing will break the chain
|
|
309
|
-
* - For `execNoError`: returning a value or throwing will return the error
|
|
310
|
-
*
|
|
311
|
-
* Because `onError` can break the chain, best practice is each plugin only handle plugin related error
|
|
312
|
-
*
|
|
313
|
-
* @param error - Error that occurred
|
|
314
|
-
* @param data - Original input data
|
|
315
|
-
* @returns ExecutorError, void, or Promise of either
|
|
316
|
-
*/
|
|
317
|
-
onError?(context: ExecutorContext<T>): Promise<ExecutorError | Error | void> | ExecutorError | Error | void;
|
|
318
|
-
/**
|
|
319
|
-
* Hook executed after successful task completion
|
|
320
|
-
* Can transform the task result
|
|
321
|
-
* @param result - Task execution result
|
|
322
|
-
* @returns Modified result or Promise of modified result
|
|
323
|
-
*/
|
|
324
|
-
onSuccess?(context: ExecutorContext<T>): void | Promise<void>;
|
|
325
|
-
/**
|
|
326
|
-
* Custom execution logic hook
|
|
327
|
-
* Only the first plugin with onExec will be used
|
|
328
|
-
* @param task - Task to be executed
|
|
329
|
-
* @returns Task result or Promise of result
|
|
330
|
-
*/
|
|
331
|
-
onExec?(context: ExecutorContext<unknown>, task: Task<unknown, unknown>): Promise<unknown> | unknown;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Base executor class providing plugin management and execution pipeline
|
|
336
|
-
*
|
|
337
|
-
* The Executor pattern implements a pluggable execution pipeline that allows:
|
|
338
|
-
* 1. Pre-processing of input data
|
|
339
|
-
* 2. Post-processing of results
|
|
340
|
-
* 3. Error handling
|
|
341
|
-
* 4. Custom execution logic
|
|
342
|
-
*
|
|
343
|
-
* execNoError returns all errors as they are., and if there is a plugin onerror handler chain in which an error occurs, it will also return the error instead of throwing it.
|
|
344
|
-
*
|
|
345
|
-
* @abstract
|
|
346
|
-
* @class Executor
|
|
347
|
-
* @category Executor
|
|
348
|
-
* @example
|
|
349
|
-
* ```typescript
|
|
350
|
-
* // Create an executor instance
|
|
351
|
-
* const executor = new AsyncExecutor();
|
|
352
|
-
*
|
|
353
|
-
* // Add plugins
|
|
354
|
-
* executor.use(new LoggerPlugin());
|
|
355
|
-
* executor.use(new RetryPlugin({ maxAttempts: 3 }));
|
|
356
|
-
*
|
|
357
|
-
* // Execute a task
|
|
358
|
-
* const result = await executor.exec(async (data) => {
|
|
359
|
-
* return await someAsyncOperation(data);
|
|
360
|
-
* });
|
|
361
|
-
* ```
|
|
362
|
-
*/
|
|
363
|
-
declare abstract class Executor<ExecutorConfig = unknown> {
|
|
364
|
-
protected config?: ExecutorConfig | undefined;
|
|
365
|
-
/**
|
|
366
|
-
* Array of active plugins
|
|
367
|
-
*
|
|
368
|
-
* - Purpose: Stores and manages executor plugins
|
|
369
|
-
* - Core Concept: Ordered plugin pipeline
|
|
370
|
-
* - Main Features:
|
|
371
|
-
* - Maintains plugin execution order
|
|
372
|
-
* - Supports plugin lifecycle management
|
|
373
|
-
* - Primary Use: Plugin orchestration and execution
|
|
374
|
-
*
|
|
375
|
-
* @example
|
|
376
|
-
* ```typescript
|
|
377
|
-
* protected plugins = [
|
|
378
|
-
* new LoggerPlugin(),
|
|
379
|
-
* new RetryPlugin()
|
|
380
|
-
* ];
|
|
381
|
-
* ```
|
|
382
|
-
*/
|
|
383
|
-
protected plugins: ExecutorPlugin[];
|
|
384
|
-
/**
|
|
385
|
-
* Creates a new Executor instance
|
|
386
|
-
*
|
|
387
|
-
* - Purpose: Initialize executor with optional configuration
|
|
388
|
-
* - Core Concept: Configurable executor setup
|
|
389
|
-
* - Main Features: Configuration injection
|
|
390
|
-
* - Primary Use: Executor instantiation
|
|
391
|
-
*
|
|
392
|
-
* @param {ExecutorConfig} config - Optional configuration object
|
|
393
|
-
*
|
|
394
|
-
* @example
|
|
395
|
-
* ```typescript
|
|
396
|
-
* const executor = new Executor({
|
|
397
|
-
* // config options
|
|
398
|
-
* });
|
|
399
|
-
* ```
|
|
400
|
-
*/
|
|
401
|
-
constructor(config?: ExecutorConfig | undefined);
|
|
402
|
-
/**
|
|
403
|
-
* Add a plugin to the executor
|
|
404
|
-
*
|
|
405
|
-
* - Purpose: Extends executor functionality through plugins
|
|
406
|
-
* - Core Concept: Plugin registration and deduplication
|
|
407
|
-
* - Main Features:
|
|
408
|
-
* - Prevents duplicate plugins if onlyOne is true
|
|
409
|
-
* - Maintains plugin execution order
|
|
410
|
-
* - Primary Use: Adding new capabilities to executor
|
|
411
|
-
*
|
|
412
|
-
* @param plugin - Plugin instance to add
|
|
413
|
-
*
|
|
414
|
-
* @example
|
|
415
|
-
* ```typescript
|
|
416
|
-
* executor.use(new LoggerPlugin());
|
|
417
|
-
* executor.use(new RetryPlugin({ maxAttempts: 3 }));
|
|
418
|
-
* ```
|
|
419
|
-
*
|
|
420
|
-
* @example
|
|
421
|
-
*
|
|
422
|
-
* Use a plain object as a plugin
|
|
423
|
-
* ```typescript
|
|
424
|
-
* executor.use({
|
|
425
|
-
* onBefore: (data) => ({ ...data, modified: true })
|
|
426
|
-
* });
|
|
427
|
-
* ```
|
|
428
|
-
*/
|
|
429
|
-
use(plugin: ExecutorPlugin): void;
|
|
430
|
-
/**
|
|
431
|
-
* Execute a plugin hook
|
|
432
|
-
*
|
|
433
|
-
* - Purpose: Provides plugin hook execution mechanism
|
|
434
|
-
* - Core Concept: Plugin lifecycle management
|
|
435
|
-
* - Main Features:
|
|
436
|
-
* - Dynamic hook execution
|
|
437
|
-
* - Support for async and sync hooks
|
|
438
|
-
* - Primary Use: Running plugin lifecycle methods
|
|
439
|
-
*
|
|
440
|
-
* @param plugins - Plugins to execute
|
|
441
|
-
* @param name - Hook name to execute
|
|
442
|
-
* @param args - Arguments for the hook
|
|
443
|
-
*
|
|
444
|
-
* @example
|
|
445
|
-
* ```typescript
|
|
446
|
-
* await executor.runHook(plugins, 'beforeExec', data);
|
|
447
|
-
* ```
|
|
448
|
-
*/
|
|
449
|
-
abstract runHooks(plugins: ExecutorPlugin[], name: keyof ExecutorPlugin, ...args: unknown[]): void | unknown | Promise<void | unknown>;
|
|
450
|
-
/**
|
|
451
|
-
* Execute a task with plugin pipeline
|
|
452
|
-
*
|
|
453
|
-
* - Purpose: Core task execution with plugin support
|
|
454
|
-
* - Core Concept: Task execution pipeline
|
|
455
|
-
* - Main Features:
|
|
456
|
-
* - Plugin hook integration
|
|
457
|
-
* - Error handling
|
|
458
|
-
* - Primary Use: Running tasks through the executor pipeline
|
|
459
|
-
*
|
|
460
|
-
* @param task - Task to execute
|
|
461
|
-
* @param data - Optional input data for task
|
|
462
|
-
* @throws {ExecutorError} If task execution fails
|
|
463
|
-
*
|
|
464
|
-
* @example
|
|
465
|
-
* ```typescript
|
|
466
|
-
* const result = await executor.exec(async (data) => {
|
|
467
|
-
* return await processData(data);
|
|
468
|
-
* });
|
|
469
|
-
* ```
|
|
470
|
-
*/
|
|
471
|
-
abstract exec<Result, Params = unknown>(task: Task<Result, Params>): Promise<Result> | Result;
|
|
472
|
-
/**
|
|
473
|
-
* Execute a task with plugin pipeline and input data
|
|
474
|
-
*
|
|
475
|
-
* - Purpose: Core task execution with plugin support and input data
|
|
476
|
-
* - Core Concept: Task execution pipeline with data
|
|
477
|
-
* - Main Features:
|
|
478
|
-
* - Plugin hook integration
|
|
479
|
-
* - Error handling
|
|
480
|
-
* - Primary Use: Running tasks with input data through the executor pipeline
|
|
481
|
-
*
|
|
482
|
-
* @param data - Input data for task
|
|
483
|
-
* @param task - Task to execute
|
|
484
|
-
* @throws {ExecutorError} If task execution fails
|
|
485
|
-
*
|
|
486
|
-
* @example
|
|
487
|
-
* ```typescript
|
|
488
|
-
* const result = await executor.exec(data, async (data) => {
|
|
489
|
-
* return await processData(data);
|
|
490
|
-
* });
|
|
491
|
-
* ```
|
|
492
|
-
*/
|
|
493
|
-
abstract exec<Result, Params = unknown>(data: unknown, task: Task<Result, Params>): Promise<Result> | Result;
|
|
494
|
-
/**
|
|
495
|
-
* Execute a task without throwing errors
|
|
496
|
-
*
|
|
497
|
-
* - Purpose: Safe task execution with error wrapping
|
|
498
|
-
* - Core Concept: Error-safe execution pipeline
|
|
499
|
-
* - Main Features:
|
|
500
|
-
* - Error wrapping in ExecutorError
|
|
501
|
-
* - Non-throwing execution
|
|
502
|
-
* - Primary Use: When error handling is preferred over exceptions
|
|
503
|
-
*
|
|
504
|
-
* @param task - Task to execute
|
|
505
|
-
*
|
|
506
|
-
* @example
|
|
507
|
-
* ```typescript
|
|
508
|
-
* const result = await executor.execNoError(async (data) => {
|
|
509
|
-
* return await riskyOperation(data);
|
|
510
|
-
* });
|
|
511
|
-
* if (result instanceof ExecutorError) {
|
|
512
|
-
* console.error('Task failed:', result);
|
|
513
|
-
* }
|
|
514
|
-
* ```
|
|
515
|
-
*/
|
|
516
|
-
abstract execNoError<Result, Params = unknown>(task: Task<Result, Params>): Promise<Result | ExecutorError> | Result | ExecutorError;
|
|
517
|
-
/**
|
|
518
|
-
* Execute a task with input data without throwing errors
|
|
519
|
-
*
|
|
520
|
-
* - Purpose: Safe task execution with error wrapping and input data
|
|
521
|
-
* - Core Concept: Error-safe execution pipeline with data
|
|
522
|
-
* - Main Features:
|
|
523
|
-
* - Error wrapping in ExecutorError
|
|
524
|
-
* - Non-throwing execution
|
|
525
|
-
* - Primary Use: When error handling is preferred over exceptions with input data
|
|
526
|
-
*
|
|
527
|
-
* @param data - Input data for task
|
|
528
|
-
* @param task - Task to execute
|
|
529
|
-
*
|
|
530
|
-
* @example
|
|
531
|
-
* ```typescript
|
|
532
|
-
* const result = await executor.execNoError(data, async (data) => {
|
|
533
|
-
* return await riskyOperation(data);
|
|
534
|
-
* });
|
|
535
|
-
* if (result instanceof ExecutorError) {
|
|
536
|
-
* console.error('Task failed:', result);
|
|
537
|
-
* }
|
|
538
|
-
* ```
|
|
539
|
-
*/
|
|
540
|
-
abstract execNoError<Result, Params = unknown>(data: unknown, task: Task<Result, Params>): Promise<Result | ExecutorError> | Result | ExecutorError;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
/**
|
|
544
|
-
* Request adapter configuration
|
|
545
|
-
*
|
|
546
|
-
* This type defines the configuration options for a request adapter.
|
|
547
|
-
* It includes properties for URL, method, headers, and other request details.
|
|
548
|
-
* The main purpose is to provide a flexible structure for configuring HTTP requests.
|
|
549
|
-
*
|
|
550
|
-
* @since 1.0.14
|
|
551
|
-
*/
|
|
552
|
-
interface RequestAdapterConfig<RequestData = unknown> {
|
|
553
|
-
/**
|
|
554
|
-
* Request URL path
|
|
555
|
-
* Will be combined with baseURL if provided
|
|
556
|
-
*
|
|
557
|
-
* Processed by FetchURLPlugin during request
|
|
558
|
-
*
|
|
559
|
-
* @todo Change to URL | Request, add attribute `input`
|
|
560
|
-
* @example
|
|
561
|
-
* ```typescript
|
|
562
|
-
* url: '/users/1'
|
|
563
|
-
* ```
|
|
564
|
-
*/
|
|
565
|
-
url?: string;
|
|
566
|
-
/**
|
|
567
|
-
* HTTP request methods supported by the executor
|
|
568
|
-
* Follows standard HTTP method definitions
|
|
569
|
-
*
|
|
570
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
|
571
|
-
* @example
|
|
572
|
-
* ```typescript
|
|
573
|
-
* method: 'GET'
|
|
574
|
-
* ```
|
|
575
|
-
*/
|
|
576
|
-
method?: string;
|
|
577
|
-
/**
|
|
578
|
-
* Base URL for all requests
|
|
579
|
-
* Will be prepended to the request URL
|
|
580
|
-
*
|
|
581
|
-
* @example
|
|
582
|
-
* ```typescript
|
|
583
|
-
* baseURL: 'https://api.example.com'
|
|
584
|
-
* // url = /users/1 => https://api.example.com/users/1
|
|
585
|
-
* // url = users/1 => https://api.example.com/users/1
|
|
586
|
-
* ```
|
|
587
|
-
*/
|
|
588
|
-
baseURL?: string;
|
|
589
|
-
/**
|
|
590
|
-
* Request body data
|
|
591
|
-
*
|
|
592
|
-
* Mapping fetch `body`
|
|
593
|
-
*
|
|
594
|
-
* @typeParam RequestData - The type of the request body data.
|
|
595
|
-
* @example
|
|
596
|
-
* ```typescript
|
|
597
|
-
* data: { name: 'John Doe' }
|
|
598
|
-
* ```
|
|
599
|
-
*/
|
|
600
|
-
data?: RequestData;
|
|
601
|
-
/**
|
|
602
|
-
* URL query parameters
|
|
603
|
-
* Will be serialized and appended to the URL
|
|
604
|
-
*
|
|
605
|
-
* @example
|
|
606
|
-
* ```typescript
|
|
607
|
-
* params: { search: 'query' }
|
|
608
|
-
* ```
|
|
609
|
-
*/
|
|
610
|
-
params?: Record<string, unknown>;
|
|
611
|
-
/**
|
|
612
|
-
* Request headers
|
|
613
|
-
*
|
|
614
|
-
* @example
|
|
615
|
-
* ```typescript
|
|
616
|
-
* headers: { 'Content-Type': 'application/json' }
|
|
617
|
-
* ```
|
|
618
|
-
*/
|
|
619
|
-
headers?: {
|
|
620
|
-
[key: string]: unknown;
|
|
621
|
-
};
|
|
622
|
-
/**
|
|
623
|
-
* Response type
|
|
624
|
-
*
|
|
625
|
-
* Specifies the type of data that the server will respond with.
|
|
626
|
-
*
|
|
627
|
-
* @example
|
|
628
|
-
* ```typescript
|
|
629
|
-
* responseType: 'json'
|
|
630
|
-
* ```
|
|
631
|
-
*/
|
|
632
|
-
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream' | 'formdata';
|
|
633
|
-
/**
|
|
634
|
-
* Request ID, used to identify the request in the abort plugin.
|
|
635
|
-
*/
|
|
636
|
-
requestId?: string;
|
|
637
|
-
[key: string]: any;
|
|
638
|
-
}
|
|
639
|
-
/**
|
|
640
|
-
* Request adapter response
|
|
641
|
-
*
|
|
642
|
-
* This type defines the structure of a response from a request adapter.
|
|
643
|
-
* It includes the response data, status, headers, and the original request configuration.
|
|
644
|
-
*
|
|
645
|
-
* @typeParam Req - The type of the request data.
|
|
646
|
-
* @typeParam Res - The type of the response data.
|
|
647
|
-
*/
|
|
648
|
-
interface RequestAdapterResponse<Req = unknown, Res = unknown> {
|
|
649
|
-
data: Res;
|
|
650
|
-
status: number;
|
|
651
|
-
statusText: string;
|
|
652
|
-
headers: {
|
|
653
|
-
[key: string]: unknown;
|
|
654
|
-
};
|
|
655
|
-
config: RequestAdapterConfig<Req>;
|
|
656
|
-
response: Response;
|
|
657
|
-
[key: string]: unknown;
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Request adapter interface
|
|
661
|
-
*
|
|
662
|
-
* This interface defines the contract for request adapters.
|
|
663
|
-
* Adapters are responsible for handling the specific details of a request,
|
|
664
|
-
* such as URL construction, headers, and response handling.
|
|
665
|
-
*
|
|
666
|
-
*/
|
|
667
|
-
interface RequestAdapterInterface<Config extends RequestAdapterConfig> {
|
|
668
|
-
/**
|
|
669
|
-
* The configuration for the request adapter.
|
|
670
|
-
*
|
|
671
|
-
* @type {Config}
|
|
672
|
-
*/
|
|
673
|
-
readonly config: Config;
|
|
674
|
-
/**
|
|
675
|
-
* Sends a request using the specified options and returns a promise with the response.
|
|
676
|
-
*
|
|
677
|
-
* @typeParam Request - The type of the request data.
|
|
678
|
-
* @typeParam Response - The type of the response data.
|
|
679
|
-
* @param options - The configuration options for the request.
|
|
680
|
-
* @returns A promise that resolves to the response of the request.
|
|
681
|
-
* @example
|
|
682
|
-
* ```typescript
|
|
683
|
-
* adapter.request({ url: '/users', method: 'GET' }).then(response => console.log(response));
|
|
684
|
-
* ```
|
|
685
|
-
*/
|
|
686
|
-
request<Request, Response>(options: RequestAdapterConfig<Request>): Promise<RequestAdapterResponse<Request, Response>>;
|
|
687
|
-
/**
|
|
688
|
-
* Retrieves the current configuration of the request adapter.
|
|
689
|
-
*
|
|
690
|
-
* @returns The current configuration.
|
|
691
|
-
* @example
|
|
692
|
-
* ```typescript
|
|
693
|
-
* const config = adapter.getConfig();
|
|
694
|
-
* ```
|
|
695
|
-
*/
|
|
696
|
-
getConfig: () => Config;
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
/**
|
|
700
|
-
* Represents a custom error class for handling request-related errors in the application
|
|
701
|
-
*
|
|
702
|
-
* RequestError extends the base ExecutorError class to provide specific error handling
|
|
703
|
-
* for HTTP requests and fetch operations. It works in conjunction with RequestErrorID
|
|
704
|
-
* to categorize different types of request failures.
|
|
705
|
-
*
|
|
706
|
-
* @since 1.0.14
|
|
707
|
-
*
|
|
708
|
-
* @example
|
|
709
|
-
* ```typescript
|
|
710
|
-
* try {
|
|
711
|
-
* await fetchData(url);
|
|
712
|
-
* } catch (error) {
|
|
713
|
-
* if (error instanceof RequestError) {
|
|
714
|
-
* // Handle request specific error
|
|
715
|
-
* console.error('Request failed:', error.message);
|
|
716
|
-
* }
|
|
717
|
-
* }
|
|
718
|
-
* ```
|
|
719
|
-
*/
|
|
720
|
-
declare class RequestError extends ExecutorError {
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Error IDs for different fetch request failure scenarios
|
|
724
|
-
* Used to identify specific error types in error handling
|
|
725
|
-
*
|
|
726
|
-
* @description
|
|
727
|
-
* This enum provides a standardized set of error identifiers that can be used
|
|
728
|
-
* to categorize and handle different types of request failures in a consistent manner.
|
|
729
|
-
* Each error ID represents a specific failure scenario that might occur during HTTP requests.
|
|
730
|
-
*
|
|
731
|
-
* @example
|
|
732
|
-
* ```typescript
|
|
733
|
-
* if (error.id === RequestErrorID.RESPONSE_NOT_OK) {
|
|
734
|
-
* // Handle non-200 response
|
|
735
|
-
* } else if (error.id === RequestErrorID.ABORT_ERROR) {
|
|
736
|
-
* // Handle aborted request
|
|
737
|
-
* }
|
|
738
|
-
* ```
|
|
739
|
-
*/
|
|
740
|
-
declare enum RequestErrorID {
|
|
741
|
-
/** Generic fetch request error */
|
|
742
|
-
REQUEST_ERROR = "REQUEST_ERROR",
|
|
743
|
-
/** Environment doesn't support fetch API */
|
|
744
|
-
ENV_FETCH_NOT_SUPPORT = "ENV_FETCH_NOT_SUPPORT",
|
|
745
|
-
/** No fetcher function provided */
|
|
746
|
-
FETCHER_NONE = "FETCHER_NONE",
|
|
747
|
-
/** Response status is not OK (not in 200-299 range) */
|
|
748
|
-
RESPONSE_NOT_OK = "RESPONSE_NOT_OK",
|
|
749
|
-
/** Request was aborted */
|
|
750
|
-
ABORT_ERROR = "ABORT_ERROR",
|
|
751
|
-
/** URL is not provided */
|
|
752
|
-
URL_NONE = "URL_NONE"
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
/**
|
|
756
|
-
* Represents a transaction for a request.
|
|
757
|
-
*
|
|
758
|
-
* This interface defines a transaction that contains a request and a response.
|
|
759
|
-
* It can be used to track the request and response of a transaction.
|
|
760
|
-
*
|
|
761
|
-
* @since 1.2.2
|
|
762
|
-
*/
|
|
763
|
-
interface RequestTransactionInterface<Request, Response> {
|
|
764
|
-
/**
|
|
765
|
-
* The request object
|
|
766
|
-
*/
|
|
767
|
-
request: Request;
|
|
768
|
-
/**
|
|
769
|
-
* The response object
|
|
770
|
-
*/
|
|
771
|
-
response: Response;
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
/**
|
|
775
|
-
* Generic interface for data serialization/deserialization operations
|
|
776
|
-
* Provides a standard contract for implementing serialization strategies
|
|
777
|
-
*
|
|
778
|
-
* This is a generic interface, you can implement it with different serialization strategies
|
|
779
|
-
*
|
|
780
|
-
* @template T - Type of data to serialize/deserialize, defaults to any
|
|
781
|
-
* @template R - Type of serialized result, defaults to string
|
|
782
|
-
*
|
|
783
|
-
* @since 1.0.10
|
|
784
|
-
*
|
|
785
|
-
* @example
|
|
786
|
-
* ```typescript
|
|
787
|
-
* // JSON serialization implementation
|
|
788
|
-
* class JSONSerializer implements Serializer {
|
|
789
|
-
* serialize(data: any): string {
|
|
790
|
-
* return JSON.stringify(data);
|
|
791
|
-
* }
|
|
792
|
-
*
|
|
793
|
-
* deserialize(data: string): any {
|
|
794
|
-
* return JSON.parse(data);
|
|
795
|
-
* }
|
|
796
|
-
* }
|
|
797
|
-
* ```
|
|
798
|
-
*/
|
|
799
|
-
interface Serializer<T = unknown, R = string> {
|
|
800
|
-
/**
|
|
801
|
-
* Serializes data into a target format
|
|
802
|
-
* @since 1.0.10
|
|
803
|
-
* @param data - Data to serialize
|
|
804
|
-
* @returns Serialized representation
|
|
805
|
-
*/
|
|
806
|
-
serialize(data: T): R;
|
|
807
|
-
/**
|
|
808
|
-
* Deserializes data from target format back to original form
|
|
809
|
-
* @since 1.0.10
|
|
810
|
-
* @param data - Data to deserialize
|
|
811
|
-
* @param defaultValue - Optional default value to return if deserialization fails
|
|
812
|
-
* @returns Original data structure
|
|
813
|
-
*/
|
|
814
|
-
deserialize(data: R, defaultValue?: T): T;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
/**
|
|
818
|
-
* Interface representing a synchronous storage mechanism.
|
|
819
|
-
*
|
|
820
|
-
* @template Key - The type of keys used to identify stored values.
|
|
821
|
-
* @template ValueType - The type of values stored, defaults to unknown.
|
|
822
|
-
*
|
|
823
|
-
* @example
|
|
824
|
-
* ```typescript
|
|
825
|
-
* const storage: SyncStorage<string, number> = ...;
|
|
826
|
-
* storage.setItem('key', 123);
|
|
827
|
-
* const value = storage.getItem('key', 0);
|
|
828
|
-
* ```
|
|
829
|
-
*/
|
|
830
|
-
interface SyncStorage<Key, ValueType = unknown> {
|
|
831
|
-
/**
|
|
832
|
-
* The number of items stored.
|
|
833
|
-
*/
|
|
834
|
-
readonly length: number;
|
|
835
|
-
/**
|
|
836
|
-
* Stores a value with the specified key.
|
|
837
|
-
*
|
|
838
|
-
* @param key - The key to identify the stored value.
|
|
839
|
-
* @param value - The value to store.
|
|
840
|
-
* @param options - Optional parameters for storage.
|
|
841
|
-
*/
|
|
842
|
-
setItem<T>(key: Key, value: T, options?: unknown): void;
|
|
843
|
-
/**
|
|
844
|
-
* Retrieves a value by key.
|
|
845
|
-
*
|
|
846
|
-
* @param key - The key of the value to retrieve.
|
|
847
|
-
* @param defaultValue - The default value to return if the key is not found.
|
|
848
|
-
* @param options - Optional parameters for retrieval.
|
|
849
|
-
* @returns The value associated with the key, or the default value if not found.
|
|
850
|
-
*/
|
|
851
|
-
getItem<T extends ValueType>(key: Key, defaultValue?: T, options?: unknown): T | null;
|
|
852
|
-
/**
|
|
853
|
-
* Removes a value by key.
|
|
854
|
-
*
|
|
855
|
-
* @param key - The key of the value to remove.
|
|
856
|
-
* @param options - Optional parameters for removal.
|
|
857
|
-
*/
|
|
858
|
-
removeItem(key: Key, options?: unknown): void;
|
|
859
|
-
/**
|
|
860
|
-
* Clears all stored values.
|
|
861
|
-
*/
|
|
862
|
-
clear(): void;
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* Interface representing an asynchronous storage mechanism.
|
|
866
|
-
*
|
|
867
|
-
* @template Key - The type of keys used to identify stored values.
|
|
868
|
-
* @template ValueType - The type of values stored, defaults to unknown.
|
|
869
|
-
*
|
|
870
|
-
* @example
|
|
871
|
-
*
|
|
872
|
-
* ```typescript
|
|
873
|
-
* const storage: AsyncStorage<string, number> = ...;
|
|
874
|
-
* await storage.setItem('key', 123);
|
|
875
|
-
* const value = await storage.getItem('key', 0);
|
|
876
|
-
* ```
|
|
877
|
-
*
|
|
878
|
-
*/
|
|
879
|
-
interface AsyncStorage<Key, ValueType = unknown> {
|
|
880
|
-
/**
|
|
881
|
-
* The number of items stored.
|
|
882
|
-
*/
|
|
883
|
-
readonly length: number;
|
|
884
|
-
/**
|
|
885
|
-
* Asynchronously stores a value with the specified key.
|
|
886
|
-
*
|
|
887
|
-
* @param key - The key to identify the stored value.
|
|
888
|
-
* @param value - The value to store.
|
|
889
|
-
* @param options - Optional parameters for storage.
|
|
890
|
-
* @returns A promise that resolves when the value is stored.
|
|
891
|
-
*/
|
|
892
|
-
setItem<T>(key: Key, value: T, options?: unknown): Promise<void>;
|
|
893
|
-
/**
|
|
894
|
-
* Asynchronously retrieves a value by key.
|
|
895
|
-
*
|
|
896
|
-
* @param key - The key of the value to retrieve.
|
|
897
|
-
* @param defaultValue - The default value to return if the key is not found.
|
|
898
|
-
* @param options - Optional parameters for retrieval.
|
|
899
|
-
* @returns A promise that resolves to the value associated with the key, or the default value if not found.
|
|
900
|
-
*/
|
|
901
|
-
getItem<T extends ValueType>(key: Key, defaultValue?: T, options?: unknown): Promise<T | null>;
|
|
902
|
-
/**
|
|
903
|
-
* Asynchronously removes a value by key.
|
|
904
|
-
*
|
|
905
|
-
* @param key - The key of the value to remove.
|
|
906
|
-
* @param options - Optional parameters for removal.
|
|
907
|
-
* @returns A promise that resolves when the value is removed.
|
|
908
|
-
*/
|
|
909
|
-
removeItem(key: Key, options?: unknown): Promise<void>;
|
|
910
|
-
/**
|
|
911
|
-
* Asynchronously clears all stored values.
|
|
912
|
-
*
|
|
913
|
-
* @returns A promise that resolves when all values are cleared.
|
|
914
|
-
*/
|
|
915
|
-
clear(): Promise<void>;
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
/**
|
|
919
|
-
* Get the value type of an object
|
|
920
|
-
*
|
|
921
|
-
* @since 1.0.14
|
|
922
|
-
*
|
|
923
|
-
* @example
|
|
924
|
-
* ```ts
|
|
925
|
-
* type T = { a: number; b: string };
|
|
926
|
-
* type V = ValueOf<T>; // V is number | string
|
|
927
|
-
* ```
|
|
928
|
-
*/
|
|
929
|
-
type ValueOf<T> = T[keyof T];
|
|
930
|
-
/**
|
|
931
|
-
* Get the intersection type of two types
|
|
932
|
-
*
|
|
933
|
-
* @since 1.0.14
|
|
934
|
-
*
|
|
935
|
-
* @example
|
|
936
|
-
* ```ts
|
|
937
|
-
* type T1 = { a: number; b: string };
|
|
938
|
-
* type T2 = { a: number; c: boolean };
|
|
939
|
-
* type I = Intersection<T1, T2>; // I is { a: number }
|
|
940
|
-
* ```
|
|
941
|
-
*/
|
|
942
|
-
type Intersection<T1, T2> = {
|
|
943
|
-
[P in keyof T1 & keyof T2]: T1[P] | T2[P];
|
|
944
|
-
};
|
|
945
|
-
|
|
946
|
-
export { Executor, ExecutorError, RequestError, RequestErrorID };
|
|
947
|
-
export type { AsyncStorage, Encryptor, ExecutorContext, ExecutorPlugin, HookRuntimes, Intersection, PromiseTask, RequestAdapterConfig, RequestAdapterInterface, RequestAdapterResponse, RequestTransactionInterface, Serializer, SyncStorage, SyncTask, Task, ValueOf };
|