UNPKG

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