From 54a8a56af19d2200de348257c59adc4ceb323a30 Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Mon, 30 May 2016 23:00:07 +0200 Subject: [PATCH 1/5] add node-fetch based on whatwg-fetch (#8931) --- node-fetch/node-fetch-tests.ts | 60 ++++++++++++++++++++++ node-fetch/node-fetch.d.ts | 91 ++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 node-fetch/node-fetch-tests.ts create mode 100644 node-fetch/node-fetch.d.ts diff --git a/node-fetch/node-fetch-tests.ts b/node-fetch/node-fetch-tests.ts new file mode 100644 index 0000000000..3188d319bc --- /dev/null +++ b/node-fetch/node-fetch-tests.ts @@ -0,0 +1,60 @@ +/// + +import fetch = require('node-fetch'); + +function test_fetchUrlWithOptions() { + var headers = new Headers(); + headers.append("Content-Type", "application/json"); + var requestOptions: RequestInit = { + method: "POST", + headers: headers, + mode: 'same-origin', + credentials: 'omit', + cache: 'default', + redirect: 'manual' + }; + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + +function test_fetchUrlWithHeadersObject() { + var requestOptions: RequestInit = { + method: "POST", + headers: { + 'Content-Type': 'application/json' + } + }; + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + +function test_fetchUrl() { + handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php")); +} + +function test_fetchUrlWithRequestObject() { + var requestOptions: RequestInit = { + method: "POST", + headers: { + 'Content-Type': 'application/json' + } + }; + var request: Request = new Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); + handlePromise(fetch(request)); +} + +function test_globalFetchVar() { + fetch('http://test.com', {}) + .then(response => { + // for test only + }); +} + +function handlePromise(promise: Promise) { + promise.then((response) => { + if (response.type === 'basic') { + // for test only + } + return response.text(); + }).then((text) => { + console.log(text); + }); +} diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts new file mode 100644 index 0000000000..eeaf049fcf --- /dev/null +++ b/node-fetch/node-fetch.d.ts @@ -0,0 +1,91 @@ +// Type definitions for fetch API +// Project: https://github.com/github/fetch +// Definitions by: Ryan Graham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class Request extends Body { + constructor(input: string|Request, init?:RequestInit); + method: string; + url: string; + headers: Headers; + context: RequestContext; + referrer: string; + mode: RequestMode; + redirect: RequestRedirect; + credentials: RequestCredentials; + cache: RequestCache; +} + +interface RequestInit { + method?: string; + headers?: HeaderInit|{ [index: string]: string }; + body?: BodyInit; + mode?: RequestMode; + redirect?: RequestRedirect; + credentials?: RequestCredentials; + cache?: RequestCache; +} + +type RequestContext = + "audio" | "beacon" | "cspreport" | "download" | "embed" | + "eventsource" | "favicon" | "fetch" | "font" | "form" | "frame" | + "hyperlink" | "iframe" | "image" | "imageset" | "import" | + "internal" | "location" | "manifest" | "object" | "ping" | "plugin" | + "prefetch" | "script" | "serviceworker" | "sharedworker" | + "subresource" | "style" | "track" | "video" | "worker" | + "xmlhttprequest" | "xslt"; +type RequestMode = "same-origin" | "no-cors" | "cors"; +type RequestRedirect = "follow" | "error" | "manual"; +type RequestCredentials = "omit" | "same-origin" | "include"; +type RequestCache = + "default" | "no-store" | "reload" | "no-cache" | + "force-cache" | "only-if-cached"; + +declare class Headers { + append(name: string, value: string): void; + delete(name: string):void; + get(name: string): string; + getAll(name: string): Array; + has(name: string): boolean; + set(name: string, value: string): void; + forEach(callback: (value: string, name: string) => void): void; +} + +declare class Body { + bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + formData(): Promise; + json(): Promise; + json(): Promise; + text(): Promise; +} +declare class Response extends Body { + constructor(body?: BodyInit, init?: ResponseInit); + static error(): Response; + static redirect(url: string, status: number): Response; + type: ResponseType; + url: string; + status: number; + ok: boolean; + statusText: string; + headers: Headers; + clone(): Response; +} + +type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; + +interface ResponseInit { + status: number; + statusText?: string; + headers?: HeaderInit; +} + +declare type HeaderInit = Headers|Array; +declare type BodyInit = ArrayBuffer|ArrayBufferView|Blob|FormData|string; +declare type RequestInfo = Request|string; + +declare module 'node-fetch' { + function fetch(url: string | Request, init?: RequestInit): Promise; + export = fetch; +} From a0599f53fc22c36817ebc5161081e6c316b1ba63 Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Tue, 31 May 2016 07:33:51 +0200 Subject: [PATCH 2/5] add myself to comment in node-fetch.d.ts --- node-fetch/node-fetch.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts index eeaf049fcf..e91ac99904 100644 --- a/node-fetch/node-fetch.d.ts +++ b/node-fetch/node-fetch.d.ts @@ -1,6 +1,7 @@ // Type definitions for fetch API // Project: https://github.com/github/fetch -// Definitions by: Ryan Graham +// Definitions by: Torsten Werner based on whatwg-fetch +// that was created by: Ryan Graham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare class Request extends Body { From 8af4eb0036e33dc5f895b4f1f330feb2369fa318 Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Tue, 31 May 2016 08:17:45 +0200 Subject: [PATCH 3/5] node-fetch.d.ts: fix comment --- node-fetch/node-fetch.d.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts index e91ac99904..4e61f6bcd2 100644 --- a/node-fetch/node-fetch.d.ts +++ b/node-fetch/node-fetch.d.ts @@ -1,7 +1,6 @@ -// Type definitions for fetch API -// Project: https://github.com/github/fetch -// Definitions by: Torsten Werner based on whatwg-fetch -// that was created by: Ryan Graham +// Type definitions for node-fetch based on whatwg-fetch +// Project: https://github.com/bitinn/node-fetch +// Definitions by: Torsten Werner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare class Request extends Body { From a6b828e1480a7f7f459840cb4c434988c6d0c39e Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Thu, 2 Jun 2016 19:45:57 +0200 Subject: [PATCH 4/5] node-fetch: reduce global namespace pollution --- node-fetch/node-fetch.d.ts | 165 +++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 82 deletions(-) diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts index 4e61f6bcd2..5934328aa6 100644 --- a/node-fetch/node-fetch.d.ts +++ b/node-fetch/node-fetch.d.ts @@ -3,89 +3,90 @@ // Definitions by: Torsten Werner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare class Request extends Body { - constructor(input: string|Request, init?:RequestInit); - method: string; - url: string; - headers: Headers; - context: RequestContext; - referrer: string; - mode: RequestMode; - redirect: RequestRedirect; - credentials: RequestCredentials; - cache: RequestCache; -} - -interface RequestInit { - method?: string; - headers?: HeaderInit|{ [index: string]: string }; - body?: BodyInit; - mode?: RequestMode; - redirect?: RequestRedirect; - credentials?: RequestCredentials; - cache?: RequestCache; -} - -type RequestContext = - "audio" | "beacon" | "cspreport" | "download" | "embed" | - "eventsource" | "favicon" | "fetch" | "font" | "form" | "frame" | - "hyperlink" | "iframe" | "image" | "imageset" | "import" | - "internal" | "location" | "manifest" | "object" | "ping" | "plugin" | - "prefetch" | "script" | "serviceworker" | "sharedworker" | - "subresource" | "style" | "track" | "video" | "worker" | - "xmlhttprequest" | "xslt"; -type RequestMode = "same-origin" | "no-cors" | "cors"; -type RequestRedirect = "follow" | "error" | "manual"; -type RequestCredentials = "omit" | "same-origin" | "include"; -type RequestCache = - "default" | "no-store" | "reload" | "no-cache" | - "force-cache" | "only-if-cached"; - -declare class Headers { - append(name: string, value: string): void; - delete(name: string):void; - get(name: string): string; - getAll(name: string): Array; - has(name: string): boolean; - set(name: string, value: string): void; - forEach(callback: (value: string, name: string) => void): void; -} - -declare class Body { - bodyUsed: boolean; - arrayBuffer(): Promise; - blob(): Promise; - formData(): Promise; - json(): Promise; - json(): Promise; - text(): Promise; -} -declare class Response extends Body { - constructor(body?: BodyInit, init?: ResponseInit); - static error(): Response; - static redirect(url: string, status: number): Response; - type: ResponseType; - url: string; - status: number; - ok: boolean; - statusText: string; - headers: Headers; - clone(): Response; -} - -type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; - -interface ResponseInit { - status: number; - statusText?: string; - headers?: HeaderInit; -} - -declare type HeaderInit = Headers|Array; -declare type BodyInit = ArrayBuffer|ArrayBufferView|Blob|FormData|string; -declare type RequestInfo = Request|string; - declare module 'node-fetch' { + + class Request extends Body { + constructor(input: string | Request, init?: RequestInit); + method: string; + url: string; + headers: Headers; + context: RequestContext; + referrer: string; + mode: RequestMode; + redirect: RequestRedirect; + credentials: RequestCredentials; + cache: RequestCache; + } + + interface RequestInit { + method?: string; + headers?: HeaderInit | { [index: string]: string }; + body?: BodyInit; + mode?: RequestMode; + redirect?: RequestRedirect; + credentials?: RequestCredentials; + cache?: RequestCache; + } + + type RequestContext = + "audio" | "beacon" | "cspreport" | "download" | "embed" | + "eventsource" | "favicon" | "fetch" | "font" | "form" | "frame" | + "hyperlink" | "iframe" | "image" | "imageset" | "import" | + "internal" | "location" | "manifest" | "object" | "ping" | "plugin" | + "prefetch" | "script" | "serviceworker" | "sharedworker" | + "subresource" | "style" | "track" | "video" | "worker" | + "xmlhttprequest" | "xslt"; + type RequestMode = "same-origin" | "no-cors" | "cors"; + type RequestRedirect = "follow" | "error" | "manual"; + type RequestCredentials = "omit" | "same-origin" | "include"; + type RequestCache = + "default" | "no-store" | "reload" | "no-cache" | + "force-cache" | "only-if-cached"; + + class Headers { + append(name: string, value: string): void; + delete(name: string): void; + get(name: string): string; + getAll(name: string): Array; + has(name: string): boolean; + set(name: string, value: string): void; + forEach(callback: (value: string, name: string) => void): void; + } + + class Body { + bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + formData(): Promise; + json(): Promise; + json(): Promise; + text(): Promise; + } + class Response extends Body { + constructor(body?: BodyInit, init?: ResponseInit); + static error(): Response; + static redirect(url: string, status: number): Response; + type: ResponseType; + url: string; + status: number; + ok: boolean; + statusText: string; + headers: Headers; + clone(): Response; + } + + type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"; + + interface ResponseInit { + status: number; + statusText?: string; + headers?: HeaderInit; + } + + type HeaderInit = Headers | Array; + type BodyInit = ArrayBuffer | ArrayBufferView | Blob | FormData | string; + type RequestInfo = Request | string; + function fetch(url: string | Request, init?: RequestInit): Promise; export = fetch; } From 83c31efdd69ef4668abe8fecc0b957300d93e4cd Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Tue, 7 Jun 2016 22:51:25 +0200 Subject: [PATCH 5/5] node-fetch: introduce namespace _fetch for tests --- node-fetch/node-fetch-tests.ts | 12 ++++++------ node-fetch/node-fetch.d.ts | 7 +++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/node-fetch/node-fetch-tests.ts b/node-fetch/node-fetch-tests.ts index 3188d319bc..28d7ff7766 100644 --- a/node-fetch/node-fetch-tests.ts +++ b/node-fetch/node-fetch-tests.ts @@ -3,9 +3,9 @@ import fetch = require('node-fetch'); function test_fetchUrlWithOptions() { - var headers = new Headers(); + var headers = new _fetch.Headers(); headers.append("Content-Type", "application/json"); - var requestOptions: RequestInit = { + var requestOptions: _fetch.RequestInit = { method: "POST", headers: headers, mode: 'same-origin', @@ -17,7 +17,7 @@ function test_fetchUrlWithOptions() { } function test_fetchUrlWithHeadersObject() { - var requestOptions: RequestInit = { + var requestOptions: _fetch.RequestInit = { method: "POST", headers: { 'Content-Type': 'application/json' @@ -31,13 +31,13 @@ function test_fetchUrl() { } function test_fetchUrlWithRequestObject() { - var requestOptions: RequestInit = { + var requestOptions: _fetch.RequestInit = { method: "POST", headers: { 'Content-Type': 'application/json' } }; - var request: Request = new Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); + var request: _fetch.Request = new _fetch.Request("http://www.andlabs.net/html5/uCOR.php", requestOptions); handlePromise(fetch(request)); } @@ -48,7 +48,7 @@ function test_globalFetchVar() { }); } -function handlePromise(promise: Promise) { +function handlePromise(promise: Promise<_fetch.Response>) { promise.then((response) => { if (response.type === 'basic') { // for test only diff --git a/node-fetch/node-fetch.d.ts b/node-fetch/node-fetch.d.ts index 5934328aa6..5cf41ea992 100644 --- a/node-fetch/node-fetch.d.ts +++ b/node-fetch/node-fetch.d.ts @@ -3,7 +3,7 @@ // Definitions by: Torsten Werner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module 'node-fetch' { +declare module _fetch { class Request extends Body { constructor(input: string | Request, init?: RequestInit); @@ -88,5 +88,8 @@ declare module 'node-fetch' { type RequestInfo = Request | string; function fetch(url: string | Request, init?: RequestInit): Promise; - export = fetch; +} + +declare module "node-fetch" { + export = _fetch.fetch; }