From 64628c14ec4d063e39e40da4c7f10065675cf84e Mon Sep 17 00:00:00 2001 From: ryan-codingintrigue Date: Tue, 17 Mar 2015 10:19:14 +0000 Subject: [PATCH] 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