UNPKG

cabbie-sync

Version:
240 lines (167 loc) 6.79 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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); var _cookie = require('./cookie'); var _driver = require('./driver'); var _driver2 = _interopRequireDefault(_driver); var _addDebugging = require('./add-debugging'); var _addDebugging2 = _interopRequireDefault(_addDebugging); var _url = require('url'); var _baseClass = require('./base-class'); var _baseClass2 = _interopRequireDefault(_baseClass); var _flowRuntime = require('flow-runtime'); var _flowRuntime2 = _interopRequireDefault(_flowRuntime); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * * This file is generated automatically, run npm run build to re-generate. **/ var Driver = _flowRuntime2.default.tdz(function () { return _driver2.default; }); /* * Validate the cookie data * * @private */ var Cookie = _flowRuntime2.default.tdz(function () { return _cookie.Cookie; }); function validateCookie(cookie /*: Cookie*/) { var completed /*: boolean*/ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var _cookieType = _flowRuntime2.default.ref(Cookie); var _completedType = _flowRuntime2.default.boolean(); _flowRuntime2.default.param('cookie', _cookieType).assert(cookie); _flowRuntime2.default.param('completed', _completedType).assert(completed); if (completed) { if (!cookie.name) { throw new Error('A cookie "name" is required.'); } if (!cookie.value) { throw new Error('a cookie "value" is required.'); } } if (!cookie.path) { cookie.path = '/'; } // localhost is a special case, the domain must be "" if (cookie.domain === 'localhost') cookie.domain = ''; return cookie; } /* * Managing cookie-storage */ _flowRuntime2.default.annotate(validateCookie, _flowRuntime2.default.function(_flowRuntime2.default.param('cookie', _flowRuntime2.default.ref(Cookie)), _flowRuntime2.default.param('completed', _flowRuntime2.default.boolean()))); var CookieStorage = function (_BaseClass) { (0, _inherits3.default)(CookieStorage, _BaseClass); function CookieStorage(driver /*: Driver*/) { (0, _classCallCheck3.default)(this, CookieStorage); var _driverType = _flowRuntime2.default.ref(Driver); _flowRuntime2.default.param('driver', _driverType).assert(driver); return (0, _possibleConstructorReturn3.default)(this, (CookieStorage.__proto__ || (0, _getPrototypeOf2.default)(CookieStorage)).call(this, driver, '/cookie')); } /* * Retrieve all cookies visible to the current page. */ (0, _createClass3.default)(CookieStorage, [{ key: 'getCookies', value: function getCookies() /*: Array<Cookie>*/ { var _returnType = _flowRuntime2.default.return(_flowRuntime2.default.array(_flowRuntime2.default.ref(Cookie))); return _returnType.assert(this.requestJSON('GET', '')); } /* * Retrieve all cookie names visible to the current page. */ }, { key: 'getKeys', value: function getKeys() /*: Array<string>*/ { var _returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.array(_flowRuntime2.default.string())); var cookies = this.requestJSON('GET', ''); return _returnType2.assert(cookies.map(function (cookie) { return cookie.name; })); } /* * Retrieve a specific cookie by name that is visible to the current page. */ }, { key: 'getCookie', value: function getCookie(name /*: string*/) /*: Cookie | void*/ { var _nameType = _flowRuntime2.default.string(); var _returnType3 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.ref(Cookie), _flowRuntime2.default.void())); _flowRuntime2.default.param('name', _nameType).assert(name); var cookies = this.getCookies(); cookies = cookies.filter(function (cookie) { return cookie.name == name; }); return _returnType3.assert(cookies.length ? cookies[0] : undefined); } /* * Set a cookie that is visible to the current page. */ }, { key: 'setCookie', value: function setCookie(cookie /*: Cookie*/) /*: void*/ { var _cookieType2 = _flowRuntime2.default.ref(Cookie); var _returnType4 = _flowRuntime2.default.return(_flowRuntime2.default.void()); _flowRuntime2.default.param('cookie', _cookieType2).assert(cookie); validateCookie(cookie, true); if (cookie.domain != null) { this.requestJSON('POST', '', { cookie: cookie }); } else { var base = this.driver.activeWindow.getUrl(); var hostname = (0, _url.parse)(base).hostname; if (hostname == null) { throw new Error('The hostname should never be undefined. This should never happen.'); } cookie.domain = hostname; validateCookie(cookie, true); this.requestJSON('POST', '', { cookie: cookie }); } } /* * Delete the cookie with the given name. */ }, { key: 'removeCookie', value: function removeCookie(name /*: string*/) /*: void*/ { var _nameType2 = _flowRuntime2.default.string(); var _returnType5 = _flowRuntime2.default.return(_flowRuntime2.default.void()); _flowRuntime2.default.param('name', _nameType2).assert(name); this.requestJSON('DELETE', '/' + name); } /* * Delete all cookies visible to the current page. */ }, { key: 'clear', value: function clear() /*: void*/ { var _returnType6 = _flowRuntime2.default.return(_flowRuntime2.default.void()); this.requestJSON('DELETE', ''); } /* * Get the number of items in the storage */ }, { key: 'getSize', value: function getSize() /*: number*/ { var _returnType7 = _flowRuntime2.default.return(_flowRuntime2.default.number()); var cookies = this.getCookies(); return _returnType7.assert(cookies.length); } }]); return CookieStorage; }(_baseClass2.default); (0, _addDebugging2.default)(CookieStorage); exports.default = CookieStorage;