UNPKG

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