|
| 1 | +// Type definitions for fetch API |
| 2 | +// Project: https://github.com/github/fetch |
| 3 | +// Definitions by: Ryan Graham <https://github.com/ryan-codingintrigue> |
| 4 | +// Definitions: https://github.com/borisyankov/DefinitelyTyped |
| 5 | + |
| 6 | +// @cmichaelgraham - removed explicit external reference |
| 7 | + |
| 8 | +// @cmichaelgraham - added BufferSource and URLSearchParams interfaces to help compilation |
| 9 | +interface URLSearchParams {} |
| 10 | + |
| 11 | +declare class Request { |
| 12 | + constructor(input: string|Request, init?:RequestInit); |
| 13 | + method: string; |
| 14 | + url: string; |
| 15 | + headers: Headers; |
| 16 | + context: RequestContext; |
| 17 | + referrer: string; |
| 18 | + mode: RequestMode; |
| 19 | + credentials: RequestCredentials; |
| 20 | + cache: RequestCache; |
| 21 | +} |
| 22 | + |
| 23 | +interface RequestInit { |
| 24 | + method?: string; |
| 25 | + headers?: HeaderInit|{ [index: string]: string }; |
| 26 | + body?: BodyInit; |
| 27 | + mode?: RequestMode; |
| 28 | + credentials?: RequestCredentials; |
| 29 | + cache?: RequestCache; |
| 30 | +} |
| 31 | + |
| 32 | +declare enum RequestContext { |
| 33 | + "audio", "beacon", "cspreport", "download", "embed", "eventsource", "favicon", "fetch", |
| 34 | + "font", "form", "frame", "hyperlink", "iframe", "image", "imageset", "import", |
| 35 | + "internal", "location", "manifest", "object", "ping", "plugin", "prefetch", "script", |
| 36 | + "serviceworker", "sharedworker", "subresource", "style", "track", "video", "worker", |
| 37 | + "xmlhttprequest", "xslt" |
| 38 | +} |
| 39 | +declare enum RequestMode { "same-origin", "no-cors", "cors" } |
| 40 | +declare enum RequestCredentials { "omit", "same-origin", "include" } |
| 41 | +declare enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" } |
| 42 | + |
| 43 | +declare class Headers { |
| 44 | + append(name: string, value: string): void; |
| 45 | + delete(name: string):void; |
| 46 | + get(name: string): string; |
| 47 | + getAll(name: string): Array<string>; |
| 48 | + has(name: string): boolean; |
| 49 | + set(name: string, value: string): void; |
| 50 | +} |
| 51 | + |
| 52 | +declare class Body { |
| 53 | + bodyUsed: boolean; |
| 54 | + arrayBuffer(): Promise<ArrayBuffer>; |
| 55 | + blob(): Promise<Blob>; |
| 56 | + formData(): Promise<FormData>; |
| 57 | + json(): Promise<any>; |
| 58 | + text(): Promise<string>; |
| 59 | +} |
| 60 | +declare class Response extends Body { |
| 61 | + constructor(body?: BodyInit, init?: ResponseInit); |
| 62 | + error(): Response; |
| 63 | + redirect(url: string, status: number): Response; |
| 64 | + type: ResponseType; |
| 65 | + url: string; |
| 66 | + status: number; |
| 67 | + ok: boolean; |
| 68 | + statusText: string; |
| 69 | + headers: Headers; |
| 70 | + clone(): Response; |
| 71 | +} |
| 72 | + |
| 73 | +declare enum ResponseType { "basic", "cors", "default", "error", "opaque" } |
| 74 | + |
| 75 | +declare class ResponseInit { |
| 76 | + status: number; |
| 77 | + statusText: string; |
| 78 | + headers: HeaderInit; |
| 79 | +} |
| 80 | + |
| 81 | +declare type HeaderInit = Headers|Array<string>; |
| 82 | +declare type BodyInit = Blob|FormData|string; |
| 83 | +declare type RequestInfo = Request|string; |
| 84 | + |
| 85 | +interface Window { |
| 86 | + fetch(url: string, init?: RequestInit): Promise<Response>; |
| 87 | +} |
0 commit comments