Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (query) {
if (!is.string(query)) {
opts.query = querystring.stringify(query);
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && is.undefined(opts.headers.accept)) {
opts.headers.accept = 'application/json';
}
const body = opts.body;
if (is.nullOrUndefined(body)) {
opts.method = (opts.method || 'GET').toUpperCase();
} else {
const headers = opts.headers;
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(opts.form || opts.json)) {
throw new TypeError('The `body` option must be a stream.Readable, string, Buffer or plain Object');
}
const canBodyBeStringified = is.plainObject(body) || is.array(body);
if ((opts.form || opts.json) && !canBodyBeStringified) {
throw new TypeError('The `body` option must be a plain Object or Array when the `form` or `json` option is used');
}
if (isFormData(body)) {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (opts.form && canBodyBeStringified) {
retries: 2,
cache: false,
decompress: true,
useElectronNet: false,
throwHttpErrors: true
},
url,
{
protocol: url.protocol || 'http:' // Override both null/undefined with default protocol
},
opts
);
const headers = lowercaseKeys(opts.headers);
for (const key of Object.keys(headers)) {
if (is.nullOrUndefined(headers[key])) {
delete headers[key];
}
}
opts.headers = Object.assign({
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
}, headers);
if (opts.decompress && is.undefined(opts.headers['accept-encoding'])) {
opts.headers['accept-encoding'] = 'gzip, deflate';
}
const query = opts.query;
if (query) {
if (!is.string(query)) {
retries: 2,
cache: false,
decompress: true,
useElectronNet: false,
throwHttpErrors: true
},
url,
{
protocol: url.protocol || 'http:' // Override both null/undefined with default protocol
},
opts
);
const headers = lowercaseKeys(opts.headers);
for (const key of Object.keys(headers)) {
if (is.nullOrUndefined(headers[key])) {
delete headers[key];
}
}
opts.headers = Object.assign({
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
}, headers);
if (opts.decompress && is.undefined(opts.headers['accept-encoding'])) {
opts.headers['accept-encoding'] = 'gzip, deflate';
}
const query = opts.query;
if (query) {
if (!is.string(query)) {
for (const [key, value] of Object.entries(headers)) {
if (is.nullOrUndefined(value)) {
delete headers[key];
}
}
if (options.json && is.undefined(headers.accept)) {
headers.accept = 'application/json';
}
if (options.decompress && is.undefined(headers['accept-encoding'])) {
headers['accept-encoding'] = 'gzip, deflate';
}
const {body} = options;
if (is.nullOrUndefined(body)) {
options.method = options.method ? options.method.toUpperCase() : 'GET';
} else {
const isObject = is.object(body) && !is.buffer(body) && !is.nodeStream(body);
if (!is.nodeStream(body) && !is.string(body) && !is.buffer(body) && !(options.form || options.json)) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (options.json && !(isObject || is.array(body))) {
throw new TypeError('The `body` option must be an Object or Array when the `json` option is used');
}
if (options.form && !isObject) {
throw new TypeError('The `body` option must be an Object when the `form` option is used');
}
if (isFormData(body)) {
formatArray(arr: Array, depth: number = 0): string {
const opts =
depth > 0 && this.opts.childOpts ? this.opts.childOpts : this.opts;
const braces = this.opts.arrayBraces;
if (!is.nullOrUndefined(opts.depth) && depth >= opts.depth) {
return `${braces[0]}Array${braces[1]}`;
}
const maxLength = opts.maxArrayLength || 100;
const output = arr.slice(0, maxLength).map(x => {
return this.formatValue(x, depth + 1).replace(/\n/g, '\n ');
});
if (arr.length > output.length) {
output.push('…');
}
return this.reduceToSingleString(output, braces);
}
...[options.timeout.request, options.timeout.connect].filter((n): n is number => !is.nullOrUndefined(n))
);
const preNormalize = (options, defaults) => {
if (is.nullOrUndefined(options.headers)) {
options.headers = {};
} else {
options.headers = lowercaseKeys(options.headers);
}
if (options.baseUrl && !options.baseUrl.toString().endsWith('/')) {
options.baseUrl += '/';
}
if (options.stream) {
options.json = false;
}
if (is.nullOrUndefined(options.hooks)) {
options.hooks = {};
} else if (!is.object(options.hooks)) {
formatMap(value: Object, depth: number): string {
const opts =
depth > 0 && this.opts.childOpts ? this.opts.childOpts : this.opts;
const braces = this.opts.mapBraces;
if (!is.nullOrUndefined(opts.depth) && depth >= opts.depth) {
return `${braces[0]}Object${braces[1]}`;
}
const maxLength = opts.maxObjectKeys || 100;
const keys = Object.keys(value);
const output = keys.slice(0, maxLength).map(key => {
const skey = this._stylizeName(String(key));
const svalue = this.formatValue(value[key], depth + 1).replace(
/\n/g,
'\n '
);
return `${skey}: ${svalue}`;
});
if (keys.length > output.length) {
output.push('…');
}
return this.reduceToSingleString(output, braces);