UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

253 lines (252 loc) 8.98 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _utils = _interopRequireDefault(require("../utils")); var Cache = /*#__PURE__*/function () { function Cache(app) { (0, _classCallCheck2["default"])(this, Cache); this.app = app; this.parsers = {}; } (0, _createClass2["default"])(Cache, [{ key: "setObjectFactory", value: function setObjectFactory(objectName, factoryMethod) { this.parsers[objectName] = factoryMethod; } }, { key: "parseItem", value: function parseItem(item) { var className = item && item.___class; if (className) { var Class = this.parsers[className]; if (Class) { item = new Class(item); } } return item; } }, { key: "stringifyItem", value: function stringifyItem(item) { if (_utils["default"].isCustomClassInstance(item)) { item.___class = item.___class || _utils["default"].getClassName(item); } return JSON.stringify(item); } }, { key: "put", value: function () { var _put = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(key, value, timeToLive) { return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!(!key || typeof key !== 'string')) { _context.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: if (!(timeToLive && (typeof timeToLive !== 'number' || timeToLive < 0))) { _context.next = 4; break; } throw new Error('Cache TimeToLive must be a positive number.'); case 4: return _context.abrupt("return", this.app.request.put({ url: this.app.urls.cacheItem(key), query: { timeout: timeToLive }, headers: { 'Content-Type': 'application/json' }, data: this.stringifyItem(value) })); case 5: case "end": return _context.stop(); } }, _callee, this); })); function put(_x, _x2, _x3) { return _put.apply(this, arguments); } return put; }() }, { key: "get", value: function () { var _get = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(key) { var _this = this; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: if (!(!key || typeof key !== 'string')) { _context2.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: return _context2.abrupt("return", this.app.request.get({ url: this.app.urls.cacheItem(key) }).then(function (item) { return _this.parseItem(item); })); case 3: case "end": return _context2.stop(); } }, _callee2, this); })); function get(_x4) { return _get.apply(this, arguments); } return get; }() }, { key: "remove", value: function () { var _remove = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(key) { return _regenerator["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: if (!(!key || typeof key !== 'string')) { _context3.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: return _context3.abrupt("return", this.app.request["delete"]({ url: this.app.urls.cacheItem(key) })); case 3: case "end": return _context3.stop(); } }, _callee3, this); })); function remove(_x5) { return _remove.apply(this, arguments); } return remove; }() }, { key: "contains", value: function () { var _contains = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(key) { return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: if (!(!key || typeof key !== 'string')) { _context4.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: return _context4.abrupt("return", this.app.request.get({ url: this.app.urls.cacheItemCheck(key) })); case 3: case "end": return _context4.stop(); } }, _callee4, this); })); function contains(_x6) { return _contains.apply(this, arguments); } return contains; }() }, { key: "expireIn", value: function () { var _expireIn = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(key, seconds) { return _regenerator["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: if (!(!key || typeof key !== 'string')) { _context5.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: if (!(typeof seconds !== 'number' || seconds <= 0)) { _context5.next = 4; break; } throw new Error('Cache Expiration must be provided and must be a number of seconds.'); case 4: return _context5.abrupt("return", this.app.request.put({ url: this.app.urls.cacheItemExpireIn(key), query: { timeout: seconds }, data: {} })); case 5: case "end": return _context5.stop(); } }, _callee5, this); })); function expireIn(_x7, _x8) { return _expireIn.apply(this, arguments); } return expireIn; }() }, { key: "expireAt", value: function () { var _expireAt = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(key, timestamp) { return _regenerator["default"].wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: if (!(!key || typeof key !== 'string')) { _context6.next = 2; break; } throw new Error('Cache Key must be provided and must be a string.'); case 2: if (!(timestamp instanceof Date)) { _context6.next = 6; break; } timestamp = timestamp.getTime(); _context6.next = 8; break; case 6: if (!(typeof timestamp !== 'number' || timestamp <= 0)) { _context6.next = 8; break; } throw new Error('Cache Expiration must be provided and must be a timestamp or an instance of Date.'); case 8: return _context6.abrupt("return", this.app.request.put({ url: this.app.urls.cacheItemExpireAt(key), query: { timestamp: timestamp }, data: {} })); case 9: case "end": return _context6.stop(); } }, _callee6, this); })); function expireAt(_x9, _x10) { return _expireAt.apply(this, arguments); } return expireAt; }() }]); return Cache; }(); exports["default"] = Cache;