UNPKG

6.64 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import {RequestOptions} from 'http';
4import {FormData} from 'formdata-polyfill/esm.min.js';
5import {
6 Blob,
7 blobFrom,
8 blobFromSync,
9 File,
10 fileFrom,
11 fileFromSync
12} from 'fetch-blob/from.js';
13
14type AbortSignal = {
15 readonly aborted: boolean;
16
17 addEventListener: (type: 'abort', listener: (this: AbortSignal) => void) => void;
18 removeEventListener: (type: 'abort', listener: (this: AbortSignal) => void) => void;
19};
20
21export type HeadersInit = Headers | Record<string, string> | Iterable<readonly [string, string]> | Iterable<Iterable<string>>;
22
23export {
24 FormData,
25 Blob,
26 blobFrom,
27 blobFromSync,
28 File,
29 fileFrom,
30 fileFromSync
31};
32
33/**
34 * This Fetch API interface allows you to perform various actions on HTTP request and response headers.
35 * These actions include retrieving, setting, adding to, and removing.
36 * A Headers object has an associated header list, which is initially empty and consists of zero or more name and value pairs.
37 * You can add to this using methods like append() (see Examples.)
38 * In all methods of this interface, header names are matched by case-insensitive byte sequence.
39 * */
40export class Headers {
41 constructor(init?: HeadersInit);
42
43 append(name: string, value: string): void;
44 delete(name: string): void;
45 get(name: string): string | null;
46 has(name: string): boolean;
47 set(name: string, value: string): void;
48 forEach(
49 callbackfn: (value: string, key: string, parent: Headers) => void,
50 thisArg?: any
51 ): void;
52
53 [Symbol.iterator](): IterableIterator<[string, string]>;
54 /**
55 * Returns an iterator allowing to go through all key/value pairs contained in this object.
56 */
57 entries(): IterableIterator<[string, string]>;
58 /**
59 * Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
60 */
61 keys(): IterableIterator<string>;
62 /**
63 * Returns an iterator allowing to go through all values of the key/value pairs contained in this object.
64 */
65 values(): IterableIterator<string>;
66
67 /** Node-fetch extension */
68 raw(): Record<string, string[]>;
69}
70
71export interface RequestInit {
72 /**
73 * A BodyInit object or null to set request's body.
74 */
75 body?: BodyInit | null;
76 /**
77 * A Headers object, an object literal, or an array of two-item arrays to set request's headers.
78 */
79 headers?: HeadersInit;
80 /**
81 * A string to set request's method.
82 */
83 method?: string;
84 /**
85 * A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
86 */
87 redirect?: RequestRedirect;
88 /**
89 * An AbortSignal to set request's signal.
90 */
91 signal?: AbortSignal | null;
92 /**
93 * A string whose value is a same-origin URL, "about:client", or the empty string, to set request’s referrer.
94 */
95 referrer?: string;
96 /**
97 * A referrer policy to set request’s referrerPolicy.
98 */
99 referrerPolicy?: ReferrerPolicy;
100
101 // Node-fetch extensions to the whatwg/fetch spec
102 agent?: RequestOptions['agent'] | ((parsedUrl: URL) => RequestOptions['agent']);
103 compress?: boolean;
104 counter?: number;
105 follow?: number;
106 hostname?: string;
107 port?: number;
108 protocol?: string;
109 size?: number;
110 highWaterMark?: number;
111 insecureHTTPParser?: boolean;
112}
113
114export interface ResponseInit {
115 headers?: HeadersInit;
116 status?: number;
117 statusText?: string;
118}
119
120export type BodyInit =
121 | Blob
122 | Buffer
123 | URLSearchParams
124 | FormData
125 | NodeJS.ReadableStream
126 | string;
127declare class BodyMixin {
128 constructor(body?: BodyInit, options?: {size?: number});
129
130 readonly body: NodeJS.ReadableStream | null;
131 readonly bodyUsed: boolean;
132 readonly size: number;
133
134 /** @deprecated Use `body.arrayBuffer()` instead. */
135 buffer(): Promise<Buffer>;
136 arrayBuffer(): Promise<ArrayBuffer>;
137 formData(): Promise<FormData>;
138 blob(): Promise<Blob>;
139 json(): Promise<unknown>;
140 text(): Promise<string>;
141}
142
143// `Body` must not be exported as a class since it's not exported from the JavaScript code.
144export interface Body extends Pick<BodyMixin, keyof BodyMixin> {}
145
146export type RequestRedirect = 'error' | 'follow' | 'manual';
147export type ReferrerPolicy = '' | 'no-referrer' | 'no-referrer-when-downgrade' | 'same-origin' | 'origin' | 'strict-origin' | 'origin-when-cross-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url';
148export type RequestInfo = string | Request;
149export class Request extends BodyMixin {
150 constructor(input: URL | RequestInfo, init?: RequestInit);
151
152 /**
153 * Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.
154 */
155 readonly headers: Headers;
156 /**
157 * Returns request's HTTP method, which is "GET" by default.
158 */
159 readonly method: string;
160 /**
161 * Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default.
162 */
163 readonly redirect: RequestRedirect;
164 /**
165 * Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler.
166 */
167 readonly signal: AbortSignal;
168 /**
169 * Returns the URL of request as a string.
170 */
171 readonly url: string;
172 /**
173 * A string whose value is a same-origin URL, "about:client", or the empty string, to set requests referrer.
174 */
175 readonly referrer: string;
176 /**
177 * A referrer policy to set requests referrerPolicy.
178 */
179 readonly referrerPolicy: ReferrerPolicy;
180 clone(): Request;
181}
182
183type ResponseType = 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
184
185export class Response extends BodyMixin {
186 constructor(body?: BodyInit | null, init?: ResponseInit);
187
188 readonly headers: Headers;
189 readonly ok: boolean;
190 readonly redirected: boolean;
191 readonly status: number;
192 readonly statusText: string;
193 readonly type: ResponseType;
194 readonly url: string;
195 clone(): Response;
196
197 static error(): Response;
198 static redirect(url: string, status?: number): Response;
199 static json(data: any, init?: ResponseInit): Response;
200}
201
202export class FetchError extends Error {
203 constructor(message: string, type: string, systemError?: Record<string, unknown>);
204
205 name: 'FetchError';
206 [Symbol.toStringTag]: 'FetchError';
207 type: string;
208 code?: string;
209 errno?: string;
210}
211
212export class AbortError extends Error {
213 type: string;
214 name: 'AbortError';
215 [Symbol.toStringTag]: 'AbortError';
216}
217
218export function isRedirect(code: number): boolean;
219export default function fetch(url: URL | RequestInfo, init?: RequestInit): Promise<Response>;
220
\No newline at end of file