UNPKG

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