From 64628c14ec4d063e39e40da4c7f10065675cf84e Mon Sep 17 00:00:00 2001 From: ryan-codingintrigue Date: Tue, 17 Mar 2015 10:19:14 +0000 Subject: [PATCH 1/4] Add support for GitHub's Fetch API polyfill --- fetch/fetch-tests.ts | 24 ++++++++++++ fetch/fetch.d.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 fetch/fetch-tests.ts create mode 100644 fetch/fetch.d.ts diff --git a/fetch/fetch-tests.ts b/fetch/fetch-tests.ts new file mode 100644 index 000000000..3d9f71e76 --- /dev/null +++ b/fetch/fetch-tests.ts @@ -0,0 +1,24 @@ +/// +/// + +function test_fetchUrlWithOptions() { + var headers = new Headers(); + headers.append("Content-Type", "application/json"); + var requestOptions: RequestInit = { + method: "POST", + headers: headers + }; + handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + +function test_fetchUrl() { + handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php")); +} + +function handlePromise(promise: Promise) { + promise.then((response) => { + return response.text(); + }).then((text) => { + console.log(text); + }); +} \ No newline at end of file diff --git a/fetch/fetch.d.ts b/fetch/fetch.d.ts new file mode 100644 index 000000000..5d39d2a56 --- /dev/null +++ b/fetch/fetch.d.ts @@ -0,0 +1,89 @@ +// Type definitions for fetch API +// Project: https://github.com/github/fetch +// Definitions by: Ryan Graham +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare class Request { + constructor(input: string|Request, init?:RequestInit); + method: string; + url: string; + headers: Headers; + context: RequestContext; + referrer: string; + mode: RequestMode; + credentials: RequestCredentials; + cache: RequestCache; +} + +interface RequestInit { + method?: string; + headers?: HeaderInit; + body?: BodyInit; + mode?: RequestMode; + credentials?: RequestCredentials; + cache?: RequestCache; +} + +declare enum 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" +} +declare enum RequestMode { "same-origin", "no-cors", "cors" } +declare enum RequestCredentials { "omit", "same-origin", "include" } +declare enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" } + +declare class Headers implements TypeScript.Iterator { + 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; + + moveNext(): boolean; + + current(): string; +} + +declare class Body { + bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + formData(): Promise; + json(): Promise; + text(): Promise; +} +declare class Response extends Body { + constructor(body?: BodyInit, init?: ResponseInit); + error(): Response; + redirect(url: string, status: number): Response; + type: ResponseType; + url: string; + status: number; + ok: boolean; + statusText: string; + headers: Headers; + clone(): Response; +} + +declare enum ResponseType { "basic", "cors", "default", "error", "opaque" } + +declare class ResponseInit { + status: number; + statusText: string; + headers: HeaderInit; +} + +declare type HeaderInit = Headers|Array; +declare type BodyInit = Blob|FormData|string; +declare type RequestInfo = Request|string; + +interface Window { + fetch(url: string, init?: RequestInit): Promise; +} \ No newline at end of file From 7a3cda0271384cbff8ce7f3d804c865c72622037 Mon Sep 17 00:00:00 2001 From: ryan-codingintrigue Date: Tue, 17 Mar 2015 18:38:47 +0000 Subject: [PATCH 2/4] Fixed casing on typescriptServices import --- fetch/fetch.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch/fetch.d.ts b/fetch/fetch.d.ts index 5d39d2a56..18340110a 100644 --- a/fetch/fetch.d.ts +++ b/fetch/fetch.d.ts @@ -3,7 +3,7 @@ // Definitions by: Ryan Graham // Definitions: https://github.com/borisyankov/DefinitelyTyped -/// +/// /// declare class Request { From 45979048d52f62f1f54195b33b36b01d759c20b1 Mon Sep 17 00:00:00 2001 From: ryan-codingintrigue Date: Sat, 21 Mar 2015 06:56:38 +0000 Subject: [PATCH 3/4] Renamed to whatwg-fetch --- fetch/fetch-tests.ts => whatwg-fetch/whatwg-fetch-tests.ts | 2 +- fetch/fetch.d.ts => whatwg-fetch/whatwg-fetch.d.ts | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename fetch/fetch-tests.ts => whatwg-fetch/whatwg-fetch-tests.ts (92%) rename fetch/fetch.d.ts => whatwg-fetch/whatwg-fetch.d.ts (100%) diff --git a/fetch/fetch-tests.ts b/whatwg-fetch/whatwg-fetch-tests.ts similarity index 92% rename from fetch/fetch-tests.ts rename to whatwg-fetch/whatwg-fetch-tests.ts index 3d9f71e76..cc3e6320a 100644 --- a/fetch/fetch-tests.ts +++ b/whatwg-fetch/whatwg-fetch-tests.ts @@ -1,4 +1,4 @@ -/// +/// /// function test_fetchUrlWithOptions() { diff --git a/fetch/fetch.d.ts b/whatwg-fetch/whatwg-fetch.d.ts similarity index 100% rename from fetch/fetch.d.ts rename to whatwg-fetch/whatwg-fetch.d.ts From 54d3f5068568fb304e38492864c431e67d522c23 Mon Sep 17 00:00:00 2001 From: ryan-codingintrigue Date: Tue, 24 Mar 2015 15:47:47 +0000 Subject: [PATCH 4/4] Removed unnecessary reference to typescriptServices --- whatwg-fetch/whatwg-fetch.d.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/whatwg-fetch/whatwg-fetch.d.ts b/whatwg-fetch/whatwg-fetch.d.ts index 18340110a..d847463bd 100644 --- a/whatwg-fetch/whatwg-fetch.d.ts +++ b/whatwg-fetch/whatwg-fetch.d.ts @@ -3,7 +3,6 @@ // Definitions by: Ryan Graham // Definitions: https://github.com/borisyankov/DefinitelyTyped -/// /// declare class Request { @@ -38,17 +37,13 @@ declare enum RequestMode { "same-origin", "no-cors", "cors" } declare enum RequestCredentials { "omit", "same-origin", "include" } declare enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" } -declare class Headers implements TypeScript.Iterator { +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; - - moveNext(): boolean; - - current(): string; } declare class Body {