UNPKG

fakeit-facet

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.

258 lines (201 loc) 9.93 kB
"use strict"; var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct"); var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property"); var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); _Object$defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; exports.request = request; var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs3/regenerator")); var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise")); var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass")); var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/getPrototypeOf")); var _lodash = require("lodash"); var _toJs = _interopRequireDefault(require("to-js")); var _request = _interopRequireDefault(require("request")); var _setCookieParser = _interopRequireDefault(require("set-cookie-parser")); var _base = _interopRequireDefault(require("../base")); var _defaultOptions = _interopRequireDefault(require("./default-options")); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /// @name SyncGateway /// @page api /// @description This is used to output data to the SyncGateway var SyncGateway = /*#__PURE__*/function (_Base) { (0, _inherits2["default"])(SyncGateway, _Base); var _super = _createSuper(SyncGateway); /// # @name constructor /// # @arg {object} options - The base options /// # @arg {object} output_options - The output options function SyncGateway() { var _this; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var output_options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; (0, _classCallCheck2["default"])(this, SyncGateway); _this = _super.call(this, options); _this.output_options = (0, _lodash.extend)({}, _defaultOptions["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, _createClass2["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, _context; // if this.prepare hasn't been called then run it first. if (this.preparing == null) { return this.prepare(); } var _this$output_options = this.output_options, name = _this$output_options.username, password = _this$output_options.password, server = _this$output_options.server, bucket = _this$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 _promise["default"].resolve(); } return request({ url: (0, _concat["default"])(_context = "".concat(server, "/")).call(_context, encodeURIComponent(bucket), "/_session"), method: 'POST', headers: { 'Content-Type': 'application/json' }, body: _toJs["default"].json({ name: name, password: password }) }).then(function (_ref) { var _ref2 = (0, _slicedToArray2["default"])(_ref, 2), res = _ref2[0], body = _ref2[1]; body = _toJs["default"].object(body); if (body.ok && res.headers['set-cookie']) { var cookie = _setCookieParser["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: ".concat(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 _output = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(id, data) { var _context2, _context3; var _this$output_options2, server, bucket, config, _context4, jar, cookie, body; return _regenerator["default"].wrap(function _callee$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: if (!(this.prepared !== true)) { _context5.next = 4; break; } if (this.preparing == null) { this.prepare(); } _context5.next = 4; return this.preparing; case 4: _this$output_options2 = this.output_options, server = _this$output_options2.server, bucket = _this$output_options2.bucket; config = { url: (0, _concat["default"])(_context2 = (0, _concat["default"])(_context3 = "".concat(server, "/")).call(_context3, bucket, "/")).call(_context2, 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 = _request["default"].jar(); cookie = _request["default"].cookie((0, _concat["default"])(_context4 = "".concat(this.session.name, "=")).call(_context4, this.session.id)); jar.setCookie(cookie, server); config.jar = jar; } _context5.t0 = _toJs["default"]; _context5.next = 10; return request(config); case 10: _context5.t1 = _context5.sent[1]; body = _context5.t0.object.call(_context5.t0, _context5.t1); if (body.error) { if (body.reason === 'Document exists') { body.reason = "The '".concat(id, "' document already exists"); } this.log('error', body.reason); } case 13: case "end": return _context5.stop(); } } }, _callee, this); })); function output(_x, _x2) { return _output.apply(this, arguments); } return output; }() }]); return SyncGateway; }(_base["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 _promise["default"](function (resolve, reject) { (0, _request["default"])(options, function (err, res, body) { /* istanbul ignore next : to hard to mock test */ if (err) return reject(err); resolve([res, body]); }); }); }