UNPKG

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