UNPKG

files.com

Version:
363 lines (362 loc) 18.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); exports.__esModule = true; exports.default = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _Api = _interopRequireDefault(require("../Api")); var errors = _interopRequireWildcard(require("../Errors")); var _utils = require("../utils"); var _Request; /* eslint-disable no-unused-vars */ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /* eslint-enable no-unused-vars */ /** * Class Request */ var Request = /*#__PURE__*/(0, _createClass2.default)(function Request() { var _this = this; var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; (0, _classCallCheck2.default)(this, Request); (0, _defineProperty2.default)(this, "attributes", {}); (0, _defineProperty2.default)(this, "options", {}); (0, _defineProperty2.default)(this, "isLoaded", function () { return !!_this.attributes.id; }); // int64 # Request ID (0, _defineProperty2.default)(this, "getId", function () { return _this.attributes.id; }); (0, _defineProperty2.default)(this, "setId", function (value) { _this.attributes.id = value; }); // string # Folder path. This must be slash-delimited, but it must neither start nor end with a slash. Maximum of 5000 characters. (0, _defineProperty2.default)(this, "getPath", function () { return _this.attributes.path; }); (0, _defineProperty2.default)(this, "setPath", function (value) { _this.attributes.path = value; }); // string # Source filename, if applicable (0, _defineProperty2.default)(this, "getSource", function () { return _this.attributes.source; }); (0, _defineProperty2.default)(this, "setSource", function (value) { _this.attributes.source = value; }); // string # Destination filename (0, _defineProperty2.default)(this, "getDestination", function () { return _this.attributes.destination; }); (0, _defineProperty2.default)(this, "setDestination", function (value) { _this.attributes.destination = value; }); // int64 # ID of automation that created request (0, _defineProperty2.default)(this, "getAutomationId", function () { return _this.attributes.automation_id; }); (0, _defineProperty2.default)(this, "setAutomationId", function (value) { _this.attributes.automation_id = value; }); // string # User making the request (if applicable) (0, _defineProperty2.default)(this, "getUserDisplayName", function () { return _this.attributes.user_display_name; }); (0, _defineProperty2.default)(this, "setUserDisplayName", function (value) { _this.attributes.user_display_name = value; }); // string # A list of user IDs to request the file from. If sent as a string, it should be comma-delimited. (0, _defineProperty2.default)(this, "getUserIds", function () { return _this.attributes.user_ids; }); (0, _defineProperty2.default)(this, "setUserIds", function (value) { _this.attributes.user_ids = value; }); // string # A list of group IDs to request the file from. If sent as a string, it should be comma-delimited. (0, _defineProperty2.default)(this, "getGroupIds", function () { return _this.attributes.group_ids; }); (0, _defineProperty2.default)(this, "setGroupIds", function (value) { _this.attributes.group_ids = value; }); (0, _defineProperty2.default)(this, "delete", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee() { var params, _args = arguments; return _regenerator.default.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: params = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}; if (_this.attributes.id) { _context.next = 1; break; } throw new errors.EmptyPropertyError('Current object has no id'); case 1: if ((0, _utils.isObject)(params)) { _context.next = 2; break; } throw new errors.InvalidParameterError("Bad parameter: params must be of type object, received ".concat((0, _utils.getType)(params))); case 2: params.id = _this.attributes.id; if (!(params.id && !(0, _utils.isInt)(params.id))) { _context.next = 3; break; } throw new errors.InvalidParameterError("Bad parameter: id must be of type Int, received ".concat((0, _utils.getType)(params.id))); case 3: if (params.id) { _context.next = 5; break; } if (!_this.attributes.id) { _context.next = 4; break; } params.id = _this.id; _context.next = 5; break; case 4: throw new errors.MissingParameterError('Parameter missing: id'); case 5: _context.next = 6; return _Api.default.sendRequest("/requests/".concat(encodeURIComponent(params.id)), 'DELETE', params, _this.options); case 6: case "end": return _context.stop(); } }, _callee); }))); (0, _defineProperty2.default)(this, "destroy", function () { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return _this.delete(params); }); (0, _defineProperty2.default)(this, "save", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2() { var newObject; return _regenerator.default.wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: if (!_this.attributes.id) { _context2.next = 1; break; } throw new errors.NotImplementedError('The Request object doesn\'t support updates.'); case 1: _context2.next = 2; return Request.create(_this.attributes, _this.options); case 2: newObject = _context2.sent; _this.attributes = _objectSpread({}, newObject.attributes); return _context2.abrupt("return", true); case 3: case "end": return _context2.stop(); } }, _callee2); }))); Object.entries(attributes).forEach(function (_ref3) { var _ref4 = (0, _slicedToArray2.default)(_ref3, 2), key = _ref4[0], value = _ref4[1]; var normalizedKey = key.replace('?', ''); _this.attributes[normalizedKey] = value; Object.defineProperty(_this, normalizedKey, { value: value, writable: false }); }); this.options = _objectSpread({}, options); }); _Request = Request; // Parameters: // cursor - string - Used for pagination. When a list request has more records available, cursors are provided in the response headers `X-Files-Cursor-Next` and `X-Files-Cursor-Prev`. Send one of those cursor value here to resume an existing list from the next available record. Note: many of our SDKs have iterator methods that will automatically handle cursor-based pagination. // per_page - int64 - Number of records to show per page. (Max: 10,000, 1,000 or less is recommended). // sort_by - object - If set, sort records by the specified field in either `asc` or `desc` direction. Valid fields are . // mine - boolean - Only show requests of the current user? (Defaults to true if current user is not a site admin.) // path - string - Path to show requests for. If omitted, shows all paths. Send `/` to represent the root directory. (0, _defineProperty2.default)(Request, "list", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee3() { var _response$data; var params, options, response, _args3 = arguments; return _regenerator.default.wrap(function (_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: params = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {}; options = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {}; if (!(params.cursor && !(0, _utils.isString)(params.cursor))) { _context3.next = 1; break; } throw new errors.InvalidParameterError("Bad parameter: cursor must be of type String, received ".concat((0, _utils.getType)(params.cursor))); case 1: if (!(params.per_page && !(0, _utils.isInt)(params.per_page))) { _context3.next = 2; break; } throw new errors.InvalidParameterError("Bad parameter: per_page must be of type Int, received ".concat((0, _utils.getType)(params.per_page))); case 2: if (!(params.path && !(0, _utils.isString)(params.path))) { _context3.next = 3; break; } throw new errors.InvalidParameterError("Bad parameter: path must be of type String, received ".concat((0, _utils.getType)(params.path))); case 3: _context3.next = 4; return _Api.default.sendRequest('/requests', 'GET', params, options); case 4: response = _context3.sent; return _context3.abrupt("return", (response === null || response === void 0 || (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.map(function (obj) { return new _Request(obj, options); })) || []); case 5: case "end": return _context3.stop(); } }, _callee3); }))); (0, _defineProperty2.default)(Request, "all", function () { var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return _Request.list(params, options); }); // Parameters: // cursor - string - Used for pagination. When a list request has more records available, cursors are provided in the response headers `X-Files-Cursor-Next` and `X-Files-Cursor-Prev`. Send one of those cursor value here to resume an existing list from the next available record. Note: many of our SDKs have iterator methods that will automatically handle cursor-based pagination. // per_page - int64 - Number of records to show per page. (Max: 10,000, 1,000 or less is recommended). // sort_by - object - If set, sort records by the specified field in either `asc` or `desc` direction. Valid fields are . // mine - boolean - Only show requests of the current user? (Defaults to true if current user is not a site admin.) // path (required) - string - Path to show requests for. If omitted, shows all paths. Send `/` to represent the root directory. (0, _defineProperty2.default)(Request, "getFolder", /*#__PURE__*/function () { var _ref6 = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee4(path) { var _response$data2; var params, options, response, _args4 = arguments; return _regenerator.default.wrap(function (_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: params = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {}; options = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : {}; if ((0, _utils.isObject)(params)) { _context4.next = 1; break; } throw new errors.InvalidParameterError("Bad parameter: params must be of type object, received ".concat((0, _utils.getType)(params))); case 1: params.path = path; if (params.path) { _context4.next = 2; break; } throw new errors.MissingParameterError('Parameter missing: path'); case 2: if (!(params.cursor && !(0, _utils.isString)(params.cursor))) { _context4.next = 3; break; } throw new errors.InvalidParameterError("Bad parameter: cursor must be of type String, received ".concat((0, _utils.getType)(params.cursor))); case 3: if (!(params.per_page && !(0, _utils.isInt)(params.per_page))) { _context4.next = 4; break; } throw new errors.InvalidParameterError("Bad parameter: per_page must be of type Int, received ".concat((0, _utils.getType)(params.per_page))); case 4: if (!(params.path && !(0, _utils.isString)(params.path))) { _context4.next = 5; break; } throw new errors.InvalidParameterError("Bad parameter: path must be of type String, received ".concat((0, _utils.getType)(params.path))); case 5: _context4.next = 6; return _Api.default.sendRequest("/requests/folders/".concat(encodeURIComponent(params.path)), 'GET', params, options); case 6: response = _context4.sent; return _context4.abrupt("return", (response === null || response === void 0 || (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.map(function (obj) { return new _Request(obj, options); })) || []); case 7: case "end": return _context4.stop(); } }, _callee4); })); return function (_x) { return _ref6.apply(this, arguments); }; }()); // Parameters: // path (required) - string - Folder path on which to request the file. // destination (required) - string - Destination filename (without extension) to request. // user_ids - string - A list of user IDs to request the file from. If sent as a string, it should be comma-delimited. // group_ids - string - A list of group IDs to request the file from. If sent as a string, it should be comma-delimited. (0, _defineProperty2.default)(Request, "create", /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee5() { var params, options, response, _args5 = arguments; return _regenerator.default.wrap(function (_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: params = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : {}; options = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {}; if (params.path) { _context5.next = 1; break; } throw new errors.MissingParameterError('Parameter missing: path'); case 1: if (params.destination) { _context5.next = 2; break; } throw new errors.MissingParameterError('Parameter missing: destination'); case 2: if (!(params.path && !(0, _utils.isString)(params.path))) { _context5.next = 3; break; } throw new errors.InvalidParameterError("Bad parameter: path must be of type String, received ".concat((0, _utils.getType)(params.path))); case 3: if (!(params.destination && !(0, _utils.isString)(params.destination))) { _context5.next = 4; break; } throw new errors.InvalidParameterError("Bad parameter: destination must be of type String, received ".concat((0, _utils.getType)(params.destination))); case 4: if (!(params.user_ids && !(0, _utils.isString)(params.user_ids))) { _context5.next = 5; break; } throw new errors.InvalidParameterError("Bad parameter: user_ids must be of type String, received ".concat((0, _utils.getType)(params.user_ids))); case 5: if (!(params.group_ids && !(0, _utils.isString)(params.group_ids))) { _context5.next = 6; break; } throw new errors.InvalidParameterError("Bad parameter: group_ids must be of type String, received ".concat((0, _utils.getType)(params.group_ids))); case 6: _context5.next = 7; return _Api.default.sendRequest('/requests', 'POST', params, options); case 7: response = _context5.sent; return _context5.abrupt("return", new _Request(response === null || response === void 0 ? void 0 : response.data, options)); case 8: case "end": return _context5.stop(); } }, _callee5); }))); var _default = exports.default = Request; module.exports = Request; module.exports.default = Request;