UNPKG

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