/** * Auto-refresh transport middleware. * * Integrates `AutoRefreshCoordinator` at the transport boundary so that ALL * typed operator/peer SDK calls benefit from automatic token refresh, not only * `auth.current()`. * * Responsibilities: * 1. Pre-flight: call `coordinator.ensureFreshToken()` before dispatching the * request. Updates `ctx.headers.Authorization` with the fresh token so * consumer middleware (which runs after this one) sees the current token. * 2. Reactive 401: if `next()` throws a 401-shaped error, trigger a refresh * via `coordinator.refreshAndRetryOnce()` and set `ctx.response` to the * retry result so the transport can return it to the caller. * 3. Loop prevention: retry requests carry `__gv_ar_attempted: true` in their * options. The middleware recognises this flag and passes through without * additional refresh logic, preventing infinite recursion. * 4. Error passthrough: terminal auth errors from the coordinator are placed * directly onto `ctx.error` rather than thrown, so the transport's * middleware-error-wrapping instrumentation does not re-wrap them as * `kind:'unknown'`. */ import type { TransportMiddleware } from '@pellux/goodvibes-transport-core'; import type { HttpJsonTransport } from '@pellux/goodvibes-transport-http/http-core'; import type { GoodVibesTokenStore } from './types.js'; import type { AutoRefreshCoordinator } from './auto-refresh.js'; /** * Create a transport middleware that integrates the `AutoRefreshCoordinator` * into every HTTP request. * * @param coordinator - The auto-refresh coordinator managing token lifecycle. * @param transport - The HTTP JSON transport used to re-issue the request on * reactive 401 retry. Must be the same transport instance * whose middleware chain contains this middleware, so that * the retry benefits from other middleware (e.g. logging, * tracing) except for another auto-refresh cycle. * @param tokenStore - The token store from which to read the fresh token after * pre-flight refresh, so `ctx.headers.Authorization` can * be updated before consumer middleware runs. * * @example * const mw = createAutoRefreshMiddleware(coordinator, transport, tokenStore); * transport.use(mw); */ export declare function createAutoRefreshMiddleware(coordinator: AutoRefreshCoordinator, transport: Pick, tokenStore: GoodVibesTokenStore): TransportMiddleware; //# sourceMappingURL=auto-refresh-middleware.d.ts.map