Fix isomorphic-fetch module setup (#9144)

This commit is contained in:
Leon Yu
2016-05-05 13:14:33 -04:00
committed by Masahiro Wakame
parent 6b44986641
commit 716ab1bdda
2 changed files with 98 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
/// <reference path="isomorphic-fetch.d.ts"/>
function test_isomorphicFetchTestCases() {
import fetchImportedViaCommonJS = require('isomorphic-fetch');
import * as fetchImportedViaES6Module from 'isomorphic-fetch';
function test_isomorphicFetchTestCases_ambient() {
expectSuccess(fetch('http://localhost:3000/good'), 'Good response');
fetch('http://localhost:3000/bad')
@@ -11,7 +14,30 @@ function test_isomorphicFetchTestCases() {
});
}
function test_whatwgTestCases() {
function test_isomorphicFetchTestCases_commonjs() {
expectSuccess(fetchImportedViaCommonJS('http://localhost:3000/good'), 'Good response');
fetchImportedViaCommonJS('http://localhost:3000/bad')
.then((response: IResponse) => {
return response.text();
})
.catch((err) => {
});
}
function test_isomorphicFetchTestCases_es6() {
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/good'), 'Good response');
fetchImportedViaES6Module('http://localhost:3000/bad')
.then((response: IResponse) => {
return response.text();
})
.catch((err) => {
});
}
function test_whatwgTestCases_ambient() {
var headers = new Headers();
headers.append("Content-Type", "application/json");
var requestOptions: RequestInit = {
@@ -43,6 +69,72 @@ function test_whatwgTestCases() {
expectSuccess(fetch(request), 'Post response:');
}
function test_whatwgTestCases_commonjs() {
var headers = new Headers();
headers.append("Content-Type", "application/json");
var requestOptions: RequestInit = {
method: "POST",
headers: headers,
mode: 'same-origin',
credentials: 'omit',
cache: 'default'
};
expectSuccess(fetchImportedViaCommonJS('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
expectSuccess(fetchImportedViaCommonJS('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
var request: Request = new Request('http://localhost:3000/poster', requestOptions);
expectSuccess(fetchImportedViaCommonJS(request), 'Post response:');
}
function test_whatwgTestCases_es6() {
var headers = new Headers();
headers.append("Content-Type", "application/json");
var requestOptions: RequestInit = {
method: "POST",
headers: headers,
mode: 'same-origin',
credentials: 'omit',
cache: 'default'
};
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
expectSuccess(fetchImportedViaES6Module('http://localhost:3000/poster', requestOptions), 'Post response:');
var requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
var request: Request = new Request('http://localhost:3000/poster', requestOptions);
expectSuccess(fetchImportedViaES6Module(request), 'Post response:');
}
function expectSuccess(promise: Promise<IResponse>, responseText: string) {
promise.then((response: IResponse) => {

View File

@@ -112,8 +112,8 @@ interface IFetchStatic {
(url: string | IRequest, init?: RequestInit): Promise<IResponse>;
}
declare module "isomorphic-fetch" {
export default IFetchStatic;
}
declare var fetch: IFetchStatic;
declare module "isomorphic-fetch" {
export = fetch;
}