fetch.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // based on https://github.com/Ethan-Arrowood/undici-fetch/blob/249269714db874351589d2d364a0645d5160ae71/index.d.ts (MIT license)
  2. // and https://github.com/node-fetch/node-fetch/blob/914ce6be5ec67a8bab63d68510aabf07cb818b6d/index.d.ts (MIT license)
  3. /// <reference types="node" />
  4. import { Blob } from 'buffer'
  5. import { URL, URLSearchParams } from 'url'
  6. import { ReadableStream } from 'stream/web'
  7. import { FormData } from './formdata'
  8. import { HeaderRecord } from './header'
  9. import Dispatcher from './dispatcher'
  10. export type RequestInfo = string | URL | Request
  11. export declare function fetch (
  12. input: RequestInfo,
  13. init?: RequestInit
  14. ): Promise<Response>
  15. export type BodyInit =
  16. | ArrayBuffer
  17. | AsyncIterable<Uint8Array>
  18. | Blob
  19. | FormData
  20. | Iterable<Uint8Array>
  21. | NodeJS.ArrayBufferView
  22. | URLSearchParams
  23. | null
  24. | string
  25. export class BodyMixin {
  26. readonly body: ReadableStream | null
  27. readonly bodyUsed: boolean
  28. readonly arrayBuffer: () => Promise<ArrayBuffer>
  29. readonly blob: () => Promise<Blob>
  30. readonly bytes: () => Promise<Uint8Array>
  31. /**
  32. * @deprecated This method is not recommended for parsing multipart/form-data bodies in server environments.
  33. * It is recommended to use a library such as [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy) as follows:
  34. *
  35. * @example
  36. * ```js
  37. * import { Busboy } from '@fastify/busboy'
  38. * import { Readable } from 'node:stream'
  39. *
  40. * const response = await fetch('...')
  41. * const busboy = new Busboy({ headers: { 'content-type': response.headers.get('content-type') } })
  42. *
  43. * // handle events emitted from `busboy`
  44. *
  45. * Readable.fromWeb(response.body).pipe(busboy)
  46. * ```
  47. */
  48. readonly formData: () => Promise<FormData>
  49. readonly json: () => Promise<unknown>
  50. readonly text: () => Promise<string>
  51. }
  52. export interface SpecIterator<T, TReturn = any, TNext = undefined> {
  53. next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
  54. }
  55. export interface SpecIterableIterator<T> extends SpecIterator<T> {
  56. [Symbol.iterator](): SpecIterableIterator<T>;
  57. }
  58. export interface SpecIterable<T> {
  59. [Symbol.iterator](): SpecIterator<T>;
  60. }
  61. export type HeadersInit = [string, string][] | HeaderRecord | Headers
  62. export declare class Headers implements SpecIterable<[string, string]> {
  63. constructor (init?: HeadersInit)
  64. readonly append: (name: string, value: string) => void
  65. readonly delete: (name: string) => void
  66. readonly get: (name: string) => string | null
  67. readonly has: (name: string) => boolean
  68. readonly set: (name: string, value: string) => void
  69. readonly getSetCookie: () => string[]
  70. readonly forEach: (
  71. callbackfn: (value: string, key: string, iterable: Headers) => void,
  72. thisArg?: unknown
  73. ) => void
  74. readonly keys: () => SpecIterableIterator<string>
  75. readonly values: () => SpecIterableIterator<string>
  76. readonly entries: () => SpecIterableIterator<[string, string]>
  77. readonly [Symbol.iterator]: () => SpecIterableIterator<[string, string]>
  78. }
  79. export type RequestCache =
  80. | 'default'
  81. | 'force-cache'
  82. | 'no-cache'
  83. | 'no-store'
  84. | 'only-if-cached'
  85. | 'reload'
  86. export type RequestCredentials = 'omit' | 'include' | 'same-origin'
  87. type RequestDestination =
  88. | ''
  89. | 'audio'
  90. | 'audioworklet'
  91. | 'document'
  92. | 'embed'
  93. | 'font'
  94. | 'image'
  95. | 'manifest'
  96. | 'object'
  97. | 'paintworklet'
  98. | 'report'
  99. | 'script'
  100. | 'sharedworker'
  101. | 'style'
  102. | 'track'
  103. | 'video'
  104. | 'worker'
  105. | 'xslt'
  106. export interface RequestInit {
  107. body?: BodyInit | null
  108. cache?: RequestCache
  109. credentials?: RequestCredentials
  110. dispatcher?: Dispatcher
  111. duplex?: RequestDuplex
  112. headers?: HeadersInit
  113. integrity?: string
  114. keepalive?: boolean
  115. method?: string
  116. mode?: RequestMode
  117. redirect?: RequestRedirect
  118. referrer?: string
  119. referrerPolicy?: ReferrerPolicy
  120. signal?: AbortSignal | null
  121. window?: null
  122. }
  123. export type ReferrerPolicy =
  124. | ''
  125. | 'no-referrer'
  126. | 'no-referrer-when-downgrade'
  127. | 'origin'
  128. | 'origin-when-cross-origin'
  129. | 'same-origin'
  130. | 'strict-origin'
  131. | 'strict-origin-when-cross-origin'
  132. | 'unsafe-url'
  133. export type RequestMode = 'cors' | 'navigate' | 'no-cors' | 'same-origin'
  134. export type RequestRedirect = 'error' | 'follow' | 'manual'
  135. export type RequestDuplex = 'half'
  136. export declare class Request extends BodyMixin {
  137. constructor (input: RequestInfo, init?: RequestInit)
  138. readonly cache: RequestCache
  139. readonly credentials: RequestCredentials
  140. readonly destination: RequestDestination
  141. readonly headers: Headers
  142. readonly integrity: string
  143. readonly method: string
  144. readonly mode: RequestMode
  145. readonly redirect: RequestRedirect
  146. readonly referrer: string
  147. readonly referrerPolicy: ReferrerPolicy
  148. readonly url: string
  149. readonly keepalive: boolean
  150. readonly signal: AbortSignal
  151. readonly duplex: RequestDuplex
  152. readonly clone: () => Request
  153. }
  154. export interface ResponseInit {
  155. readonly status?: number
  156. readonly statusText?: string
  157. readonly headers?: HeadersInit
  158. }
  159. export type ResponseType =
  160. | 'basic'
  161. | 'cors'
  162. | 'default'
  163. | 'error'
  164. | 'opaque'
  165. | 'opaqueredirect'
  166. export type ResponseRedirectStatus = 301 | 302 | 303 | 307 | 308
  167. export declare class Response extends BodyMixin {
  168. constructor (body?: BodyInit, init?: ResponseInit)
  169. readonly headers: Headers
  170. readonly ok: boolean
  171. readonly status: number
  172. readonly statusText: string
  173. readonly type: ResponseType
  174. readonly url: string
  175. readonly redirected: boolean
  176. readonly clone: () => Response
  177. static error (): Response
  178. static json (data: any, init?: ResponseInit): Response
  179. static redirect (url: string | URL, status: ResponseRedirectStatus): Response
  180. }