UNPKG

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