UNPKG

14.1 kBTypeScriptView Raw
1// Type definitions for d3-fetch 2.0
2// Project: https://d3js.org/d3-fetch/
3// Definitions by: Hugues Stefanski <https://github.com/ledragon>
4// denisname <https://github.com/denisname>
5// Nathan Bierema <https://github.com/Methuselah96>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.3
8
9// Last module patch version validated against: 2.0.0
10
11import { DSVParsedArray, DSVRowArray, DSVRowString } from "d3-dsv";
12
13/**
14 * Fetches the binary file at the specified input URL and returns it as a Promise of a Blob.
15 * If init is specified, it is passed along to the underlying call to fetch.
16 *
17 * @param url A valid URL string.
18 * @param init An optional request initialization object.
19 */
20export function blob(url: string, init?: RequestInit): Promise<Blob>;
21
22/**
23 * Fetches the binary file at the specified input URL and returns it as a Promise of an ArrayBuffer.
24 * If init is specified, it is passed along to the underlying call to fetch.
25 *
26 * @param url A valid URL string.
27 * @param init An optional request initialization object.
28 */
29export function buffer(url: string, init?: RequestInit): Promise<ArrayBuffer>;
30
31/**
32 * Fetches the CSV file at the specified input URL and returns
33 * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
34 * objects are represented as strings.
35 *
36 * If init is specified, it is passed along to the underlying call to fetch.
37 *
38 * The generic parameter describes the column names as a union of string literal types.
39 *
40 * @param url A valid URL string.
41 * @param init An optional request initialization object.
42 */
43export function csv<Columns extends string>(
44 url: string,
45 init?: RequestInit
46): Promise<DSVRowArray<Columns>>;
47/**
48 * Fetches the CSV file at the specified input URL and returns
49 * a promise of an array of objects representing the parsed rows.
50 *
51 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
52 * see dsv.csvParse for details.
53 *
54 * The first generic parameter describes the type of the object representation of a parsed row.
55 * The second generic parameter describes the column names as a union of string literal types.
56 *
57 * @param url A valid URL string.
58 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
59 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
60 * the row is skipped and will be omitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object.
61 * In effect, row is similar to applying a map and filter operator to the returned rows.
62 */
63export function csv<ParsedRow extends object, Columns extends string = string>(
64 url: string,
65 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
66): Promise<DSVParsedArray<ParsedRow>>;
67/**
68 * Fetches the CSV file at the specified input URL and returns
69 * a promise of an array of objects representing the parsed rows.
70 *
71 * The init object is passed along to the underlying call to fetch.
72 *
73 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
74 * see dsv.csvParse for details.
75 *
76 * The first generic parameter describes the type of the object representation of a parsed row.
77 * The second generic parameter describes the column names as a union of string literal types.
78 *
79 * @param url A valid URL string.
80 * @param init An request initialization object.
81 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
82 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
83 * the row is skipped and will be omitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object.
84 * In effect, row is similar to applying a map and filter operator to the returned rows.
85 */
86export function csv<ParsedRow extends object, Columns extends string = string>(
87 url: string,
88 init: RequestInit,
89 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
90): Promise<DSVParsedArray<ParsedRow>>;
91
92/**
93 * Fetches the DSV file with the specified delimiter character at the specified input URL and returns
94 * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
95 * objects are represented as strings.
96 *
97 * If init is specified, it is passed along to the underlying call to fetch.
98 *
99 * The generic parameter describes the column names as a union of string literal types.
100 *
101 * @param delimiter The delimiter character used in the DSV file to be fetched.
102 * @param url A valid URL string.
103 * @param init An optional request initialization object.
104 */
105export function dsv<Columns extends string>(
106 delimiter: string,
107 url: string,
108 init?: RequestInit
109): Promise<DSVRowArray<Columns>>;
110/**
111 * Fetches the DSV file with the specified delimiter character at the specified input URL and returns
112 * a promise of an array of objects representing the parsed rows.
113 *
114 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
115 * see dsv.parse for details.
116 *
117 * The first generic parameter describes the type of the object representation of a parsed row.
118 * The second generic parameter describes the column names as a union of string literal types.
119 *
120 * @param delimiter The delimiter character used in the DSV file to be fetched.
121 * @param url A valid URL string.
122 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
123 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
124 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
125 * In effect, row is similar to applying a map and filter operator to the returned rows.
126 */
127export function dsv<ParsedRow extends object, Columns extends string = string>(
128 delimiter: string,
129 url: string,
130 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
131): Promise<DSVParsedArray<ParsedRow>>;
132/**
133 * Fetches the DSV file with the specified delimiter character at the specified input URL and returns
134 * a promise of an array of objects representing the parsed rows.
135 *
136 * The init object is passed along to the underlying call to fetch.
137 *
138 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
139 * see dsv.parse for details.
140 *
141 * The first generic parameter describes the type of the object representation of a parsed row.
142 * The second generic parameter describes the column names as a union of string literal types.
143 *
144 * @param delimiter The delimiter character used in the DSV file to be fetched.
145 * @param url A valid URL string.
146 * @param init An request initialization object.
147 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
148 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
149 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
150 * In effect, row is similar to applying a map and filter operator to the returned rows.
151 */
152export function dsv<ParsedRow extends object, Columns extends string = string>(
153 delimiter: string,
154 url: string,
155 init: RequestInit,
156 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
157): Promise<DSVParsedArray<ParsedRow>>;
158
159/**
160 * Fetches the file at the specified input URL as text, parses it as HTML and returns a Promise of an HTML DOM Document.
161 *
162 * If init is specified, it is passed along to the underlying call to fetch.
163 *
164 * @param url A valid URL string.
165 * @param init An optional request initialization object.
166 */
167export function html(url: string, init?: RequestInit): Promise<Document>;
168
169/**
170 * Fetches the image at the specified input URL and returns a promise of an HTML image element.
171 *
172 * If init is specified, sets any additional properties on the image before loading.
173 *
174 * @param url A valid URL string.
175 * @param init An optional object of image properties to set.
176 */
177export function image(url: string, init?: Partial<HTMLImageElement>): Promise<HTMLImageElement>;
178
179/**
180 * Fetches the json file at the specified input URL and returns it as a Promise of a parsed JSON object.
181 *
182 * If init is specified, it is passed along to the underlying call to fetch.
183 *
184 * If the server returns a status code of [204 No Content](https://developer.mozilla.org/docs/Web/HTTP/Status/204)
185 * or [205 Reset Content](https://developer.mozilla.org/docs/Web/HTTP/Status/205), the promise resolves to `undefined`.
186 *
187 * The generic parameter describes the type of the object parsed from the returned JSON.
188 *
189 * @param url A valid URL string.
190 * @param init An optional request initialization object.
191 */
192export function json<ParsedJSONObject extends any>(url: string, init?: RequestInit): Promise<ParsedJSONObject | undefined>;
193
194/**
195 * Fetches the file at the specified input URL as text, parses it as SVG and returns a Promise of an SVG Document.
196 *
197 * If init is specified, it is passed along to the underlying call to fetch.
198 *
199 * @param url A valid URL string.
200 * @param init An optional request initialization object.
201 */
202export function svg(url: string, init?: RequestInit): Promise<Document>;
203
204/**
205 * Fetches the text file at the specified input URL and returns it as a Promise of a string.
206 *
207 * If init is specified, it is passed along to the underlying call to fetch.
208 *
209 * @param url A valid URL string.
210 * @param init An optional request initialization object.
211 */
212export function text(url: string, init?: RequestInit): Promise<string>;
213
214/**
215 * Fetches the TSV file at the specified input URL and returns
216 * a promise of an array of objects representing the parsed rows.
217 *
218 * If init is specified, it is passed along to the underlying call to fetch.
219 *
220 * The generic parameter describes the column names as a union of string literal types.
221 *
222 * @param url A valid URL string.
223 * @param init An optional request initialization object.
224 */
225export function tsv<Columns extends string>(
226 url: string,
227 init?: RequestInit
228): Promise<DSVRowArray<Columns>>;
229/**
230 * Fetches the TSV file at the specified input URL and returns
231 * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
232 * objects are represented as strings.
233 *
234 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
235 * see dsv.tsvParse for details.
236 *
237 * The first generic parameter describes the type of the object representation of a parsed row.
238 * The second generic parameter describes the column names as a union of string literal types.
239 *
240 * @param url A valid URL string.
241 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
242 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
243 * the row is skipped and will be omitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object.
244 * In effect, row is similar to applying a map and filter operator to the returned rows.
245 */
246export function tsv<ParsedRow extends object, Columns extends string = string>(
247 url: string,
248 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
249): Promise<DSVParsedArray<ParsedRow>>;
250/**
251 * Fetches the TSV file at the specified input URL and returns
252 * a promise of an array of objects representing the parsed rows.
253 *
254 * The init object is passed along to the underlying call to fetch.
255 *
256 * The specified row conversion function is used to map and filter row objects to a more-specific representation;
257 * see dsv.tsvParse for details.
258 *
259 * The first generic parameter describes the type of the object representation of a parsed row.
260 * The second generic parameter describes the column names as a union of string literal types.
261 *
262 * @param url A valid URL string.
263 * @param init An request initialization object.
264 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
265 * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined,
266 * the row is skipped and will be omitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object.
267 * In effect, row is similar to applying a map and filter operator to the returned rows.
268 */
269export function tsv<ParsedRow extends object, Columns extends string = string>(
270 url: string,
271 init: RequestInit,
272 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
273): Promise<DSVParsedArray<ParsedRow>>;
274
275/**
276 * Fetches the file at the specified input URL as text, parses it as XML and returns a Promise of an XML Document.
277 *
278 * If init is specified, it is passed along to the underlying call to fetch.
279 *
280 * @param url A valid URL string.
281 * @param init An optional request initialization object.
282 */
283export function xml(url: string, init?: RequestInit): Promise<XMLDocument>;