/** * Engine module for FetchEngine. * * Contains the core engine class, event definitions, and internal types. */ import { ObserverEngine } from '@logosdx/observer'; import { HookEngine } from '@logosdx/hooks'; import type { EventMap, EventData as EventsEventData, DedupeEventData as EventsDedupeEventData, CacheEventData as EventsCacheEventData, RateLimitEventData as EventsRateLimitEventData, RetryEventData as EventsRetryEventData, StateEventData as EventsStateEventData, PropertyEventData as EventsPropertyEventData, OptionsEventData as EventsOptionsEventData } from './events.ts'; import type { FetchEngineCore, FetchLifecycle, FetchPlugin } from './types.ts'; import { FetchState } from '../state/index.ts'; import { ConfigStore } from '../options/index.ts'; import { HeadersManager } from '../properties/headers.ts'; import { ParamsManager } from '../properties/params.ts'; import { PropertyStore } from '../properties/store.ts'; import type { EngineConfig, EngineType, EngineRequestConfig, EngineLifecycle, ValidateConfig, CallConfig, DetermineTypeFn as OptionsDetermineTypeFn, InstanceHeaders as OptionsInstanceHeaders, InstanceParams as OptionsInstanceParams, InstanceState as OptionsInstanceState } from '../options/types.ts'; import type { HttpMethods, DictAndT } from '../types.ts'; import { FetchPromise } from './fetch-promise.ts'; import type { FetchStreamPromise, ResponseDirective } from './fetch-promise.ts'; export * from './events.ts'; export * from './types.ts'; export { FetchPromise } from './fetch-promise.ts'; export type { FetchStreamPromise, ResponseDirective } from './fetch-promise.ts'; export type { CallConfig } from '../options/types.ts'; /** * Response headers type for type inference. */ export interface InstanceResponseHeaders extends Record { } /** * Creates a wrapper around `fetch` with configurable defaults, retry logic, * request deduplication, caching, and rate limiting. * * Provides resilient HTTP client for production applications that need * reliable API communication with comprehensive error handling. * * @template H - Type of request headers * @template P - Type of request params * @template S - Type of instance state * @template RH - Type of response headers * * @example * ```typescript * // Basic setup with error handling * const api = new FetchEngine({ * baseUrl: 'https://api.example.com', * defaultType: 'json', * headers: { 'Authorization': 'Bearer token' } * }); * * const [user, err] = await attempt(() => api.get('/users/123')); * if (err) { * console.error('Failed to fetch user:', err); * return; * } * ``` * * @example * ```typescript * // Advanced setup with plugins * const api = new FetchEngine({ * baseUrl: 'https://api.example.com', * plugins: [ * retryPlugin({ maxAttempts: 3, baseDelay: 1000 }), * cachePlugin({ ttl: 60000 }), * dedupePlugin(true) * ] * }); * ``` */ export declare class FetchEngine extends ObserverEngine> implements FetchEngineCore { /** * Symbol to use the default value or configuration. * * When returned from `determineType`, uses built-in content-type detection. */ static useDefault: symbol; /** * State store for managing instance state. */ readonly state: FetchState; /** * Options store for accessing all configuration. */ readonly config: ConfigStore; /** * Headers manager for adding/removing/resolving headers. */ readonly headers: HeadersManager; /** * Params manager for adding/removing/resolving URL parameters. */ readonly params: ParamsManager

; /** * Hook engine for the request lifecycle pipeline. * * Register hooks to intercept, modify, or short-circuit requests. * Plugins install their hooks here at negative priorities so user * hooks at priority 0 run after built-in policies. */ readonly hooks: HookEngine>; /** * Create a new FetchEngine instance. * * @param opts - Configuration options */ constructor(opts: EngineConfig); /** * Install a plugin at runtime. * * The plugin's `install()` method is called with this engine instance. * Returns an unsubscribe function that removes the plugin's hooks. * * @param plugin - Plugin to install * @returns Cleanup function to uninstall the plugin */ use(plugin: FetchPlugin): () => void; /** * Property store for headers (FetchEngineCore compliance). */ get headerStore(): PropertyStore>; /** * Property store for params (FetchEngineCore compliance). */ get paramStore(): PropertyStore>; /** * Makes a GET request to retrieve data. */ get(path: string, options?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes a POST request to create a new resource. */ post(path: string, payload?: Data, options?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes a PUT request to replace a resource. */ put(path: string, payload?: Data, options?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes a PATCH request to partially update a resource. */ patch(path: string, payload?: Data, options?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes a DELETE request to remove a resource. */ delete(path: string, payload?: Data, options?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes an HTTP OPTIONS request to check server capabilities. */ options(path: string, opts?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes an HTTP HEAD request to retrieve headers only. */ head(path: string, opts?: CallConfig): FetchPromise, DictAndT

, ResHdr>; /** * Makes an HTTP request with the specified method. */ request(method: HttpMethods, path: string, options?: CallConfig & { payload?: Data; }): FetchPromise, DictAndT

, ResHdr>; /** * Clear all cached responses. */ clearCache(): void; /** * Clear a specific cache entry. */ clearCacheKey(key: string): void; /** * Delete a specific cache entry. */ deleteCache(key: string): Promise; /** * Invalidate cache entries matching a predicate. */ invalidateCache(predicate: (key: string) => boolean): Promise; /** * Invalidate cache entries by path pattern. */ invalidatePath(pattern: string | RegExp | ((key: string) => boolean)): Promise; /** * Get cache statistics. */ cacheStats(): any; /** * Destroy the FetchEngine instance. * * Aborts all pending requests and cleans up resources. * After calling destroy(), the instance cannot be used. */ destroy(): void; /** * Check if the engine has been destroyed. */ isDestroyed(): boolean; } /** * Namespace for FetchEngine types. */ export declare namespace FetchEngine { interface InstanceHeaders extends OptionsInstanceHeaders { } interface InstanceParams extends OptionsInstanceParams { } interface InstanceState extends OptionsInstanceState { } interface InstanceResponseHeaders extends Record { } /** Response body type (json, text, blob, etc.) */ type Type = EngineType; /** Full configuration options for FetchEngine */ type Config = EngineConfig; /** Request options passed to callbacks */ type RequestOpts = EngineRequestConfig; /** Function type for determining response body type */ type DetermineTypeFn = OptionsDetermineTypeFn; /** Per-request configuration options */ type CallConfig = import('../options/types.ts').CallConfig; /** Lifecycle hooks for requests */ type Lifecycle = EngineLifecycle; /** Validation configuration */ type Validate = ValidateConfig; /** Headers type that combines a custom type with string dict */ type Headers = DictAndT; /** Params type that combines a custom type with string dict */ type Params = DictAndT; /** Response headers type */ type ResponseHeaders = DictAndT; /** Header key names */ type HeaderKeys = keyof Headers; /** Event data for FetchEngine events */ type EventData = EventsEventData; /** Event data for deduplication events */ type DedupeEventData = EventsDedupeEventData; /** Event data for cache events */ type CacheEventData = EventsCacheEventData; /** Event data for rate limit events */ type RateLimitEventData = EventsRateLimitEventData; /** Event data for retry events */ type RetryEventData = EventsRetryEventData; /** Event data for state mutation events */ type StateEventData = EventsStateEventData; /** Event data for property (header/param) events */ type PropertyEventData = EventsPropertyEventData; /** Event data for options change events */ type OptionsEventData = EventsOptionsEventData; /** Event map for ObserverEngine */ type EventMap = import('./events.ts').EventMap; /** Promise that can be aborted and carries a response directive */ type Promise = FetchPromise; type StreamPromise = FetchStreamPromise, DictAndT

, DictAndT>; type Directive = ResponseDirective; }