UNPKG

4.15 kBTypeScriptView Raw
1// Type definitions for Fetch API
2// Project: https://github.com/github/fetch
3// Definitions by: Ryan Graham <https://github.com/ryan-codingintrigue>, Kagami Sascha Rosylight <https://github.com/saschanaz>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/// <reference types="whatwg-streams" />
7
8interface Window {
9 fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
10}
11declare let fetch: typeof window.fetch;
12
13declare type HeadersInit = Headers | string[][] | { [key: string]: string };
14declare class Headers {
15 constructor(init?: HeadersInit);
16
17 append(name: string, value: string): void;
18 delete(name: string): void;
19 get(name: string): string | null;
20 has(name: string): boolean;
21 set(name: string, value: string): void;
22
23 // WebIDL pair iterator: iterable<ByteString, ByteString>
24 entries(): IterableIterator<[string, string]>;
25 forEach(callback: (value: string, index: number, headers: Headers) => void, thisArg?: any): void;
26 keys(): IterableIterator<string>;
27 values(): IterableIterator<string>;
28 [Symbol.iterator](): IterableIterator<[string, string]>;
29}
30
31declare type BodyInit = Blob | ArrayBufferView | ArrayBuffer | FormData /* | URLSearchParams */ | string;
32declare type ResponseBodyInit = BodyInit | ReadableStream;
33interface Body {
34 readonly bodyUsed: boolean;
35 arrayBuffer(): Promise<ArrayBuffer>;
36 blob(): Promise<Blob>;
37 formData(): Promise<FormData>;
38 json(): Promise<any>;
39 json<T>(): Promise<T>;
40 text(): Promise<string>;
41}
42
43declare type RequestInfo = Request | string;
44declare class Request {
45 constructor(input: RequestInfo, init?: RequestInit);
46
47 readonly method: string;
48 readonly url: string;
49 readonly headers: Headers;
50
51 readonly type: RequestType
52 readonly destination: RequestDestination;
53 readonly referrer: string;
54 readonly referrerPolicy: ReferrerPolicy;
55 readonly mode: RequestMode;
56 readonly credentials: RequestCredentials;
57 readonly cache: RequestCache;
58 readonly redirect: RequestRedirect;
59 readonly integrity: string;
60 readonly keepalive: boolean;
61
62 clone(): Request;
63}
64interface Request extends Body {}
65interface RequestInit {
66 method?: string;
67 headers?: HeadersInit;
68 body?: BodyInit;
69 referrer?: string;
70 referrerPolicy?: ReferrerPolicy;
71 mode?: RequestMode;
72 credentials?: RequestCredentials;
73 cache?: RequestCache;
74 redirect?: RequestRedirect;
75 integrity?: string;
76 window?: null; // can only be set to null
77}
78
79type RequestType = "" | "audio" | "font" | "image" | "script" | "style" | "track" | "video";
80type RequestDestination = "" | "document" | "embed" | "font" | "image" | "manifest" | "media" | "object" | "report" | "script" | "serviceworker" | "sharedworker" | "style" | "worker" | "xslt";
81type RequestMode = "navigate" | "same-origin" | "no-cors" | "cors";
82type RequestCredentials = "omit" | "same-origin" | "include";
83type RequestCache = "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
84type RequestRedirect = "follow" | "error" | "manual";
85
86type ReferrerPolicy = "" | "no-referrer" | "no-referrer-when-downgrade" | "same-origin" | "origin" | "strict-origin" | "origin-when-cross-origin" | "strict-origin-when-cross-origin" | "unsafe-url";
87
88declare class Response {
89 constructor(body?: ResponseBodyInit, init?: ResponseInit);
90
91 static error(): Response;
92 static redirect(url: string, status?: number): Response;
93
94 readonly type: ResponseType;
95
96 readonly url: string;
97 readonly redirected: boolean;
98 readonly status: number;
99 readonly ok: boolean;
100 readonly statusText: string;
101 readonly headers: Headers;
102 readonly body: ReadableStream | null;
103 readonly trailer: Promise<Headers>;
104
105 clone(): Response;
106}
107interface Response extends Body {}
108
109interface ResponseInit {
110 status?: number;
111 statusText?: string;
112 headers?: HeadersInit;
113}
114
115type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect";