审查视图

app/types/tencentcos.d.ts 4.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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;
}