base.ts 3.71 KB
// 基类执行细节已下沉到 helpers,保留接口编排与可覆盖点
import type { RequestConfig, ResponseData } from "../../types";
import { processResponseSafely, transformToResponseData as defaultTransform, checkHttpStatus as defaultCheckHttpStatus } from "../handlers/response-helpers";
import { handleRequestError } from "../handlers/error-handler";
import { sendRequestGeneric, handleRequestGeneric, createPlatformRequestOptions } from "../handlers/request-helpers";
import type { Adapter, IResponseConfig } from "../types";
import { dispatchEvent } from "../../monitoring/events";

/**
 * 基础适配器类(更“薄”)
 * - 只负责方法编排与对外接口
 * - 具体管线逻辑委托给 helpers,子类可按需重写挂钩方法
 */
export abstract class BaseAdapter {
  /**
   * 由子类提供平台请求器(如 uni.request / wx.request),要求返回对象包含 abort 方法
   */
  protected abstract getPlatformRequester(): (options: any) => { abort: () => void };

  /**
   * 执行实际的请求操作;默认通过平台请求器实现,子类可重写以适配 Web fetch 等
   */
  protected executeRequest(
    _config: RequestConfig,
    options: any
  ): Promise<any> {
    const requester = this.getPlatformRequester();
    return new Promise((resolve, reject) => {
      requester({
        ...options,
        success: (res: any) => resolve(res),
        fail: (err: any) => reject(err),
      });
    });
  }

  // // 兼容:暴露基础请求选项创建方法,保持子类可调用
  // protected createBaseRequestOptions(config: RequestConfig) {
  //   return createBaseOptions(config);
  // }

  protected createRequestOptions(
    config: RequestConfig,
    fullURL: string
  ): any {
    return createPlatformRequestOptions(config, fullURL);
  }

  /** 将原始响应转换为标准 ResponseData;可覆盖 */
  protected transformResponse(
    rawResponse: any,
    config: RequestConfig
  ): ResponseData<any> | Promise<ResponseData<any>> {
    return defaultTransform(rawResponse, config);
  }

  /** 检查 HTTP 状态码并在错误时抛出异常;可覆盖 */
  protected checkHttpStatus(data: IResponseConfig): void {
    return defaultCheckHttpStatus(data);
  }

  /** 处理响应数据;可覆盖 */
  protected handleResponse(data: IResponseConfig): ResponseData<any> {
    return processResponseSafely(data);
  }

  /** 处理请求错误;可覆盖 */
  protected handleError(
    error: any,
    config: RequestConfig,
    signal?: AbortSignal
  ): never {
    dispatchEvent("error", error, config);
    return handleRequestError(error, config, signal);
  }

  /** 创建适配器函数 */
  public createAdapter(): Adapter {
    return (config: RequestConfig): Promise<ResponseData<any>> => {
      return this.handleRequest(config);
    };
  }

  /** 处理请求的通用逻辑 - 委托给 helpers */
  protected handleRequest(config: RequestConfig): Promise<ResponseData<any>> {
    return handleRequestGeneric(config, {
      createRequestOptions: (c, url) => this.createRequestOptions(c, url),
      sendRequest: (c, options, url) => this.sendRequest(c, options, url),
    });
  }

  /** 发送请求的通用逻辑 - 委托给 helpers */
  protected sendRequest(
    config: RequestConfig,
    options: any,
    fullURL: string
  ): Promise<ResponseData<any>> {
    return sendRequestGeneric(config, options, fullURL, {
      executeRequest: (c, o) => this.executeRequest(c, o),
      transformResponse: (raw, c) => this.transformResponse(raw, c),
      checkHttpStatus: (resp, c) => this.checkHttpStatus({responseData:resp,config:c}),
      handleResponse: (resp, c) => this.handleResponse({responseData:resp,config:c}),
      handleError: (err, c, sig) => this.handleError(err, c, sig),
    });
  }
}