/** * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * * @file src/http_client.ts * @author leeight */ import { Readable, Writable } from 'stream'; import { EventEmitter } from 'events'; import type { BceConfig, Credentials, HttpMethod, HttpHeaders, BceResponse } from './types/common'; /** 签名计算函数类型 */ export type SignatureFunction = (credentials: Credentials, httpMethod: HttpMethod, path: string, params: Record, headers: HttpHeaders, context: HttpClient) => string | Promise | Promise<{ authorization: string; xbceDate?: string; }>; /** HTTP 请求体类型 */ export type RequestBody = string | Buffer | Readable | Blob | ArrayBuffer | FormData | null | undefined; /** * HTTP 客户端类 * * @example * ```typescript * const client = new HttpClient({ * endpoint: 'https://bj.bcebos.com', * credentials: { ak: 'your-ak', sk: 'your-sk' } * }); * * const response = await client.sendRequest('GET', '/path', null, {}, {}); * ``` */ export declare class HttpClient extends EventEmitter { /** 配置对象 */ config: BceConfig; /** HTTP(S) 请求对象 */ private _req; /** * 构造函数 * @param config HTTP 客户端配置 */ constructor(config: BceConfig); /** * 基于对象路径更新配置参数值 * @param path 配置路径,用点分隔 * @param value 新的值 * @returns 更新后的配置对象 */ updateConfigByPath(path: string, value: any): BceConfig; /** * 发送 HTTP 请求 * @param httpMethod HTTP 方法 * @param path 请求路径 * @param body 请求体 * @param headers 请求头 * @param params 查询参数 * @param signFunction 签名函数 * @param outputStream 输出流 * @returns Promise */ sendRequest(httpMethod: HttpMethod, path: string, body?: RequestBody, headers?: HttpHeaders, params?: Record, signFunction?: SignatureFunction, outputStream?: Writable): Promise; /** * 执行 HTTP 请求 * @param options 请求选项 * @param body 请求体 * @param outputStream 输出流 * @returns Promise */ private _doRequest; /** * 猜测内容长度 * @param data 数据 * @returns 内容长度 */ private _guessContentLength; /** * 修复响应头部 * @param headers 原始头部 * @returns 修复后的头部 */ private _fixHeaders; /** * 接收响应数据 * @param res 响应对象 * @returns Promise */ private _recvResponse; /** * 发送请求数据 * @param req 请求对象 * @param data 数据 */ private _sendRequest; /** * 构建查询字符串 * @param params 参数对象 * @returns 查询字符串 */ buildQueryString(params?: Record): string; /** * 获取请求 URL * @param path 路径 * @param params 参数 * @returns 完整的请求 URL */ private _getRequestUrl; /** * 创建成功响应 * @param httpHeaders HTTP 头部 * @param body 响应体 * @returns 成功响应对象 */ private _createSuccessResponse; /** * 创建失败响应 * @param statusCode 状态码 * @param message 错误消息 * @param code 错误代码 * @param requestId 请求 ID * @param xBceDate BCE 日期 * @returns 失败响应对象 */ private _createFailureResponse; /** * 检查是否为 Promise * @param obj 对象 * @returns 是否为 Promise */ private _isPromise; /** * 检查是否为 XHR2 兼容对象 * @param obj 对象 * @returns 是否兼容 */ private _isXHR2Compatible; } export default HttpClient; //# sourceMappingURL=http_client.d.ts.map