import { type BasicAuth, type BearerAuth } from './net.common.ts';
export * from './net.common.ts';
export interface FullResponse
{
status: number;
headers: Headers;
body: TBody;
}
export interface RequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'HEAD' | 'DELETE' | 'PATCH';
queries?: Record;
headers?: Record | Headers;
body?: string | Record | ArrayBufferView | ArrayBuffer | Blob | URLSearchParams | FormData;
type?: 'application/json' | 'application/x-www-form-urlencoded' | 'multipart/form-data';
encoding?: 'binary';
retries?: true | number;
timeout?: number;
auth?: BasicAuth | BearerAuth;
cookies?: Record;
cors?: boolean;
credentials?: RequestCredentials;
}
export interface RequestRawOptions extends RequestOptions {
raw: true;
}
export interface RequestFullOptions extends RequestOptions {
full: true;
}
export interface RequestError extends Error {
url: URL;
options: RequestOptions | RequestFullOptions | RequestRawOptions;
response?: FullResponse;
}
/**
- url: 可以只有 pathname 部分
- options?:
- method?: `有 body 时为 POST, 否则为 GET` 'GET' | 'POST' | ··
- queries?: 添加到 url 上的参数,是 Record,true/false 会被转换为 0/1
- headers?: http 请求头 (Record 或者 Headers 类型),其中 key 必须是小写的
- body?: http 请求体,可以是 string, Record (会自动 JSON.stringify), ArrayBuffer(View), Blob,
URLSearchParams (type 为 x-www-form-urlencoded), FormData (type 为 form-data)
- type?: `'application/json'` 有 body 时设置 http 请求头中的 content-type 头
- encoding?: 设置为 'binary' 时返回 ArrayBuffer 的 body
- retries?: `false` 可以传入 true (默认 2 次) 或 重试次数
- timeout?: `5 * 1000`
- auth?: BasicAuth | BearerAuth
- raw?: `false` 传入后返回整个 response */
export declare function request(url: string | URL): Promise;
export declare function request(url: string | URL, options: RequestRawOptions): Promise;
export declare function request(url: string | URL, options: RequestFullOptions & {
encoding: 'binary';
}): Promise>;
export declare function request(url: string | URL, options: RequestFullOptions): Promise>;
export declare function request(url: string | URL, options: RequestOptions & {
encoding: 'binary';
}): Promise;
export declare function request(url: string | URL, options: RequestOptions): Promise;
/** 发起 http 请求并将响应体作为 json 解析 */
export declare function request_json(url: string, options?: RequestOptions): Promise;