index.ts 1.24 KB
import type { RequestConfig } from '../types';
import type { Adapter } from './types';
import webAdapter from './adapters/web';
import miniprogramAdapter from './adapters/miniprogram';
import uniappAdapter from './adapters/uniapp';
import { detectEnvironment, EnvironmentType } from '../utils';

// 重新导出签名相关功能
export { generateSignature, type SignatureOptions, type SignatureResult } from '../signature';

/**
 * 获取适合当前环境的适配器
 * @param {any} options 适配器选项,包含响应拦截器配置等
 * @returns {Adapter} 适配器函数
 */
export function getAdapter(options?: any): Adapter {
  // 使用环境检测函数选择合适的适配器
  const env = detectEnvironment();
  
  // 返回适配器函数,但不直接调用它
  // 这样在实际请求时,适配器会接收完整的请求配置(包含httpOptions)
  if (env === EnvironmentType.MINIPROGRAM) {
    // 小程序环境
    return miniprogramAdapter;
  } else if (env === EnvironmentType.UNIAPP) {
    // uniapp环境
    return uniappAdapter;
  } else {
    // Web环境
    return webAdapter;
  }
}

// 导出所有适配器
export {
  webAdapter,
  miniprogramAdapter,
  uniappAdapter
};

// 导出适配器相关类型
export * from './types';