1 |
|
2 |
|
3 | import { DSVParsedArray, DSVRowString } from "d3-dsv";
|
4 |
|
5 | export interface Request {
|
6 | |
7 |
|
8 |
|
9 |
|
10 | abort(): this;
|
11 |
|
12 | |
13 |
|
14 |
|
15 | get(): this;
|
16 | |
17 |
|
18 |
|
19 | get<RequestData>(data: RequestData): this;
|
20 | |
21 |
|
22 |
|
23 | get<ResponseData>(callback: (error: any, d: ResponseData) => void): this;
|
24 | /**
|
25 | * Equivalent to `request.send` with the GET method: `request.send("GET", data, callback)`.
|
26 | */
|
27 | get<RequestData, ResponseData>(data: RequestData, callback: (error: any, d: ResponseData) => void): this;
|
28 |
|
29 | /**
|
30 | * Returns the current value of the request header with the specified name.
|
31 | * Header names are case-insensitive.
|
32 | */
|
33 | header(name: string): string;
|
34 | /**
|
35 | * Sets the request header with the specified name to the specified value and returns this request instance.
|
36 | * If value is null, removes the request header with the specified name instead.
|
37 | * Header names are case-insensitive.
|
38 | *
|
39 | * Request headers can only be modified before the request is sent.
|
40 | * Therefore, you cannot pass a callback to the request constructor if you wish to specify a header;
|
41 | * use `request.get` or similar instead.
|
42 | */
|
43 | header(name: string, value: string | null): this;
|
44 |
|
45 | /**
|
46 | * Returns the current mime type, which defaults to null.
|
47 | */
|
48 | mimeType(): string | null;
|
49 | /**
|
50 | * Sets the request mime type to the specified value and returns this request instance.
|
51 | * If type is null, clears the current mime type (if any) instead.
|
52 | *
|
53 | * The mime type is used to both set the "Accept" request header and for `overrideMimeType`, where supported.
|
54 | *
|
55 | * The request mime type can only be modified before the request is sent.
|
56 | * Therefore, you cannot pass a callback to the request constructor if you wish to override the mime type;
|
57 | * use `request.get` or similar instead.
|
58 | */
|
59 | mimeType(value: string | null): this;
|
60 |
|
61 | /**
|
62 | * Returns the currently-assigned listener for the "beforesend" type, if any.
|
63 | */
|
64 | on(type: "beforesend"): ((this: this, xhr: XMLHttpRequest) => void) | undefined;
|
65 | /**
|
66 | * Returns the currently-assigned listener for the "progress" type, if any.
|
67 | */
|
68 | on(type: "progress"): ((this: this, progressEvent: ProgressEvent) => void) | undefined;
|
69 | /**
|
70 | * Returns the currently-assigned listener for the "error" type, if any.
|
71 | */
|
72 | on(type: "error"): ((this: this, error: any) => void) | undefined;
|
73 | /**
|
74 | * Returns the currently-assigned listener for the "load" type, if any.
|
75 | */
|
76 | on<ResponseData>(type: "load"): ((this: this, data: ResponseData) => void) | undefined;
|
77 | /**
|
78 | * Returns the currently-assigned listener for the specified type, if any.
|
79 | */
|
80 | on(type: string): ((this: this, data: any) => void) | undefined;
|
81 |
|
82 | /**
|
83 | * Removes the current event listener for the specified type, if any.
|
84 | */
|
85 | on(type: string, listener: null): this;
|
86 |
|
87 | /**
|
88 | * Sets the event listener for the "beforesend" type,
|
89 | * to allow custom headers and the like to be set before the request is sent,
|
90 | * and returns this request instance.
|
91 | *
|
92 | * If an event listener was already registered for the same type, the existing listener is removed before the new listener is added.
|
93 | * To register multiple listeners for the same type, the type may be followed by an optional name, such as `beforesend.foo`. See d3-dispatch for details.
|
94 | */
|
95 | on(type: "beforesend", listener: (this: this, xhr: XMLHttpRequest) => void): this;
|
96 | /**
|
97 | * Sets the event listener for the "progress" type,
|
98 | * to monitor the progress of the request,
|
99 | * and returns this request instance.
|
100 | *
|
101 | * If an event listener was already registered for the same type, the existing listener is removed before the new listener is added.
|
102 | * To register multiple listeners for the same type, the type may be followed by an optional name, such as `progress.foo`. See d3-dispatch for details.
|
103 | */
|
104 | on(type: "progress", listener: (this: this, progressEvent: ProgressEvent) => void): this;
|
105 | /**
|
106 | * Sets the event listener for the "error" type,
|
107 | * when the request completes unsuccessfully; this includes 4xx and 5xx response codes,
|
108 | * and returns this request instance.
|
109 | *
|
110 | * If an event listener was already registered for the same type, the existing listener is removed before the new listener is added.
|
111 | * To register multiple listeners for the same type, the type may be followed by an optional name, such as `error.foo`. See d3-dispatch for details.
|
112 | */
|
113 | on(type: "error", listener: (this: this, error: any) => void): this;
|
114 | /**
|
115 | * Sets the event listener for the "load" type,
|
116 | * when the request completes successfully,
|
117 | * and returns this request instance.
|
118 | *
|
119 | * If an event listener was already registered for the same type, the existing listener is removed before the new listener is added.
|
120 | * To register multiple listeners for the same type, the type may be followed by an optional name, such as `load.foo`. See d3-dispatch for details.
|
121 | */
|
122 | on<ResponseData>(type: "load", listener: (this: this, data: ResponseData) => void): this;
|
123 | /**
|
124 | * Sets the event listener for the specified type,
|
125 | * and returns this request instance.
|
126 | *
|
127 | * The type must be one of the following: "beforesend", "progress", "load", "error".
|
128 | *
|
129 | * If an event listener was already registered for the same type, the existing listener is removed before the new listener is added.
|
130 | * To register multiple listeners for the same type, the type may be followed by an optional name, such as `load.foo`. See d3-dispatch for details.
|
131 | */
|
132 | on(type: string, listener: (this: this, data: any) => void): this;
|
133 |
|
134 | /**
|
135 | * Returns the current password, which defaults to null.
|
136 | */
|
137 | password(): string | null;
|
138 | /**
|
139 | * Sets the password for authentication to the specified string and returns this request instance.
|
140 | */
|
141 | password(value: string | null): this;
|
142 |
|
143 | /**
|
144 | * Equivalent to `request.send` with the POST method: `request.send("POST")`.
|
145 | */
|
146 | post(): this;
|
147 | /**
|
148 | * Equivalent to `request.send` with the POST method: `request.send("POST", data)`.
|
149 | */
|
150 | post<RequestData>(data: RequestData): this;
|
151 | /**
|
152 | * Equivalent to `request.send` with the POST method: `request.send("POST", callback)`.
|
153 | */
|
154 | post<ResponseData>(callback: (this: this, error: any, d: ResponseData) => void): this;
|
155 | /**
|
156 | * Equivalent to `request.send` with the POST method: `request.send("POST", data, callback)`.
|
157 | */
|
158 | post<RequestData, ResponseData>(
|
159 | data: RequestData,
|
160 | callback: (this: this, error: any, d: ResponseData) => void,
|
161 | ): this;
|
162 |
|
163 | /**
|
164 | * Sets the response value function to the specified function and returns this request instance.
|
165 | * The response value function is used to map the response XMLHttpRequest object to a useful data value.
|
166 | * See the convenience methods `json` and `text` for examples.
|
167 | */
|
168 | response<ResponseData>(callback: (this: this, response: XMLHttpRequest) => ResponseData): this;
|
169 |
|
170 | /**
|
171 | * Returns the current response type, which defaults to `` (the empty string).
|
172 | */
|
173 | responseType(): XMLHttpRequestResponseType | undefined;
|
174 | /**
|
175 | * Sets the response type attribute of the request and returns this request instance. Typical values are: `` (the empty string), `arraybuffer`, `blob`, `document`, and `text`.
|
176 | */
|
177 | responseType(value: XMLHttpRequestResponseType): this;
|
178 |
|
179 | /**
|
180 | * Issues this request using the specified method (such as GET or POST).
|
181 | *
|
182 | * The listeners "load" and "error" should be registered via `request.on`.
|
183 | */
|
184 | send(method: string): this;
|
185 | /**
|
186 | * Issues this request using the specified method (such as GET or POST), posting the specified data in the request body, and returns this request instance.
|
187 | *
|
188 | * The listeners "load" and "error" should be registered via `request.on`.
|
189 | */
|
190 | send<RequestData>(method: string, data: RequestData): this;
|
191 | /**
|
192 | * Issues this request using the specified method (such as GET or POST) and returns this request instance.
|
193 | * The callback will be invoked asynchronously when the request succeeds or fails.
|
194 | * The callback is invoked with two arguments: the error, if any, and the response value.
|
195 | * The response value is undefined if an error occurs.
|
196 | */
|
197 | send<ResponseData>(method: string, callback: (this: this, error: any | null, d: ResponseData | null) => void): this;
|
198 | /**
|
199 | * Issues this request using the specified method (such as GET or POST), posting the specified data in the request body, and returns this request instance.
|
200 | * The callback will be invoked asynchronously when the request succeeds or fails.
|
201 | * The callback is invoked with two arguments: the error, if any, and the response value.
|
202 | * The response value is undefined if an error occurs.
|
203 | */
|
204 | send<RequestData, ResponseData>(
|
205 | method: string,
|
206 | data: RequestData,
|
207 | callback: (this: this, error: any | null, d: ResponseData | null) => void,
|
208 | ): this;
|
209 |
|
210 | /**
|
211 | * Returns the current response timeout, which defaults to 0.
|
212 | */
|
213 | timeout(): number;
|
214 | /**
|
215 | * Sets the timeout attribute of the request to the specified number of milliseconds and returns this request instance.
|
216 | */
|
217 | timeout(value: number): this;
|
218 |
|
219 | /**
|
220 | * Returns the current user name, which defaults to null.
|
221 | */
|
222 | user(): string | null;
|
223 | /**
|
224 | * Sets the user name for authentication to the specified string and returns this request instance.
|
225 | */
|
226 | user(value: string | null): this;
|
227 | }
|
228 |
|
229 | export interface DsvRequest extends Request {
|
230 | row<ParsedRow extends object>(
|
231 | value: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow,
|
232 | ): DsvRequest;
|
233 | }
|
234 |
|
235 | /**
|
236 | * Returns a new request for the CSV file at the specified url with the default mime type `text/csv`.
|
237 | */
|
238 | export function csv(url: string): DsvRequest;
|
239 | /**
|
240 | * Returns a new request for the CSV file at the specified url with the default mime type `text/csv`.
|
241 | * And send a GET request.
|
242 | */
|
243 | export function csv(
|
244 | url: string,
|
245 | callback: (this: DsvRequest, error: any, d: DSVParsedArray<DSVRowString>) => void,
|
246 | ): DsvRequest;
|
247 | /**
|
248 | * Returns a new request for the CSV file at the specified url with the default mime type `text/csv`.
|
249 | * And send a GET request.
|
250 | * Use a row conversion function to map and filter row objects to a more-specific representation; see `dsv.parse` for details.
|
251 | */
|
252 | export function csv<ParsedRow extends object>(
|
253 | url: string,
|
254 | row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow,
|
255 | callback: (this: DsvRequest, error: any, d: DSVParsedArray<ParsedRow>) => void,
|
256 | ): DsvRequest;
|
257 |
|
258 | /**
|
259 | * Returns a new request for the HTML file at the specified url with the default mime type `text/html`. The HTML file is returned as a document fragment.
|
260 | */
|
261 | export function html(url: string): Request;
|
262 | /**
|
263 | * Returns a new request for the HTML file at the specified url with the default mime type `text/html`. The HTML file is returned as a document fragment.
|
264 | * And send a GET request.
|
265 | */
|
266 | export function html(url: string, callback: (this: Request, error: any, d: DocumentFragment) => void): Request;
|
267 |
|
268 | /**
|
269 | * Returns a new request to get the JSON file at the specified url with the default mime type `application/json`.
|
270 | */
|
271 | export function json(url: string): Request;
|
272 | /**
|
273 | * Returns a new request to get the JSON file at the specified url with the default mime type `application/json`.
|
274 | * And send a GET request.
|
275 | */
|
276 | export function json<ParsedObject extends { [key: string]: any }>(
|
277 | url: string,
|
278 | callback: (this: Request, error: any, d: ParsedObject) => void,
|
279 | ): Request;
|
280 |
|
281 | /**
|
282 | * Returns a new request for specified url. The returned request is not yet sent and can be further configured.
|
283 | *
|
284 | * See `d3.json`, `d3.csv`, `d3.tsv`, `d3.text`, `d3.html` and `d3.xml` for content-specific convenience constructors.
|
285 | */
|
286 | export function request(url: string): Request;
|
287 | /**
|
288 | * Returns a new request for specified url. It is equivalent to calling `request.get` immediately after construction: `d3.request(url).get(callback)`.
|
289 | * And send a GET request.
|
290 | *
|
291 | * If you wish to specify a request header or a mime type, you must not specify a callback to the constructor.
|
292 | * Use `request.header` or `request.mimeType` followed by `request.get` instead.
|
293 | *
|
294 | * See `d3.json`, `d3.csv`, `d3.tsv`, `d3.text`, `d3.html` and `d3.xml` for content-specific convenience constructors.
|
295 | */
|
296 | export function request(url: string, callback: (this: Request, error: any, d: XMLHttpRequest) => void): Request;
|
297 |
|
298 | /**
|
299 | * Returns a new request to get the text file at the specified url with the default mime type `text/plain`.
|
300 | */
|
301 | export function text(url: string): Request;
|
302 | /**
|
303 | * Returns a new request to get the text file at the specified url with the default mime type `text/plain`.
|
304 | * And send a GET request.
|
305 | */
|
306 | export function text(url: string, callback: (this: Request, error: any, d: string) => void): Request;
|
307 |
|
308 | /**
|
309 | * Returns a new request for a TSV file at the specified url with the default mime type `text/tab-separated-values`.
|
310 | */
|
311 | export function tsv(url: string): DsvRequest;
|
312 | /**
|
313 | * Returns a new request for a TSV file at the specified url with the default mime type `text/tab-separated-values`.
|
314 | * And send a GET request.
|
315 | */
|
316 | export function tsv(
|
317 | url: string,
|
318 | callback: (this: DsvRequest, error: any, d: DSVParsedArray<DSVRowString>) => void,
|
319 | ): DsvRequest;
|
320 | /**
|
321 | * Returns a new request for a TSV file at the specified url with the default mime type `text/tab-separated-values`.
|
322 | * And send a GET request.
|
323 | * Use a row conversion function to map and filter row objects to a more-specific representation; see `dsv.parse` for details.
|
324 | */
|
325 | export function tsv<ParsedRow extends object>(
|
326 | url: string,
|
327 | row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow,
|
328 | callback: (this: DsvRequest, error: any, d: DSVParsedArray<ParsedRow>) => void,
|
329 | ): DsvRequest;
|
330 |
|
331 | /**
|
332 | * Returns a new request to get the XML file at the specified url with the default mime type `application/xml`.
|
333 | */
|
334 | export function xml(url: string): Request;
|
335 | /**
|
336 | * Returns a new request to get the XML file at the specified url with the default mime type `application/xml`.
|
337 | * And send a GET request.
|
338 | */
|
339 | export function xml(url: string, callback: (this: Request, error: any, d: any) => void): Request;
|
340 |
|
\ | No newline at end of file |