UNPKG

12.8 kBTypeScriptView Raw
1/// <reference types="node" resolution-mode="require"/>
2import type { Buffer } from 'node:buffer';
3import type { Spread } from 'type-fest';
4import type { CancelableRequest } from './as-promise/types.js';
5import type { Response } from './core/response.js';
6import type Options from './core/options.js';
7import { type PaginationOptions, type OptionsInit } from './core/options.js';
8import type Request from './core/index.js';
9type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
10type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
11/**
12Defaults for each Got instance.
13*/
14export type InstanceDefaults = {
15 /**
16 An object containing the default options of Got.
17 */
18 options: Options;
19 /**
20 An array of functions. You execute them directly by calling `got()`.
21 They are some sort of "global hooks" - these functions are called first.
22 The last handler (*it's hidden*) is either `asPromise` or `asStream`, depending on the `options.isStream` property.
23
24 @default []
25 */
26 handlers: HandlerFunction[];
27 /**
28 A read-only boolean describing whether the defaults are mutable or not.
29 If set to `true`, you can update headers over time, for example, update an access token when it expires.
30
31 @default false
32 */
33 mutableDefaults: boolean;
34};
35/**
36A Request object returned by calling Got, or any of the Got HTTP alias request functions.
37*/
38export type GotReturn = Request | CancelableRequest;
39/**
40A function to handle options and returns a Request object.
41It acts sort of like a "global hook", and will be called before any actual request is made.
42*/
43export type HandlerFunction = <T extends GotReturn>(options: Options, next: (options: Options) => T) => T | Promise<T>;
44/**
45The options available for `got.extend()`.
46*/
47export type ExtendOptions = {
48 /**
49 An array of functions. You execute them directly by calling `got()`.
50 They are some sort of "global hooks" - these functions are called first.
51 The last handler (*it's hidden*) is either `asPromise` or `asStream`, depending on the `options.isStream` property.
52
53 @default []
54 */
55 handlers?: HandlerFunction[];
56 /**
57 A read-only boolean describing whether the defaults are mutable or not.
58 If set to `true`, you can update headers over time, for example, update an access token when it expires.
59
60 @default false
61 */
62 mutableDefaults?: boolean;
63} & OptionsInit;
64export type StrictOptions = Except<OptionsInit, 'isStream' | 'responseType' | 'resolveBodyOnly'>;
65export type StreamOptions = Merge<OptionsInit, {
66 isStream?: true;
67}>;
68export type OptionsWithPagination<T = unknown, R = unknown> = Merge<OptionsInit, {
69 pagination?: PaginationOptions<T, R>;
70}>;
71/**
72An instance of `got.paginate`.
73*/
74export type GotPaginate = {
75 /**
76 Same as `GotPaginate.each`.
77 */
78 <T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
79 /**
80 Same as `GotPaginate.each`.
81 */
82 <T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
83 /**
84 Returns an async iterator.
85
86 See pagination.options for more pagination options.
87
88 @example
89 ```
90 import got from 'got';
91
92 const countLimit = 10;
93
94 const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
95 pagination: {countLimit}
96 });
97
98 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
99
100 for await (const commitData of pagination) {
101 console.log(commitData.commit.message);
102 }
103 ```
104 */
105 each: (<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>) => AsyncIterableIterator<T>) & (<T, R = unknown>(options?: OptionsWithPagination<T, R>) => AsyncIterableIterator<T>);
106 /**
107 Returns a Promise for an array of all results.
108
109 See pagination.options for more pagination options.
110
111 @example
112 ```
113 import got from 'got';
114
115 const countLimit = 10;
116
117 const results = await got.paginate.all('https://api.github.com/repos/sindresorhus/got/commits', {
118 pagination: {countLimit}
119 });
120
121 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
122 console.log(results);
123 ```
124 */
125 all: (<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>) => Promise<T[]>) & (<T, R = unknown>(options?: OptionsWithPagination<T, R>) => Promise<T[]>);
126};
127export type OptionsOfTextResponseBody = Merge<StrictOptions, {
128 isStream?: false;
129 responseType?: 'text';
130}>;
131export type OptionsOfTextResponseBodyOnly = Merge<StrictOptions, {
132 isStream?: false;
133 resolveBodyOnly: true;
134 responseType?: 'text';
135}>;
136export type OptionsOfTextResponseBodyWrapped = Merge<StrictOptions, {
137 isStream?: false;
138 resolveBodyOnly: false;
139 responseType?: 'text';
140}>;
141export type OptionsOfJSONResponseBody = Merge<StrictOptions, {
142 isStream?: false;
143 responseType?: 'json';
144}>;
145export type OptionsOfJSONResponseBodyOnly = Merge<StrictOptions, {
146 isStream?: false;
147 resolveBodyOnly: true;
148 responseType?: 'json';
149}>;
150export type OptionsOfJSONResponseBodyWrapped = Merge<StrictOptions, {
151 isStream?: false;
152 resolveBodyOnly: false;
153 responseType?: 'json';
154}>;
155export type OptionsOfBufferResponseBody = Merge<StrictOptions, {
156 isStream?: false;
157 responseType?: 'buffer';
158}>;
159export type OptionsOfBufferResponseBodyOnly = Merge<StrictOptions, {
160 isStream?: false;
161 resolveBodyOnly: true;
162 responseType?: 'buffer';
163}>;
164export type OptionsOfBufferResponseBodyWrapped = Merge<StrictOptions, {
165 isStream?: false;
166 resolveBodyOnly: false;
167 responseType?: 'buffer';
168}>;
169export type OptionsOfUnknownResponseBody = Merge<StrictOptions, {
170 isStream?: false;
171}>;
172export type OptionsOfUnknownResponseBodyOnly = Merge<StrictOptions, {
173 isStream?: false;
174 resolveBodyOnly: true;
175}>;
176export type OptionsOfUnknownResponseBodyWrapped = Merge<StrictOptions, {
177 isStream?: false;
178 resolveBodyOnly: false;
179}>;
180export type GotRequestFunction<U extends ExtendOptions = Record<string, unknown>> = {
181 (url: string | URL, options?: OptionsOfTextResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<string> : CancelableRequest<Response<string>>;
182 <T>(url: string | URL, options?: OptionsOfJSONResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<T> : CancelableRequest<Response<T>>;
183 (url: string | URL, options?: OptionsOfBufferResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<Buffer> : CancelableRequest<Response<Buffer>>;
184 (url: string | URL, options?: OptionsOfUnknownResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest : CancelableRequest<Response>;
185 (url: string | URL, options?: OptionsOfTextResponseBodyWrapped): CancelableRequest<Response<string>>;
186 <T>(url: string | URL, options?: OptionsOfJSONResponseBodyWrapped): CancelableRequest<Response<T>>;
187 (url: string | URL, options?: OptionsOfBufferResponseBodyWrapped): CancelableRequest<Response<Buffer>>;
188 (url: string | URL, options?: OptionsOfUnknownResponseBodyWrapped): CancelableRequest<Response>;
189 (url: string | URL, options?: OptionsOfTextResponseBodyOnly): CancelableRequest<string>;
190 <T>(url: string | URL, options?: OptionsOfJSONResponseBodyOnly): CancelableRequest<T>;
191 (url: string | URL, options?: OptionsOfBufferResponseBodyOnly): CancelableRequest<Buffer>;
192 (url: string | URL, options?: OptionsOfUnknownResponseBodyOnly): CancelableRequest;
193 (options: OptionsOfTextResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<string> : CancelableRequest<Response<string>>;
194 <T>(options: OptionsOfJSONResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<T> : CancelableRequest<Response<T>>;
195 (options: OptionsOfBufferResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest<Buffer> : CancelableRequest<Response<Buffer>>;
196 (options: OptionsOfUnknownResponseBody): U['resolveBodyOnly'] extends true ? CancelableRequest : CancelableRequest<Response>;
197 (options: OptionsOfTextResponseBodyWrapped): CancelableRequest<Response<string>>;
198 <T>(options: OptionsOfJSONResponseBodyWrapped): CancelableRequest<Response<T>>;
199 (options: OptionsOfBufferResponseBodyWrapped): CancelableRequest<Response<Buffer>>;
200 (options: OptionsOfUnknownResponseBodyWrapped): CancelableRequest<Response>;
201 (options: OptionsOfTextResponseBodyOnly): CancelableRequest<string>;
202 <T>(options: OptionsOfJSONResponseBodyOnly): CancelableRequest<T>;
203 (options: OptionsOfBufferResponseBodyOnly): CancelableRequest<Buffer>;
204 (options: OptionsOfUnknownResponseBodyOnly): CancelableRequest;
205 (url: string | URL, options?: Merge<OptionsInit, {
206 isStream: true;
207 }>): Request;
208 (options: Merge<OptionsInit, {
209 isStream: true;
210 }>): Request;
211 (url: string | URL, options?: OptionsInit): CancelableRequest | Request;
212 (options: OptionsInit): CancelableRequest | Request;
213 (url: undefined, options: undefined, defaults: Options): CancelableRequest | Request;
214};
215/**
216All available HTTP request methods provided by Got.
217*/
218export type HTTPAlias = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
219type GotStreamFunction = ((url?: string | URL, options?: Merge<OptionsInit, {
220 isStream?: true;
221}>) => Request) & ((options?: Merge<OptionsInit, {
222 isStream?: true;
223}>) => Request);
224/**
225An instance of `got.stream()`.
226*/
227export type GotStream = GotStreamFunction & Record<HTTPAlias, GotStreamFunction>;
228/**
229An instance of `got`.
230*/
231export type Got<GotOptions extends ExtendOptions = ExtendOptions> = {
232 /**
233 Sets `options.isStream` to `true`.
234
235 Returns a [duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with additional events:
236 - request
237 - response
238 - redirect
239 - uploadProgress
240 - downloadProgress
241 - error
242 */
243 stream: GotStream;
244 /**
245 Returns an async iterator.
246
247 See pagination.options for more pagination options.
248
249 @example
250 ```
251 import got from 'got';
252
253 const countLimit = 10;
254
255 const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
256 pagination: {countLimit}
257 });
258
259 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
260
261 for await (const commitData of pagination) {
262 console.log(commitData.commit.message);
263 }
264 ```
265 */
266 paginate: GotPaginate;
267 /**
268 The Got defaults used in that instance.
269 */
270 defaults: InstanceDefaults;
271 /**
272 Configure a new `got` instance with default `options`.
273 The `options` are merged with the parent instance's `defaults.options` using `got.mergeOptions`.
274 You can access the resolved options with the `.defaults` property on the instance.
275
276 Additionally, `got.extend()` accepts two properties from the `defaults` object: `mutableDefaults` and `handlers`.
277
278 It is also possible to merges many instances into a single one:
279 - options are merged using `got.mergeOptions()` (including hooks),
280 - handlers are stored in an array (you can access them through `instance.defaults.handlers`).
281
282 @example
283 ```
284 import got from 'got';
285
286 const client = got.extend({
287 prefixUrl: 'https://example.com',
288 headers: {
289 'x-unicorn': 'rainbow'
290 }
291 });
292
293 client.get('demo');
294
295 // HTTP Request =>
296 // GET /demo HTTP/1.1
297 // Host: example.com
298 // x-unicorn: rainbow
299 ```
300 */
301 extend<T extends Array<Got | ExtendOptions>>(...instancesOrOptions: T): Got<MergeExtendsConfig<T>>;
302} & Record<HTTPAlias, GotRequestFunction<GotOptions>> & GotRequestFunction<GotOptions>;
303export type ExtractExtendOptions<T> = T extends Got<infer GotOptions> ? GotOptions : T;
304/**
305Merges the options of multiple Got instances.
306*/
307export type MergeExtendsConfig<Value extends Array<Got | ExtendOptions>> = Value extends readonly [Value[0], ...infer NextValue] ? NextValue[0] extends undefined ? Value[0] extends infer OnlyValue ? OnlyValue extends ExtendOptions ? OnlyValue : OnlyValue extends Got<infer GotOptions> ? GotOptions : OnlyValue : never : ExtractExtendOptions<Value[0]> extends infer FirstArg extends ExtendOptions ? ExtractExtendOptions<NextValue[0] extends ExtendOptions | Got ? NextValue[0] : never> extends infer NextArg extends ExtendOptions ? Spread<FirstArg, NextArg> extends infer Merged extends ExtendOptions ? NextValue extends [NextValue[0], ...infer NextRest] ? NextRest extends Array<Got | ExtendOptions> ? MergeExtendsConfig<[Merged, ...NextRest]> : never : never : never : never : never : never;
308export {};
309
\No newline at end of file