On newest node-fetch, arrayBuffer are supported.

This commit is contained in:
Yonggang Luo
2018-04-02 00:07:31 +08:00
parent def3d5d177
commit 73cde47eb1
2 changed files with 13 additions and 4 deletions

View File

@@ -77,6 +77,7 @@ export class Body {
json<T>(): Promise<T>;
text(): Promise<string>;
buffer(): Promise<Buffer>;
arrayBuffer(): Promise<ArrayBuffer>;
}
export class Response extends Body {

View File

@@ -30,6 +30,10 @@ 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() {
var requestOptions: RequestInit = {
method: "POST",
@@ -53,13 +57,17 @@ function test_globalFetchVar() {
});
}
function handlePromise(promise: Promise<Response>) {
promise.then((response) => {
function handlePromise(promise: Promise<Response>, isArrayBuffer: boolean = false) {
promise.then((response):Promise<string | ArrayBuffer> => {
if (response.type === 'basic') {
// for test only
}
return response.text();
}).then((text) => {
if (isArrayBuffer) {
return response.arrayBuffer();
} else {
return response.text();
}
}).then((text:string | ArrayBuffer) => {
console.log(text);
});
}