index.ts 938 Bytes
import type { GatewayId, GatewayStrategy } from '../types';
import { djSmartcarlifeGateway, scrmCarlifeGateway, scrmShopmallGateway } from './glsx';

/**
 * 网关注册表:接入一个新系统 = 在这里多登记一个策略。
 * 未登记的网关直接抛错,避免出现「请求发出去了但走的是默认签名」这种最难查的故障。
 */
const strategies: Partial<Record<GatewayId, GatewayStrategy>> = {
  [scrmShopmallGateway.id]: scrmShopmallGateway,
  [scrmCarlifeGateway.id]: scrmCarlifeGateway,
  [djSmartcarlifeGateway.id]: djSmartcarlifeGateway,
};

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

export function resolveGateway(id: GatewayId): GatewayStrategy {
  const strategy = strategies[id];
  if (!strategy) {
    throw new Error(`网关「${id}」尚未接入:请在 src/adapter/gateways 登记一个策略。`);
  }
  return strategy;
}