// Make an object with the options as expected by request()
module.exports = (method, url, port, options) => {
// Make sure it's a simple object
Iif (typeof options === 'string') options = { url: options };
// Assign independent parts
options = Object.assign({}, { headers: {} }, options, { url, method });
if (options && options.body && typeof options.body === 'string') {
options.headers['Content-Type'] = 'text/plain';
}
// Make sure it has a right URL or localhost otherwise
Eif (!/^https?:\/\//.test(options.url)) {
options.url = `http://localhost:${port}${options.url}`;
}
// Set it to send a JSON when appropriate
if (options.body && typeof options.body === 'object') {
options.json = true;
}
// Finally return the fully formed object
return options;
};
|