mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-02 06:29:40 +08:00
On newest node-fetch, arrayBuffer are supported.
This commit is contained in:
1
types/node-fetch/index.d.ts
vendored
1
types/node-fetch/index.d.ts
vendored
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user