/
create.ts
322 lines (259 loc) 路 8.33 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {URL} from 'url';
import is from '@sindresorhus/is';
import asPromise, {
// Response
Response,
// Options
Options,
NormalizedOptions,
// Hooks
InitHook,
// Errors
ParseError,
RequestError,
CacheError,
ReadError,
HTTPError,
MaxRedirectsError,
TimeoutError,
UnsupportedProtocolError,
UploadError,
CancelError
} from './as-promise';
import {
GotReturn,
ExtendOptions,
Got,
HTTPAlias,
HandlerFunction,
InstanceDefaults,
GotPaginate,
GotStream,
GotRequestFunction,
OptionsWithPagination,
StreamOptions
} from './types';
import createRejection from './as-promise/create-rejection';
import Request, {kIsNormalizedAlready, setNonEnumerableProperties, Defaults} from './core';
import deepFreeze from './utils/deep-freeze';
const errors = {
RequestError,
CacheError,
ReadError,
HTTPError,
MaxRedirectsError,
TimeoutError,
ParseError,
CancelError,
UnsupportedProtocolError,
UploadError
};
// The `delay` package weighs 10KB (!)
const delay = async (ms: number) => new Promise(resolve => {
setTimeout(resolve, ms);
});
const {normalizeArguments} = Request;
const mergeOptions = (...sources: Options[]): NormalizedOptions => {
let mergedOptions: NormalizedOptions | undefined;
for (const source of sources) {
mergedOptions = normalizeArguments(undefined, source, mergedOptions);
}
return mergedOptions!;
};
const getPromiseOrStream = (options: NormalizedOptions): GotReturn => options.isStream ? new Request(undefined, options) : asPromise(options);
const isGotInstance = (value: Got | ExtendOptions): value is Got => (
'defaults' in value && 'options' in value.defaults
);
const aliases: readonly HTTPAlias[] = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
export const defaultHandler: HandlerFunction = (options, next) => next(options);
const callInitHooks = (hooks: InitHook[] | undefined, options?: Options): void => {
if (hooks) {
for (const hook of hooks) {
hook(options!);
}
}
};
const create = (defaults: InstanceDefaults): Got => {
// Proxy properties from next handlers
defaults._rawHandlers = defaults.handlers;
defaults.handlers = defaults.handlers.map(fn => ((options, next) => {
// This will be assigned by assigning result
let root!: ReturnType<typeof next>;
const result = fn(options, newOptions => {
root = next(newOptions);
return root;
});
if (result !== root && !options.isStream && root) {
const typedResult = result as Promise<unknown>;
const {then: promiseThen, catch: promiseCatch, finally: promiseFianlly} = typedResult;
Object.setPrototypeOf(typedResult, Object.getPrototypeOf(root));
Object.defineProperties(typedResult, Object.getOwnPropertyDescriptors(root));
// These should point to the new promise
// eslint-disable-next-line promise/prefer-await-to-then
typedResult.then = promiseThen;
typedResult.catch = promiseCatch;
typedResult.finally = promiseFianlly;
}
return result;
}));
// Got interface
const got: Got = ((url: string | URL, options: Options = {}, _defaults?: Defaults): GotReturn => {
let iteration = 0;
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
return defaults.handlers[iteration++](
newOptions,
iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers
) as GotReturn;
};
// TODO: Remove this in Got 12.
if (is.plainObject(url)) {
const mergedOptions = {
...url as Options,
...options
};
setNonEnumerableProperties([url as Options, options], mergedOptions);
options = mergedOptions;
url = undefined as any;
}
try {
// Call `init` hooks
let initHookError: Error | undefined;
try {
callInitHooks(defaults.options.hooks.init, options);
callInitHooks(options.hooks?.init, options);
} catch (error) {
initHookError = error;
}
// Normalize options & call handlers
const normalizedOptions = normalizeArguments(url, options, _defaults ?? defaults.options);
normalizedOptions[kIsNormalizedAlready] = true;
if (initHookError) {
throw new RequestError(initHookError.message, initHookError, normalizedOptions);
}
return iterateHandlers(normalizedOptions);
} catch (error) {
if (options.isStream) {
throw error;
} else {
return createRejection(error, defaults.options.hooks.beforeError, options.hooks?.beforeError);
}
}
}) as Got;
got.extend = (...instancesOrOptions) => {
const optionsArray: Options[] = [defaults.options];
let handlers: HandlerFunction[] = [...defaults._rawHandlers!];
let isMutableDefaults: boolean | undefined;
for (const value of instancesOrOptions) {
if (isGotInstance(value)) {
optionsArray.push(value.defaults.options);
handlers.push(...value.defaults._rawHandlers!);
isMutableDefaults = value.defaults.mutableDefaults;
} else {
optionsArray.push(value);
if ('handlers' in value) {
handlers.push(...value.handlers!);
}
isMutableDefaults = value.mutableDefaults;
}
}
handlers = handlers.filter(handler => handler !== defaultHandler);
if (handlers.length === 0) {
handlers.push(defaultHandler);
}
return create({
options: mergeOptions(...optionsArray),
handlers,
mutableDefaults: Boolean(isMutableDefaults)
});
};
// Pagination
const paginateEach = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T> {
// TODO: Remove this `@ts-expect-error` when upgrading to TypeScript 4.
// Error: Argument of type 'Merge<Options, PaginationOptions<T, R>> | undefined' is not assignable to parameter of type 'Options | undefined'.
// @ts-expect-error
let normalizedOptions = normalizeArguments(url, options, defaults.options);
normalizedOptions.resolveBodyOnly = false;
const pagination = normalizedOptions.pagination!;
if (!is.object(pagination)) {
throw new TypeError('`options.pagination` must be implemented');
}
const all: T[] = [];
let {countLimit} = pagination;
let numberOfRequests = 0;
while (numberOfRequests < pagination.requestLimit) {
if (numberOfRequests !== 0) {
// eslint-disable-next-line no-await-in-loop
await delay(pagination.backoff);
}
// @ts-expect-error FIXME!
// TODO: Throw when result is not an instance of Response
// eslint-disable-next-line no-await-in-loop
const result = (await got(undefined, undefined, normalizedOptions)) as Response;
// eslint-disable-next-line no-await-in-loop
const parsed = await pagination.transform(result);
const current: T[] = [];
for (const item of parsed) {
if (pagination.filter(item, all, current)) {
if (!pagination.shouldContinue(item, all, current)) {
return;
}
yield item as T;
if (pagination.stackAllItems) {
all.push(item as T);
}
current.push(item as T);
if (--countLimit <= 0) {
return;
}
}
}
const optionsToMerge = pagination.paginate(result, all, current);
if (optionsToMerge === false) {
return;
}
if (optionsToMerge === result.request.options) {
normalizedOptions = result.request.options;
} else if (optionsToMerge !== undefined) {
normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
}
numberOfRequests++;
}
});
got.paginate = paginateEach as GotPaginate;
got.paginate.all = (async <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) => {
const results: T[] = [];
for await (const item of paginateEach<T, R>(url, options)) {
results.push(item);
}
return results;
}) as GotPaginate['all'];
// For those who like very descriptive names
got.paginate.each = paginateEach as GotPaginate['each'];
// Stream API
got.stream = ((url: string | URL, options?: StreamOptions) => got(url, {...options, isStream: true})) as GotStream;
// Shortcuts
for (const method of aliases) {
got[method] = ((url: string | URL, options?: Options): GotReturn => got(url, {...options, method})) as GotRequestFunction;
got.stream[method] = ((url: string | URL, options?: StreamOptions) => {
return got(url, {...options, method, isStream: true});
}) as GotStream;
}
Object.assign(got, errors);
Object.defineProperty(got, 'defaults', {
value: defaults.mutableDefaults ? defaults : deepFreeze(defaults),
writable: defaults.mutableDefaults,
configurable: defaults.mutableDefaults,
enumerable: true
});
got.mergeOptions = mergeOptions;
return got;
};
export default create;
export * from './types';