fetch.d.ts 5.3 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 interface BodyMixin {
  25. readonly body: ReadableStream | null
  26. readonly bodyUsed: boolean
  27. readonly arrayBuffer: () => Promise<ArrayBuffer>
  28. readonly blob: () => Promise<Blob>
  29. readonly formData: () => Promise<FormData>
  30. readonly json: () => Promise<unknown>
  31. readonly text: () => Promise<string>
  32. }
  33. export interface SpecIterator<T, TReturn = any, TNext = undefined> {
  34. next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
  35. }
  36. export interface SpecIterableIterator<T> extends SpecIterator<T> {
  37. [Symbol.iterator](): SpecIterableIterator<T>;
  38. }
  39. export interface SpecIterable<T> {
  40. [Symbol.iterator](): SpecIterator<T>;
  41. }
  42. export type HeadersInit = string[][] | Record<string, string | ReadonlyArray<string>> | Headers
  43. export declare class Headers implements SpecIterable<[string, string]> {
  44. constructor (init?: HeadersInit)
  45. readonly append: (name: string, value: string) => void
  46. readonly delete: (name: string) => void
  47. readonly get: (name: string) => string | null
  48. readonly has: (name: string) => boolean
  49. readonly set: (name: string, value: string) => void
  50. readonly getSetCookie: () => string[]
  51. readonly forEach: (
  52. callbackfn: (value: string, key: string, iterable: Headers) => void,
  53. thisArg?: unknown
  54. ) => void
  55. readonly keys: () => SpecIterableIterator<string>
  56. readonly values: () => SpecIterableIterator<string>
  57. readonly entries: () => SpecIterableIterator<[string, string]>
  58. readonly [Symbol.iterator]: () => SpecIterator<[string, string]>
  59. }
  60. export type RequestCache =
  61. | 'default'
  62. | 'force-cache'
  63. | 'no-cache'
  64. | 'no-store'
  65. | 'only-if-cached'
  66. | 'reload'
  67. export type RequestCredentials = 'omit' | 'include' | 'same-origin'
  68. type RequestDestination =
  69. | ''
  70. | 'audio'
  71. | 'audioworklet'
  72. | 'document'
  73. | 'embed'
  74. | 'font'
  75. | 'image'
  76. | 'manifest'
  77. | 'object'
  78. | 'paintworklet'
  79. | 'report'
  80. | 'script'
  81. | 'sharedworker'
  82. | 'style'
  83. | 'track'
  84. | 'video'
  85. | 'worker'
  86. | 'xslt'
  87. export interface RequestInit {
  88. method?: string
  89. keepalive?: boolean
  90. headers?: HeadersInit
  91. body?: BodyInit
  92. redirect?: RequestRedirect
  93. integrity?: string
  94. signal?: AbortSignal
  95. credentials?: RequestCredentials
  96. mode?: RequestMode
  97. referrer?: string
  98. referrerPolicy?: ReferrerPolicy
  99. window?: null
  100. dispatcher?: Dispatcher
  101. duplex?: RequestDuplex
  102. }
  103. export type ReferrerPolicy =
  104. | ''
  105. | 'no-referrer'
  106. | 'no-referrer-when-downgrade'
  107. | 'origin'
  108. | 'origin-when-cross-origin'
  109. | 'same-origin'
  110. | 'strict-origin'
  111. | 'strict-origin-when-cross-origin'
  112. | 'unsafe-url';
  113. export type RequestMode = 'cors' | 'navigate' | 'no-cors' | 'same-origin'
  114. export type RequestRedirect = 'error' | 'follow' | 'manual'
  115. export type RequestDuplex = 'half'
  116. export declare class Request implements BodyMixin {
  117. constructor (input: RequestInfo, init?: RequestInit)
  118. readonly cache: RequestCache
  119. readonly credentials: RequestCredentials
  120. readonly destination: RequestDestination
  121. readonly headers: Headers
  122. readonly integrity: string
  123. readonly method: string
  124. readonly mode: RequestMode
  125. readonly redirect: RequestRedirect
  126. readonly referrerPolicy: string
  127. readonly url: string
  128. readonly keepalive: boolean
  129. readonly signal: AbortSignal
  130. readonly duplex: RequestDuplex
  131. readonly body: ReadableStream | null
  132. readonly bodyUsed: boolean
  133. readonly arrayBuffer: () => Promise<ArrayBuffer>
  134. readonly blob: () => Promise<Blob>
  135. readonly formData: () => Promise<FormData>
  136. readonly json: () => Promise<unknown>
  137. readonly text: () => Promise<string>
  138. readonly clone: () => Request
  139. }
  140. export interface ResponseInit {
  141. readonly status?: number
  142. readonly statusText?: string
  143. readonly headers?: HeadersInit
  144. }
  145. export type ResponseType =
  146. | 'basic'
  147. | 'cors'
  148. | 'default'
  149. | 'error'
  150. | 'opaque'
  151. | 'opaqueredirect'
  152. export type ResponseRedirectStatus = 301 | 302 | 303 | 307 | 308
  153. export declare class Response implements BodyMixin {
  154. constructor (body?: BodyInit, init?: ResponseInit)
  155. readonly headers: Headers
  156. readonly ok: boolean
  157. readonly status: number
  158. readonly statusText: string
  159. readonly type: ResponseType
  160. readonly url: string
  161. readonly redirected: boolean
  162. readonly body: ReadableStream | null
  163. readonly bodyUsed: boolean
  164. readonly arrayBuffer: () => Promise<ArrayBuffer>
  165. readonly blob: () => Promise<Blob>
  166. readonly formData: () => Promise<FormData>
  167. readonly json: () => Promise<unknown>
  168. readonly text: () => Promise<string>
  169. readonly clone: () => Response
  170. static error (): Response
  171. static json(data: any, init?: ResponseInit): Response
  172. static redirect (url: string | URL, status: ResponseRedirectStatus): Response
  173. }