import type { ClientMetadataProvider } from './adapters/ClientMetadataProvider'; import type { Fetcher } from './adapters/Fetcher'; import type { HtmlRewriterAdapter } from './adapters/HtmlRewriterAdapter'; import type { IdentityProvider } from './adapters/IdentityProvider'; import type { ResourceProvider } from './adapters/ResourceProvider'; import type { MOSProxyLogger } from './logger'; import { MOSProxy, type MOSProxyHtmlPipelineErrorHandler } from './MOSProxy'; import type { MOSConfigInput, MosAuthenticatedApiRoute } from './types'; /** * Fluent builder for `MOSProxy`. Provide configuration and platform adapters, then `build()`. * * Defaults: * - custom endpoint routing: enabled * - link rewriting: enabled * - surface decisions: enabled * - HTML transformation: enabled (auto-skipped per request for non-HTML responses) * - origin/API fetcher: `globalThis.fetch` (override on runtimes that need a backend binding, e.g. Fastly) * * @example * ```ts * const proxy = new MOSProxyBuilder() * .withConfig({ * originUrl: 'https://news.example.com', * surfaceSlug: 'web', * mosHost: 'https://api.monetizationos.com', * mosSecretKey: process.env.MONETIZATION_OS_SECRET_KEY!, * mosEndpointsPrefix: '/mos-endpoints/', * anonymousSessionCookieName: 'anon-session-id', * authenticatedUserJwtCookieName: '__session', * injectScriptUrl: 'https://assets.monetizationos.com/web-components-latest.js', * surfaceDecisionsIgnorePaths: '', * // Optional: forward specific cookies to surface decisions * surfaceDecisionsCookies: '^__session$, ^theme$', * originRequestHeaders: { 'X-Api-Key': process.env.ORIGIN_API_KEY! }, * createAnonymousIdentifierFallback: true, * }) * .withHtmlRewriter(myHtmlRewriterAdapter) * .build() * * export default { fetch: (request: Request) => proxy.handle(request) } * ``` */ export declare class MOSProxyBuilder { private _config; private _originFetcher; private _apiFetcher; private _htmlRewriter; private _clientMetadataProvider; private _identityProvider; private _resourceProvider; private _logger; private _onHtmlPipelineError; private _customEndpoints; private _linkRewriting; private _surfaceDecisions; private _htmlTransformation; private _additionalAuthenticatedApiRoutes; withConfig(config: MOSConfigInput): this; withOriginFetcher(fetcher: Fetcher): this; withApiFetcher(fetcher: Fetcher): this; withHtmlRewriter(rewriter: HtmlRewriterAdapter): this; withClientMetadata(provider: ClientMetadataProvider): this; /** * Override identity provision for the surface-decisions API. Provide `resolve` to control the * identity payload sent to the API (e.g. resolved from a request header instead of cookies), * and/or `persist` to control how identity is recorded back on the response (e.g. suppress the * default anonymous-session cookie or write it elsewhere). Either method is optional; omitted * methods fall back to the built-in defaults. */ withIdentityProvider(provider: IdentityProvider): this; /** * Override the resource object sent to the surface-decisions API per request. `build` receives * only the `Request`; the proxy derives defaults (`{ id: pathname, meta: pageMetadata }`) and * shallow-merges your returned record over them. Return only the keys you want to add or * override (e.g. a content tier or canonical id) — `id`/`meta` are preserved unless you set them. */ withResourceProvider(provider: ResourceProvider): this; withLogger(logger: MOSProxyLogger): this; /** * Register a callback to handle HTML pipeline errors. The proxy fails open by default and * returns the last safe (unconsumed) response — provide this if you want to shape that * response yourself (e.g. render a custom error page, return a 503, or re-throw so your * platform's error middleware handles it). If the callback throws or returns a non-Response * value, the proxy logs a warning and falls back to the last safe response. */ withHtmlPipelineErrorHandler(handler: MOSProxyHtmlPipelineErrorHandler): this; withoutCustomEndpoints(): this; withoutLinkRewriting(): this; withoutSurfaceDecisions(): this; /** * Disable the HTML transformation pipeline entirely (stages 3–6). * * Use for API-only proxies: neither an HTML rewriter adapter nor an API fetcher is required * if custom endpoints and surface decisions are both disabled alongside this. */ withoutHtmlTransformation(): this; /** * Add an additional route which, when matched by an incoming request, will be forwarded to the MonetizationOS API * with authentication handled by the configured identity provider. * This is in addition to the default `/mos-api/offer-redemptions` route. */ withMosAuthenticatedApiRoutes(...routes: MosAuthenticatedApiRoute[]): this; build(): MOSProxy; }