import { Adapter } from 'requete/adapter'; import { Logger } from 'requete/shared'; import { TimeoutAbortController } from './AbortController'; export type Method = 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH'; export type RequestBody = BodyInit | null | Record | Record[]; export type RequestQueryRecord = Record>; export type RequestQuery = string | URLSearchParams | RequestQueryRecord; export interface RequestConfig { baseURL?: string; /** request timeout (ms) */ timeout?: number; /** response body type */ responseType?: 'json' | 'formData' | 'text' | 'blob' | 'arrayBuffer'; /** A string indicating how the request will interact with the browser's cache to set request's cache. */ cache?: RequestCache; /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */ credentials?: RequestCredentials; /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ headers?: HeadersInit; /** A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ integrity?: string; /** A boolean to set request's keepalive. */ keepalive?: boolean; /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */ mode?: RequestMode; /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ redirect?: RequestRedirect; /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */ referrer?: string; /** A referrer policy to set request's referrerPolicy. */ referrerPolicy?: ReferrerPolicy; /** enable logger or set logger level # */ verbose?: boolean | number; /** * parse json function * (for transform response) * @default JSON.parse */ toJSON?(body: string): any; } export interface IRequest extends Omit { url: string; /** * A string to set request's method. * @default GET */ method?: Method; /** A string or object to set querystring of url */ params?: RequestQuery; /** request`s body */ data?: RequestBody; /** * A TimeoutAbortController to set request's signal. * @default TimeoutAbortController */ abort?: TimeoutAbortController | null; /** specify request adapter */ adapter?: Adapter; /** flexible custom field */ custom?: Record; } /** {@link https://developer.mozilla.org/en-US/docs/Web/API/Response} */ export interface IResponse { headers: Headers; ok: boolean; redirected: boolean; status: number; statusText: string; type: ResponseType; url: string; data: Data; responseText?: string; } export interface IContext extends IResponse { /** * request config. * and empty `Headers` object as default */ request: IRequest & { method: Method; headers: Headers; }; /** * set `ctx.request.headers` * * *And header names are matched by case-insensitive byte sequence.* * * @example * ```ts * // set a header * ctx.set('name', '') * * // remove a header * ctx.set('name', null) * ctx.set('name') * * // set headers * ctx.set({ name1: '', name2: '' }) * ``` */ set(headerOrName: HeadersInit | string, value?: string | null): this; /** * Add extra params to `request.url`. * If there are duplicate keys, then the original key-values will be removed. */ params(params: RequestQuery): this; /** * get `ctx.request.abort`, * and **create one if not exist** * @throws {RequestError} */ abort(): TimeoutAbortController; /** throw {@link RequestError} */ throw(e: string | Error): void; /** * Assign to current context */ assign(context: Partial): void; /** * Replay current request * And assign new context to current, with replay`s response */ replay(): Promise; } export type Middleware = (ctx: IContext, next: () => Promise) => Promise; type AliasConfig = Omit; export declare class Requete { static defaults: RequestConfig; private configs?; private adapter; private middlewares; logger: Logger; constructor(config?: RequestConfig); /** * add middleware function * * @attention * - The calling order of middleware should follow the **Onion Model**. * like {@link https://github.com/koajs/koa/blob/master/docs/guide.md#writing-middleware Koajs}. * - `next()` must be called asynchronously in middleware * * @example * ```ts * http.use(async (ctx, next) => { * // set request header * ctx.set('Authorization', '') * * // wait for request responding * await next() * * // transformed response body * console.log(ctx.data) * * // throw a request error * if (!ctx.data) ctx.throw('no response data') * }) * ``` */ use(middleware: Middleware): this; private createRequest; private createContext; private invoke; request(config: IRequest): Promise>; get(url: string, config?: AliasConfig): Promise>; delete(url: string, config?: AliasConfig): Promise>; head(url: string, config?: AliasConfig): Promise>; options(url: string, config?: AliasConfig): Promise>; post(url: string, data?: RequestBody, config?: AliasConfig): Promise>; put(url: string, data?: RequestBody, config?: AliasConfig): Promise>; patch(url: string, data?: RequestBody, config?: AliasConfig): Promise>; } export {};