mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
Merge pull request #34655 from nikcorg/node-fetch/feat/blob-constructor
Type definitions for `Blob` constructor arguments
This commit is contained in:
Vendored
+16
-3
@@ -116,9 +116,17 @@ export class Headers implements Iterable<[string, string]> {
|
||||
[Symbol.iterator](): Iterator<[string, string]>;
|
||||
}
|
||||
|
||||
type BlobPart = ArrayBuffer | ArrayBufferView | Blob | string;
|
||||
|
||||
interface BlobOptions {
|
||||
type?: string;
|
||||
endings?: "transparent" | "native";
|
||||
}
|
||||
|
||||
export class Blob {
|
||||
type: string;
|
||||
size: number;
|
||||
constructor(blobParts?: BlobPart[], options?: BlobOptions);
|
||||
readonly type: string;
|
||||
readonly size: number;
|
||||
slice(start?: number, end?: number): Blob;
|
||||
}
|
||||
|
||||
@@ -174,7 +182,12 @@ export interface ResponseInit {
|
||||
export type HeadersInit = Headers | string[][] | { [key: string]: string };
|
||||
// HeaderInit is exported to support backwards compatibility. See PR #34382
|
||||
export type HeaderInit = HeadersInit;
|
||||
export type BodyInit = ArrayBuffer | ArrayBufferView | NodeJS.ReadableStream | string | URLSearchParams;
|
||||
export type BodyInit =
|
||||
ArrayBuffer
|
||||
| ArrayBufferView
|
||||
| NodeJS.ReadableStream
|
||||
| string
|
||||
| URLSearchParams;
|
||||
export type RequestInfo = string | Request;
|
||||
|
||||
declare function fetch(
|
||||
|
||||
@@ -1,81 +1,101 @@
|
||||
import fetch, { Headers, Request, RequestInit, Response, FetchError } from 'node-fetch';
|
||||
import fetch, {
|
||||
Blob,
|
||||
Headers,
|
||||
Request,
|
||||
RequestInit,
|
||||
Response,
|
||||
FetchError
|
||||
} 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));
|
||||
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));
|
||||
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"));
|
||||
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"));
|
||||
}
|
||||
|
||||
function test_fetchUrlArrayBuffer() {
|
||||
handlePromise(fetch("http://www.andlabs.net/html5/uCOR.php"), true);
|
||||
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;
|
||||
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));
|
||||
handlePromise(fetch(request));
|
||||
}
|
||||
|
||||
function test_globalFetchVar() {
|
||||
fetch('http://test.com', {})
|
||||
.then(response => {
|
||||
// for test only
|
||||
});
|
||||
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 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[]
|
||||
const headers = new Headers();
|
||||
const myHeader = "foo";
|
||||
headers.raw()[myHeader]; // $ExpectType string[]
|
||||
}
|
||||
|
||||
function test_isRedirect() {
|
||||
@@ -84,6 +104,13 @@ function test_isRedirect() {
|
||||
}
|
||||
|
||||
function test_FetchError() {
|
||||
new FetchError('message', 'type', 'systemError');
|
||||
new FetchError('message', 'type');
|
||||
new FetchError("message", "type", "systemError");
|
||||
new FetchError("message", "type");
|
||||
}
|
||||
|
||||
function test_Blob() {
|
||||
new Blob();
|
||||
new Blob(["beep", "boop"]);
|
||||
new Blob(["beep", "boop"], { endings: "native" });
|
||||
new Blob(["beep", "boop"], { type: "text/plain" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user