|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +// Modified from flow-typed repo: |
| 13 | +// https://github.com/flow-typed/flow-typed/blob/master/definitions/npm/node-fetch_v2.x.x/flow_v0.104.x-/node-fetch_v2.x.x.js |
| 14 | + |
| 15 | +declare module 'node-fetch' { |
| 16 | + import type http from 'http'; |
| 17 | + import type https from 'https'; |
| 18 | + import type {URL} from 'url'; |
| 19 | + import type {Readable} from 'stream'; |
| 20 | + |
| 21 | + declare export type AbortSignal = { |
| 22 | + +aborted: boolean, |
| 23 | + +onabort: (event?: {...}) => void, |
| 24 | + |
| 25 | + +addEventListener: (name: string, cb: () => mixed) => void, |
| 26 | + +removeEventListener: (name: string, cb: () => mixed) => void, |
| 27 | + +dispatchEvent: (event: {...}) => void, |
| 28 | + ... |
| 29 | + }; |
| 30 | + |
| 31 | + declare export class Request mixins Body { |
| 32 | + constructor( |
| 33 | + input: string | {href: string, ...} | Request, |
| 34 | + init?: RequestInit, |
| 35 | + ): this; |
| 36 | + context: RequestContext; |
| 37 | + headers: Headers; |
| 38 | + method: string; |
| 39 | + redirect: RequestRedirect; |
| 40 | + referrer: string; |
| 41 | + url: string; |
| 42 | + |
| 43 | + // node-fetch extensions |
| 44 | + agent: http.Agent | https.Agent; |
| 45 | + compress: boolean; |
| 46 | + counter: number; |
| 47 | + follow: number; |
| 48 | + hostname: string; |
| 49 | + port: number; |
| 50 | + protocol: string; |
| 51 | + size: number; |
| 52 | + timeout: number; |
| 53 | + } |
| 54 | + |
| 55 | + declare type HeaderObject = {[index: string]: string | number, ...}; |
| 56 | + |
| 57 | + declare export type RequestInit = {| |
| 58 | + body?: BodyInit, |
| 59 | + headers?: HeaderObject | null, |
| 60 | + method?: string, |
| 61 | + redirect?: RequestRedirect, |
| 62 | + signal?: AbortSignal | null, |
| 63 | + |
| 64 | + // node-fetch extensions |
| 65 | + agent?: (URL => http.Agent | https.Agent) | http.Agent | https.Agent | null, |
| 66 | + compress?: boolean, |
| 67 | + follow?: number, |
| 68 | + size?: number, |
| 69 | + timeout?: number, |
| 70 | + |}; |
| 71 | + |
| 72 | + declare export interface FetchError extends Error { |
| 73 | + // cannot set name due to incompatible extend error |
| 74 | + // name: 'FetchError'; |
| 75 | + type: string; |
| 76 | + code: ?number; |
| 77 | + errno: ?number; |
| 78 | + } |
| 79 | + |
| 80 | + declare export interface AbortError extends Error { |
| 81 | + // cannot set name due to incompatible extend error |
| 82 | + // name: 'AbortError'; |
| 83 | + type: 'aborted'; |
| 84 | + } |
| 85 | + |
| 86 | + declare type RequestContext = |
| 87 | + | 'audio' |
| 88 | + | 'beacon' |
| 89 | + | 'cspreport' |
| 90 | + | 'download' |
| 91 | + | 'embed' |
| 92 | + | 'eventsource' |
| 93 | + | 'favicon' |
| 94 | + | 'fetch' |
| 95 | + | 'font' |
| 96 | + | 'form' |
| 97 | + | 'frame' |
| 98 | + | 'hyperlink' |
| 99 | + | 'iframe' |
| 100 | + | 'image' |
| 101 | + | 'imageset' |
| 102 | + | 'import' |
| 103 | + | 'internal' |
| 104 | + | 'location' |
| 105 | + | 'manifest' |
| 106 | + | 'object' |
| 107 | + | 'ping' |
| 108 | + | 'plugin' |
| 109 | + | 'prefetch' |
| 110 | + | 'script' |
| 111 | + | 'serviceworker' |
| 112 | + | 'sharedworker' |
| 113 | + | 'subresource' |
| 114 | + | 'style' |
| 115 | + | 'track' |
| 116 | + | 'video' |
| 117 | + | 'worker' |
| 118 | + | 'xmlhttprequest' |
| 119 | + | 'xslt'; |
| 120 | + declare type RequestRedirect = 'error' | 'follow' | 'manual'; |
| 121 | + |
| 122 | + declare export class Headers { |
| 123 | + append(name: string, value: string): void; |
| 124 | + delete(name: string): void; |
| 125 | + forEach(callback: (value: string, name: string) => void): void; |
| 126 | + get(name: string): string; |
| 127 | + getAll(name: string): Array<string>; |
| 128 | + has(name: string): boolean; |
| 129 | + raw(): {[k: string]: string[], ...}; |
| 130 | + set(name: string, value: string): void; |
| 131 | + entries(): Iterator<[string, string]>; |
| 132 | + keys(): Iterator<string>; |
| 133 | + values(): Iterator<string>; |
| 134 | + @@iterator(): Iterator<[string, string]>; |
| 135 | + } |
| 136 | + |
| 137 | + declare export class Body { |
| 138 | + buffer(): Promise<Buffer>; |
| 139 | + json(): Promise<any>; |
| 140 | + json<T>(): Promise<T>; |
| 141 | + text(): Promise<string>; |
| 142 | + body: stream$Readable; |
| 143 | + bodyUsed: boolean; |
| 144 | + } |
| 145 | + |
| 146 | + declare export class Response mixins Body { |
| 147 | + constructor(body?: BodyInit, init?: ResponseInit): this; |
| 148 | + clone(): Response; |
| 149 | + error(): Response; |
| 150 | + redirect(url: string, status: number): Response; |
| 151 | + headers: Headers; |
| 152 | + ok: boolean; |
| 153 | + status: number; |
| 154 | + statusText: string; |
| 155 | + size: number; |
| 156 | + timeout: number; |
| 157 | + type: ResponseType; |
| 158 | + url: string; |
| 159 | + } |
| 160 | + |
| 161 | + declare type ResponseType = |
| 162 | + | 'basic' |
| 163 | + | 'cors' |
| 164 | + | 'default' |
| 165 | + | 'error' |
| 166 | + | 'opaque' |
| 167 | + | 'opaqueredirect'; |
| 168 | + |
| 169 | + declare interface ResponseInit { |
| 170 | + headers?: HeaderInit; |
| 171 | + status: number; |
| 172 | + statusText?: string; |
| 173 | + } |
| 174 | + |
| 175 | + declare type HeaderInit = Headers | Array<string>; |
| 176 | + declare type BodyInit = |
| 177 | + | string |
| 178 | + | null |
| 179 | + | Buffer |
| 180 | + | Blob |
| 181 | + | Readable |
| 182 | + | URLSearchParams; |
| 183 | + |
| 184 | + declare export default function fetch( |
| 185 | + url: string | Request, |
| 186 | + init?: RequestInit, |
| 187 | + ): Promise<Response>; |
| 188 | +} |
0 commit comments