registry.ts 724 Bytes
import type { GatewayStrategy } from '../core/types';

/**
 * 网关注册表:接入一个新系统 = 登记一个 GatewayStrategy。
 * 未登记的网关 resolveGateway 直接抛错,避免出现「请求发出去了但走的是默认签名」这种最难查的故障。
 */
const strategies: Record<string, GatewayStrategy> = {};

export function registerGateway(strategy: GatewayStrategy): void {
  strategies[strategy.id] = strategy;
}

export function resolveGateway(id: string): GatewayStrategy {
  const strategy = strategies[id];
  if (!strategy) {
    throw new Error(
      `网关「${id}」尚未接入:请在该网关定义处调用 registerGateway 登记一个策略。`,
    );
  }
  return strategy;
}