got 11.5.2 → 11.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/source/as-promise/index.d.ts +1 -3
- package/dist/source/as-promise/index.js +44 -125
- package/dist/source/as-promise/normalize-arguments.d.ts +3 -0
- package/dist/source/as-promise/normalize-arguments.js +78 -0
- package/dist/source/as-promise/parse-body.d.ts +3 -0
- package/dist/source/as-promise/parse-body.js +25 -0
- package/dist/source/as-promise/types.d.ts +206 -24
- package/dist/source/as-promise/types.js +18 -7
- package/dist/source/{as-promise → core}/calculate-retry-delay.d.ts +2 -1
- package/dist/source/core/calculate-retry-delay.js +29 -0
- package/dist/source/core/index.d.ts +822 -5
- package/dist/source/core/index.js +198 -39
- package/dist/source/core/utils/is-response-ok.d.ts +2 -0
- package/dist/source/core/utils/is-response-ok.js +8 -0
- package/dist/source/create.js +16 -7
- package/dist/source/index.js +3 -2
- package/dist/source/types.d.ts +240 -1
- package/package.json +20 -16
- package/readme.md +75 -7
- package/dist/source/as-promise/calculate-retry-delay.js +0 -38
- package/dist/source/as-promise/core.d.ts +0 -13
- package/dist/source/as-promise/core.js +0 -127
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseBody = exports.knownBodyTypes = void 0;
|
|
4
|
-
const is_1 = require("@sindresorhus/is");
|
|
5
|
-
const types_1 = require("./types");
|
|
6
|
-
const core_1 = require("../core");
|
|
7
|
-
if (!core_1.knownHookEvents.includes('beforeRetry')) {
|
|
8
|
-
core_1.knownHookEvents.push('beforeRetry', 'afterResponse');
|
|
9
|
-
}
|
|
10
|
-
exports.knownBodyTypes = ['json', 'buffer', 'text'];
|
|
11
|
-
exports.parseBody = (response, responseType, parseJson, encoding) => {
|
|
12
|
-
const { rawBody } = response;
|
|
13
|
-
try {
|
|
14
|
-
if (responseType === 'text') {
|
|
15
|
-
return rawBody.toString(encoding);
|
|
16
|
-
}
|
|
17
|
-
if (responseType === 'json') {
|
|
18
|
-
return rawBody.length === 0 ? '' : parseJson(rawBody.toString());
|
|
19
|
-
}
|
|
20
|
-
if (responseType === 'buffer') {
|
|
21
|
-
return Buffer.from(rawBody);
|
|
22
|
-
}
|
|
23
|
-
throw new types_1.ParseError({
|
|
24
|
-
message: `Unknown body type '${responseType}'`,
|
|
25
|
-
name: 'Error'
|
|
26
|
-
}, response);
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
throw new types_1.ParseError(error, response);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
class PromisableRequest extends core_1.default {
|
|
33
|
-
static normalizeArguments(url, nonNormalizedOptions, defaults) {
|
|
34
|
-
const options = super.normalizeArguments(url, nonNormalizedOptions, defaults);
|
|
35
|
-
if (is_1.default.null_(options.encoding)) {
|
|
36
|
-
throw new TypeError('To get a Buffer, set `options.responseType` to `buffer` instead');
|
|
37
|
-
}
|
|
38
|
-
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.encoding);
|
|
39
|
-
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.resolveBodyOnly);
|
|
40
|
-
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.methodRewriting);
|
|
41
|
-
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.isStream);
|
|
42
|
-
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.responseType);
|
|
43
|
-
// `options.responseType`
|
|
44
|
-
if (options.responseType === undefined) {
|
|
45
|
-
options.responseType = 'text';
|
|
46
|
-
}
|
|
47
|
-
// `options.retry`
|
|
48
|
-
const { retry } = options;
|
|
49
|
-
if (defaults) {
|
|
50
|
-
options.retry = { ...defaults.retry };
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
options.retry = {
|
|
54
|
-
calculateDelay: retryObject => retryObject.computedValue,
|
|
55
|
-
limit: 0,
|
|
56
|
-
methods: [],
|
|
57
|
-
statusCodes: [],
|
|
58
|
-
errorCodes: [],
|
|
59
|
-
maxRetryAfter: undefined
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
if (is_1.default.object(retry)) {
|
|
63
|
-
options.retry = {
|
|
64
|
-
...options.retry,
|
|
65
|
-
...retry
|
|
66
|
-
};
|
|
67
|
-
options.retry.methods = [...new Set(options.retry.methods.map(method => method.toUpperCase()))];
|
|
68
|
-
options.retry.statusCodes = [...new Set(options.retry.statusCodes)];
|
|
69
|
-
options.retry.errorCodes = [...new Set(options.retry.errorCodes)];
|
|
70
|
-
}
|
|
71
|
-
else if (is_1.default.number(retry)) {
|
|
72
|
-
options.retry.limit = retry;
|
|
73
|
-
}
|
|
74
|
-
if (is_1.default.undefined(options.retry.maxRetryAfter)) {
|
|
75
|
-
options.retry.maxRetryAfter = Math.min(
|
|
76
|
-
// TypeScript is not smart enough to handle `.filter(x => is.number(x))`.
|
|
77
|
-
// eslint-disable-next-line unicorn/no-fn-reference-in-iterator
|
|
78
|
-
...[options.timeout.request, options.timeout.connect].filter(is_1.default.number));
|
|
79
|
-
}
|
|
80
|
-
// `options.pagination`
|
|
81
|
-
if (is_1.default.object(options.pagination)) {
|
|
82
|
-
if (defaults) {
|
|
83
|
-
options.pagination = {
|
|
84
|
-
...defaults.pagination,
|
|
85
|
-
...options.pagination
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
const { pagination } = options;
|
|
89
|
-
if (!is_1.default.function_(pagination.transform)) {
|
|
90
|
-
throw new Error('`options.pagination.transform` must be implemented');
|
|
91
|
-
}
|
|
92
|
-
if (!is_1.default.function_(pagination.shouldContinue)) {
|
|
93
|
-
throw new Error('`options.pagination.shouldContinue` must be implemented');
|
|
94
|
-
}
|
|
95
|
-
if (!is_1.default.function_(pagination.filter)) {
|
|
96
|
-
throw new TypeError('`options.pagination.filter` must be implemented');
|
|
97
|
-
}
|
|
98
|
-
if (!is_1.default.function_(pagination.paginate)) {
|
|
99
|
-
throw new Error('`options.pagination.paginate` must be implemented');
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
// JSON mode
|
|
103
|
-
if (options.responseType === 'json' && options.headers.accept === undefined) {
|
|
104
|
-
options.headers.accept = 'application/json';
|
|
105
|
-
}
|
|
106
|
-
return options;
|
|
107
|
-
}
|
|
108
|
-
static mergeOptions(...sources) {
|
|
109
|
-
let mergedOptions;
|
|
110
|
-
for (const source of sources) {
|
|
111
|
-
mergedOptions = PromisableRequest.normalizeArguments(undefined, source, mergedOptions);
|
|
112
|
-
}
|
|
113
|
-
return mergedOptions;
|
|
114
|
-
}
|
|
115
|
-
_beforeError(error) {
|
|
116
|
-
if (this.destroyed) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (!(error instanceof core_1.RequestError)) {
|
|
120
|
-
error = new core_1.RequestError(error.message, error, this);
|
|
121
|
-
}
|
|
122
|
-
// Let the promise decide whether to abort or not
|
|
123
|
-
// It is also responsible for the `beforeError` hook
|
|
124
|
-
this.emit('error', error);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
exports.default = PromisableRequest;
|