DefinitelyTyped/types/node-fetch/node-fetch-tests.ts
Oliver Joseph Ash aee338f484 node-fetch: correct Headers.raw type (#27364)
* `node-fetch`: add test for `Headers.raw`

* `node-fetch`: `Headers.raw`: add `string[]` to index signature value type

* `node-fetch`: `Headers.raw`: add `undefined` to index signature value type

* `node-fetch`: enable `strictNullChecks` and fix errors

* Revert "`node-fetch`: `Headers.raw`: add `undefined` to index signature value type"

This reverts commit 15ad14d72c42fa1f3ba472bc15f38a88230f3a4f.

* Fix expect type format

* Remove string
2018-07-20 18:01:15 -07:00

80 lines
2.0 KiB
TypeScript

import fetch, { Headers, Request, RequestInit, Response } from 'node-fetch';
import { Agent } from "http";
function test_fetchUrlWithOptions() {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const requestOptions: RequestInit = {
compress: true,
follow: 10,
headers,
method: "POST",
redirect: 'manual',
size: 100,
timeout: 5000,
};
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions));
}
function test_fetchUrlWithHeadersObject() {
const requestOptions: RequestInit = {
headers: {
'Content-Type': 'application/json'
},
method: "POST",
};
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions));
}
function test_fetchUrl() {
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"));
}
function test_fetchUrlArrayBuffer() {
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"), true);
}
function test_fetchUrlWithRequestObject() {
const requestOptions: RequestInit = {
method: "POST",
headers: {
'Content-Type': 'application/json'
}
};
const request: Request = new Request("http://www.andlabs.net/html5/uCOR.php", requestOptions);
const timeout: number = request.timeout;
const size: number = request.size;
const agent: Agent | undefined = request.agent;
const protocol: string = request.protocol;
handlePromise(fetch(request));
}
function test_globalFetchVar() {
fetch('http://test.com', {})
.then(response => {
// for test only
});
}
function handlePromise(promise: Promise<Response>, isArrayBuffer: boolean = false) {
promise.then((response): Promise<string | ArrayBuffer> => {
if (response.type === 'basic') {
// for test only
}
if (isArrayBuffer) {
return response.arrayBuffer();
} else {
return response.text();
}
}).then((text: string | ArrayBuffer) => {
console.log(text);
});
}
function test_headersRaw() {
const headers = new Headers();
const myHeader = 'foo';
headers.raw()[myHeader]; // $ExpectType string[]
}