From 2a858618c3f721512c52cbf8bbe943d8b7be4931 Mon Sep 17 00:00:00 2001 From: Hugues Stefanski Date: Mon, 29 Jan 2018 20:44:25 +0100 Subject: [PATCH 1/5] Backbone for d3-fetch --- types/d3-fetch/d3-fetch-tests.ts | 0 types/d3-fetch/index.d.ts | 5 +++++ types/d3-fetch/tsconfig.json | 22 ++++++++++++++++++++++ types/d3-fetch/tslint.json | 1 + 4 files changed, 28 insertions(+) create mode 100644 types/d3-fetch/d3-fetch-tests.ts create mode 100644 types/d3-fetch/index.d.ts create mode 100644 types/d3-fetch/tsconfig.json create mode 100644 types/d3-fetch/tslint.json diff --git a/types/d3-fetch/d3-fetch-tests.ts b/types/d3-fetch/d3-fetch-tests.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/types/d3-fetch/index.d.ts b/types/d3-fetch/index.d.ts new file mode 100644 index 0000000000..f5b195ca04 --- /dev/null +++ b/types/d3-fetch/index.d.ts @@ -0,0 +1,5 @@ +// Type definitions for d3-fetch 1.0.1 +// Project: https://d3js.org/d3-fetch/ +// Definitions by: Hugues Stefanski +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + diff --git a/types/d3-fetch/tsconfig.json b/types/d3-fetch/tsconfig.json new file mode 100644 index 0000000000..d4e382e338 --- /dev/null +++ b/types/d3-fetch/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "d3-fetch-tests.ts" + ] +} diff --git a/types/d3-fetch/tslint.json b/types/d3-fetch/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/d3-fetch/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From d25d2ee1b42624281a7a36bbe2daf7970ecb02a8 Mon Sep 17 00:00:00 2001 From: Hugues Stefanski Date: Mon, 29 Jan 2018 21:12:59 +0100 Subject: [PATCH 2/5] Completed fetch API --- types/d3-fetch/d3-fetch-tests.ts | 15 ++++++++++++ types/d3-fetch/index.d.ts | 42 ++++++++++++++++++++++++++++++++ types/d3-fetch/tsconfig.json | 3 ++- 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/types/d3-fetch/d3-fetch-tests.ts b/types/d3-fetch/d3-fetch-tests.ts index e69de29bb2..b82ea8ffbe 100644 --- a/types/d3-fetch/d3-fetch-tests.ts +++ b/types/d3-fetch/d3-fetch-tests.ts @@ -0,0 +1,15 @@ +import * as d3Fetch from 'd3-fetch'; + +interface MyType { + foo: string; +} + +let promise: Promise; +const url = 'foo.bar'; + +const init: RequestInit = {}; + +promise = d3Fetch.blob(url); +promise = d3Fetch.blob(url, init); +promise = d3Fetch.buffer(url); +promise = d3Fetch.buffer(url, init); diff --git a/types/d3-fetch/index.d.ts b/types/d3-fetch/index.d.ts index f5b195ca04..f46a779003 100644 --- a/types/d3-fetch/index.d.ts +++ b/types/d3-fetch/index.d.ts @@ -3,3 +3,45 @@ // Definitions by: Hugues Stefanski // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/**Fetches the binary file at the specified input URL as a Blob. */ +export function blob(input: string): Promise; +/**Fetches the binary file at the specified input URL as a Blob. Passes init along to the underlying call to fetch. */ +export function blob(input: string, init: RequestInit): Promise; +/**Fetches the binary file at the specified input URL as an ArrayBuffer. */ +export function buffer(input: string): Promise; +/**Fetches the binary file at the specified input URL as an ArrayBuffer. Passes init along to the underlying call to fetch. */ +export function buffer(input: string, init: RequestInit): Promise; + +/**Equivalent to d3.dsv with the comma character as the delimiter. */ +export function csv(input: string): Promise; +/**Equivalent to d3.dsv with the comma character as the delimiter. */ +export function csv(input: string, init: RequestInit): Promise; +/**Equivalent to d3.dsv with the comma character as the delimiter. */ +export function csv(input: string, init: RequestInit, row: (d: any) => any): Promise; + +/**Fetches the DSV file at the specified input URL. */ +export function dsv(delimiter: string, input: string): Promise; +export function dsv(delimiter: string, input: string, init: RequestInit): Promise; +export function dsv(delimiter: string, input: string, init: RequestInit, row: any): Promise; + +/**Fetches the image at the specified input URL. */ +export function image(input: string): Promise; +/**Fetches the image at the specified input URL. Passes init along to the underlying call to fetch. */ +export function image(input: string, init: RequestInit): Promise; + +/**Fetches the json file at the specified input URL. */ +export function json(input: string): Promise; +/**Fetches the json file at the specified input URL. Passes init along to the underlying call to fetch. */ +export function json(input: string, init: RequestInit): Promise; + +/**Fetches the text file at the specified input URL. */ +export function text(input: string): Promise; +/**Fetches the text file at the specified input URL. Passes init along to the underlying call to fetch. */ +export function text(input: string, init: RequestInit): Promise; + +/**Equivalent to d3.dsv with the tab character as the delimiter. */ +export function tsv(input: string): Promise; +/**Equivalent to d3.dsv with the tab character as the delimiter. */ +export function tsv(input: string, init: RequestInit): Promise; +/**Equivalent to d3.dsv with the tab character as the delimiter. */ +export function tsv(input: string, init: RequestInit, row: (d: any) => any): Promise; diff --git a/types/d3-fetch/tsconfig.json b/types/d3-fetch/tsconfig.json index d4e382e338..4d18bd9ade 100644 --- a/types/d3-fetch/tsconfig.json +++ b/types/d3-fetch/tsconfig.json @@ -2,7 +2,8 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6" + "es2017", + "dom" ], "noImplicitAny": true, "noImplicitThis": true, From a1e32e69642cd3ca0f7a80946412e138d5649982 Mon Sep 17 00:00:00 2001 From: Hugues Stefanski Date: Mon, 29 Jan 2018 21:23:56 +0100 Subject: [PATCH 3/5] Strong typings of parsing methods --- types/d3-fetch/index.d.ts | 55 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/types/d3-fetch/index.d.ts b/types/d3-fetch/index.d.ts index f46a779003..6a62630808 100644 --- a/types/d3-fetch/index.d.ts +++ b/types/d3-fetch/index.d.ts @@ -3,45 +3,62 @@ // Definitions by: Hugues Stefanski // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +import { DSVParsedArray, DSVRowString, DSVRowAny } from 'd3-dsv'; + /**Fetches the binary file at the specified input URL as a Blob. */ -export function blob(input: string): Promise; +export function blob(url: string): Promise; /**Fetches the binary file at the specified input URL as a Blob. Passes init along to the underlying call to fetch. */ -export function blob(input: string, init: RequestInit): Promise; +export function blob(url: string, init: RequestInit): Promise; /**Fetches the binary file at the specified input URL as an ArrayBuffer. */ -export function buffer(input: string): Promise; +export function buffer(url: string): Promise; /**Fetches the binary file at the specified input URL as an ArrayBuffer. Passes init along to the underlying call to fetch. */ -export function buffer(input: string, init: RequestInit): Promise; +export function buffer(url: string, init: RequestInit): Promise; /**Equivalent to d3.dsv with the comma character as the delimiter. */ -export function csv(input: string): Promise; +export function csv(url: string): Promise>; /**Equivalent to d3.dsv with the comma character as the delimiter. */ -export function csv(input: string, init: RequestInit): Promise; +export function csv(url: string, init: RequestInit): Promise>; /**Equivalent to d3.dsv with the comma character as the delimiter. */ -export function csv(input: string, init: RequestInit, row: (d: any) => any): Promise; +export function csv( + url: string, + init: RequestInit, + row: (d: DSVRowAny) => ParsedRow +): Promise>; /**Fetches the DSV file at the specified input URL. */ -export function dsv(delimiter: string, input: string): Promise; -export function dsv(delimiter: string, input: string, init: RequestInit): Promise; -export function dsv(delimiter: string, input: string, init: RequestInit, row: any): Promise; +export function dsv(delimiter: string, url: string): Promise>; +/**Fetches the DSV file at the specified input URL. */ +export function dsv(delimiter: string, url: string, init: RequestInit): Promise>; +/**Fetches the DSV file at the specified input URL. */ +export function dsv( + delimiter: string, + url: string, + init: RequestInit, + row: (d: DSVRowAny) => ParsedRow +): Promise>; /**Fetches the image at the specified input URL. */ -export function image(input: string): Promise; +export function image(url: string): Promise; /**Fetches the image at the specified input URL. Passes init along to the underlying call to fetch. */ -export function image(input: string, init: RequestInit): Promise; +export function image(url: string, init: RequestInit): Promise; /**Fetches the json file at the specified input URL. */ -export function json(input: string): Promise; +export function json(url: string): Promise; /**Fetches the json file at the specified input URL. Passes init along to the underlying call to fetch. */ -export function json(input: string, init: RequestInit): Promise; +export function json(url: string, init: RequestInit): Promise; /**Fetches the text file at the specified input URL. */ -export function text(input: string): Promise; +export function text(url: string): Promise; /**Fetches the text file at the specified input URL. Passes init along to the underlying call to fetch. */ -export function text(input: string, init: RequestInit): Promise; +export function text(url: string, init: RequestInit): Promise; /**Equivalent to d3.dsv with the tab character as the delimiter. */ -export function tsv(input: string): Promise; +export function tsv(url: string): Promise>; /**Equivalent to d3.dsv with the tab character as the delimiter. */ -export function tsv(input: string, init: RequestInit): Promise; +export function tsv(url: string, init: RequestInit): Promise>; /**Equivalent to d3.dsv with the tab character as the delimiter. */ -export function tsv(input: string, init: RequestInit, row: (d: any) => any): Promise; +export function tsv( + url: string, + init: RequestInit, + row: (d: DSVRowAny) => ParsedRow +): Promise>; From 1d9a51fd97c2496378fb4507c390aa46ec75db7f Mon Sep 17 00:00:00 2001 From: Hugues Stefanski Date: Mon, 29 Jan 2018 21:29:16 +0100 Subject: [PATCH 4/5] Partial d3-fetch tests --- types/d3-fetch/d3-fetch-tests.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/types/d3-fetch/d3-fetch-tests.ts b/types/d3-fetch/d3-fetch-tests.ts index b82ea8ffbe..9c88ed8674 100644 --- a/types/d3-fetch/d3-fetch-tests.ts +++ b/types/d3-fetch/d3-fetch-tests.ts @@ -13,3 +13,10 @@ promise = d3Fetch.blob(url); promise = d3Fetch.blob(url, init); promise = d3Fetch.buffer(url); promise = d3Fetch.buffer(url, init); + +promise = d3Fetch.image(url); +promise = d3Fetch.image(url, init); +promise = d3Fetch.json(url); +promise = d3Fetch.json(url, init); +let myString: Promise = d3Fetch.text(url); +myString = d3Fetch.text(url, init); From 24c16d91da489001a967cc03e9fdb5915c6ec66b Mon Sep 17 00:00:00 2001 From: Hugues Stefanski Date: Mon, 29 Jan 2018 21:36:08 +0100 Subject: [PATCH 5/5] Completed tests for d3-fetch --- types/d3-fetch/d3-fetch-tests.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/types/d3-fetch/d3-fetch-tests.ts b/types/d3-fetch/d3-fetch-tests.ts index 9c88ed8674..1dfff0e04f 100644 --- a/types/d3-fetch/d3-fetch-tests.ts +++ b/types/d3-fetch/d3-fetch-tests.ts @@ -1,4 +1,5 @@ import * as d3Fetch from 'd3-fetch'; +import { DSVParsedArray, DSVRowString, DSVRowAny } from 'd3-dsv'; interface MyType { foo: string; @@ -13,10 +14,26 @@ promise = d3Fetch.blob(url); promise = d3Fetch.blob(url, init); promise = d3Fetch.buffer(url); promise = d3Fetch.buffer(url, init); - promise = d3Fetch.image(url); promise = d3Fetch.image(url, init); promise = d3Fetch.json(url); promise = d3Fetch.json(url, init); -let myString: Promise = d3Fetch.text(url); + +let myString: Promise; +myString = d3Fetch.text(url); myString = d3Fetch.text(url, init); + +const parseRow = (d: {}) => { + const myType: MyType = { foo: 'foo' }; + return myType; +}; +let promise2: Promise>; +promise2 = d3Fetch.csv(url); +promise2 = d3Fetch.csv(url, init); +promise2 = d3Fetch.csv(url, init, parseRow); +promise2 = d3Fetch.dsv(';', url); +promise2 = d3Fetch.dsv(';', url, init); +promise2 = d3Fetch.dsv(';', url, init, parseRow); +promise2 = d3Fetch.tsv(url); +promise2 = d3Fetch.tsv(url, init); +promise2 = d3Fetch.tsv(url, init, parseRow);