UNPKG

6.23 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import FormData = require("form-data");
4import { RequestOptions } from "http";
5import { URL, URLSearchParams } from "url";
6import { AbortSignal } from "./externals";
7
8declare class Request extends Body {
9 constructor(input: RequestInfo, init?: RequestInit);
10 clone(): Request;
11 context: RequestContext;
12 headers: Headers;
13 method: string;
14 redirect: RequestRedirect;
15 referrer: string;
16 url: string;
17
18 // node-fetch extensions to the whatwg/fetch spec
19 agent?: RequestOptions["agent"] | ((parsedUrl: URL) => RequestOptions["agent"]);
20 compress: boolean;
21 counter: number;
22 follow: number;
23 hostname: string;
24 port?: number | undefined;
25 protocol: string;
26 size: number;
27 timeout: number;
28}
29
30interface RequestInit {
31 // whatwg/fetch standard options
32 body?: BodyInit | undefined;
33 headers?: HeadersInit | undefined;
34 method?: string | undefined;
35 redirect?: RequestRedirect | undefined;
36 signal?: AbortSignal | null | undefined;
37
38 // node-fetch extensions
39 agent?: RequestOptions["agent"] | ((parsedUrl: URL) => RequestOptions["agent"]); // =null http.Agent instance, allows custom proxy, certificate etc.
40 compress?: boolean | undefined; // =true support gzip/deflate content encoding. false to disable
41 follow?: number | undefined; // =20 maximum redirect count. 0 to not follow redirect
42 size?: number | undefined; // =0 maximum response body size in bytes. 0 to disable
43 timeout?: number | undefined; // =0 req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
44
45 // node-fetch does not support mode, cache or credentials options
46}
47
48type RequestContext =
49 | "audio"
50 | "beacon"
51 | "cspreport"
52 | "download"
53 | "embed"
54 | "eventsource"
55 | "favicon"
56 | "fetch"
57 | "font"
58 | "form"
59 | "frame"
60 | "hyperlink"
61 | "iframe"
62 | "image"
63 | "imageset"
64 | "import"
65 | "internal"
66 | "location"
67 | "manifest"
68 | "object"
69 | "ping"
70 | "plugin"
71 | "prefetch"
72 | "script"
73 | "serviceworker"
74 | "sharedworker"
75 | "style"
76 | "subresource"
77 | "track"
78 | "video"
79 | "worker"
80 | "xmlhttprequest"
81 | "xslt";
82type RequestMode = "cors" | "no-cors" | "same-origin";
83type RequestRedirect = "error" | "follow" | "manual";
84type RequestCredentials = "omit" | "include" | "same-origin";
85
86type RequestCache =
87 | "default"
88 | "force-cache"
89 | "no-cache"
90 | "no-store"
91 | "only-if-cached"
92 | "reload";
93
94declare class Headers implements Iterable<[string, string]> {
95 constructor(init?: HeadersInit);
96 forEach(callback: (value: string, name: string) => void): void;
97 append(name: string, value: string): void;
98 delete(name: string): void;
99 get(name: string): string | null;
100 has(name: string): boolean;
101 raw(): { [k: string]: string[] };
102 set(name: string, value: string): void;
103
104 // Iterable methods
105 entries(): IterableIterator<[string, string]>;
106 keys(): IterableIterator<string>;
107 values(): IterableIterator<string>;
108 [Symbol.iterator](): Iterator<[string, string]>;
109}
110
111type BlobPart = ArrayBuffer | ArrayBufferView | Blob | string;
112
113interface BlobOptions {
114 type?: string | undefined;
115 endings?: "transparent" | "native" | undefined;
116}
117
118declare class Blob {
119 constructor(blobParts?: BlobPart[], options?: BlobOptions);
120 readonly type: string;
121 readonly size: number;
122 slice(start?: number, end?: number): Blob;
123 text(): Promise<string>;
124 arrayBuffer(): Promise<ArrayBuffer>;
125}
126
127declare class Body {
128 constructor(body?: any, opts?: { size?: number | undefined; timeout?: number | undefined });
129 arrayBuffer(): Promise<ArrayBuffer>;
130 blob(): Promise<Blob>;
131 body: NodeJS.ReadableStream;
132 bodyUsed: boolean;
133 buffer(): Promise<Buffer>;
134 json(): Promise<any>;
135 size: number;
136 text(): Promise<string>;
137 textConverted(): Promise<string>;
138 timeout: number;
139}
140
141interface SystemError extends Error {
142 code?: string | undefined;
143}
144
145declare class AbortError extends Error {
146 readonly name: "AbortError";
147 constructor(message: string);
148 readonly type: "aborted";
149}
150
151declare class FetchError extends Error {
152 name: "FetchError";
153 constructor(message: string, type: string, systemError?: SystemError);
154 type: string;
155 code?: string | undefined;
156 errno?: string | undefined;
157}
158
159declare class Response extends Body {
160 constructor(body?: BodyInit, init?: ResponseInit);
161 static error(): Response;
162 static redirect(url: string, status: number): Response;
163 clone(): Response;
164 headers: Headers;
165 ok: boolean;
166 redirected: boolean;
167 status: number;
168 statusText: string;
169 type: ResponseType;
170 url: string;
171}
172
173type ResponseType =
174 | "basic"
175 | "cors"
176 | "default"
177 | "error"
178 | "opaque"
179 | "opaqueredirect";
180
181interface ResponseInit {
182 headers?: HeadersInit | undefined;
183 size?: number | undefined;
184 status?: number | undefined;
185 statusText?: string | undefined;
186 timeout?: number | undefined;
187 url?: string | undefined;
188 counter?: number | undefined;
189}
190
191interface URLLike {
192 href: string;
193}
194
195type HeadersInit = Headers | string[][] | { [key: string]: string | string[] };
196type BodyInit =
197 | ArrayBuffer
198 | ArrayBufferView
199 | NodeJS.ReadableStream
200 | string
201 | URLSearchParams
202 | FormData;
203type RequestInfo = string | URLLike | Request;
204
205declare function fetch(
206 url: RequestInfo,
207 init?: RequestInit,
208): Promise<Response>;
209
210declare namespace fetch {
211 export {
212 AbortError,
213 Blob,
214 Body,
215 BodyInit,
216 FetchError,
217 Headers,
218 HeadersInit,
219 // HeaderInit is exported to support backwards compatibility. See PR #34382
220 HeadersInit as HeaderInit,
221 Request,
222 RequestCache,
223 RequestContext,
224 RequestCredentials,
225 RequestInfo,
226 RequestInit,
227 RequestMode,
228 RequestRedirect,
229 Response,
230 ResponseInit,
231 ResponseType,
232 };
233 export function isRedirect(code: number): boolean;
234
235 import _default = fetch;
236 export { _default as default };
237}
238
239export = fetch;
240
\No newline at end of file