UNPKG

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