diff --git a/packages/react-native/index.js.flow b/packages/react-native/index.js.flow index 573dfa1f328..c48200c5542 100644 --- a/packages/react-native/index.js.flow +++ b/packages/react-native/index.js.flow @@ -21,6 +21,8 @@ export type {HostInstance} from './src/private/types/HostInstance'; export type {HostComponent} from './src/private/types/HostComponent'; export {default as registerCallableModule} from './Libraries/Core/registerCallableModule'; +/// + // #region Components export {default as AccessibilityInfo} from './Libraries/Components/AccessibilityInfo/AccessibilityInfo'; diff --git a/packages/react-native/src/types/globals.d.ts b/packages/react-native/src/types/globals.d.ts new file mode 100644 index 00000000000..9616f6d0d96 --- /dev/null +++ b/packages/react-native/src/types/globals.d.ts @@ -0,0 +1,628 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import type {ErrorUtils} from '../../Libraries/vendor/core/ErrorUtils'; + +declare global { + interface NodeRequire { + (id: string): any; + } + + var require: NodeRequire; + + /** + * Console polyfill + * @see https://reactnative.dev/docs/javascript-environment#polyfills + */ + interface Console { + error(message?: any, ...optionalParams: any[]): void; + info(message?: any, ...optionalParams: any[]): void; + log(message?: any, ...optionalParams: any[]): void; + warn(message?: any, ...optionalParams: any[]): void; + trace(message?: any, ...optionalParams: any[]): void; + debug(message?: any, ...optionalParams: any[]): void; + table(...data: any[]): void; + groupCollapsed(label?: string): void; + groupEnd(): void; + group(label?: string): void; + } + + var console: Console; + + /** + * This contains the non-native `XMLHttpRequest` object, which you can use if you want to route network requests + * through DevTools (to trace them): + * + * global.XMLHttpRequest = global.originalXMLHttpRequest; + * + * @see https://github.com/facebook/react-native/issues/934 + */ + const originalXMLHttpRequest: any; + + const __BUNDLE_START_TIME__: number; + const ErrorUtils: ErrorUtils; + + /** + * This variable is set to true when react-native is running in Dev mode + * @example + * if (__DEV__) console.log('Running in dev mode') + */ + const __DEV__: boolean; + + const HermesInternal: null | {}; + + // #region Timer Functions + + function clearInterval(handle: number): void; + function clearTimeout(handle: number): void; + function setInterval(handler: () => void, timeout: number): number; + function setInterval( + handler: (...args: Args) => void, + timeout?: number, + ...args: Args + ): number; + function setTimeout(handler: () => void, timeout: number): number; + function setTimeout( + handler: (...args: Args) => void, + timeout?: number, + ...args: Args + ): number; + function clearImmediate(handle: number): void; + function setImmediate(handler: () => void): number; + function setImmediate( + handler: (...args: Args) => void, + ...args: Args + ): number; + + function cancelAnimationFrame(handle: number): void; + function requestAnimationFrame(callback: (time: number) => void): number; + + function fetchBundle( + bundleId: number, + callback: (error?: Error | null) => void, + ): void; + + // #endregion + // #region Fetch API + + interface GlobalFetch { + fetch(input: RequestInfo, init?: RequestInit): Promise; + } + + function fetch(input: RequestInfo, init?: RequestInit): Promise; + + interface WindowOrWorkerGlobalScope { + fetch(input: RequestInfo, init?: RequestInit): Promise; + } + + type EndingType = 'native' | 'transparent'; + type BlobPart = BufferSource | Blob | string; + type BufferSource = ArrayBufferView | ArrayBuffer; + + interface BlobPropertyBag { + endings?: EndingType | undefined; + type?: string | undefined; + } + + interface Blob { + readonly size: number; + readonly type: string; + slice(start?: number, end?: number, contentType?: string): Blob; + } + + interface BlobOptions { + type: string; + lastModified: number; + } + + var Blob: { + prototype: Blob; + new (blobParts?: Array, options?: BlobOptions): Blob; + }; + + interface FilePropertyBag extends BlobPropertyBag { + lastModified?: number | undefined; + } + + interface File extends Blob { + readonly name: string; + readonly lastModified: number; + } + + var File: { + prototype: File; + new (fileParts: BlobPart[], name: string, options?: FilePropertyBag): File; + }; + + type FormDataValue = + | string + | {name?: string | undefined; type?: string | undefined; uri: string}; + + type FormDataPart = + | { + string: string; + headers: {[name: string]: string}; + } + | { + uri: string; + headers: {[name: string]: string}; + name?: string | undefined; + type?: string | undefined; + }; + + class FormData { + append(key: string, value: any): void; + getAll(key: string): Array; + getParts(): Array; + } + + interface Body { + readonly bodyUsed: boolean; + arrayBuffer(): Promise; + blob(): Promise; + json(): Promise; + text(): Promise; + formData(): Promise; + } + + interface Headers { + append(name: string, value: string): void; + delete(name: string): void; + forEach( + callbackfn: (value: string, key: string, parent: Headers) => void, + thisArg?: any, + ): void; + get(name: string): string | null; + has(name: string): boolean; + set(name: string, value: string): void; + entries(): IterableIterator<[string, string]>; + keys(): IterableIterator; + values(): IterableIterator; + [Symbol.iterator](): IterableIterator<[string, string]>; + } + + var Headers: { + prototype: Headers; + new (init?: HeadersInit_): Headers; + }; + + /** + * React Native's implementation of fetch allows this syntax for uploading files from + * local filesystem. + * See https://github.com/facebook/react-native/blob/master/Libraries/Network/convertRequestBody.js#L22 + */ + interface _SourceUri { + uri: string; + [key: string]: any; + } + + type BodyInit_ = + | _SourceUri + | Blob + | Int8Array + | Int16Array + | Int32Array + | Uint8Array + | Uint16Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array + | DataView + | ArrayBuffer + | FormData + | string + | null; + + interface RequestInit { + body?: BodyInit_ | null | undefined; + credentials?: RequestCredentials_ | undefined; + headers?: HeadersInit_ | undefined; + integrity?: string | undefined; + keepalive?: boolean | undefined; + method?: string | undefined; + mode?: RequestMode_ | undefined; + referrer?: string | undefined; + window?: null | undefined; + signal?: AbortSignal | undefined; + } + + interface Request extends Object, Body { + readonly credentials: RequestCredentials_; + readonly headers: Headers; + readonly method: string; + readonly mode: RequestMode_; + readonly referrer: string; + readonly url: string; + readonly signal: AbortSignal; + clone(): Request; + } + + var Request: { + prototype: Request; + new (input: Request | string, init?: RequestInit): Request; + }; + + type RequestInfo = Request | string; + + interface ResponseInit { + headers?: HeadersInit_ | undefined; + status?: number | undefined; + statusText?: string | undefined; + } + + 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; + } + + var Response: { + prototype: Response; + new (body?: BodyInit_ | null, init?: ResponseInit): Response; + error(): Response; + json(data: any, init?: ResponseInit): Response; + redirect: (url: string | URL, status?: number) => Response; + }; + + type HeadersInit_ = [string, string][] | Record | Headers; + type RequestCredentials_ = 'omit' | 'same-origin' | 'include'; + type RequestMode_ = 'navigate' | 'same-origin' | 'no-cors' | 'cors'; + type ResponseType_ = + | 'basic' + | 'cors' + | 'default' + | 'error' + | 'opaque' + | 'opaqueredirect'; + + // #endregion + // #region XMLHttpRequest + + interface ProgressEvent extends Event { + readonly lengthComputable: boolean; + readonly loaded: number; + readonly total: number; + readonly target: T | null; + } + + interface XMLHttpRequestEventMap extends XMLHttpRequestEventTargetEventMap { + readystatechange: Event; + } + + interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { + onreadystatechange: ((this: XMLHttpRequest, ev: Event) => any) | null; + readonly readyState: number; + readonly response: any; + readonly responseText: string; + responseType: XMLHttpRequestResponseType; + readonly responseURL: string; + readonly responseXML: Document | null; + readonly status: number; + readonly statusText: string; + timeout: number; + readonly upload: XMLHttpRequestUpload; + withCredentials: boolean; + abort(): void; + getAllResponseHeaders(): string; + getResponseHeader(header: string): string | null; + open( + method: string, + url: string, + async?: boolean, + user?: string | null, + password?: string | null, + ): void; + overrideMimeType(mime: string): void; + send(data?: any): void; + setRequestHeader(header: string, value: string): void; + readonly DONE: 4; + readonly HEADERS_RECEIVED: 2; + readonly LOADING: 3; + readonly OPENED: 1; + readonly UNSENT: 0; + addEventListener( + type: K, + listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, + ): void; + removeEventListener( + type: K, + listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, + ): void; + } + + var XMLHttpRequest: { + prototype: XMLHttpRequest; + new (): XMLHttpRequest; + readonly DONE: 4; + readonly HEADERS_RECEIVED: 2; + readonly LOADING: 3; + readonly OPENED: 1; + readonly UNSENT: 0; + }; + + interface XMLHttpRequestEventTargetEventMap { + abort: ProgressEvent; + error: ProgressEvent; + load: ProgressEvent; + loadend: ProgressEvent; + loadstart: ProgressEvent; + progress: ProgressEvent; + timeout: ProgressEvent; + } + + interface XMLHttpRequestEventTarget { + onabort: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + onerror: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + onload: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + onloadend: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + onloadstart: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + onprogress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + ontimeout: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; + addEventListener( + type: K, + listener: ( + this: XMLHttpRequestEventTarget, + ev: XMLHttpRequestEventTargetEventMap[K], + ) => any, + ): void; + removeEventListener( + type: K, + listener: ( + this: XMLHttpRequestEventTarget, + ev: XMLHttpRequestEventTargetEventMap[K], + ) => any, + ): void; + } + + interface XMLHttpRequestUpload + extends EventTarget, + XMLHttpRequestEventTarget { + addEventListener( + type: K, + listener: ( + this: XMLHttpRequestUpload, + ev: XMLHttpRequestEventTargetEventMap[K], + ) => any, + ): void; + removeEventListener( + type: K, + listener: ( + this: XMLHttpRequestUpload, + ev: XMLHttpRequestEventTargetEventMap[K], + ) => any, + ): void; + } + + var XMLHttpRequestUpload: { + prototype: XMLHttpRequestUpload; + new (): XMLHttpRequestUpload; + }; + + type XMLHttpRequestResponseType = + | '' + | 'arraybuffer' + | 'blob' + | 'document' + | 'json' + | 'text'; + + interface URL { + href: string; + readonly searchParams: URLSearchParams; + + toJSON(): string; + toString(): string; + } + + var URL: { + prototype: URL; + new (url: string | URL, base?: string | URL): URL; + createObjectURL(obj: Blob): string; + revokeObjectURL(url: string): void; + }; + + /** + * Based on definitions of lib.dom and lib.dom.iterable + */ + interface URLSearchParams { + append(key: string, value: string): void; + toString(): string; + + [Symbol.iterator](): IterableIterator<[string, string]>; + } + + var URLSearchParams: { + prototype: URLSearchParams; + new ( + init?: string[][] | Record | string | URLSearchParams, + ): URLSearchParams; + }; + + interface WebSocketMessageEvent extends Event { + data?: any | undefined; + } + interface WebSocketErrorEvent extends Event { + message: string; + } + interface WebSocketCloseEvent extends Event { + code?: number | undefined; + reason?: string | undefined; + message?: string | undefined; + } + + type WebsocketMessageEventListener = ( + event: 'message', + handler: (e: WebSocketMessageEvent) => void, + ) => void; + type WebsocketErrorEventListener = ( + event: 'error', + handler: (e: WebSocketErrorEvent) => void, + ) => void; + type WebsocketOpenEventListener = ( + event: 'open', + handler: () => void, + ) => void; + type WebsocketCloseEventListener = ( + event: 'close', + handler: (e: WebSocketCloseEvent) => void, + ) => void; + + type WebsocketEventListener = WebsocketMessageEventListener & + WebsocketErrorEventListener & + WebsocketOpenEventListener & + WebsocketCloseEventListener; + + interface WebSocket extends EventTarget { + readonly readyState: number; + send(data: string | ArrayBuffer | ArrayBufferView | Blob): void; + close(code?: number, reason?: string): void; + ping(): void; + onopen: (() => void) | null; + onmessage: ((event: WebSocketMessageEvent) => void) | null; + onerror: ((event: WebSocketErrorEvent) => void) | null; + onclose: ((event: WebSocketCloseEvent) => void) | null; + addEventListener: WebsocketEventListener; + removeEventListener: WebsocketEventListener; + } + + var WebSocket: { + prototype: WebSocket; + new ( + uri: string, + protocols?: string | string[] | null, + options?: { + headers: {[headerName: string]: string}; + [optionName: string]: any; + } | null, + ): WebSocket; + readonly CLOSED: number; + readonly CLOSING: number; + readonly CONNECTING: number; + readonly OPEN: number; + }; + + // #endregion + // #region Abort Controller + + interface AbortEvent extends Event { + type: 'abort'; + } + + class AbortSignal implements EventTarget { + /** + * AbortSignal cannot be constructed directly. + */ + constructor(); + /** + * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise. + */ + readonly aborted: boolean; + + onabort: (event: AbortEvent) => void; + + addEventListener: ( + type: 'abort', + listener: (this: AbortSignal, event: any) => any, + options?: + | boolean + | { + capture?: boolean | undefined; + once?: boolean | undefined; + passive?: boolean | undefined; + }, + ) => void; + + dispatchEvent(event: Event): boolean; + + removeEventListener: ( + type: 'abort', + listener: (this: AbortSignal, event: any) => any, + options?: + | boolean + | { + capture?: boolean | undefined; + }, + ) => void; + } + + class AbortController { + /** + * Initialize this controller. + */ + constructor(); + /** + * Returns the `AbortSignal` object associated with this object. + */ + readonly signal: AbortSignal; + /** + * Abort and signal to any observers that the associated activity is to be aborted. + */ + abort(): void; + } + + interface FileReaderEventMap { + abort: ProgressEvent; + error: ProgressEvent; + load: ProgressEvent; + loadend: ProgressEvent; + loadstart: ProgressEvent; + progress: ProgressEvent; + } + + interface FileReader extends EventTarget { + readonly error: Error | null; + onabort: ((this: FileReader, ev: ProgressEvent) => any) | null; + onerror: ((this: FileReader, ev: ProgressEvent) => any) | null; + onload: ((this: FileReader, ev: ProgressEvent) => any) | null; + onloadend: + | ((this: FileReader, ev: ProgressEvent) => any) + | null; + onloadstart: + | ((this: FileReader, ev: ProgressEvent) => any) + | null; + onprogress: + | ((this: FileReader, ev: ProgressEvent) => any) + | null; + readonly readyState: number; + readonly result: string | ArrayBuffer; + abort(): void; + readAsArrayBuffer(blob: Blob): void; + readAsDataURL(blob: Blob): void; + readAsText(blob: Blob, encoding?: string): void; + readonly DONE: 2; + readonly EMPTY: 0; + readonly LOADING: 1; + addEventListener( + type: K, + listener: (this: FileReader, ev: FileReaderEventMap[K]) => any, + options?: boolean, + ): void; + removeEventListener( + type: K, + listener: (this: FileReader, ev: FileReaderEventMap[K]) => any, + options?: boolean, + ): void; + } + + var FileReader: { + prototype: FileReader; + new (): FileReader; + readonly DONE: 2; + readonly EMPTY: 0; + readonly LOADING: 1; + }; + + // #endregion +} diff --git a/packages/react-native/types/__typetests__/globals.tsx b/packages/react-native/types/__typetests__/globals.tsx index e14a8029d6d..70d8b33b17a 100644 --- a/packages/react-native/types/__typetests__/globals.tsx +++ b/packages/react-native/types/__typetests__/globals.tsx @@ -175,4 +175,4 @@ socket.onerror = e => console.log(e.message); const formData = new FormData(); formData.append('file', { fileName: 'example' }); console.log(formData.getParts()); -console.log(formData.getAll()); +console.log(formData.getAll('username')); diff --git a/packages/react-native/types/index.d.ts b/packages/react-native/types/index.d.ts index 53638959475..d376104312c 100644 --- a/packages/react-native/types/index.d.ts +++ b/packages/react-native/types/index.d.ts @@ -68,7 +68,7 @@ /// /// /// -/// +/// /// export * from '../Libraries/ActionSheetIOS/ActionSheetIOS'; @@ -158,54 +158,3 @@ export * from './public/DeprecatedPropertiesAlias'; export * from './public/Insets'; export * from './public/ReactNativeRenderer'; export * from './public/ReactNativeTypes'; - -import type {ErrorUtils} from '../Libraries/vendor/core/ErrorUtils'; - -declare global { - interface NodeRequire { - (id: string): any; - } - - var require: NodeRequire; - - /** - * Console polyfill - * @see https://reactnative.dev/docs/javascript-environment#polyfills - */ - interface Console { - error(message?: any, ...optionalParams: any[]): void; - info(message?: any, ...optionalParams: any[]): void; - log(message?: any, ...optionalParams: any[]): void; - warn(message?: any, ...optionalParams: any[]): void; - trace(message?: any, ...optionalParams: any[]): void; - debug(message?: any, ...optionalParams: any[]): void; - table(...data: any[]): void; - groupCollapsed(label?: string): void; - groupEnd(): void; - group(label?: string): void; - } - - var console: Console; - - /** - * This contains the non-native `XMLHttpRequest` object, which you can use if you want to route network requests - * through DevTools (to trace them): - * - * global.XMLHttpRequest = global.originalXMLHttpRequest; - * - * @see https://github.com/facebook/react-native/issues/934 - */ - const originalXMLHttpRequest: any; - - const __BUNDLE_START_TIME__: number; - const ErrorUtils: ErrorUtils; - - /** - * This variable is set to true when react-native is running in Dev mode - * @example - * if (__DEV__) console.log('Running in dev mode') - */ - const __DEV__: boolean; - - const HermesInternal: null | {}; -} diff --git a/packages/react-native/types/modules/globals.d.ts b/packages/react-native/types/modules/globals.d.ts deleted file mode 100644 index e22b936f340..00000000000 --- a/packages/react-native/types/modules/globals.d.ts +++ /dev/null @@ -1,599 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -/* - * This file is necessary to declare global functions that might also be included by `--lib dom`. - * Due to a TypeScript bug, these cannot be placed inside a `declare global` block in index.d.ts. - * https://github.com/Microsoft/TypeScript/issues/16430 - */ - -// -// Timer Functions -// -declare function clearInterval(handle: number): void; -declare function clearTimeout(handle: number): void; -declare function setInterval(handler: () => void, timeout: number): number; -declare function setInterval( - handler: (...args: Args) => void, - timeout?: number, - ...args: Args -): number; -declare function setTimeout(handler: () => void, timeout: number): number; -declare function setTimeout( - handler: (...args: Args) => void, - timeout?: number, - ...args: Args -): number; -declare function clearImmediate(handle: number): void; -declare function setImmediate(handler: () => void): number; -declare function setImmediate( - handler: (...args: Args) => void, - ...args: Args -): number; - -declare function cancelAnimationFrame(handle: number): void; -declare function requestAnimationFrame( - callback: (time: number) => void, -): number; - -declare function fetchBundle( - bundleId: number, - callback: (error?: Error | null) => void, -): void; - -// -// Fetch API -// - -declare interface GlobalFetch { - fetch(input: RequestInfo, init?: RequestInit): Promise; -} - -declare function fetch( - input: RequestInfo, - init?: RequestInit, -): Promise; - -declare interface WindowOrWorkerGlobalScope { - fetch(input: RequestInfo, init?: RequestInit): Promise; -} - -interface Blob { - readonly size: number; - readonly type: string; - slice(start?: number, end?: number, contentType?: string): Blob; -} - -interface BlobOptions { - type: string; - lastModified: number; -} - -declare var Blob: { - prototype: Blob; - new (blobParts?: Array, options?: BlobOptions): Blob; -}; - -interface File extends Blob { - name: string; - lastModified: number; -} - -declare var File: { - prototype: File; - new ( - fileParts?: Array, - name?: string, - options?: BlobOptions, - ): File; -}; - -type FormDataValue = - | string - | {name?: string | undefined; type?: string | undefined; uri: string}; - -type FormDataPart = - | { - string: string; - headers: {[name: string]: string}; - } - | { - uri: string; - headers: {[name: string]: string}; - name?: string | undefined; - type?: string | undefined; - }; - -declare class FormData { - append(name: string, value: any): void; - getAll(): Array; - getParts(): Array; -} - -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; - entries(): IterableIterator<[string, string]>; - keys(): IterableIterator; - values(): IterableIterator; - [Symbol.iterator](): IterableIterator<[string, string]>; -} - -declare var Headers: { - prototype: Headers; - new (init?: HeadersInit_): Headers; -}; - -/** - * React Native's implementation of fetch allows this syntax for uploading files from - * local filesystem. - * See https://github.com/facebook/react-native/blob/master/Libraries/Network/convertRequestBody.js#L22 - */ -interface _SourceUri { - uri: string; - [key: string]: any; -} - -type BodyInit_ = - | _SourceUri - | Blob - | Int8Array - | Int16Array - | Int32Array - | Uint8Array - | Uint16Array - | Uint32Array - | Uint8ClampedArray - | Float32Array - | Float64Array - | DataView - | ArrayBuffer - | FormData - | string - | null; - -declare interface RequestInit { - body?: BodyInit_ | undefined; - credentials?: RequestCredentials_ | undefined; - headers?: HeadersInit_ | undefined; - integrity?: string | undefined; - keepalive?: boolean | undefined; - method?: string | undefined; - mode?: RequestMode_ | undefined; - referrer?: string | undefined; - window?: any | undefined; - signal?: AbortSignal | undefined; -} - -declare interface Request extends Object, Body { - readonly credentials: RequestCredentials_; - readonly headers: Headers; - readonly method: string; - readonly mode: RequestMode_; - readonly referrer: string; - readonly url: string; - readonly signal: AbortSignal | undefined; - clone(): Request; -} - -declare var Request: { - prototype: Request; - new (input: Request | string, init?: RequestInit): Request; -}; - -declare type RequestInfo = Request | string; - -declare interface ResponseInit { - headers?: HeadersInit_ | undefined; - status?: number | undefined; - statusText?: string | undefined; -} - -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'; - -// -// XMLHttpRequest -// - -declare interface ProgressEvent - extends Event { - readonly lengthComputable: boolean; - readonly loaded: number; - readonly total: number; - readonly target: T | null; -} - -interface XMLHttpRequestEventMap extends XMLHttpRequestEventTargetEventMap { - readystatechange: Event; -} - -interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - // msCaching: string; - onreadystatechange: ((this: XMLHttpRequest, ev: Event) => any) | null; - readonly readyState: number; - readonly response: any; - readonly responseText: string; - responseType: XMLHttpRequestResponseType; - readonly responseURL: string; - readonly responseXML: Document | null; - readonly status: number; - readonly statusText: string; - timeout: number; - readonly upload: XMLHttpRequestUpload; - withCredentials: boolean; - abort(): void; - getAllResponseHeaders(): string; - getResponseHeader(header: string): string | null; - // msCachingEnabled(): boolean; - open( - method: string, - url: string, - async?: boolean, - user?: string | null, - password?: string | null, - ): void; - overrideMimeType(mime: string): void; - send(data?: any): void; - setRequestHeader(header: string, value: string): void; - readonly DONE: 4; - readonly HEADERS_RECEIVED: 2; - readonly LOADING: 3; - readonly OPENED: 1; - readonly UNSENT: 0; - addEventListener( - type: K, - listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, - ): void; - // addEventListener(type: string, listener: EventListenerOrEventListenerObject): void; - removeEventListener( - type: K, - listener: (this: XMLHttpRequest, ev: XMLHttpRequestEventMap[K]) => any, - ): void; - // removeEventListener(type: string, listener: EventListenerOrEventListenerObject): void; -} - -declare var XMLHttpRequest: { - prototype: XMLHttpRequest; - new (): XMLHttpRequest; - readonly DONE: 4; - readonly HEADERS_RECEIVED: 2; - readonly LOADING: 3; - readonly OPENED: 1; - readonly UNSENT: 0; -}; - -interface XMLHttpRequestEventTargetEventMap { - abort: ProgressEvent; - error: ProgressEvent; - load: ProgressEvent; - loadend: ProgressEvent; - loadstart: ProgressEvent; - progress: ProgressEvent; - timeout: ProgressEvent; -} - -interface XMLHttpRequestEventTarget { - onabort: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - onerror: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - onload: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - onloadend: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - onloadstart: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - onprogress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - ontimeout: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null; - addEventListener( - type: K, - listener: ( - this: XMLHttpRequestEventTarget, - ev: XMLHttpRequestEventTargetEventMap[K], - ) => any, - ): void; - // addEventListener(type: string, listener: EventListenerOrEventListenerObject): void; - removeEventListener( - type: K, - listener: ( - this: XMLHttpRequestEventTarget, - ev: XMLHttpRequestEventTargetEventMap[K], - ) => any, - ): void; - // removeEventListener(type: string, listener: EventListenerOrEventListenerObject): void; -} - -interface XMLHttpRequestUpload extends EventTarget, XMLHttpRequestEventTarget { - addEventListener( - type: K, - listener: ( - this: XMLHttpRequestUpload, - ev: XMLHttpRequestEventTargetEventMap[K], - ) => any, - ): void; - // addEventListener(type: string, listener: EventListenerOrEventListenerObject): void; - removeEventListener( - type: K, - listener: ( - this: XMLHttpRequestUpload, - ev: XMLHttpRequestEventTargetEventMap[K], - ) => any, - ): void; - // removeEventListener(type: string, listener: EventListenerOrEventListenerObject): void; -} - -declare var XMLHttpRequestUpload: { - prototype: XMLHttpRequestUpload; - new (): XMLHttpRequestUpload; -}; - -declare type XMLHttpRequestResponseType = - | '' - | 'arraybuffer' - | 'blob' - | 'document' - | 'json' - | 'text'; - -/** - * Based on definition from lib.dom but using class syntax. - * The properties are mutable to support users that use a `URL` polyfill, but the implementation - * built into React Native (as of 0.63) does not implement all the properties. - */ -declare class URL { - static createObjectURL(blob: Blob): string; - static revokeObjectURL(url: string): void; - - constructor(url: string, base?: string); - - href: string; - readonly origin: string; - protocol: string; - username: string; - password: string; - host: string; - hostname: string; - port: string; - pathname: string; - search: string; - readonly searchParams: URLSearchParams; - hash: string; - - toJSON(): string; -} - -/** - * Based on definitions of lib.dom and lib.dom.iterable - */ -declare class URLSearchParams { - constructor( - init?: string[][] | Record | string | URLSearchParams, - ); - - append(name: string, value: string): void; - delete(name: string): void; - get(name: string): string | null; - getAll(name: string): string[]; - has(name: string): boolean; - set(name: string, value: string): void; - sort(): void; - forEach( - callbackfn: (value: string, key: string, parent: URLSearchParams) => void, - thisArg?: any, - ): void; - [Symbol.iterator](): IterableIterator<[string, string]>; - - entries(): IterableIterator<[string, string]>; - keys(): IterableIterator; - values(): IterableIterator; -} - -interface WebSocketMessageEvent extends Event { - data?: any | undefined; -} -interface WebSocketErrorEvent extends Event { - message: string; -} -interface WebSocketCloseEvent extends Event { - code?: number | undefined; - reason?: string | undefined; - message?: string | undefined; -} - -type WebsocketMessageEventListener = ( - event: 'message', - handler: (e: WebSocketMessageEvent) => void, -) => void; -type WebsocketErrorEventListener = ( - event: 'error', - handler: (e: WebSocketErrorEvent) => void, -) => void; -type WebsocketOpenEventListener = (event: 'open', handler: () => void) => void; -type WebsocketCloseEventListener = ( - event: 'close', - handler: (e: WebSocketCloseEvent) => void, -) => void; - -type WebsocketEventListener = WebsocketMessageEventListener & - WebsocketErrorEventListener & - WebsocketOpenEventListener & - WebsocketCloseEventListener; - -interface WebSocket extends EventTarget { - readonly readyState: number; - send(data: string | ArrayBuffer | ArrayBufferView | Blob): void; - close(code?: number, reason?: string): void; - ping(): void; - onopen: (() => void) | null; - onmessage: ((event: WebSocketMessageEvent) => void) | null; - onerror: ((event: WebSocketErrorEvent) => void) | null; - onclose: ((event: WebSocketCloseEvent) => void) | null; - addEventListener: WebsocketEventListener; - removeEventListener: WebsocketEventListener; -} - -declare var WebSocket: { - prototype: WebSocket; - new ( - uri: string, - protocols?: string | string[] | null, - options?: { - headers: {[headerName: string]: string}; - [optionName: string]: any; - } | null, - ): WebSocket; - readonly CLOSED: number; - readonly CLOSING: number; - readonly CONNECTING: number; - readonly OPEN: number; -}; - -// -// Abort Controller -// - -interface AbortEvent extends Event { - type: 'abort'; -} - -declare class AbortSignal implements EventTarget { - /** - * AbortSignal cannot be constructed directly. - */ - constructor(); - /** - * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise. - */ - readonly aborted: boolean; - - onabort: (event: AbortEvent) => void; - - addEventListener: ( - type: 'abort', - listener: (this: AbortSignal, event: any) => any, - options?: - | boolean - | { - capture?: boolean | undefined; - once?: boolean | undefined; - passive?: boolean | undefined; - }, - ) => void; - - removeEventListener: ( - type: 'abort', - listener: (this: AbortSignal, event: any) => any, - options?: - | boolean - | { - capture?: boolean | undefined; - }, - ) => void; -} - -declare class AbortController { - /** - * Initialize this controller. - */ - constructor(); - /** - * Returns the `AbortSignal` object associated with this object. - */ - readonly signal: AbortSignal; - /** - * Abort and signal to any observers that the associated activity is to be aborted. - */ - abort(): void; -} - -interface FileReaderEventMap { - abort: ProgressEvent; - error: ProgressEvent; - load: ProgressEvent; - loadend: ProgressEvent; - loadstart: ProgressEvent; - progress: ProgressEvent; -} - -interface FileReader extends EventTarget { - readonly error: Error | null; - onabort: ((this: FileReader, ev: ProgressEvent) => any) | null; - onerror: ((this: FileReader, ev: ProgressEvent) => any) | null; - onload: ((this: FileReader, ev: ProgressEvent) => any) | null; - onloadend: ((this: FileReader, ev: ProgressEvent) => any) | null; - onloadstart: - | ((this: FileReader, ev: ProgressEvent) => any) - | null; - onprogress: ((this: FileReader, ev: ProgressEvent) => any) | null; - readonly readyState: number; - readonly result: string | ArrayBuffer; - abort(): void; - readAsArrayBuffer(blob: Blob): void; - // readAsBinaryString(blob: Blob): void; - readAsDataURL(blob: Blob): void; - readAsText(blob: Blob, encoding?: string): void; - readonly DONE: 2; - readonly EMPTY: 0; - readonly LOADING: 1; - addEventListener( - type: K, - listener: (this: FileReader, ev: FileReaderEventMap[K]) => any, - options?: boolean, - ): void; - // addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; - removeEventListener( - type: K, - listener: (this: FileReader, ev: FileReaderEventMap[K]) => any, - options?: boolean, - ): void; - // removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void; -} - -declare var FileReader: { - prototype: FileReader; - new (): FileReader; - readonly DONE: 2; - readonly EMPTY: 0; - readonly LOADING: 1; -};