1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | import * as fs from "fs";
|
20 | import * as http from "http";
|
21 | import * as stream from "stream";
|
22 | import * as cookiejar from "cookiejar";
|
23 |
|
24 | type CallbackHandler = (err: any, res: request.Response) => void;
|
25 |
|
26 | type Serializer = (obj: any) => string;
|
27 |
|
28 | type BrowserParser = (str: string) => any;
|
29 |
|
30 | type NodeParser = (res: request.Response, callback: (err: Error | null, body: any) => void) => void;
|
31 |
|
32 | type Parser = BrowserParser | NodeParser;
|
33 |
|
34 | type MultipartValueSingle = Blob | Buffer | fs.ReadStream | string | boolean | number;
|
35 |
|
36 | type MultipartValue = MultipartValueSingle | MultipartValueSingle[];
|
37 |
|
38 | declare const request: request.SuperAgentStatic;
|
39 |
|
40 | declare namespace request {
|
41 | interface SuperAgentRequest extends Request {
|
42 | agent(agent?: http.Agent): this;
|
43 |
|
44 | cookies: string;
|
45 | method: string;
|
46 | url: string;
|
47 | }
|
48 | interface SuperAgentStatic extends SuperAgent<SuperAgentRequest> {
|
49 | (url: string): SuperAgentRequest;
|
50 |
|
51 | (method: string, url: string): SuperAgentRequest;
|
52 |
|
53 | agent(): this & Request;
|
54 | serialize: { [type: string]: Serializer };
|
55 | parse: { [type: string]: Parser };
|
56 | }
|
57 |
|
58 | interface SuperAgent<Req extends SuperAgentRequest> extends stream.Stream {
|
59 | jar: cookiejar.CookieJar;
|
60 | attachCookies(req: Req): void;
|
61 | checkout(url: string, callback?: CallbackHandler): Req;
|
62 | connect(url: string, callback?: CallbackHandler): Req;
|
63 | copy(url: string, callback?: CallbackHandler): Req;
|
64 | del(url: string, callback?: CallbackHandler): Req;
|
65 | delete(url: string, callback?: CallbackHandler): Req;
|
66 | get(url: string, callback?: CallbackHandler): Req;
|
67 | head(url: string, callback?: CallbackHandler): Req;
|
68 | lock(url: string, callback?: CallbackHandler): Req;
|
69 | merge(url: string, callback?: CallbackHandler): Req;
|
70 | mkactivity(url: string, callback?: CallbackHandler): Req;
|
71 | mkcol(url: string, callback?: CallbackHandler): Req;
|
72 | move(url: string, callback?: CallbackHandler): Req;
|
73 | notify(url: string, callback?: CallbackHandler): Req;
|
74 | options(url: string, callback?: CallbackHandler): Req;
|
75 | patch(url: string, callback?: CallbackHandler): Req;
|
76 | post(url: string, callback?: CallbackHandler): Req;
|
77 | propfind(url: string, callback?: CallbackHandler): Req;
|
78 | proppatch(url: string, callback?: CallbackHandler): Req;
|
79 | purge(url: string, callback?: CallbackHandler): Req;
|
80 | put(url: string, callback?: CallbackHandler): Req;
|
81 | report(url: string, callback?: CallbackHandler): Req;
|
82 | saveCookies(res: Response): void;
|
83 | search(url: string, callback?: CallbackHandler): Req;
|
84 | subscribe(url: string, callback?: CallbackHandler): Req;
|
85 | trace(url: string, callback?: CallbackHandler): Req;
|
86 | unlock(url: string, callback?: CallbackHandler): Req;
|
87 | unsubscribe(url: string, callback?: CallbackHandler): Req;
|
88 | }
|
89 |
|
90 | interface ResponseError extends Error {
|
91 | status?: number | undefined;
|
92 | response?: Response | undefined;
|
93 | }
|
94 |
|
95 | interface HTTPError extends Error {
|
96 | status: number;
|
97 | text: string;
|
98 | method: string;
|
99 | path: string;
|
100 | }
|
101 |
|
102 | interface Response extends NodeJS.ReadableStream {
|
103 | accepted: boolean;
|
104 | badRequest: boolean;
|
105 | body: any;
|
106 | charset: string;
|
107 | clientError: boolean;
|
108 | error: false | HTTPError;
|
109 | files: any;
|
110 | forbidden: boolean;
|
111 | get(header: string): string;
|
112 | get(header: "Set-Cookie"): string[];
|
113 | header: any;
|
114 | headers: any;
|
115 | info: boolean;
|
116 | links: Record<string, string>;
|
117 | noContent: boolean;
|
118 | notAcceptable: boolean;
|
119 | notFound: boolean;
|
120 | ok: boolean;
|
121 | redirect: boolean;
|
122 | serverError: boolean;
|
123 | status: number;
|
124 | statusCode: number;
|
125 | statusType: number;
|
126 | text: string;
|
127 | type: string;
|
128 | unauthorized: boolean;
|
129 | xhr: any;
|
130 | redirects: string[];
|
131 | }
|
132 |
|
133 | interface Request extends Promise<Response> {
|
134 | abort(): void;
|
135 | accept(type: string): this;
|
136 | attach(
|
137 | field: string,
|
138 | file: MultipartValueSingle,
|
139 | options?: string | { filename?: string | undefined; contentType?: string | undefined },
|
140 | ): this;
|
141 | auth(user: string, pass: string, options?: { type: "basic" | "auto" }): this;
|
142 | auth(token: string, options: { type: "bearer" }): this;
|
143 | buffer(val?: boolean): this;
|
144 | ca(cert: string | string[] | Buffer | Buffer[]): this;
|
145 | cert(cert: string | string[] | Buffer | Buffer[]): this;
|
146 | clearTimeout(): this;
|
147 | disableTLSCerts(): this;
|
148 | end(callback?: CallbackHandler): void;
|
149 | field(name: string, val: MultipartValue): this;
|
150 | field(fields: { [fieldName: string]: MultipartValue }): this;
|
151 | get(field: string): string;
|
152 | http2(enable?: boolean): this;
|
153 | key(cert: string | string[] | Buffer | Buffer[]): this;
|
154 | ok(callback: (res: Response) => boolean): this;
|
155 | on(name: "error", handler: (err: any) => void): this;
|
156 | on(name: "progress", handler: (event: ProgressEvent) => void): this;
|
157 | on(name: "response", handler: (response: Response) => void): this;
|
158 | on(name: string, handler: (event: any) => void): this;
|
159 | parse(parser: Parser): this;
|
160 | part(): this;
|
161 | pfx(cert: string | string[] | Buffer | Buffer[] | { pfx: string | Buffer; passphrase: string }): this;
|
162 | pipe(stream: NodeJS.WritableStream, options?: object): stream.Writable;
|
163 | query(val: object | string): this;
|
164 | redirects(n: number): this;
|
165 | responseType(type: string): this;
|
166 | retry(count?: number, callback?: CallbackHandler): this;
|
167 | send(data?: string | object): this;
|
168 | serialize(serializer: Serializer): this;
|
169 | set(field: object): this;
|
170 | set(field: string, val: string): this;
|
171 | set(field: "Cookie", val: string[]): this;
|
172 | timeout(ms: number | { deadline?: number | undefined; response?: number | undefined }): this;
|
173 | trustLocalhost(enabled?: boolean): this;
|
174 | type(val: string): this;
|
175 | unset(field: string): this;
|
176 | use(fn: Plugin): this;
|
177 | withCredentials(): this;
|
178 | write(data: string | Buffer, encoding?: string): boolean;
|
179 | maxResponseSize(size: number): this;
|
180 | }
|
181 |
|
182 | type Plugin = (req: SuperAgentRequest) => void;
|
183 |
|
184 | interface ProgressEvent {
|
185 | direction: "download" | "upload";
|
186 | loaded: number;
|
187 | percent?: number | undefined;
|
188 | total?: number | undefined;
|
189 | }
|
190 | }
|
191 |
|
192 | export = request;
|