2020-04-29 18:44:27 +03:00
|
|
|
const got = require('got');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const validator = require('../data/validation').validator;
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const ghostVersion = require('./ghost-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
|
|
|
};
|