fakeit
Version:
Command-line utility that generates fake data which can be output as JSON, YAML, CSON, or CSV formats based on models defined in YAML.
273 lines (204 loc) • 8.78 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
exports.request = request;
var _lodash = require('lodash');
var _defaultOptions = require('./default-options');
var _defaultOptions2 = _interopRequireDefault(_defaultOptions);
var _base = require('../base');
var _base2 = _interopRequireDefault(_base);
var _toJs = require('to-js');
var _toJs2 = _interopRequireDefault(_toJs);
var _request = require('request');
var _request2 = _interopRequireDefault(_request);
var _setCookieParser = require('set-cookie-parser');
var _setCookieParser2 = _interopRequireDefault(_setCookieParser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/// @name SyncGateway
/// @page api
/// @description This is used to output data to the SyncGateway
var SyncGateway = function (_Base) {
(0, _inherits3.default)(SyncGateway, _Base);
///# @name constructor
///# @arg {object} options - The base options
///# @arg {object} output_options - The output options
function SyncGateway() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var output_options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _classCallCheck3.default)(this, SyncGateway);
var _this = (0, _possibleConstructorReturn3.default)(this, (SyncGateway.__proto__ || (0, _getPrototypeOf2.default)(SyncGateway)).call(this, options));
_this.output_options = (0, _lodash.extend)({}, _defaultOptions2.default, output_options);
_this.prepared = false;
return _this;
}
///# @name prepare
///# @description
///# This is used to prepare the saving functionality that is determined by the
///# options that were passed to the constructor.
///# It sets a variable of `this.preparing` that ultimately calls `this.setup` that returns a promise.
///# This way when you go to save data it, that function will know if the setup is complete or not and
///# wait for it to be done before it starts saving data.
///# @returns {promise} - The setup function that was called
///# @async
/* istanbul ignore next */
(0, _createClass3.default)(SyncGateway, [{
key: 'prepare',
value: function prepare() {
this.preparing = true;
this.preparing = this.setup();
return this.preparing;
}
///# @name setup
///# @description
///# This is used to setup the saving function that will be used.
///# @async
/* istanbul ignore next */
}, {
key: 'setup',
value: function setup() {
var _this2 = this;
// if this.prepare hasn't been called then run it first.
if (this.preparing == null) {
return this.prepare();
}
var _output_options = this.output_options,
name = _output_options.username,
password = _output_options.password,
server = _output_options.server,
bucket = _output_options.bucket;
// If there's no `name`, and `password` there's no need to
// run authentication if the sync db is allowing guest
if (!name && !password) {
process.nextTick(function () {
_this2.prepared = true;
});
return _promise2.default.resolve();
}
return request({
url: server + '/' + encodeURIComponent(bucket) + '/_session',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: _toJs2.default.json({ name: name, password: password })
}).then(function (_ref) {
var _ref2 = (0, _slicedToArray3.default)(_ref, 2),
res = _ref2[0],
body = _ref2[1];
body = _toJs2.default.object(body);
if (body.ok && res.headers['set-cookie']) {
var cookie = _setCookieParser2.default.parse(res);
_this2.session = {
name: cookie[0].name,
id: cookie[0].value
};
} else if (body.error) {
return _this2.log('error', body.error);
} else {
return _this2.log('error', 'Unable to connect to Sync Gateway');
}
_this2.prepared = true;
}).catch(function (err) {
_this2.log('error', 'Unable to connect to Sync Gateway: ' + err.message);
});
}
///# @name output
///# @description
///# This is used to output the data that's passed to it
///# @arg {string} id - The id to use for this data
///# @arg {object, array, string} data - The data that you want to be saved
///# @async
}, {
key: 'output',
value: function () {
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(id, data) {
var _output_options2, server, bucket, options, jar, cookie, body;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!(this.prepared !== true)) {
_context.next = 4;
break;
}
if (this.preparing == null) {
this.prepare();
}
_context.next = 4;
return this.preparing;
case 4:
_output_options2 = this.output_options, server = _output_options2.server, bucket = _output_options2.bucket;
options = {
url: server + '/' + bucket + '/' + encodeURIComponent(id),
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: data
};
// if there is a an authenticated sync session use it
if (this.session) {
jar = _request2.default.jar();
cookie = _request2.default.cookie(this.session.name + '=' + this.session.id);
jar.setCookie(cookie, server);
options.jar = jar;
}
_context.t0 = _toJs2.default;
_context.next = 10;
return request(options);
case 10:
_context.t1 = _context.sent[1];
body = _context.t0.object.call(_context.t0, _context.t1);
if (body.error) {
if (body.reason === 'Document exists') {
body.reason = 'The \'' + id + '\' document already exists';
}
this.log('error', body.reason);
}
case 13:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function output(_x3, _x4) {
return _ref3.apply(this, arguments);
}
return output;
}()
}]);
return SyncGateway;
}(_base2.default);
/// @name request
/// @description
/// This converts the call back style of `request` to be a promise style
/// @arg {object} options - The options to pass onto the `request` function
/// @returns {array} - The first item in the array is the `res`, and the second item is the `body` response.
/// @async
exports.default = SyncGateway;
function request() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new _promise2.default(function (resolve, reject) {
(0, _request2.default)(options, function (err, res, body) {
/* istanbul ignore next : to hard to mock test */
if (err) return reject(err);
resolve([res, body]);
});
});
}