UNPKG

6.19 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}
125
126declare class Body {
127 constructor(body?: any, opts?: { size?: number | undefined; timeout?: number | undefined });
128 arrayBuffer(): Promise<ArrayBuffer>;
129 blob(): Promise<Blob>;
130 body: NodeJS.ReadableStream;
131 bodyUsed: boolean;
132 buffer(): Promise<Buffer>;
133 json(): Promise<any>;
134 size: number;
135 text(): Promise<string>;
136 textConverted(): Promise<string>;
137 timeout: number;
138}
139
140interface SystemError extends Error {
141 code?: string | undefined;
142}
143
144declare class AbortError extends Error {
145 readonly name: "AbortError";
146 constructor(message: string);
147 readonly type: "aborted";
148}
149
150declare class FetchError extends Error {
151 name: "FetchError";
152 constructor(message: string, type: string, systemError?: SystemError);
153 type: string;
154 code?: string | undefined;
155 errno?: string | undefined;
156}
157
158declare class Response extends Body {
159 constructor(body?: BodyInit, init?: ResponseInit);
160 static error(): Response;
161 static redirect(url: string, status: number): Response;
162 clone(): Response;
163 headers: Headers;
164 ok: boolean;
165 redirected: boolean;
166 status: number;
167 statusText: string;
168 type: ResponseType;
169 url: string;
170}
171
172type ResponseType =
173 | "basic"
174 | "cors"
175 | "default"
176 | "error"
177 | "opaque"
178 | "opaqueredirect";
179
180interface ResponseInit {
181 headers?: HeadersInit | undefined;
182 size?: number | undefined;
183 status?: number | undefined;
184 statusText?: string | undefined;
185 timeout?: number | undefined;
186 url?: string | undefined;
187 counter?: number | undefined;
188}
189
190interface URLLike {
191 href: string;
192}
193
194type HeadersInit = Headers | string[][] | { [key: string]: string | string[] };
195type BodyInit =
196 | ArrayBuffer
197 | ArrayBufferView
198 | NodeJS.ReadableStream
199 | string
200 | URLSearchParams
201 | FormData;
202type RequestInfo = string | URLLike | Request;
203
204declare function fetch(
205 url: RequestInfo,
206 init?: RequestInit,
207): Promise<Response>;
208
209declare namespace fetch {
210 export {
211 AbortError,
212 Blob,
213 Body,
214 BodyInit,
215 FetchError,
216 Headers,
217 HeadersInit,
218 // HeaderInit is exported to support backwards compatibility. See PR #34382
219 HeadersInit as HeaderInit,
220 Request,
221 RequestCache,
222 RequestContext,
223 RequestCredentials,
224 RequestInfo,
225 RequestInit,
226 RequestMode,
227 RequestRedirect,
228 Response,
229 ResponseInit,
230 ResponseType,
231 };
232 export function isRedirect(code: number): boolean;
233
234 import _default = fetch;
235 export { _default as default };
236}
237
238export = fetch;
239
\No newline at end of file