/** * Shared middleware for openapi-fetch clients. * * Provides reusable authentication and logging middleware that can be * used across all API clients (OCAPI, SLAS, SCAPI, etc.). * * @module clients/middleware */ import type { Middleware } from 'openapi-fetch'; import type { AuthStrategy } from '../auth/types.js'; /** * Configuration for extra parameters middleware. */ export interface ExtraParamsConfig { /** Extra query parameters to add to the URL */ query?: Record; /** Extra body fields to merge into JSON request bodies */ body?: Record; /** Extra HTTP headers to add to all requests */ headers?: Record; } /** * Creates authentication middleware for openapi-fetch. * * This middleware intercepts requests and adds OAuth authentication headers * using the provided AuthStrategy. It also handles 401 responses by invalidating * the token and retrying the request once with a fresh token. * * @param auth - The authentication strategy to use * @returns Middleware that adds auth headers to requests and retries on 401 */ export declare function createAuthMiddleware(auth: AuthStrategy): Middleware; /** * Configuration for rate limiting middleware. */ export interface RateLimitMiddlewareConfig { /** * Maximum number of retry attempts when a rate limit response is received. * Defaults to 3. */ maxRetries?: number; /** * Base delay in milliseconds used for exponential backoff when no Retry-After * header is present. Defaults to 1000ms. */ baseDelayMs?: number; /** * Maximum delay in milliseconds between retries. Defaults to 30000ms. */ maxDelayMs?: number; /** * HTTP status codes that should trigger rate limit handling. * Defaults to [429]. 503 is often used for overload, but is not included * by default to avoid surprising retries for maintenance windows. */ statusCodes?: number[]; /** * Optional log prefix (e.g., 'MRT') used in log messages. */ prefix?: string; /** * Optional fetch implementation used for retries when the middleware context * does not provide a re-dispatch helper. */ fetch?: (request: Request) => Promise; } /** * Creates rate limiting middleware for openapi-fetch clients. * * This middleware inspects responses for rate-limit status codes (by default * 429 Too Many Requests), uses the Retry-After header when present to * determine a delay, and retries the request up to a configurable limit. * * The middleware is generic and can be used by MRT and other clients. It does * not currently read CLI configuration directly; callers should pass * configuration via the factory function. */ export declare function createRateLimitMiddleware(config?: RateLimitMiddlewareConfig): Middleware; /** * Configuration for logging middleware. */ export interface LoggingMiddlewareConfig { /** * Prefix for log messages (e.g., 'OCAPI', 'SLAS', 'MRT'). */ prefix?: string; /** * Body keys to mask in logs (replaced with '...' placeholder). * Useful for large payloads like base64-encoded file data. * @example ['data', 'password', 'secret'] */ maskBodyKeys?: string[]; } /** * Creates logging middleware for openapi-fetch clients. * * Logs request/response details at debug and trace levels. * * @param config - Logging configuration or prefix string for backwards compatibility * @returns Middleware that logs requests and responses * * @example * // Simple usage with just a prefix * client.use(createLoggingMiddleware('OCAPI')); * * @example * // With body masking for large payloads * client.use(createLoggingMiddleware({ * prefix: 'MRT', * maskBodyKeys: ['data'] // Masks base64-encoded bundle data * })); */ export declare function createLoggingMiddleware(config?: string | LoggingMiddlewareConfig): Middleware; /** * Creates middleware that adds extra query parameters and/or body fields to requests. * * This is useful for internal/power-user scenarios where you need to pass * parameters that aren't in the typed OpenAPI schema. * * @param config - Configuration with extra query and/or body params * @returns Middleware that adds extra params to requests * * @example * ```typescript * const client = createOdsClient(config, auth); * client.use(createExtraParamsMiddleware({ * query: { debug: 'true', internal_flag: '1' }, * body: { _internal: { trace: true } } * })); * ``` */ /** * Safety middleware using SafetyGuard. */ import type { SafetyGuard } from '../safety/safety-guard.js'; /** * Creates safety middleware that evaluates HTTP requests against a SafetyGuard. * * This middleware intercepts HTTP requests BEFORE they are sent. It evaluates * each request against the guard's rules and level, throwing: * - {@link SafetyBlockedError} for blocked operations * - {@link SafetyConfirmationRequired} for operations needing confirmation * * Callers that want confirmation support should wrap their SDK calls with * {@link withSafetyConfirmation}. Otherwise, both error types propagate as errors. * * @param guard - The SafetyGuard instance * @returns Middleware that evaluates operations against safety rules * * @example * ```typescript * const guard = new SafetyGuard({ level: 'NO_DELETE' }); * const client = createOdsClient(config, auth); * client.use(createSafetyMiddleware(guard)); * ``` */ export declare function createSafetyMiddleware(guard: SafetyGuard): Middleware; /** * Configuration for User-Agent middleware. */ export interface UserAgentConfig { /** * The User-Agent string to set on requests. */ userAgent: string; } /** * Creates middleware that sets the User-Agent header on requests. * * Sets both the standard `User-Agent` header and a custom `sfdc_user_agent` header * with the same value. * * @param config - Configuration with the User-Agent string * @returns Middleware that sets the User-Agent headers * * @example * ```typescript * const client = createOcapiClient(config, auth); * client.use(createUserAgentMiddleware({ userAgent: 'b2c-cli/0.1.0' })); * ``` */ export declare function createUserAgentMiddleware(config: UserAgentConfig): Middleware; export declare function createExtraParamsMiddleware(config: ExtraParamsConfig): Middleware;