UNPKG

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