DefinitelyTyped/node-fetch/node-fetch-tests.ts
Andy b06c56b70b Update "node-fetch" typings (#11834)
* Update "node-fetch" typings

Convert to external module and remove `namespace _fetch`

* Fix test

* Don't need patch version
2016-10-20 08:47:19 -07:00

59 lines
1.4 KiB
TypeScript

import fetch, { Headers, Request, RequestInit, Response } from '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<Response>) {
promise.then((response) => {
if (response.type === 'basic') {
// for test only
}
return response.text();
}).then((text) => {
console.log(text);
});
}