From 7c9875bc0285a2c288578ffcb21196429db7cdaa Mon Sep 17 00:00:00 2001 From: Andrew Goodale Date: Mon, 30 Apr 2018 12:15:36 -0400 Subject: [PATCH] Add missing fetch types compatible with TS 2.6+ (#25255) --- types/react-native/globals.d.ts | 93 ++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/types/react-native/globals.d.ts b/types/react-native/globals.d.ts index b222b50ca4..92ae988ecb 100644 --- a/types/react-native/globals.d.ts +++ b/types/react-native/globals.d.ts @@ -21,4 +21,95 @@ declare function requestAnimationFrame(callback: (time: number) => void): number declare function fetchBundle(bundleId: number, callback: (error?: Error | null) => void): void; -declare function fetch(input: RequestInfo, init?: RequestInit): Promise; +// +// Fetch API +// + +declare function fetch(input: Request | string, init?: RequestInit): Promise; + +interface Blob {} + +declare interface FormData { + append(name: string, value: any): void; +} + +declare interface Body { + readonly bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + json(): Promise; + text(): Promise; + formData(): Promise; +} + +declare interface Headers { + append(name: string, value: string): void; + delete(name: string): void; + forEach(callback: Function, thisArg?: any): void; + get(name: string): string | null; + has(name: string): boolean; + set(name: string, value: string): void; +} + +declare var Headers: { + prototype: Headers; + new(init?: HeadersInit_): Headers; +}; + +type BodyInit_ = Blob | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView | ArrayBuffer | FormData | string | null; + +declare interface RequestInit { + body?: BodyInit_; + credentials?: RequestCredentials_; + headers?: HeadersInit_; + integrity?: string; + keepalive?: boolean; + method?: string; + mode?: RequestMode_; + referrer?: string; + window?: any; +} + +declare interface Request extends Object, Body { + readonly credentials: RequestCredentials_; + readonly headers: Headers; + readonly method: string; + readonly mode: RequestMode_; + readonly referrer: string; + readonly url: string; + clone(): Request; +} + +declare var Request: { + prototype: Request; + new(input: Request | string, init?: RequestInit): Request; +}; + +declare interface ResponseInit { + headers?: HeadersInit_; + status?: number; + statusText?: string; +} + +declare interface Response extends Object, Body { + readonly headers: Headers; + readonly ok: boolean; + readonly status: number; + readonly statusText: string; + readonly type: ResponseType_; + readonly url: string; + readonly redirected: boolean; + clone(): Response; +} + +declare var Response: { + prototype: Response; + new(body?: BodyInit_, init?: ResponseInit): Response; + error: () => Response; + redirect: (url: string, status?: number) => Response; +}; + +type HeadersInit_ = Headers | string[][] | { [key: string]: string }; +type RequestCredentials_ = "omit" | "same-origin" | "include"; +type RequestMode_ = "navigate" | "same-origin" | "no-cors" | "cors"; +type ResponseType_ = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect";