@qlover/fe-corekit 2.1.0 → 2.2.0

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
@@ -1,3 +1,4 @@
1
+ import { LoggerInterface } from '@qlover/logger';
1
2
  import { AxiosRequestConfig, AxiosStatic } from 'axios';
2
3
 
3
4
  /**
@@ -1909,6 +1910,781 @@ declare class SyncExecutor<ExecutorConfig extends ExecutorConfigInterface = Exec
1909
1910
  protected run<Result, Params = unknown>(data: Params, actualTask: SyncTask<Result, Params>): Result;
1910
1911
  }
1911
1912
 
1913
+ /**
1914
+ * Configuration interface for AbortPlugin
1915
+ *
1916
+ * Defines the parameters needed to control request cancellation behavior,
1917
+ * including identifiers, abort signals, timeout settings, and callback functions
1918
+ */
1919
+ interface AbortPluginConfig {
1920
+ /**
1921
+ * Unique identifier for the abort operation
1922
+ *
1923
+ * Used to identify and manage specific abort controller instances
1924
+ * If not provided, will use `requestId` or auto-generated value
1925
+ *
1926
+ * @optional
1927
+ * @example `"user-profile-fetch"`
1928
+ */
1929
+ id?: string;
1930
+ /**
1931
+ * Request unique identifier
1932
+ *
1933
+ * Alternative to `id`, used for identifying specific requests
1934
+ * Useful when tracking requests across different systems
1935
+ *
1936
+ * @optional
1937
+ * @example `"req_123456789"`
1938
+ */
1939
+ requestId?: string;
1940
+ /**
1941
+ * Callback function triggered when operation is aborted
1942
+ *
1943
+ * Receives the abort configuration (excluding the callback itself to prevent recursion)
1944
+ * Useful for cleanup operations or user notifications
1945
+ *
1946
+ * @optional
1947
+ * @example
1948
+ * ```typescript
1949
+ * onAborted: (config) => {
1950
+ * console.log(`Request ${config.id} was cancelled`);
1951
+ * // Perform cleanup operations
1952
+ * }
1953
+ * ```
1954
+ */
1955
+ onAborted?<T extends AbortPluginConfig>(config: T): void;
1956
+ /**
1957
+ * AbortSignal instance for request cancellation
1958
+ *
1959
+ * If not provided, the plugin will create and manage one automatically
1960
+ * Can be provided externally to integrate with existing abort mechanisms
1961
+ *
1962
+ * @optional
1963
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | AbortSignal MDN}
1964
+ */
1965
+ signal?: AbortSignal;
1966
+ /**
1967
+ * Timeout duration in milliseconds
1968
+ *
1969
+ * When set, the operation will be automatically aborted after this duration
1970
+ * Helps prevent hanging requests and manages resource cleanup
1971
+ *
1972
+ * @optional
1973
+ * @default `undefined` (no timeout)
1974
+ * @example `5000` // 5 seconds timeout
1975
+ * @example `30000` // 30 seconds timeout
1976
+ */
1977
+ abortTimeout?: number;
1978
+ }
1979
+ /**
1980
+ * Error identifier constant for abort-related errors
1981
+ *
1982
+ * Used to identify abort errors in error handling logic
1983
+ */
1984
+ declare const ABORT_ERROR_ID = "ABORT_ERROR";
1985
+ /**
1986
+ * Configuration extractor function type
1987
+ *
1988
+ * Extracts `AbortPluginConfig` from executor context parameters
1989
+ * Enables flexible configuration passing in different execution contexts
1990
+ *
1991
+ * @template T - Type of the parameters object
1992
+ * @param parameters - Executor context parameters
1993
+ * @returns Extracted abort configuration
1994
+ *
1995
+ * @example
1996
+ * ```typescript
1997
+ * const extractor: AbortConfigExtractor<MyParams> = (params) => ({
1998
+ * id: params.requestId,
1999
+ * abortTimeout: params.timeout,
2000
+ * signal: params.customSignal
2001
+ * });
2002
+ * ```
2003
+ */
2004
+ type AbortConfigExtractor<T> = (parameters: T) => AbortPluginConfig;
2005
+ /**
2006
+ * Configuration options for initializing AbortPlugin
2007
+ *
2008
+ * Defines optional settings for logger integration, default timeout,
2009
+ * and custom configuration extraction logic
2010
+ *
2011
+ * @template T - Type of the executor parameters
2012
+ */
2013
+ interface AbortPluginOptions<T> {
2014
+ /**
2015
+ * Logger instance for debugging and monitoring
2016
+ *
2017
+ * When provided, the plugin will log abort events, timeouts,
2018
+ * and cleanup operations for debugging purposes
2019
+ *
2020
+ * @optional
2021
+ * @example
2022
+ * ```typescript
2023
+ * import { Logger } from '@qlover/logger';
2024
+ * const logger = new Logger({ level: 'debug' });
2025
+ * ```
2026
+ */
2027
+ logger?: LoggerInterface;
2028
+ /**
2029
+ * Default timeout duration in milliseconds
2030
+ *
2031
+ * Applied to all requests unless overridden by individual request config
2032
+ * Helps establish consistent timeout behavior across the application
2033
+ *
2034
+ * @optional
2035
+ * @default `undefined` (no default timeout)
2036
+ * @example `10000` // 10 seconds default timeout
2037
+ */
2038
+ timeout?: number;
2039
+ /**
2040
+ * Custom configuration extractor function
2041
+ *
2042
+ * Extracts `AbortPluginConfig` from executor context parameters
2043
+ * Allows customization of how abort configuration is retrieved
2044
+ *
2045
+ * @default Direct cast of parameters to `AbortPluginConfig`
2046
+ * @optional
2047
+ * @example
2048
+ * ```typescript
2049
+ * getConfig: (params) => ({
2050
+ * id: params.requestId,
2051
+ * abortTimeout: params.customTimeout,
2052
+ * signal: params.abortSignal
2053
+ * })
2054
+ * ```
2055
+ */
2056
+ getConfig?: AbortConfigExtractor<T>;
2057
+ }
2058
+ /**
2059
+ * Custom error class for abort operations
2060
+ *
2061
+ * Extends `ExecutorError` to provide rich abort-specific error information,
2062
+ * including abort identifiers, timeout details, and user-friendly descriptions
2063
+ *
2064
+ * Core features:
2065
+ * - Abort identification: Tracks which operation was aborted via `abortId`
2066
+ * - Timeout tracking: Records timeout duration when abort is timeout-triggered
2067
+ * - Timeout detection: Provides `isTimeout()` method to distinguish timeout aborts
2068
+ * - Friendly descriptions: Generates human-readable error messages with context
2069
+ *
2070
+ * @example Basic abort error
2071
+ * ```typescript
2072
+ * const error = new AbortError(
2073
+ * 'Operation cancelled by user',
2074
+ * 'fetch-user-123'
2075
+ * );
2076
+ * console.log(error.getDescription());
2077
+ * // Output: "Operation cancelled by user (Request: fetch-user-123)"
2078
+ * ```
2079
+ *
2080
+ * @example Timeout abort error
2081
+ * ```typescript
2082
+ * const error = new AbortError(
2083
+ * 'Request timed out',
2084
+ * 'api-call-456',
2085
+ * 5000
2086
+ * );
2087
+ * if (error.isTimeout()) {
2088
+ * console.log(`Request ${error.abortId} timed out after ${error.timeout}ms`);
2089
+ * }
2090
+ * ```
2091
+ */
2092
+ declare class AbortError extends ExecutorError {
2093
+ /**
2094
+ * Unique identifier for the aborted operation
2095
+ *
2096
+ * Helps track and identify which specific operation was aborted
2097
+ * Corresponds to `id` or `requestId` from `AbortPluginConfig`
2098
+ *
2099
+ * @optional
2100
+ * @example `"fetch-user-profile-123"`
2101
+ * @example `"upload-file-456"`
2102
+ */
2103
+ readonly abortId?: string;
2104
+ /**
2105
+ * Timeout duration in milliseconds
2106
+ *
2107
+ * Only populated when abort was triggered by a timeout
2108
+ * Used to distinguish between manual aborts and timeout-based aborts
2109
+ *
2110
+ * @optional
2111
+ * @example `5000` // 5 seconds timeout
2112
+ */
2113
+ readonly timeout?: number;
2114
+ /**
2115
+ * Creates a new AbortError instance
2116
+ *
2117
+ * @param message - Human-readable error message describing why the operation was aborted
2118
+ * @param abortId - Optional identifier for the aborted operation
2119
+ * @param timeout - Optional timeout duration in milliseconds (indicates timeout-based abort)
2120
+ *
2121
+ * @example Manual abort
2122
+ * ```typescript
2123
+ * new AbortError('User cancelled the upload', 'upload-123')
2124
+ * ```
2125
+ *
2126
+ * @example Timeout abort
2127
+ * ```typescript
2128
+ * new AbortError('Request exceeded time limit', 'api-call-456', 5000)
2129
+ * ```
2130
+ */
2131
+ constructor(message: string, abortId?: string, timeout?: number);
2132
+ /**
2133
+ * Checks if the abort was triggered by a timeout
2134
+ *
2135
+ * Useful for distinguishing between manual cancellations and timeout-based aborts,
2136
+ * enabling different error handling strategies
2137
+ *
2138
+ * @returns `true` if abort was caused by timeout, `false` otherwise
2139
+ *
2140
+ * @example
2141
+ * ```typescript
2142
+ * try {
2143
+ * await fetchData();
2144
+ * } catch (error) {
2145
+ * if (error instanceof AbortError && error.isTimeout()) {
2146
+ * console.log('Request timed out, please try again');
2147
+ * } else {
2148
+ * console.log('Request was cancelled');
2149
+ * }
2150
+ * }
2151
+ * ```
2152
+ */
2153
+ isTimeout(): boolean;
2154
+ /**
2155
+ * Generates a user-friendly error description with context
2156
+ *
2157
+ * Combines the error message with additional context like request ID and timeout duration
2158
+ * Provides more informative error messages for debugging and user feedback
2159
+ *
2160
+ * @returns Formatted error description string
2161
+ *
2162
+ * @example Without context
2163
+ * ```typescript
2164
+ * const error = new AbortError('Operation aborted');
2165
+ * error.getDescription(); // "Operation aborted"
2166
+ * ```
2167
+ *
2168
+ * @example With request ID
2169
+ * ```typescript
2170
+ * const error = new AbortError('Operation aborted', 'req-123');
2171
+ * error.getDescription(); // "Operation aborted (Request: req-123)"
2172
+ * ```
2173
+ *
2174
+ * @example With timeout context
2175
+ * ```typescript
2176
+ * const error = new AbortError('Timeout', 'req-456', 5000);
2177
+ * error.getDescription(); // "Timeout (Request: req-456, Timeout: 5000ms)"
2178
+ * ```
2179
+ */
2180
+ getDescription(): string;
2181
+ }
2182
+ /**
2183
+ * Executor plugin for managing request cancellation and timeout handling
2184
+ *
2185
+ * Core concept:
2186
+ * Provides comprehensive abort control for asynchronous operations by managing
2187
+ * `AbortController` instances, timeout mechanisms, and cleanup operations.
2188
+ * Integrates seamlessly with the executor plugin system to handle operation
2189
+ * lifecycle and error management.
2190
+ *
2191
+ * Main features:
2192
+ * - Request cancellation: Manual and automatic abort control with per-request tracking
2193
+ * - Generates unique identifiers for each request
2194
+ * - Manages `AbortController` instances lifecycle
2195
+ * - Supports both programmatic and user-triggered cancellation
2196
+ * - Prevents duplicate requests by aborting previous ones with same ID
2197
+ *
2198
+ * - Timeout management: Automatic operation timeout with configurable duration
2199
+ * - Sets up timeout timers for each operation
2200
+ * - Automatically aborts operations exceeding timeout
2201
+ * - Provides timeout-specific error information
2202
+ * - Cleans up timers to prevent memory leaks
2203
+ *
2204
+ * - Signal integration: Seamless integration with AbortSignal API
2205
+ * - Creates and manages `AbortSignal` instances
2206
+ * - Supports external signal injection
2207
+ * - Provides `raceWithAbort` for signal-unaware operations
2208
+ * - Handles signal events and state changes
2209
+ *
2210
+ * - Resource cleanup: Automatic cleanup of controllers and timers
2211
+ * - Removes completed operation resources
2212
+ * - Clears timeout timers on success or error
2213
+ * - Prevents memory leaks from abandoned operations
2214
+ * - Supports batch cleanup with `abortAll()`
2215
+ *
2216
+ * - Error handling: Rich abort error information with context
2217
+ * - Distinguishes between manual and timeout aborts
2218
+ * - Provides detailed error descriptions
2219
+ * - Maintains error context across plugin lifecycle
2220
+ * - Supports custom error callbacks via `onAborted`
2221
+ *
2222
+ * @template TParameters - Type of executor context parameters, defaults to `AbortPluginConfig`
2223
+ *
2224
+ * @example Basic usage with executor
2225
+ * ```typescript
2226
+ * import { Executor } from '@/executor';
2227
+ * import { AbortPlugin } from '@/executor/plugins';
2228
+ *
2229
+ * const executor = new Executor();
2230
+ * const abortPlugin = new AbortPlugin();
2231
+ * executor.use(abortPlugin);
2232
+ *
2233
+ * // Execute with abort support
2234
+ * const result = await executor.execute({
2235
+ * id: 'fetch-users',
2236
+ * abortTimeout: 5000
2237
+ * });
2238
+ * ```
2239
+ *
2240
+ * @example Manual abort
2241
+ * ```typescript
2242
+ * const abortPlugin = new AbortPlugin();
2243
+ * executor.use(abortPlugin);
2244
+ *
2245
+ * // Start operation
2246
+ * const promise = executor.execute({ id: 'long-running-task' });
2247
+ *
2248
+ * // Cancel after 2 seconds
2249
+ * setTimeout(() => {
2250
+ * abortPlugin.abort('long-running-task');
2251
+ * }, 2000);
2252
+ * ```
2253
+ *
2254
+ * @example With custom configuration extractor
2255
+ * ```typescript
2256
+ * interface MyParams {
2257
+ * requestId: string;
2258
+ * timeout: number;
2259
+ * customSignal?: AbortSignal;
2260
+ * }
2261
+ *
2262
+ * const abortPlugin = new AbortPlugin<MyParams>({
2263
+ * logger: myLogger,
2264
+ * getConfig: (params) => ({
2265
+ * id: params.requestId,
2266
+ * abortTimeout: params.timeout,
2267
+ * signal: params.customSignal
2268
+ * })
2269
+ * });
2270
+ * ```
2271
+ *
2272
+ * @example With abort callback
2273
+ * ```typescript
2274
+ * await executor.execute({
2275
+ * id: 'upload-file',
2276
+ * abortTimeout: 30000,
2277
+ * onAborted: (config) => {
2278
+ * console.log(`Upload ${config.id} was cancelled`);
2279
+ * // Clean up temporary files
2280
+ * cleanupTempFiles();
2281
+ * }
2282
+ * });
2283
+ * ```
2284
+ *
2285
+ * @example Abort all pending operations
2286
+ * ```typescript
2287
+ * // Start multiple operations
2288
+ * executor.execute({ id: 'task-1' });
2289
+ * executor.execute({ id: 'task-2' });
2290
+ * executor.execute({ id: 'task-3' });
2291
+ *
2292
+ * // Cancel all at once
2293
+ * abortPlugin.abortAll();
2294
+ * ```
2295
+ */
2296
+ declare class AbortPlugin<TParameters> implements ExecutorPlugin {
2297
+ /**
2298
+ * Plugin identifier name
2299
+ *
2300
+ * Used by the executor system to identify this plugin
2301
+ */
2302
+ readonly pluginName = "AbortPlugin";
2303
+ /**
2304
+ * Ensures only one instance of this plugin can be registered
2305
+ *
2306
+ * Prevents conflicts from multiple abort plugin instances
2307
+ */
2308
+ readonly onlyOne = true;
2309
+ /**
2310
+ * Counter for auto-generating request identifiers
2311
+ *
2312
+ * Incremented each time a new request without ID is processed
2313
+ *
2314
+ * @private
2315
+ */
2316
+ private requestCounter;
2317
+ /**
2318
+ * Map of active abort controllers indexed by request key
2319
+ *
2320
+ * Stores `AbortController` instances for ongoing operations
2321
+ * Enables abort operations by request identifier
2322
+ *
2323
+ * @protected
2324
+ */
2325
+ protected readonly controllers: Map<string, AbortController>;
2326
+ /**
2327
+ * Map of active timeout timers indexed by request key
2328
+ *
2329
+ * Stores timeout IDs for cleanup when operations complete
2330
+ * Prevents memory leaks from abandoned timers
2331
+ *
2332
+ * @protected
2333
+ */
2334
+ protected readonly timeouts: Map<string, NodeJS.Timeout>;
2335
+ /**
2336
+ * Configuration extractor function
2337
+ *
2338
+ * Extracts `AbortPluginConfig` from executor parameters
2339
+ * Enables flexible configuration in different contexts
2340
+ *
2341
+ * @protected
2342
+ */
2343
+ protected readonly getConfig: AbortConfigExtractor<TParameters>;
2344
+ /**
2345
+ * Optional logger instance for debugging
2346
+ *
2347
+ * When provided, logs abort events, timeouts, and cleanup operations
2348
+ *
2349
+ * @protected
2350
+ * @optional
2351
+ */
2352
+ protected readonly logger?: LoggerInterface;
2353
+ /**
2354
+ * Creates a new AbortPlugin instance
2355
+ *
2356
+ * @param options - Plugin configuration options
2357
+ * @param options.logger - Logger instance for debugging and monitoring
2358
+ * @param options.timeout - Default timeout duration for all requests
2359
+ * @param options.getConfig - Custom configuration extractor function
2360
+ *
2361
+ * @example With logger
2362
+ * ```typescript
2363
+ * import { Logger } from '@qlover/logger';
2364
+ *
2365
+ * const abortPlugin = new AbortPlugin({
2366
+ * logger: new Logger({ level: 'debug' })
2367
+ * });
2368
+ * ```
2369
+ *
2370
+ * @example With custom extractor
2371
+ * ```typescript
2372
+ * const abortPlugin = new AbortPlugin({
2373
+ * getConfig: (params) => ({
2374
+ * id: params.customId,
2375
+ * abortTimeout: params.maxWaitTime
2376
+ * })
2377
+ * });
2378
+ * ```
2379
+ */
2380
+ constructor(options?: AbortPluginOptions<TParameters>);
2381
+ /**
2382
+ * Checks if an error is abort-related (static utility method)
2383
+ *
2384
+ * Comprehensive check for various abort error types including custom `AbortError`,
2385
+ * native browser `AbortError`, `ExecutorError` with abort ID, `DOMException`,
2386
+ * and abort events. Can be used independently without plugin instance.
2387
+ *
2388
+ * Detection logic:
2389
+ * 1. Custom `AbortError` instance
2390
+ * 2. Native `Error` with name 'AbortError'
2391
+ * 3. `ExecutorError` with `ABORT_ERROR_ID`
2392
+ * 4. `DOMException` with name 'AbortError'
2393
+ * 5. `Event` with type 'abort'
2394
+ *
2395
+ * @param error - Error object to check
2396
+ * @returns `true` if error is abort-related, `false` otherwise
2397
+ *
2398
+ * @example Basic usage
2399
+ * ```typescript
2400
+ * try {
2401
+ * await fetch(url, { signal });
2402
+ * } catch (error) {
2403
+ * if (AbortPlugin.isAbortError(error)) {
2404
+ * console.log('Request was cancelled');
2405
+ * } else {
2406
+ * console.error('Request failed:', error);
2407
+ * }
2408
+ * }
2409
+ * ```
2410
+ *
2411
+ * @example In error handler
2412
+ * ```typescript
2413
+ * function handleError(error: unknown) {
2414
+ * if (AbortPlugin.isAbortError(error)) {
2415
+ * // User cancelled - don't show error message
2416
+ * return;
2417
+ * }
2418
+ * showErrorNotification(error);
2419
+ * }
2420
+ * ```
2421
+ */
2422
+ static isAbortError(error?: unknown): error is AbortError;
2423
+ /**
2424
+ * Generates unique request identifier from configuration
2425
+ *
2426
+ * Priority order:
2427
+ * 1. `requestId` from config
2428
+ * 2. `id` from config
2429
+ * 3. Auto-generated identifier: `{pluginName}-{counter}`
2430
+ *
2431
+ * @param config - Abort plugin configuration
2432
+ * @returns Unique request identifier string
2433
+ *
2434
+ * @protected
2435
+ * @example
2436
+ * ```typescript
2437
+ * // With requestId
2438
+ * generateRequestKey({ requestId: 'fetch-user' }) // "fetch-user"
2439
+ *
2440
+ * // With id
2441
+ * generateRequestKey({ id: 'upload-file' }) // "upload-file"
2442
+ *
2443
+ * // Auto-generated
2444
+ * generateRequestKey({}) // "AbortPlugin-1"
2445
+ * ```
2446
+ */
2447
+ protected generateRequestKey(config: AbortPluginConfig): string;
2448
+ /**
2449
+ * Cleans up resources associated with a request
2450
+ *
2451
+ * Removes abort controller and clears timeout timer for the specified request
2452
+ * Prevents memory leaks by releasing references and stopping timers
2453
+ * Only logs when resources actually exist to avoid noise
2454
+ *
2455
+ * @param config - Configuration object or request identifier string
2456
+ *
2457
+ * @example With config object
2458
+ * ```typescript
2459
+ * cleanup({ id: 'fetch-users' });
2460
+ * ```
2461
+ *
2462
+ * @example With identifier string
2463
+ * ```typescript
2464
+ * cleanup('fetch-users');
2465
+ * ```
2466
+ */
2467
+ cleanup(config: AbortPluginConfig | string): void;
2468
+ /**
2469
+ * Executor lifecycle hook: called before operation execution
2470
+ *
2471
+ * Performs the following setup operations:
2472
+ * 1. Extracts abort configuration from context parameters
2473
+ * 2. Generates unique request key
2474
+ * 3. Aborts any existing request with same key (prevents duplicates)
2475
+ * 4. Creates new `AbortController` if signal not provided
2476
+ * 5. Sets up timeout timer if `abortTimeout` configured
2477
+ * 6. Injects abort signal into configuration
2478
+ *
2479
+ * @param context - Executor context containing parameters and metadata
2480
+ *
2481
+ * @example Configuration in context
2482
+ * ```typescript
2483
+ * executor.execute({
2484
+ * id: 'fetch-data',
2485
+ * abortTimeout: 5000,
2486
+ * onAborted: (config) => console.log('Aborted:', config.id)
2487
+ * });
2488
+ * ```
2489
+ */
2490
+ onBefore(context: ExecutorContext<TParameters>): void;
2491
+ /**
2492
+ * Executor lifecycle hook: called after successful execution
2493
+ *
2494
+ * Cleans up resources (controller and timeout) for completed operation
2495
+ * Ensures no memory leaks from successful operations
2496
+ *
2497
+ * @param context - Executor context containing parameters and metadata
2498
+ *
2499
+ * @example
2500
+ * ```typescript
2501
+ * // After successful execution, resources are automatically cleaned
2502
+ * await executor.execute({ id: 'task-1' });
2503
+ * // AbortController and timeout for 'task-1' are now removed
2504
+ * ```
2505
+ */
2506
+ onSuccess({ parameters }: ExecutorContext<TParameters>): void;
2507
+ /**
2508
+ * Executor lifecycle hook: called when execution fails
2509
+ *
2510
+ * Handles abort-related errors and cleans up resources
2511
+ *
2512
+ * Error handling logic:
2513
+ * 1. Check if error is abort-related using `isAbortError()`
2514
+ * 2. If abort error: Extract reason from signal, cleanup resources, return `AbortError`
2515
+ * 3. If other error: Still cleanup resources to prevent leaks
2516
+ *
2517
+ * @param context - Executor context containing error, parameters, and metadata
2518
+ * @returns `AbortError` if error is abort-related, `void` otherwise
2519
+ *
2520
+ * @example Abort error handling
2521
+ * ```typescript
2522
+ * try {
2523
+ * await executor.execute({ id: 'task-1', abortTimeout: 100 });
2524
+ * } catch (error) {
2525
+ * if (error instanceof AbortError) {
2526
+ * console.log(error.getDescription());
2527
+ * // "The operation was aborted due to timeout (Request: task-1, Timeout: 100ms)"
2528
+ * }
2529
+ * }
2530
+ * ```
2531
+ */
2532
+ onError({ error, parameters }: ExecutorContext<TParameters>): ExecutorError | void;
2533
+ /**
2534
+ * Manually aborts a specific operation
2535
+ *
2536
+ * Triggers abort for the specified request, calls `onAborted` callback if provided,
2537
+ * and cleans up all associated resources
2538
+ *
2539
+ * @param config - Configuration object with request identifier or identifier string
2540
+ * @returns `true` if operation was aborted, `false` if no matching operation found
2541
+ *
2542
+ * @example Abort by ID
2543
+ * ```typescript
2544
+ * abortPlugin.abort('fetch-users');
2545
+ * ```
2546
+ *
2547
+ * @example Abort with config
2548
+ * ```typescript
2549
+ * abortPlugin.abort({ id: 'upload-file' });
2550
+ * ```
2551
+ *
2552
+ * @example Conditional abort
2553
+ * ```typescript
2554
+ * if (userClickedCancel) {
2555
+ * const aborted = abortPlugin.abort('long-task');
2556
+ * if (aborted) {
2557
+ * console.log('Task cancelled successfully');
2558
+ * }
2559
+ * }
2560
+ * ```
2561
+ */
2562
+ abort(config: AbortPluginConfig | string): boolean;
2563
+ /**
2564
+ * Aborts all pending operations
2565
+ *
2566
+ * Iterates through all active controllers, aborts each operation,
2567
+ * and clears all controllers and timeout timers
2568
+ *
2569
+ * Use cases:
2570
+ * - User logs out
2571
+ * - Component unmounts
2572
+ * - Application shutdown
2573
+ * - Navigation away from page
2574
+ *
2575
+ * @example Component cleanup
2576
+ * ```typescript
2577
+ * class MyComponent {
2578
+ * private abortPlugin = new AbortPlugin();
2579
+ *
2580
+ * onDestroy() {
2581
+ * // Cancel all pending requests
2582
+ * this.abortPlugin.abortAll();
2583
+ * }
2584
+ * }
2585
+ * ```
2586
+ *
2587
+ * @example User logout
2588
+ * ```typescript
2589
+ * function logout() {
2590
+ * abortPlugin.abortAll(); // Cancel all API calls
2591
+ * clearUserData();
2592
+ * redirectToLogin();
2593
+ * }
2594
+ * ```
2595
+ */
2596
+ abortAll(): void;
2597
+ /**
2598
+ * Creates a Promise that rejects when signal is aborted
2599
+ *
2600
+ * Returns both the promise and a cleanup function to remove event listener
2601
+ * Prevents memory leaks by allowing proper cleanup of abort event handlers
2602
+ *
2603
+ * Implementation details:
2604
+ * 1. Immediately rejects if signal is already aborted
2605
+ * 2. Attaches event listener for future abort events
2606
+ * 3. Returns cleanup function to remove listener
2607
+ * 4. Uses signal reason if available, otherwise creates new `AbortError`
2608
+ *
2609
+ * @param signal - AbortSignal to monitor
2610
+ * @returns Object containing promise and cleanup function
2611
+ * @returns promise - Promise that rejects on abort
2612
+ * @returns cleanup - Function to remove event listener
2613
+ *
2614
+ * @private Internal use only
2615
+ *
2616
+ * @example Internal usage
2617
+ * ```typescript
2618
+ * const { promise, cleanup } = this.createAbortPromise(signal);
2619
+ * try {
2620
+ * await Promise.race([someOperation(), promise]);
2621
+ * } finally {
2622
+ * cleanup(); // Always cleanup to prevent memory leak
2623
+ * }
2624
+ * ```
2625
+ */
2626
+ private createAbortPromise;
2627
+ /**
2628
+ * Wraps an async operation with `Promise.race` to ensure abort signal responsiveness
2629
+ *
2630
+ * Defensive mechanism for operations that don't natively check abort signals
2631
+ * Uses promise racing to interrupt operations when signal is aborted,
2632
+ * even if the underlying operation ignores the signal
2633
+ *
2634
+ * Use cases:
2635
+ * - Third-party APIs that don't support `AbortSignal`
2636
+ * - Legacy code without abort handling
2637
+ * - Gateway operations that don't check signal
2638
+ * - Any async operation needing guaranteed cancellation
2639
+ *
2640
+ * @template T - Type of the promise result
2641
+ * @param promise - Promise to wrap with abort capability
2642
+ * @param signal - AbortSignal to monitor, if not provided returns original promise
2643
+ * @returns Wrapped promise that rejects immediately when signal is aborted
2644
+ *
2645
+ * @example Basic usage
2646
+ * ```typescript
2647
+ * const controller = new AbortController();
2648
+ * const result = await abortPlugin.raceWithAbort(
2649
+ * fetch('/api/data'), // Even if fetch doesn't check signal
2650
+ * controller.signal
2651
+ * );
2652
+ * ```
2653
+ *
2654
+ * @example With third-party library
2655
+ * ```typescript
2656
+ * const result = await abortPlugin.raceWithAbort(
2657
+ * legacyApiCall(), // Library doesn't support abort
2658
+ * signal
2659
+ * );
2660
+ * ```
2661
+ *
2662
+ * @example Without signal (pass-through)
2663
+ * ```typescript
2664
+ * // When signal is not provided, returns original promise
2665
+ * const result = await abortPlugin.raceWithAbort(somePromise());
2666
+ * ```
2667
+ *
2668
+ * @example Timeout with abort
2669
+ * ```typescript
2670
+ * const controller = new AbortController();
2671
+ * setTimeout(() => controller.abort(), 5000);
2672
+ *
2673
+ * try {
2674
+ * const result = await abortPlugin.raceWithAbort(
2675
+ * longRunningOperation(),
2676
+ * controller.signal
2677
+ * );
2678
+ * } catch (error) {
2679
+ * if (AbortPlugin.isAbortError(error)) {
2680
+ * console.log('Operation timed out');
2681
+ * }
2682
+ * }
2683
+ * ```
2684
+ */
2685
+ raceWithAbort<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
2686
+ }
2687
+
1912
2688
  /**
1913
2689
  * Configuration options for the RetryPlugin
1914
2690
  *
@@ -3947,4 +4723,4 @@ type Intersection<T1, T2> = {
3947
4723
  [P in keyof T1 & keyof T2]: T1[P] | T2[P];
3948
4724
  };
3949
4725
 
3950
- export { AsyncExecutor, type AsyncStorageInterface, Base64Serializer, type Encryptor, Executor, type ExecutorConfigInterface, type ExecutorContext, ExecutorError, type ExecutorPlugin, type ExpireOptions, FetchAbortPlugin, FetchURLPlugin, type HookRuntimes, type HookType, type Intersection, JSONSerializer, type JSONSerializerOptions, KeyStorage, KeyStorageInterface, type KeyStorageOptions, ObjectStorage, type ObjectStorageOptions, type PipeArg, type PromiseTask, RequestAdapterAxios, type RequestAdapterConfig, RequestAdapterFetch, type RequestAdapterFetchConfig, type RequestAdapterInterface, type RequestAdapterResponse, RequestError, RequestErrorID, RequestManager, RequestScheduler, RequestTransaction, type RequestTransactionInterface, type RetryOptions, RetryPlugin, type SerializerIneterface, type StorageValue, SyncExecutor, SyncStorage, type SyncStorageInterface, type SyncTask, type Task, type ValueOf };
4726
+ export { ABORT_ERROR_ID, type AbortConfigExtractor, AbortError, AbortPlugin, type AbortPluginConfig, type AbortPluginOptions, AsyncExecutor, type AsyncStorageInterface, Base64Serializer, type Encryptor, Executor, type ExecutorConfigInterface, type ExecutorContext, ExecutorError, type ExecutorPlugin, type ExpireOptions, FetchAbortPlugin, FetchURLPlugin, type HookRuntimes, type HookType, type Intersection, JSONSerializer, type JSONSerializerOptions, KeyStorage, KeyStorageInterface, type KeyStorageOptions, ObjectStorage, type ObjectStorageOptions, type PipeArg, type PromiseTask, RequestAdapterAxios, type RequestAdapterConfig, RequestAdapterFetch, type RequestAdapterFetchConfig, type RequestAdapterInterface, type RequestAdapterResponse, RequestError, RequestErrorID, RequestManager, RequestScheduler, RequestTransaction, type RequestTransactionInterface, type RetryOptions, RetryPlugin, type SerializerIneterface, type StorageValue, SyncExecutor, SyncStorage, type SyncStorageInterface, type SyncTask, type Task, type ValueOf };