@zhengxs/erniebot
Version:
非官方 JS-SDK,可以调用文心大模型的能力,包含文本创作、通用对话、语义向量、AI作图等
91 lines (90 loc) • 3.81 kB
TypeScript
import { HTTPClient, Fetch, HTTPSearchParams, APIRequestInit, APIHeaders, APIRequestConfig, APIRequestOptions, APIResponseProps, MaybePromise } from '../interfaces';
import { APIPromise } from './promise';
import { APIError } from '../error';
export declare class MultipartBody {
body: any;
constructor(body: any);
get [Symbol.toStringTag](): string;
}
export interface APIClientOptions {
/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*/
baseURL?: string;
/**
* 请求头
*/
headers?: APIHeaders;
/**
* The maximum amount of time (in milliseconds) that the client should wait for a response
* from the server before timing out a single request.
*
* Note that request timeouts are retried by default, so in a worst-case scenario you may wait
* much longer than this timeout before the promise succeeds or fails.
*
* @defaultValue 10 minutes
*/
timeout?: number;
/**
* TODO 待实现
*
* @defaultValue 2
*/
maxRetries?: number;
/**
* 网络请求
*/
fetch?: Fetch;
[key: string]: any;
}
export declare class APIClient {
baseURL: string;
headers: APIHeaders;
timeout: number;
maxRetries: number;
fetch: Fetch;
constructor(options?: APIClientOptions);
get<Rsp>(path: string, opts?: Omit<APIRequestConfig, 'method'>): APIPromise<Rsp>;
post<Rsp>(path: string, opts?: Omit<APIRequestConfig, 'method'>): APIPromise<Rsp>;
patch<Rsp>(path: string, opts?: Omit<APIRequestConfig, 'method'>): APIPromise<Rsp>;
put<Rsp>(path: string, opts?: Omit<APIRequestConfig, 'method'>): APIPromise<Rsp>;
delete<Rsp>(path: string, opts?: Omit<APIRequestConfig, 'method'>): APIPromise<Rsp>;
private methodRequest;
request<Rsp = any>(options: APIRequestOptions, remainingRetries?: number | null): APIPromise<Rsp>;
protected defaultQuery(): Promise<HTTPSearchParams>;
/**
* Override this to add your own default headers, for example:
*
* ```js
* {
* ...super.defaultHeaders(),
* Authorization: 'Bearer 123',
* }
* ```
*/
protected defaultHeaders(options: APIRequestOptions): APIHeaders;
protected parseResponse<T>({ response, options, controller }: APIResponseProps): Promise<T>;
protected authHeaders(options: APIRequestOptions): APIHeaders;
protected getUserAgent(): string;
protected validateHeaders(headers: APIHeaders, customHeaders: APIHeaders): void;
/**
* Used as a callback for mutating the given `RequestInit` object.
*
* This is useful for cases where you want to add certain headers based off of
* the request properties, e.g. `method` or `url`.
*/
protected prepareRequest(request: APIRequestInit, { url, options }: {
url: string;
options: APIRequestOptions;
}): MaybePromise<any>;
protected getRequestClient(): HTTPClient;
fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
protected buildURL({ path, query }: APIRequestOptions): Promise<string>;
private makeRequest;
protected makeStatusError(status: number | undefined, error: unknown | undefined, message: string | undefined, headers: Headers | undefined): APIError;
private buildRequest;
}
export declare function isMultipartBody(body: any): body is MultipartBody;
export declare function calculateContentLength(body: BodyInit | null): string | null;
export declare function mergeHTTPSearchParams(target: HTTPSearchParams | undefined, source?: HTTPSearchParams): URLSearchParams;
export declare function createResponseHeaders(headers: Awaited<ReturnType<Fetch>>['headers']): Record<string, string>;