fetch.d.ts 5.4 KB

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