tencentcos.d.ts 4.2 KB
declare module "cos-js-sdk-v5" {
  // 进度信息接口
  export interface ProgressInfo {
    loaded: number;
    total: number;
    speed: number;
    percent: number;
  }

  // 鉴权凭证回调类型
  export interface Credentials {
    TmpSecretId: string;
    TmpSecretKey: string;
    SecurityToken: string;
    StartTime: number;
    ExpiredTime: number;
  }

  // 获取签名回调参数
  type GetAuthorizationCallback =
    | string // 格式一:直接返回签名字符串
    | {
        // 格式二:返回对象形式
        Authorization: string;
        SecurityToken?: string;
      };

  // 鉴权回调选项
  type GetAuthorizationOptions =
    | {
        // 格式一:临时密钥模式
        Bucket: string;
        Region: string;
      }
    | {
        // 格式二:签名计算模式
        Method: string;
        Pathname: string;
        Key: string;
        Query: Record<string, any>;
        Headers: Record<string, any>;
      };

  // SDK 配置项
  export interface COSOptions {
    // 基础鉴权参数
    SecretId?: string;
    SecretKey?: string;
    SecurityToken?: string;
    getAuthorization?: (
      options: GetAuthorizationOptions,
      callback: (credentials: Credentials | GetAuthorizationCallback) => void,
    ) => void;

    // 并发控制参数
    FileParallelLimit?: number;
    ChunkParallelLimit?: number;
    CopyChunkParallelLimit?: number;
    ChunkRetryTimes?: number;

    // 分片配置
    ChunkSize?: number;
    SliceSize?: number;
    CopyChunkSize?: number;
    CopySliceSize?: number;

    // 网络配置
    Protocol?: "http" | "https";
    ServiceDomain?: string;
    Domain?: string;
    Proxy?: string;
    Timeout?: number;
    KeepAlive?: boolean;
    StrictSsl?: boolean;

    // 高级配置
    UploadQueueSize?: number;
    UploadCheckContentMd5?: boolean;
    ProgressInterval?: number;
    UseAccelerate?: boolean;
  }

  // 上传参数
  export interface UploadParams {
    Bucket: string;
    Region: string;
    Key: string;
    Body: File | Blob | string | Buffer;
    CacheControl?: string;
    ContentDisposition?: string;
    ContentEncoding?: string;
    ContentType?: string;
    Expires?: string;
    StorageClass?: string;
    onProgress?: (progressData: ProgressInfo) => void;
  }

  // 响应数据结构
  export interface ResponseData {
    statusCode: number;
    headers: Record<string, string>;
    Location: string;
    ETag: string;
  }

  // 错误处理
  export interface ErrorData {
    statusCode?: number;
    error?: Error;
    message?: string;
  }

  // 回调函数类型
  type Callback<T> = (error: ErrorData | null, data: T & ResponseData) => void;

  // COS 核心类
  export class COS {
    constructor(options: COSOptions);

    // 对象操作
    uploadFile(
      params: UploadParams,
      callback?: Callback<{ Location: string; ETag: string }>,
    ): Promise<ResponseData>;

    getObject(
      params: {
        Bucket: string;
        Region: string;
        Key: string;
        ResponseContentType?: string;
      },
      callback?: Callback<{ Body: Buffer }>,
    ): Promise<ResponseData & { Body: Buffer }>;

    deleteObject(
      params: {
        Bucket: string;
        Region: string;
        Key: string;
      },
      callback?: Callback<void>,
    ): Promise<ResponseData>;

    // 存储桶操作
    getBucket(
      params: {
        Bucket: string;
        Region: string;
        Prefix?: string;
        Marker?: string;
        MaxKeys?: number;
      },
      callback?: Callback<{
        Contents: {
          Key: string;
          LastModified: string;
          ETag: string;
          Size: number;
          StorageClass: string;
        }[];
      }>,
    ): Promise<ResponseData>;

    // 分片上传
    sliceUploadFile(
      params: UploadParams & {
        TaskReady?: (taskId: string) => void;
        onHashProgress?: (progressData: ProgressInfo) => void;
        onProgress?: (progressData: ProgressInfo) => void;
      },
      callback?: Callback<{ Location: string; ETag: string }>,
    ): Promise<ResponseData>;
  }

  // 浏览器环境 File 类型扩展
  export interface File extends Blob {
    readonly lastModified: number;
    readonly name: string;
  }

  export default COS;
}