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