UNPKG

5.17 kBTypeScriptView Raw
1// Type definitions for node-fetch 2.1
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// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
7/// <reference types="node" />
8
9import { Agent } from "http";
10import { URLSearchParams } from "url";
11
12declare namespace fetch {
13 class Request extends Body {
14 constructor(input: string | { href: string } | Request, init?: RequestInit);
15 clone(): Request;
16 context: RequestContext;
17 headers: Headers;
18 method: string;
19 redirect: RequestRedirect;
20 referrer: string;
21 url: string;
22
23 // node-fetch extensions to the whatwg/fetch spec
24 agent?: Agent;
25 compress: boolean;
26 counter: number;
27 follow: number;
28 hostname: string;
29 port?: number;
30 protocol: string;
31 size: number;
32 timeout: number;
33 }
34
35 interface RequestInit {
36 // whatwg/fetch standard options
37 body?: BodyInit;
38 headers?: HeaderInit | { [index: string]: string };
39 method?: string;
40 redirect?: RequestRedirect;
41
42 // node-fetch extensions
43 agent?: Agent; // =null http.Agent instance, allows custom proxy, certificate etc.
44 compress?: boolean; // =true support gzip/deflate content encoding. false to disable
45 follow?: number; // =20 maximum redirect count. 0 to not follow redirect
46 size?: number; // =0 maximum response body size in bytes. 0 to disable
47 timeout?: number; // =0 req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
48
49 // node-fetch does not support mode, cache or credentials options
50 }
51
52 type RequestContext =
53 "audio"
54 | "beacon"
55 | "cspreport"
56 | "download"
57 | "embed"
58 | "eventsource"
59 | "favicon"
60 | "fetch"
61 | "font"
62 | "form"
63 | "frame"
64 | "hyperlink"
65 | "iframe"
66 | "image"
67 | "imageset"
68 | "import"
69 | "internal"
70 | "location"
71 | "manifest"
72 | "object"
73 | "ping"
74 | "plugin"
75 | "prefetch"
76 | "script"
77 | "serviceworker"
78 | "sharedworker"
79 | "style"
80 | "subresource"
81 | "track"
82 | "video"
83 | "worker"
84 | "xmlhttprequest"
85 | "xslt";
86 type RequestMode = "cors" | "no-cors" | "same-origin";
87 type RequestRedirect = "error" | "follow" | "manual";
88 type RequestCredentials = "omit" | "include" | "same-origin";
89
90 type RequestCache =
91 "default"
92 | "force-cache"
93 | "no-cache"
94 | "no-store"
95 | "only-if-cached"
96 | "reload";
97
98 class Headers implements Iterable<[string, string]> {
99 constructor(init?: Headers | { [k: string]: string });
100 forEach(callback: (value: string, name: string) => void): void;
101 append(name: string, value: string): void;
102 delete(name: string): void;
103 get(name: string): string | null;
104 getAll(name: string): string[];
105 has(name: string): boolean;
106 raw(): { [k: string]: string[] };
107 set(name: string, value: string): void;
108
109 // Iterator methods
110 entries(): Iterator<[string, string]>;
111 keys(): Iterator<string>;
112 values(): Iterator<[string]>;
113 [Symbol.iterator](): Iterator<[string, string]>;
114 }
115
116 class Blob {
117 type: string;
118 size: number;
119 slice(start?: number, end?: number): Blob;
120 }
121
122 class Body {
123 constructor(body?: any, opts?: { size?: number; timeout?: number });
124 arrayBuffer(): Promise<ArrayBuffer>;
125 blob(): Promise<Buffer>;
126 body: NodeJS.ReadableStream;
127 bodyUsed: boolean;
128 buffer(): Promise<Buffer>;
129 json(): Promise<any>;
130 text(): Promise<string>;
131 textConverted(): Promise<string>;
132 }
133
134 class FetchError extends Error {
135 name: "FetchError";
136 constructor(message: string, type: string, systemError: string);
137 }
138
139 class Response extends Body {
140 constructor(body?: BodyInit, init?: ResponseInit);
141 static error(): Response;
142 static redirect(url: string, status: number): Response;
143 clone(): Response;
144 headers: Headers;
145 ok: boolean;
146 size: number;
147 status: number;
148 statusText: string;
149 timeout: number;
150 type: ResponseType;
151 url: string;
152 }
153
154 type ResponseType =
155 "basic"
156 | "cors"
157 | "default"
158 | "error"
159 | "opaque"
160 | "opaqueredirect";
161
162 interface ResponseInit {
163 headers?: HeaderInit;
164 status: number;
165 statusText?: string;
166 }
167
168 type HeaderInit = Headers | string[];
169 type BodyInit = ArrayBuffer | ArrayBufferView | NodeJS.ReadableStream | string | URLSearchParams;
170 type RequestInfo = string | Request;
171}
172
173declare function fetch(
174 url: string | fetch.Request,
175 init?: fetch.RequestInit
176): Promise<fetch.Response>;
177export = fetch;