Alokai

createConfigSwitcherExtension

Creates a middleware extension that resolves and merges a per-request integration configuration, allowing a single middleware instance to serve multiple configurations (for example, one per domain or locale).

On each request the extension resolves a configuration id using the configured strategy, looks up the matching configuration from the provided configuration map (or factory), and deep-merges it onto the base configuration. Resolved configurations are cached in memory to avoid re-fetching them on every request. When configuration is an empty object, the extension is treated as not configured and passes the base configuration through unchanged.

Signature

createConfigSwitcherExtension: <TIntegrationConfig extends IntegrationConfig = IntegrationConfig>(
	config: ConfigSwitcherExtensionParams<TIntegrationConfig>
) => ApiClientExtension

Parameters

NameRequiredTypeDescription
configRequiredConfigSwitcherExtensionParams<TIntegrationConfig>The config switcher parameters.

Returns

An ApiClientExtension that resolves and merges the per-request configuration in its beforeCreate hook.

Referenced Types

Examples

import { createConfigSwitcherExtension } from "@alokai/connect";

const extension = createConfigSwitcherExtension({
  configuration: {
    us: { apiUrl: "https://us.example.com/api" },
    de: { apiUrl: "https://de.example.com/api" },
  },
  defaultHeaderValue: "us",
  cacheTTL: 60,
});

On this page