UNPKG

7.65 kBTypeScriptView Raw
1// Type definitions for SuperAgent 4.1
2// Project: https://github.com/visionmedia/superagent
3// Definitions by: Nico Zelaya <https://github.com/NicoZelaya>
4// Michael Ledin <https://github.com/mxl>
5// Pap Lőrinc <https://github.com/paplorinc>
6// Shrey Jain <https://github.com/shreyjain1994>
7// Alec Zopf <https://github.com/zopf>
8// Adam Haglund <https://github.com/beeequeue>
9// Lukas Elmer <https://github.com/lukaselmer>
10// Jesse Rogers <https://github.com/theQuazz>
11// Chris Arnesen <https://github.com/carnesen>
12// Anders Kindberg <https://github.com/ghostganz>
13// LuckyWind_sck <https://github.com/LuckyWindsck>
14// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
15// TypeScript Version: 3.0
16
17/// <reference types="node" />
18
19import * as fs from "fs";
20import * as http from "http";
21import * as stream from "stream";
22import * as cookiejar from "cookiejar";
23
24type CallbackHandler = (err: any, res: request.Response) => void;
25
26type Serializer = (obj: any) => string;
27
28type BrowserParser = (str: string) => any;
29
30type NodeParser = (res: request.Response, callback: (err: Error | null, body: any) => void) => void;
31
32type Parser = BrowserParser | NodeParser;
33
34type MultipartValueSingle = Blob | Buffer | fs.ReadStream | string | boolean | number;
35
36type MultipartValue = MultipartValueSingle | MultipartValueSingle[];
37
38declare const request: request.SuperAgentStatic;
39
40declare 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 // tslint:disable-next-line:unified-signatures
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
192export = request;