import type { FetchPlugin } from '../../engine/types.ts'; import type { CookieConfig } from './types.ts'; import { CookieJar } from './jar.ts'; /** * Return shape of `cookiePlugin()` — a `FetchPlugin` augmented with jar access, * adapter lifecycle methods, and a graceful shutdown `flush()`. */ export interface CookiePlugin extends FetchPlugin { /** The underlying cookie jar. Users may call `jar.set`, `jar.get`, etc. directly. */ readonly jar: CookieJar; /** * Load persisted cookies from the adapter (if configured). Safe to call * multiple times; errors from the adapter are swallowed. */ init(): Promise; /** * Force any pending coalesced persistence to commit and perform one final * save. Call this on graceful shutdown (process exit, logout, etc.). * * Resolves after the save completes; rejects if the adapter throws. A * no-op when no adapter is configured. */ flush(): Promise; } /** * Cookie plugin for FetchEngine. * * Transparently manages a cookie jar across requests: * - `beforeRequest`: injects matching cookies as the `Cookie` header. * - `afterRequest`: captures `Set-Cookie` response headers into the jar. * * Works in both browser (alongside native cookie jar) and Node.js * (where the native fetch has no cookie jar). * * Persistence is coalesced via `queueMicrotask`: any burst of jar mutations * in the same tick produces exactly one `adapter.save()` call. For graceful * shutdown, call `plugin.flush()`. * * For horizontal scaling, provide an `adapter` (e.g., Redis) and set * `syncOnRequest: true` to reload the jar from the adapter before each request. * * @example * const cookies = cookiePlugin(); * const api = new FetchEngine({ baseUrl: '...', plugins: [cookies] }); * * // Access jar directly for manual management * cookies.jar.clear(); * cookies.jar.clearSession(); * * // Graceful shutdown * await cookies.flush(); */ export declare function cookiePlugin(config?: CookieConfig): CookiePlugin;