From a3a294fb4fe220ccd6ee34db73d823be1e33da39 Mon Sep 17 00:00:00 2001 From: Ken Sheedlo Date: Sun, 5 Apr 2015 19:46:19 -0700 Subject: [PATCH] Add support for headers as object in whatwg-fetch The current typing supports passing headers through the Headers class, but the spec also supports passing headers as an object mapping header names to header values. See the [Github example](https://github.com/github/fetch#post-json). --- whatwg-fetch/whatwg-fetch-tests.ts | 12 +++++++++++- whatwg-fetch/whatwg-fetch.d.ts | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/whatwg-fetch/whatwg-fetch-tests.ts b/whatwg-fetch/whatwg-fetch-tests.ts index cc3e6320af..5248f5d1dd 100644 --- a/whatwg-fetch/whatwg-fetch-tests.ts +++ b/whatwg-fetch/whatwg-fetch-tests.ts @@ -11,6 +11,16 @@ function test_fetchUrlWithOptions() { handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); } +function test_fetchUrlWithHeadersObject() { + var requestOptions: RequestInit = { + method: "POST", + headers: { + 'Content-Type': 'application/json' + } + }; + handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions)); +} + function test_fetchUrl() { handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php")); } @@ -21,4 +31,4 @@ function handlePromise(promise: Promise) { }).then((text) => { console.log(text); }); -} \ No newline at end of file +} diff --git a/whatwg-fetch/whatwg-fetch.d.ts b/whatwg-fetch/whatwg-fetch.d.ts index d847463bdf..4a4a9612c3 100644 --- a/whatwg-fetch/whatwg-fetch.d.ts +++ b/whatwg-fetch/whatwg-fetch.d.ts @@ -19,7 +19,7 @@ declare class Request { interface RequestInit { method?: string; - headers?: HeaderInit; + headers?: HeaderInit|{ [index: string]: string }; body?: BodyInit; mode?: RequestMode; credentials?: RequestCredentials; @@ -81,4 +81,4 @@ declare type RequestInfo = Request|string; interface Window { fetch(url: string, init?: RequestInit): Promise; -} \ No newline at end of file +}