@qlover/fe-corekit 1.2.1 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -551,7 +551,7 @@ declare abstract class Executor<ExecutorConfig = unknown> {
551
551
  *
552
552
  * @since 1.0.14
553
553
  */
554
- type RequestAdapterConfig<RequestData = unknown> = {
554
+ interface RequestAdapterConfig<RequestData = unknown> {
555
555
  /**
556
556
  * Request URL path
557
557
  * Will be combined with baseURL if provided
@@ -637,7 +637,7 @@ type RequestAdapterConfig<RequestData = unknown> = {
637
637
  */
638
638
  requestId?: string;
639
639
  [key: string]: any;
640
- };
640
+ }
641
641
  /**
642
642
  * Request adapter response
643
643
  *
@@ -647,7 +647,7 @@ type RequestAdapterConfig<RequestData = unknown> = {
647
647
  * @typeParam Req - The type of the request data.
648
648
  * @typeParam Res - The type of the response data.
649
649
  */
650
- type RequestAdapterResponse<Req = unknown, Res = unknown> = {
650
+ interface RequestAdapterResponse<Req = unknown, Res = unknown> {
651
651
  data: Res;
652
652
  status: number;
653
653
  statusText: string;
@@ -657,7 +657,7 @@ type RequestAdapterResponse<Req = unknown, Res = unknown> = {
657
657
  config: RequestAdapterConfig<Req>;
658
658
  response: Response;
659
659
  [key: string]: unknown;
660
- };
660
+ }
661
661
  /**
662
662
  * Request adapter interface
663
663
  *
@@ -754,6 +754,25 @@ declare enum RequestErrorID {
754
754
  URL_NONE = "URL_NONE"
755
755
  }
756
756
 
757
+ /**
758
+ * Represents a transaction for a request.
759
+ *
760
+ * This interface defines a transaction that contains a request and a response.
761
+ * It can be used to track the request and response of a transaction.
762
+ *
763
+ * @since 1.2.2
764
+ */
765
+ interface RequestTransactionInterface<Request, Response> {
766
+ /**
767
+ * The request object
768
+ */
769
+ request: Request;
770
+ /**
771
+ * The response object
772
+ */
773
+ response: Response;
774
+ }
775
+
757
776
  /**
758
777
  * Generic interface for data serialization/deserialization operations
759
778
  * Provides a standard contract for implementing serialization strategies
@@ -1684,12 +1703,27 @@ declare class Logger {
1684
1703
  *
1685
1704
  * @since 1.0.14
1686
1705
  */
1687
- type RequestAdapterFetchConfig<Request = unknown> = Omit<globalThis.RequestInit, 'headers'> & RequestAdapterConfig<Request> & {
1706
+ interface RequestAdapterFetchConfig<Request = unknown> extends RequestAdapterConfig<Request>, Omit<globalThis.RequestInit, 'headers'> {
1707
+ /**
1708
+ * The fetcher function
1709
+ *
1710
+ * You can override the default fetch function
1711
+ *
1712
+ * Some environments may not have a global fetch function, or you may want to override the default fetch logic.
1713
+ *
1714
+ * @example
1715
+ * ```typescript
1716
+ * const fetchRequest = new FetchRequest({ fetcher: customFetch });
1717
+ * ```
1718
+ *
1719
+ * @example Or configure it for each request
1720
+ * ```typescript
1721
+ * const fetchRequest = new FetchRequest();
1722
+ * fetchRequest.request({ url: '/data', fetcher: customFetch });
1723
+ * ```
1724
+ */
1688
1725
  fetcher?: typeof fetch;
1689
- onStreamProgress?: (progress: number) => void;
1690
- signal?: AbortSignal;
1691
- onAbort?(config: RequestAdapterFetchConfig): void;
1692
- };
1726
+ }
1693
1727
  declare class RequestAdapterFetch implements RequestAdapterInterface<RequestAdapterFetchConfig> {
1694
1728
  readonly config: RequestAdapterFetchConfig;
1695
1729
  private executor;
@@ -1740,7 +1774,7 @@ declare class RequestAdapterFetch implements RequestAdapterInterface<RequestAdap
1740
1774
  * @param config The configuration used for the fetch request.
1741
1775
  * @returns A RequestAdapterResponse containing the processed response data.
1742
1776
  */
1743
- toAdapterResponse<Request>(data: unknown, response: Response, config: RequestAdapterFetchConfig<Request>): RequestAdapterResponse<Request>;
1777
+ toAdapterResponse<Request, Res = unknown>(data: Res, response: Response, config: RequestAdapterFetchConfig<Request>): RequestAdapterResponse<Request, Res>;
1744
1778
  /**
1745
1779
  * Extracts headers from the fetch Response object and returns them as a record.
1746
1780
  *
@@ -2044,6 +2078,46 @@ declare class FetchURLPlugin implements ExecutorPlugin {
2044
2078
  onError({ error }: ExecutorContext): RequestError;
2045
2079
  }
2046
2080
 
2081
+ /**
2082
+ * Represents a manager for handling HTTP requests.
2083
+ *
2084
+ * This interface defines a manager that contains an adapter and an executor.
2085
+ * It provides methods for adding plugins to the executor and making requests.
2086
+ *
2087
+ * Why this is an abstract class?
2088
+ *
2089
+ * - Because the Executor can be overridden at runtime, the type cannot be fixed,
2090
+ * so we need to reasonably flexibly control it.
2091
+ * - So we need to redefine the request type when implementing the current class.
2092
+ *
2093
+ * @since 1.2.2
2094
+ */
2095
+ declare abstract class RequestManager<Config extends RequestAdapterConfig> {
2096
+ protected readonly adapter: RequestAdapterInterface<Config>;
2097
+ protected readonly executor: AsyncExecutor;
2098
+ constructor(adapter: RequestAdapterInterface<Config>, executor?: AsyncExecutor);
2099
+ /**
2100
+ * Adds a plugin to the executor.
2101
+ *
2102
+ * @since 1.2.2
2103
+ *
2104
+ * @param plugin - The plugin to be used by the executor.
2105
+ * @returns The current instance of RequestManagerInterface for chaining.
2106
+ */
2107
+ usePlugin(plugin: ExecutorPlugin | ExecutorPlugin[]): this;
2108
+ /**
2109
+ * Executes a request with the given configuration.
2110
+ *
2111
+ * This method need to be overridden by the subclass, override type definition of request method.
2112
+ *
2113
+ * Of course, you can also override its logic.
2114
+ *
2115
+ * @param config - The configuration for the request.
2116
+ * @returns A promise that resolves to the response of the request.
2117
+ */
2118
+ request(config: unknown): Promise<unknown>;
2119
+ }
2120
+
2047
2121
  /**
2048
2122
  * Represents a scheduler for managing HTTP requests.
2049
2123
  *
@@ -2098,31 +2172,13 @@ declare class FetchURLPlugin implements ExecutorPlugin {
2098
2172
  *
2099
2173
  * @template Config - The configuration type extending RequestAdapterConfig.
2100
2174
  */
2101
- declare class RequestScheduler<Config extends RequestAdapterConfig> {
2102
- readonly adapter: RequestAdapterInterface<Config>;
2103
- readonly executor: AsyncExecutor;
2104
- /**
2105
- * Initializes a new instance of the RequestScheduler class.
2106
- *
2107
- * @since 1.0.14
2108
- *
2109
- * @param adapter - The request adapter interface to be used for making requests.
2110
- */
2111
- constructor(adapter: RequestAdapterInterface<Config>);
2112
- /**
2113
- * Adds a plugin to the request execution process.
2114
- *
2115
- * @since 1.0.14
2116
- *
2117
- * @param plugin - The plugin to be used by the executor.
2118
- * @returns The current instance of RequestScheduler for chaining.
2119
- */
2120
- usePlugin(plugin: ExecutorPlugin): this;
2175
+ declare class RequestScheduler<Config extends RequestAdapterConfig> extends RequestManager<Config> {
2121
2176
  /**
2122
2177
  * Executes a request with the given configuration.
2123
2178
  *
2124
2179
  * @since 1.0.14
2125
2180
  *
2181
+ * @override
2126
2182
  * @param config - The configuration for the request.
2127
2183
  * @returns A promise that resolves to the response of the request.
2128
2184
  */
@@ -2192,6 +2248,213 @@ declare class RequestScheduler<Config extends RequestAdapterConfig> {
2192
2248
  connect<Request, Response>(url: string, config?: RequestAdapterConfig<Request>): Promise<RequestAdapterResponse<Response, Request>>;
2193
2249
  }
2194
2250
 
2251
+ /**
2252
+ * Represents a transaction for managing HTTP requests.
2253
+ *
2254
+ * Now `RequestTransaction` and `RequestScheduler` have the same purpose, only different in form.
2255
+ *
2256
+ * If you want to override the return type of the request, you can use `RequestTransaction`.
2257
+ *
2258
+ * If you don't consider type, you can also use `requestScheduler`, just manually declare the return type.
2259
+ *
2260
+ * If you have the need to override the response or execution context, it is recommended to use `RequestTransaction`,
2261
+ * because it can match the type through typescript.
2262
+ *
2263
+ * The `request` method of `RequestTransaction` can do the following:
2264
+ * 1. Directly declare the return type through generics
2265
+ * 2. Declare the config type through generics
2266
+ * 3. Declare the request parameters and return values through `RequestTransactionInterface`
2267
+ *
2268
+ * Currently, it does not support passing parameters through generics, but you can use `RequestTransactionInterface`.
2269
+ *
2270
+ * @example Directly declare the return type through generics
2271
+ * ```typescript
2272
+ * const client = new RequestTransaction<CustomConfig>(new RequestAdapterFetch());
2273
+ *
2274
+ * client
2275
+ * .request<{
2276
+ * list: string[];
2277
+ * }>({ url: 'https://api.example.com/data', method: 'GET', hasCatchError: true })
2278
+ * .then((response) => {
2279
+ * console.log(response.data.list);
2280
+ * });
2281
+ * ```
2282
+ *
2283
+ * @example Declare the config type through generics
2284
+ * ```typescript
2285
+ * const client = new RequestTransaction<
2286
+ * RequestAdapterConfig & { hasCatchError?: boolean }
2287
+ * >(new RequestAdapterFetch());
2288
+ *
2289
+ * client.request({
2290
+ * url: 'https://api.example.com/data',
2291
+ * method: 'GET',
2292
+ * hasCatchError: true,
2293
+ * data: { name: 'John Doe' }
2294
+ * });
2295
+ * ```
2296
+ *
2297
+ * @example You can also extend the config type
2298
+ *
2299
+ * ```typescript
2300
+ * interface CustomConfig extends RequestAdapterConfig {
2301
+ * hasCatchError?: boolean;
2302
+ * }
2303
+ *
2304
+ * const client = new RequestTransaction<CustomConfig>(new RequestAdapterFetch());
2305
+ *
2306
+ * client.request({
2307
+ * url: 'https://api.example.com/data',
2308
+ * method: 'GET',
2309
+ * hasCatchError: true,
2310
+ * data: { name: 'John Doe' }
2311
+ * });
2312
+ * ```
2313
+ *
2314
+ * @example Directly declare the request parameters and return values through `RequestTransactionInterface`
2315
+ * ```typescript
2316
+ * interface CustomConfig extends RequestAdapterConfig {
2317
+ * hasCatchError?: boolean;
2318
+ * }
2319
+ * interface CustomResponse<T = unknown>
2320
+ * extends RequestAdapterResponse<unknown, T> {
2321
+ * catchError?: unknown;
2322
+ * }
2323
+ *
2324
+ * const client = new RequestTransaction<CustomConfig>(new RequestAdapterFetch());
2325
+ *
2326
+ * client
2327
+ * .request<
2328
+ * RequestTransactionInterface<
2329
+ * CustomConfig,
2330
+ * CustomResponse<{ list?: string[] }>
2331
+ * >
2332
+ * >({ url: 'https://api.example.com/data', method: 'GET', hasCatchError: true })
2333
+ * .then((response) => {
2334
+ * console.log(response.catchError);
2335
+ * // => (property) CustomResponse<T = unknown>.catchError?: unknown
2336
+ * console.log(response.data.list);
2337
+ * // => (property) list?: string[] | undefined
2338
+ * });
2339
+ * ```
2340
+ *
2341
+ * @example Finally, you can also use `RequestTransaction` to create a complete api client
2342
+ * ```typescript
2343
+ * // catch plugin
2344
+ * interface CatchPluginConfig {
2345
+ * hasCatchError?: boolean;
2346
+ * }
2347
+ * interface CatchPluginResponseData {
2348
+ * catchError?: unknown;
2349
+ * }
2350
+ *
2351
+ * // The main api client configuration
2352
+ * // You can inherit multiple config types from other plugins
2353
+ * interface ApiClientConfig extends RequestAdapterConfig, CatchPluginConfig {}
2354
+ *
2355
+ * // The main api client response type
2356
+ * // You can inherit multiple response types from other plugins
2357
+ * interface ApiClientResponse<T = unknown>
2358
+ * extends RequestAdapterResponse<unknown, T>,
2359
+ * CatchPluginResponseData {}
2360
+ *
2361
+ * // Independently declare a specific method Transaction
2362
+ * interface ApiTestTransaction
2363
+ * extends RequestTransactionInterface<
2364
+ * ApiClientConfig,
2365
+ * ApiClientResponse<{ list: string[] }>
2366
+ * > {}
2367
+ * class ApiClient extends RequestTransaction<ApiClientConfig> {
2368
+ * // If you don't require displaying the return type of the method
2369
+ * // Automatically deduce: Promise<ApiClientResponse<{ list: string[] }>>
2370
+ * test() {
2371
+ * return this.request<ApiTestTransaction>({
2372
+ * url: 'https://api.example.com/data',
2373
+ * method: 'GET',
2374
+ * hasCatchError: true
2375
+ * });
2376
+ * }
2377
+ *
2378
+ * // Displayly declare
2379
+ * test2(data: ApiTestTransaction['request']): Promise<ApiTestTransaction['response']> {
2380
+ * return this.request({
2381
+ * url: 'https://api.example.com/data',
2382
+ * method: 'GET',
2383
+ * hasCatchError: true,
2384
+ * data
2385
+ * });
2386
+ * }
2387
+ * }
2388
+ *
2389
+ * // Call the test method
2390
+ *
2391
+ * const req = new ApiClient(new RequestAdapterFetch());
2392
+ *
2393
+ * req.test().then((response) => {
2394
+ * console.log(response.catchError);
2395
+ * // => (property) CustomResponse<T = unknown>.catchError?: unknown
2396
+ * console.log(response.data.list);
2397
+ * // => (property) list?: string[] | undefined
2398
+ * });
2399
+ * ```
2400
+ *
2401
+ * If you are not satisfied with `RequestTransaction`, you can completely rewrite your own `RequestTransaction`.
2402
+ *
2403
+ * Only need to inherit `RequestManager` and override the `request` method.
2404
+ *
2405
+ * Finally, if you don't need typescript support, then it's basically the same as `RequestScheduler`,
2406
+ * except that some of the shortcuts have different parameters.
2407
+ *
2408
+ * @since 1.2.2
2409
+ */
2410
+ declare class RequestTransaction<Config extends RequestAdapterConfig<unknown>> extends RequestManager<Config> {
2411
+ /**
2412
+ * Makes an HTTP request with flexible type definitions
2413
+ * @template Transaction - Can be either the direct response data type or a RequestTransactionInterface
2414
+ * @param config - Request configuration object
2415
+ * @returns Promise of response data
2416
+ */
2417
+ request<Transaction = unknown>(config: Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request'] : Config): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2418
+ /**
2419
+ * Sends a GET request
2420
+ * @template Transaction - Response data type or RequestTransactionInterface
2421
+ * @param {string} url - The URL to send the request to
2422
+ * @param {Omit<Config, 'url' | 'method'>} [config] - Additional configuration options
2423
+ */
2424
+ get<Transaction = unknown>(url: string, config?: Omit<Config, 'url' | 'method'>): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2425
+ /**
2426
+ * Sends a POST request
2427
+ * @template Transaction - Response data type or RequestTransactionInterface
2428
+ * @param {string} url - The URL to send the request to
2429
+ * @param {Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data']} [data] - The data to be sent in the request body
2430
+ * @param {Omit<Config, 'url' | 'method' | 'data'>} [config] - Additional configuration options
2431
+ */
2432
+ post<Transaction = unknown>(url: string, data?: Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data'], config?: Omit<Config, 'url' | 'method' | 'data'>): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2433
+ /**
2434
+ * Sends a PUT request
2435
+ * @template Transaction - Response data type or RequestTransactionInterface
2436
+ * @param {string} url - The URL to send the request to
2437
+ * @param {Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data']} [data] - The data to be sent in the request body
2438
+ * @param {Omit<Config, 'url' | 'method' | 'data'>} [config] - Additional configuration options
2439
+ */
2440
+ put<Transaction = unknown>(url: string, data?: Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data'], config?: Omit<Config, 'url' | 'method' | 'data'>): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2441
+ /**
2442
+ * Sends a DELETE request
2443
+ * @template Transaction - Response data type or RequestTransactionInterface
2444
+ * @param {string} url - The URL to send the request to
2445
+ * @param {Omit<Config, 'url' | 'method'>} [config] - Additional configuration options
2446
+ */
2447
+ delete<Transaction = unknown>(url: string, config?: Omit<Config, 'url' | 'method'>): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2448
+ /**
2449
+ * Sends a PATCH request
2450
+ * @template Transaction - Response data type or RequestTransactionInterface
2451
+ * @param {string} url - The URL to send the request to
2452
+ * @param {Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data']} [data] - The data to be sent in the request body
2453
+ * @param {Omit<Config, 'url' | 'method' | 'data'>} [config] - Additional configuration options
2454
+ */
2455
+ patch<Transaction = unknown>(url: string, data?: Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['request']['data'] : Config['data'], config?: Omit<Config, 'url' | 'method' | 'data'>): Promise<Transaction extends RequestTransactionInterface<Config, unknown> ? Transaction['response'] : RequestAdapterResponse<unknown, Transaction>>;
2456
+ }
2457
+
2195
2458
  /**
2196
2459
  * Enhanced JSON serialization implementation that combines standard JSON API with additional features
2197
2460
  *
@@ -2570,5 +2833,5 @@ declare class JSONStorage implements SyncStorage<string> {
2570
2833
  clear(): void;
2571
2834
  }
2572
2835
 
2573
- export { AsyncExecutor, Base64Serializer, Executor, ExecutorError, FetchAbortPlugin, FetchURLPlugin, JSONSerializer, JSONStorage, LEVELS, Logger, RequestAdapterAxios, RequestAdapterFetch, RequestError, RequestErrorID, RequestScheduler, RetryPlugin, SyncExecutor };
2574
- export type { AsyncStorage, Encryptor, ExecOptions, ExecutorContext, ExecutorPlugin, HookRuntimes, Intersection, LogArgument, LogLevel, PromiseTask, RequestAdapterConfig, RequestAdapterFetchConfig, RequestAdapterInterface, RequestAdapterResponse, RetryOptions, Serializer, SyncStorage, SyncTask, Task, ValueOf };
2836
+ export { AsyncExecutor, Base64Serializer, Executor, ExecutorError, FetchAbortPlugin, FetchURLPlugin, JSONSerializer, JSONStorage, LEVELS, Logger, RequestAdapterAxios, RequestAdapterFetch, RequestError, RequestErrorID, RequestManager, RequestScheduler, RequestTransaction, RetryPlugin, SyncExecutor };
2837
+ export type { AsyncStorage, Encryptor, ExecOptions, ExecutorContext, ExecutorPlugin, HookRuntimes, Intersection, LogArgument, LogLevel, PromiseTask, RequestAdapterConfig, RequestAdapterFetchConfig, RequestAdapterInterface, RequestAdapterResponse, RequestTransactionInterface, RetryOptions, Serializer, SyncStorage, SyncTask, Task, ValueOf };