Files
DefinitelyTyped/types/d3-fetch/index.d.ts
Tom Wanzek 133d79faac fix(d3-fetch): update signatures and expand JSDoc comments (#23378)
* fix(d3-fetch):  update signatures and expand JSDoc comments

* fix(image): init object is not request init, but rather initializes image element properties
* fix(json): add generic parameter to allow return type casting
* fix(csv, dsv, tsv): overload signatures to be more specific about return type (with or without row parsing function).
* fix(csv, dsv, tsv): update row parsing function signature to be in line with parser signature in d3-dsv, which is called under the hood.
* chore(tslint): add tslint rules to allow signature overloading and single-use generic
* doc(*) Expand JSDoc comments
* chore(tests): update tests

* chore(d3-fetch): add validation reference patch release version
2018-02-08 07:59:55 -08:00

237 lines
11 KiB
TypeScript

// Type definitions for d3-fetch 1.0
// Project: https://d3js.org/d3-fetch/
// Definitions by: Hugues Stefanski <https://github.com/ledragon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
// Last module patch version validated against: 1.0.1
import { DSVParsedArray, DSVRowString, DSVRowAny } from 'd3-dsv';
/**
* Fetches the binary file at the specified input URL and returns it as a Promise of a Blob.
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function blob(url: string, init?: RequestInit): Promise<Blob>;
/**
* Fetches the binary file at the specified input URL and returns it as a Promise of an ArrayBuffer.
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function buffer(url: string, init?: RequestInit): Promise<ArrayBuffer>;
/**
* Fetches the CSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
* objects are represented as strings.
*
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function csv(
url: string,
init?: RequestInit,
): Promise<DSVParsedArray<DSVRowString>>;
/**
* Fetches the CSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.csvParse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param url A valid URL string.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function csv<ParsedRow extends DSVRowAny>(
url: string,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;
/**
* Fetches the CSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* The init object is passed along to the underlying call to fetch.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.csvParse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param url A valid URL string.
* @param init An request initialization object.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function csv<ParsedRow extends DSVRowAny>(
url: string,
init: RequestInit,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;
/**
* Fetches the DSV file with the specified delimiter character at the specified input URL and returns
* a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
* objects are represented as strings.
*
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param delimiter The delimiter character used in the DSV file to be fetched.
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function dsv(
delimiter: string,
url: string,
init?: RequestInit,
): Promise<DSVParsedArray<DSVRowString>>;
/**
* Fetches the DSV file with the specified delimiter character at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.parse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param delimiter The delimiter character used in the DSV file to be fetched.
* @param url A valid URL string.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function dsv<ParsedRow extends DSVRowAny>(
delimiter: string,
url: string,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;
/**
* Fetches the DSV file with the specified delimiter character at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* The init object is passed along to the underlying call to fetch.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.parse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param delimiter The delimiter character used in the DSV file to be fetched.
* @param url A valid URL string.
* @param init An request initialization object.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function dsv<ParsedRow extends DSVRowAny>(
delimiter: string,
url: string,
init: RequestInit,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;
/**
* Fetches the image at the specified input URL and returns a promise of an HTML image element.
*
* If init is specified, sets any additional properties on the image before loading.
*
* @param url A valid URL string.
* @param init An optional object of image properties to set.
*/
export function image(url: string, init?: {[key: string]: any}): Promise<HTMLImageElement>;
/**
* Fetches the json file at the specified input URL and returns it as a Promise of a parsed JSON object.
*
* If init is specified, it is passed along to the underlying call to fetch.
*
* The generic parameter describes the type of the object parsed from the returned JSON.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function json<ParsedJSONObject extends any>(url: string, init?: RequestInit): Promise<ParsedJSONObject>;
/**
* Fetches the text file at the specified input URL and returns it as a Promise of a string.
*
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function text(url: string, init?: RequestInit): Promise<string>;
/**
* Fetches the TSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* If init is specified, it is passed along to the underlying call to fetch.
*
* @param url A valid URL string.
* @param init An optional request initialization object.
*/
export function tsv(
url: string,
init?: RequestInit,
): Promise<DSVParsedArray<DSVRowString>>;
/**
* Fetches the TSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row
* objects are represented as strings.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.tsvParse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param url A valid URL string.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function tsv<ParsedRow extends DSVRowAny>(
url: string,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;
/**
* Fetches the TSV file at the specified input URL and returns
* a promise of an array of objects representing the parsed rows.
*
* The init object is passed along to the underlying call to fetch.
*
* The specified row conversion function is used to map and filter row objects to a more-specific representation;
* see dsv.tsvParse for details.
*
* The generic parameter describes the type of the object representation of a parsed row.
*
* @param url A valid URL string.
* @param init An request initialization object.
* @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
* 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,
* the row is skipped and will be ommitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object.
* In effect, row is similar to applying a map and filter operator to the returned rows.
*/
export function tsv<ParsedRow extends DSVRowAny>(
url: string,
init: RequestInit,
row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null
): Promise<DSVParsedArray<ParsedRow>>;