/** * @fileoverview Proxy configuration utilities for MRT middleware. * * This module provides functions for configuring HTTP proxy middleware, * including request/response header rewriting and proxy setup for external services. * It's designed to work with http-proxy-middleware and integrates with the SSR proxying utilities. * * @author Salesforce Commerce Cloud * @version 0.0.1 */ import type { IncomingMessage, ServerResponse, ClientRequest } from 'http'; import { rewriteProxyRequestHeaders } from './ssr-proxying.js'; import { type Options, type RequestHandler } from 'http-proxy-middleware'; /** * Parameters for applyProxyRequestHeaders function */ interface ApplyProxyRequestHeadersParams { /** The proxy request object from http-proxy-middleware */ proxyRequest: ClientRequest; /** The incoming request object */ incomingRequest: IncomingMessage; /** Whether this is a caching proxy */ caching?: boolean; /** The proxy path being used */ proxyPath: string; /** The target host to proxy to */ targetHost: string; /** The protocol to use for the target */ targetProtocol: string; /** Hostname suffixes for which x-sfdc-access-control should be forwarded */ accessControlHeaderForwardingHostnames?: string[]; /** When true, preserve the original User-Agent header in non-caching proxy requests */ preserveUserAgent?: boolean; /** @internal Test hook: override rewrite function */ rewriteRequestHeaders?: (opts: Parameters[0]) => ReturnType; } /** * Parameters for configureProxy function */ interface ConfigureProxyParams { /** The hostname where the Express app is running */ appHostname: string; /** The proxy path pattern */ proxyPath: string; /** The protocol to use for the target */ targetProtocol: string; /** The target host to proxy to */ targetHost: string; /** The protocol to use for the app (defaults to https) */ appProtocol?: string; /** Whether this is a caching proxy */ caching?: boolean; /** Hostname suffixes for which x-sfdc-access-control should be forwarded */ accessControlHeaderForwardingHostnames?: string[]; /** When true, preserve the original User-Agent header in non-caching proxy requests */ preserveUserAgent?: boolean; } /** * Optional test hook: provide a custom createProxyMiddleware implementation. * @internal */ export type CreateProxyMiddlewareFn = (config: Options) => RequestHandler; /** * Configuration object for a proxy */ export interface ProxyConfig { /** The target host URL */ host: string; /** The proxy path pattern */ path: string; } /** * Return type for configureProxy function */ export interface ProxyResult { /** The proxy middleware function */ fn: RequestHandler; /** The proxy path pattern */ path: string; } /** * Applies proxy request headers by rewriting and copying headers from the incoming request * to the proxy request using the SSR proxying utilities. * * This function handles header transformation, addition, and removal for proxy requests, * ensuring that the proxied request has the correct headers for the target service. * * @param params - Parameters for applying proxy request headers * @param params.proxyRequest - The proxy request object from http-proxy-middleware * @param params.incomingRequest - The incoming request object * @param params.caching - Whether this is a caching proxy (defaults to false) * @param params.proxyPath - The proxy path being used * @param params.targetHost - The target host to proxy to * @param params.targetProtocol - The protocol to use for the target * * @example * ```typescript * applyProxyRequestHeaders({ * proxyRequest: clientRequest, * incomingRequest: incomingMessage, * caching: false, * proxyPath: 'api', * targetHost: 'api.example.com', * targetProtocol: 'https' * }); * ``` */ export declare const applyProxyRequestHeaders: ({ proxyRequest, incomingRequest, caching, proxyPath, targetHost, targetProtocol, accessControlHeaderForwardingHostnames, preserveUserAgent, rewriteRequestHeaders: rewriteFn, }: ApplyProxyRequestHeadersParams) => void; /** * Configures a single proxy middleware with the specified parameters. * * This function creates a complete proxy configuration including request/response * header rewriting, error handling, and cookie domain rewriting. The configuration * is designed to match CloudFront behavior for consistency between local development * and production environments. * * @param params - Configuration parameters for the proxy * @param params.appHostname - The hostname where the Express app is running * @param params.proxyPath - The proxy path pattern * @param params.targetProtocol - The protocol to use for the target * @param params.targetHost - The target host to proxy to * @param params.appProtocol - The protocol to use for the app (defaults to 'https') * @param params.caching - Whether this is a caching proxy * @returns Proxy result containing the middleware function and path * * @example * ```typescript * const proxy = configureProxy({ * appHostname: 'localhost:3000', * proxyPath: 'api', * targetProtocol: 'https', * targetHost: 'api.example.com', * appProtocol: 'https', * caching: false * }); * * app.use(`/mobify/proxy/${proxy.path}`, proxy.fn); * ``` */ export declare const configureProxy: ({ appHostname, proxyPath, targetProtocol, targetHost, appProtocol, caching, accessControlHeaderForwardingHostnames, preserveUserAgent, }: ConfigureProxyParams, createProxyFn?: CreateProxyMiddlewareFn) => ProxyResult; /** * Configures multiple proxy middlewares from an array of proxy configurations. * * This function processes an array of proxy configurations and creates corresponding * proxy middleware functions for each one. It automatically determines the target * protocol from the host URL and creates non-caching proxies by default. * * @param proxyConfigs - Array of proxy configurations * @param appHostname - The hostname where the Express app is running * @param appProtocol - The protocol to use for the app (defaults to 'https') * @returns Array of proxy results containing middleware functions and paths * * @example * ```typescript * const proxyConfigs = [ * { host: 'https://api.example.com', path: 'api' }, * { host: 'http://internal.service.com', path: 'internal' } * ]; * * const proxies = configureProxying(proxyConfigs, 'localhost:3000', 'https'); * * proxies.forEach(({ fn, path }) => { * app.use(`/mobify/proxy/${path}`, fn); * }); * ``` */ export declare const configureProxying: (proxyConfigs: ProxyConfig[], appHostname: string, appProtocol?: string, createProxyFn?: CreateProxyMiddlewareFn, accessControlHeaderForwardingHostnames?: string[], preserveUserAgent?: boolean) => ProxyResult[]; export {};