UNPKG

6.5 kBTypeScriptView Raw
1import * as http from "http";
2import * as http2 from "http2";
3import * as https from "https";
4import { Stream } from "stream";
5import methods = require("methods");
6
7import SAgent = require("./agent");
8import { Blob } from "buffer";
9import { ReadStream } from "fs";
10import { LookupFunction } from "net";
11import RequestBase = require("../request-base");
12import ResponseBase = require("./response");
13import { AppendOptions } from "form-data";
14import { AgentOptions as SAgentOptions, CBHandler, URLType } from "../../types";
15import { Request as Http2Request } from "./http2wrapper";
16
17type HttpMethod<Req extends request.Request> =
18 | ((url: URLType, callback?: CBHandler) => Req)
19 | ((url: URLType, data?: string | Record<string, any>, callback?: CBHandler) => Req);
20
21type RequestMethods<Req extends request.Request> = {
22 [key in (typeof methods[number]) | "del"]: HttpMethod<Req>;
23};
24
25declare class SARequest extends Stream implements RequestBase {
26 constructor(method: string, url: URLType);
27
28 method: string;
29 url: string;
30 cookies: string;
31 req: http.ClientRequest | Http2Request;
32 res: http.IncomingMessage | (http2.IncomingHttpHeaders & http2.IncomingHttpStatusHeader);
33
34 [Symbol.toStringTag]: string;
35
36 attach(
37 field: string,
38 file: request.MultipartValueSingle,
39 options?: string | { filename?: string | undefined; contentType?: string | undefined },
40 ): this;
41 abort(): this;
42 accept(type: string): this;
43 agent(): SAgent | http.Agent | https.Agent;
44 agent(agent: SAgent | http.Agent | https.Agent): this;
45 auth(token: string, options: { type: "bearer" }): this;
46 auth(user: string, pass: string, options?: { type: "basic" | "auto"; encoder?: (str: string) => string }): this;
47 buffer(val?: boolean): this;
48 ca(cert: string | string[] | Buffer | Buffer[]): this;
49 catch<TResult = never>(
50 onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null,
51 ): Promise<ResponseBase | TResult>;
52 cert(cert: string | string[] | Buffer | Buffer[]): this;
53 clearTimeout(): this;
54 connect(override: string | { [hostname: string]: false | string | { host: string; port: number } }): this;
55 disableTLSCerts(): this;
56 end(callback?: CBHandler): void;
57 field(
58 fields: {
59 [fieldName: string]:
60 | (string | number | boolean | Blob | Buffer | ReadStream)
61 | Array<string | number | boolean | Blob | Buffer | ReadStream>;
62 },
63 ): this;
64 field(
65 name: string,
66 val:
67 | (string | number | boolean | Blob | Buffer | ReadStream)
68 | Array<string | number | boolean | Blob | Buffer | ReadStream>,
69 options?: AppendOptions | string,
70 ): this;
71 finally(onfinally?: (() => void) | null): Promise<ResponseBase>;
72 get(header: string): string;
73 getHeader(header: string): string;
74 http2(enable?: boolean): this;
75 key(cert: string | string[] | Buffer | Buffer[]): this;
76 lookup(): LookupFunction;
77 lookup(lookup: LookupFunction): this;
78 maxResponseSize(n: number): this;
79 ok(callback: (res: ResponseBase) => boolean): this;
80 parse(
81 parser:
82 | ((str: string) => any)
83 | ((res: ResponseBase, callback: (err: Error | null, body: any) => void) => void),
84 ): this;
85 pfx(cert: string | string[] | Buffer | Buffer[] | { pfx: string | Buffer; passphrase: string }): this;
86 query(val: Record<string, any> | string): this;
87 redirects(n: number): this;
88 responseType(type: string): this;
89 retry(count?: number, callback?: CBHandler): this;
90 send(data?: string | object): this;
91 serialize(serializer: (obj: any) => string): this;
92 set(field: "Cookie", val: string[]): this;
93 set(field: http.IncomingHttpHeaders): this;
94 set(field: string, val: string): this;
95 sortQuery(sort?: boolean | ((a: string, b: string) => number)): this;
96 then<TResult1 = ResponseBase, TResult2 = never>(
97 onfulfilled?: ((value: ResponseBase) => TResult1 | PromiseLike<TResult1>) | null,
98 onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
99 ): Promise<TResult1 | TResult2>;
100 timeout(ms: number | { deadline?: number; response?: number }): this;
101 toJSON(): { method: string; url: string; data?: string | object; headers: Array<string | string[]> };
102 trustLocalhost(enabled?: boolean): this;
103 type(val: string): this;
104 unset(field: string): this;
105 use(fn: (req: this) => void): this;
106 withCredentials(on?: boolean): this;
107 write(data: string | Buffer, encoding?: string): boolean;
108}
109
110declare namespace request {
111 // eslint-disable-next-line @typescript-eslint/no-empty-interface
112 interface Request extends SARequest {}
113 // eslint-disable-next-line @typescript-eslint/no-empty-interface
114 interface Response extends ResponseBase {}
115 type SuperAgentRequest = Request;
116 type Agent = SAgent;
117 type Plugin = (req: Request) => void;
118 type AgentOptions = SAgentOptions;
119
120 type CallbackHandler = CBHandler;
121
122 type MultipartValueSingle = Blob | Buffer | ReadStream | string | boolean | number;
123
124 interface ProgressEvent {
125 direction: "download" | "upload";
126 loaded: number;
127 percent?: number | undefined;
128 total?: number | undefined;
129 }
130
131 interface ResponseError extends Error {
132 status?: number | undefined;
133 response?: Response | undefined;
134 timeout?: boolean | undefined;
135 }
136
137 interface HTTPError extends Error {
138 status: number;
139 text: string;
140 method: string;
141 path: string;
142 }
143
144 type SuperAgent<Req extends Request = Request> = RequestMethods<Req> & Stream;
145
146 interface SuperAgentStatic<Req extends Request = Request> extends SuperAgent<Req> {
147 (url: URLType): Request;
148 (method: string, url: URLType): Request;
149 (url: URLType, cb: CBHandler): void;
150
151 Request: typeof SARequest;
152 Response: typeof ResponseBase;
153 agent: typeof SAgent & ((options?: SAgentOptions) => InstanceType<typeof SAgent>);
154 protocols: {
155 "http:": typeof http;
156 "https:": typeof https;
157 "http2:": typeof http2;
158 };
159 serialize: Record<string, (...args: any[]) => string>;
160 parse: Record<string, (res: Response, cb: (err: any, res: Response) => void) => void>;
161 buffer: Record<string, boolean>;
162 }
163}
164
165declare const request: request.SuperAgentStatic;
166
167export = request;