UNPKG

9.38 kBTypeScriptView Raw
1/// <reference types="node" resolution-mode="require"/>
2import type { Buffer } from 'node:buffer';
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 OptionsOfTextResponseBody = Merge<OptionsInit, {
64 isStream?: false;
65 resolveBodyOnly?: false;
66 responseType?: 'text';
67}>;
68export type OptionsOfJSONResponseBody = Merge<OptionsInit, {
69 isStream?: false;
70 resolveBodyOnly?: false;
71 responseType?: 'json';
72}>;
73export type OptionsOfBufferResponseBody = Merge<OptionsInit, {
74 isStream?: false;
75 resolveBodyOnly?: false;
76 responseType: 'buffer';
77}>;
78export type OptionsOfUnknownResponseBody = Merge<OptionsInit, {
79 isStream?: false;
80 resolveBodyOnly?: false;
81}>;
82export type StrictOptions = Except<OptionsInit, 'isStream' | 'responseType' | 'resolveBodyOnly'>;
83export type StreamOptions = Merge<OptionsInit, {
84 isStream?: true;
85}>;
86type ResponseBodyOnly = {
87 resolveBodyOnly: true;
88};
89export type OptionsWithPagination<T = unknown, R = unknown> = Merge<OptionsInit, {
90 pagination?: PaginationOptions<T, R>;
91}>;
92/**
93An instance of `got.paginate`.
94*/
95export type GotPaginate = {
96 /**
97 Same as `GotPaginate.each`.
98 */
99 <T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
100 /**
101 Same as `GotPaginate.each`.
102 */
103 <T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
104 /**
105 Returns an async iterator.
106
107 See pagination.options for more pagination options.
108
109 @example
110 ```
111 import got from 'got';
112
113 const countLimit = 10;
114
115 const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
116 pagination: {countLimit}
117 });
118
119 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
120
121 for await (const commitData of pagination) {
122 console.log(commitData.commit.message);
123 }
124 ```
125 */
126 each: (<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>) => AsyncIterableIterator<T>) & (<T, R = unknown>(options?: OptionsWithPagination<T, R>) => AsyncIterableIterator<T>);
127 /**
128 Returns a Promise for an array of all results.
129
130 See pagination.options for more pagination options.
131
132 @example
133 ```
134 import got from 'got';
135
136 const countLimit = 10;
137
138 const results = await got.paginate.all('https://api.github.com/repos/sindresorhus/got/commits', {
139 pagination: {countLimit}
140 });
141
142 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
143 console.log(results);
144 ```
145 */
146 all: (<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>) => Promise<T[]>) & (<T, R = unknown>(options?: OptionsWithPagination<T, R>) => Promise<T[]>);
147};
148export type GotRequestFunction = {
149 (url: string | URL, options?: OptionsOfTextResponseBody): CancelableRequest<Response<string>>;
150 <T>(url: string | URL, options?: OptionsOfJSONResponseBody): CancelableRequest<Response<T>>;
151 (url: string | URL, options?: OptionsOfBufferResponseBody): CancelableRequest<Response<Buffer>>;
152 (url: string | URL, options?: OptionsOfUnknownResponseBody): CancelableRequest<Response>;
153 (options: OptionsOfTextResponseBody): CancelableRequest<Response<string>>;
154 <T>(options: OptionsOfJSONResponseBody): CancelableRequest<Response<T>>;
155 (options: OptionsOfBufferResponseBody): CancelableRequest<Response<Buffer>>;
156 (options: OptionsOfUnknownResponseBody): CancelableRequest<Response>;
157 (url: string | URL, options?: (Merge<OptionsOfTextResponseBody, ResponseBodyOnly>)): CancelableRequest<string>;
158 <T>(url: string | URL, options?: (Merge<OptionsOfJSONResponseBody, ResponseBodyOnly>)): CancelableRequest<T>;
159 (url: string | URL, options?: (Merge<OptionsOfBufferResponseBody, ResponseBodyOnly>)): CancelableRequest<Buffer>;
160 (options: (Merge<OptionsOfTextResponseBody, ResponseBodyOnly>)): CancelableRequest<string>;
161 <T>(options: (Merge<OptionsOfJSONResponseBody, ResponseBodyOnly>)): CancelableRequest<T>;
162 (options: (Merge<OptionsOfBufferResponseBody, ResponseBodyOnly>)): CancelableRequest<Buffer>;
163 (url: string | URL, options?: Merge<OptionsInit, {
164 isStream: true;
165 }>): Request;
166 (options: Merge<OptionsInit, {
167 isStream: true;
168 }>): Request;
169 (url: string | URL, options?: OptionsInit): CancelableRequest | Request;
170 (options: OptionsInit): CancelableRequest | Request;
171 (url: undefined, options: undefined, defaults: Options): CancelableRequest | Request;
172};
173/**
174All available HTTP request methods provided by Got.
175*/
176export type HTTPAlias = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete';
177type GotStreamFunction = ((url?: string | URL, options?: Merge<OptionsInit, {
178 isStream?: true;
179}>) => Request) & ((options?: Merge<OptionsInit, {
180 isStream?: true;
181}>) => Request);
182/**
183An instance of `got.stream()`.
184*/
185export type GotStream = GotStreamFunction & Record<HTTPAlias, GotStreamFunction>;
186/**
187An instance of `got`.
188*/
189export type Got = {
190 /**
191 Sets `options.isStream` to `true`.
192
193 Returns a [duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with additional events:
194 - request
195 - response
196 - redirect
197 - uploadProgress
198 - downloadProgress
199 - error
200 */
201 stream: GotStream;
202 /**
203 Returns an async iterator.
204
205 See pagination.options for more pagination options.
206
207 @example
208 ```
209 import got from 'got';
210
211 const countLimit = 10;
212
213 const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
214 pagination: {countLimit}
215 });
216
217 console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
218
219 for await (const commitData of pagination) {
220 console.log(commitData.commit.message);
221 }
222 ```
223 */
224 paginate: GotPaginate;
225 /**
226 The Got defaults used in that instance.
227 */
228 defaults: InstanceDefaults;
229 /**
230 Configure a new `got` instance with default `options`.
231 The `options` are merged with the parent instance's `defaults.options` using `got.mergeOptions`.
232 You can access the resolved options with the `.defaults` property on the instance.
233
234 Additionally, `got.extend()` accepts two properties from the `defaults` object: `mutableDefaults` and `handlers`.
235
236 It is also possible to merges many instances into a single one:
237 - options are merged using `got.mergeOptions()` (including hooks),
238 - handlers are stored in an array (you can access them through `instance.defaults.handlers`).
239
240 @example
241 ```
242 import got from 'got';
243
244 const client = got.extend({
245 prefixUrl: 'https://example.com',
246 headers: {
247 'x-unicorn': 'rainbow'
248 }
249 });
250
251 client.get('demo');
252
253 // HTTP Request =>
254 // GET /demo HTTP/1.1
255 // Host: example.com
256 // x-unicorn: rainbow
257 ```
258 */
259 extend: (...instancesOrOptions: Array<Got | ExtendOptions>) => Got;
260} & Record<HTTPAlias, GotRequestFunction> & GotRequestFunction;
261export {};
262
\No newline at end of file