2020-04-29 18:44:27 +03:00
|
|
|
const got = require('got');
|
|
|
|
const _ = require('lodash');
|
2021-06-15 21:46:27 +03:00
|
|
|
const validator = require('@tryghost/validator');
|
2020-04-29 18:44:27 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-06-16 11:36:58 +03:00
|
|
|
const ghostVersion = require('@tryghost/version');
|
2019-01-28 19:01:34 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const defaultOptions = {
|
2019-01-28 19:01:34 +03:00
|
|
|
headers: {
|
|
|
|
'user-agent': 'Ghost/' + ghostVersion.original + ' (https://github.com/TryGhost/Ghost)'
|
|
|
|
}
|
|
|
|
};
|
2017-09-07 14:17:24 +03:00
|
|
|
|
|
|
|
module.exports = function request(url, options) {
|
|
|
|
if (_.isEmpty(url) || !validator.isURL(url)) {
|
2020-03-25 13:25:25 +03:00
|
|
|
return Promise.reject(new errors.InternalServerError({
|
2017-09-07 14:17:24 +03:00
|
|
|
message: 'URL empty or invalid.',
|
|
|
|
code: 'URL_MISSING_INVALID',
|
|
|
|
context: url
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const mergedOptions = _.merge({}, defaultOptions, options);
|
2019-01-28 19:01:34 +03:00
|
|
|
|
|
|
|
return got(url, mergedOptions);
|
2017-09-07 14:17:24 +03:00
|
|
|
};
|