mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
whatwg-fetch: Deprecate. Now provided by '--lib dom' (#14785)
This commit is contained in:
Vendored
-115
@@ -1,115 +0,0 @@
|
||||
// Type definitions for Fetch API
|
||||
// Project: https://github.com/github/fetch
|
||||
// Definitions by: Ryan Graham <https://github.com/ryan-codingintrigue>, Kagami Sascha Rosylight <https://github.com/saschanaz>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="whatwg-streams" />
|
||||
|
||||
interface Window {
|
||||
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
|
||||
}
|
||||
declare let fetch: typeof window.fetch;
|
||||
|
||||
declare type HeadersInit = Headers | string[][] | { [key: string]: string };
|
||||
declare class Headers {
|
||||
constructor(init?: HeadersInit);
|
||||
|
||||
append(name: string, value: string): void;
|
||||
delete(name: string): void;
|
||||
get(name: string): string | null;
|
||||
has(name: string): boolean;
|
||||
set(name: string, value: string): void;
|
||||
|
||||
// WebIDL pair iterator: iterable<ByteString, ByteString>
|
||||
entries(): IterableIterator<[string, string]>;
|
||||
forEach(callback: (value: string, index: number, headers: Headers) => void, thisArg?: any): void;
|
||||
keys(): IterableIterator<string>;
|
||||
values(): IterableIterator<string>;
|
||||
[Symbol.iterator](): IterableIterator<[string, string]>;
|
||||
}
|
||||
|
||||
declare type BodyInit = Blob | ArrayBufferView | ArrayBuffer | FormData /* | URLSearchParams */ | string;
|
||||
declare type ResponseBodyInit = BodyInit | ReadableStream;
|
||||
interface Body {
|
||||
readonly bodyUsed: boolean;
|
||||
arrayBuffer(): Promise<ArrayBuffer>;
|
||||
blob(): Promise<Blob>;
|
||||
formData(): Promise<FormData>;
|
||||
json(): Promise<any>;
|
||||
json<T>(): Promise<T>;
|
||||
text(): Promise<string>;
|
||||
}
|
||||
|
||||
declare type RequestInfo = Request | string;
|
||||
declare class Request {
|
||||
constructor(input: RequestInfo, init?: RequestInit);
|
||||
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
readonly headers: Headers;
|
||||
|
||||
readonly type: RequestType
|
||||
readonly destination: RequestDestination;
|
||||
readonly referrer: string;
|
||||
readonly referrerPolicy: ReferrerPolicy;
|
||||
readonly mode: RequestMode;
|
||||
readonly credentials: RequestCredentials;
|
||||
readonly cache: RequestCache;
|
||||
readonly redirect: RequestRedirect;
|
||||
readonly integrity: string;
|
||||
readonly keepalive: boolean;
|
||||
|
||||
clone(): Request;
|
||||
}
|
||||
interface Request extends Body {}
|
||||
interface RequestInit {
|
||||
method?: string;
|
||||
headers?: HeadersInit;
|
||||
body?: BodyInit;
|
||||
referrer?: string;
|
||||
referrerPolicy?: ReferrerPolicy;
|
||||
mode?: RequestMode;
|
||||
credentials?: RequestCredentials;
|
||||
cache?: RequestCache;
|
||||
redirect?: RequestRedirect;
|
||||
integrity?: string;
|
||||
window?: null; // can only be set to null
|
||||
}
|
||||
|
||||
type RequestType = "" | "audio" | "font" | "image" | "script" | "style" | "track" | "video";
|
||||
type RequestDestination = "" | "document" | "embed" | "font" | "image" | "manifest" | "media" | "object" | "report" | "script" | "serviceworker" | "sharedworker" | "style" | "worker" | "xslt";
|
||||
type RequestMode = "navigate" | "same-origin" | "no-cors" | "cors";
|
||||
type RequestCredentials = "omit" | "same-origin" | "include";
|
||||
type RequestCache = "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
|
||||
type RequestRedirect = "follow" | "error" | "manual";
|
||||
|
||||
type ReferrerPolicy = "" | "no-referrer" | "no-referrer-when-downgrade" | "same-origin" | "origin" | "strict-origin" | "origin-when-cross-origin" | "strict-origin-when-cross-origin" | "unsafe-url";
|
||||
|
||||
declare class Response {
|
||||
constructor(body?: ResponseBodyInit, init?: ResponseInit);
|
||||
|
||||
static error(): Response;
|
||||
static redirect(url: string, status?: number): Response;
|
||||
|
||||
readonly type: ResponseType;
|
||||
|
||||
readonly url: string;
|
||||
readonly redirected: boolean;
|
||||
readonly status: number;
|
||||
readonly ok: boolean;
|
||||
readonly statusText: string;
|
||||
readonly headers: Headers;
|
||||
readonly body: ReadableStream | null;
|
||||
readonly trailer: Promise<Headers>;
|
||||
|
||||
clone(): Response;
|
||||
}
|
||||
interface Response extends Body {}
|
||||
|
||||
interface ResponseInit {
|
||||
status?: number;
|
||||
statusText?: string;
|
||||
headers?: HeadersInit;
|
||||
}
|
||||
|
||||
type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect";
|
||||
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6",
|
||||
"dom"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"whatwg-fetch-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
|
||||
|
||||
function test_HeadersCopiedFromHeaders() {
|
||||
var source = new Headers();
|
||||
source.append('Content-Type', 'application/json');
|
||||
return new Headers(source);
|
||||
}
|
||||
|
||||
function test_HeadersCopiedFromHash() {
|
||||
var source: DOMStringMap = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
return new Headers(source);
|
||||
}
|
||||
|
||||
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(window.fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions));
|
||||
}
|
||||
|
||||
function test_fetchUrlWithHeadersObject() {
|
||||
var requestOptions: RequestInit = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
};
|
||||
handlePromise(window.fetch("http://www.andlabs.net/html5/uCOR.php", requestOptions));
|
||||
}
|
||||
|
||||
function test_fetchUrl() {
|
||||
handlePromise(window.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(window.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);
|
||||
});
|
||||
}
|
||||
|
||||
function test_Body_json() {
|
||||
interface FooBar {
|
||||
foo: string;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json<FooBar>())
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => <Promise<FooBar>> response.json())
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json() as Promise<FooBar>)
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json())
|
||||
.then(fooBar => {
|
||||
// fooBar is of type any, not of type FooBar
|
||||
|
||||
// FIXME Was behaving properly with TypeScript 2.0.10, not anymore with 2.1.4
|
||||
// See Wrong type with Promise chaining and 2.1.4 https://github.com/Microsoft/TypeScript/issues/12409
|
||||
//console.log(fooBar.foo);
|
||||
//console.log(fooBar.bar);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user