UNPKG

5.44 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 // Node-fetch extensions to the whatwg/fetch spec
76 agent?: Agent | ((parsedUrl: URL) => Agent);
77 compress?: boolean;
78 counter?: number;
79 follow?: number;
80 hostname?: string;
81 port?: number;
82 protocol?: string;
83 size?: number;
84 highWaterMark?: number;
85 insecureHTTPParser?: boolean;
86}
87
88export interface ResponseInit {
89 headers?: HeadersInit;
90 status?: number;
91 statusText?: string;
92}
93
94export type BodyInit =
95 | Blob
96 | Buffer
97 | URLSearchParams
98 | NodeJS.ReadableStream
99 | string;
100declare class BodyMixin {
101 constructor(body?: BodyInit, options?: {size?: number});
102
103 readonly body: NodeJS.ReadableStream | null;
104 readonly bodyUsed: boolean;
105 readonly size: number;
106
107 buffer(): Promise<Buffer>;
108 arrayBuffer(): Promise<ArrayBuffer>;
109 blob(): Promise<Blob>;
110 json(): Promise<unknown>;
111 text(): Promise<string>;
112}
113
114// `Body` must not be exported as a class since it's not exported from the JavaScript code.
115export interface Body extends Pick<BodyMixin, keyof BodyMixin> {}
116
117export type RequestRedirect = 'error' | 'follow' | 'manual';
118export type RequestInfo = string | Request;
119export class Request extends BodyMixin {
120 constructor(input: RequestInfo, init?: RequestInit);
121
122 /**
123 * 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.
124 */
125 readonly headers: Headers;
126 /**
127 * Returns request's HTTP method, which is "GET" by default.
128 */
129 readonly method: string;
130 /**
131 * 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.
132 */
133 readonly redirect: RequestRedirect;
134 /**
135 * Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler.
136 */
137 readonly signal: AbortSignal;
138 /**
139 * Returns the URL of request as a string.
140 */
141 readonly url: string;
142 clone(): Request;
143}
144
145type ResponseType = 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
146
147export class Response extends BodyMixin {
148 constructor(body?: BodyInit | null, init?: ResponseInit);
149
150 readonly headers: Headers;
151 readonly ok: boolean;
152 readonly redirected: boolean;
153 readonly status: number;
154 readonly statusText: string;
155 readonly type: ResponseType;
156 readonly url: string;
157 clone(): Response;
158
159 static error(): Response;
160}
161
162export class FetchError extends Error {
163 constructor(message: string, type: string, systemError?: Record<string, unknown>);
164
165 name: 'FetchError';
166 [Symbol.toStringTag]: 'FetchError';
167 type: string;
168 code?: string;
169 errno?: string;
170}
171
172export class AbortError extends Error {
173 type: string;
174 name: 'AbortError';
175 [Symbol.toStringTag]: 'AbortError';
176}
177
178export function isRedirect(code: number): boolean;
179export default function fetch(url: RequestInfo, init?: RequestInit): Promise<Response>;
180
\No newline at end of file