UNPKG

@userlist/push

Version:
489 lines (462 loc) 18 kB
'use strict'; var https = require('https'); function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return (String )(t); } function toPropertyKey(t) { var i = toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: false }), e; } var Relation = /*#__PURE__*/function () { function Relation(scope, resource) { _classCallCheck(this, Relation); this.scope = scope; this.resource = resource; } return _createClass(Relation, [{ key: "push", value: function push(attributes) { var resource = new this.resource(attributes); return this.client.post(this.endpoint, resource); } }, { key: "create", value: function create() { return this.push.apply(this, arguments); } }, { key: "update", value: function update() { return this.push.apply(this, arguments); } }, { key: "delete", value: function _delete(attributes) { var resource = new this.resource(attributes); return this.client["delete"](this.endpoint, resource); } }, { key: "client", get: function get() { return this.scope.client; } }, { key: "endpoint", get: function get() { return this.resource.endpoint; } }]); }(); function _defineProperty(e, r, t) { return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: true } : { done: false, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = true, u = false; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = true, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function decamelize(string) { var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '_'; return string.replace(/([a-z\d])([A-Z])/g, "$1".concat(separator, "$2")).replace(/([A-Z]+)([A-Z][a-z\d]+)/g, "$1".concat(separator, "$2")).toLowerCase(); } var defaultConfiguration = { pushKey: null, pushEndpoint: 'https://push.userlist.com' }; var Config = /*#__PURE__*/function () { function Config(configFromConstructor) { var _this = this; _classCallCheck(this, Config); var configFromDefault = this.configFromDefault, configFromEnvironment = this.configFromEnvironment; if (configFromConstructor instanceof Config) { configFromConstructor = configFromConstructor.config; } this.config = Object.assign({}, configFromDefault, configFromEnvironment, configFromConstructor); var _iterator = _createForOfIteratorHelper(this.configKeys), _step; try { var _loop = function _loop() { var key = _step.value; Object.defineProperty(_this, key, { set: function set(value) { this.config[key] = value; }, get: function get() { return this.config[key]; } }); }; for (_iterator.s(); !(_step = _iterator.n()).done;) { _loop(); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } return _createClass(Config, [{ key: "configKeys", get: function get() { return Object.keys(this.configFromDefault); } }, { key: "configFromDefault", get: function get() { return defaultConfiguration; } }, { key: "configFromEnvironment", get: function get() { var config = {}; var _iterator2 = _createForOfIteratorHelper(this.configKeys), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var key = _step2.value; var value = process.env["USERLIST_".concat(decamelize(key).toUpperCase())]; if (typeof value !== 'undefined') { config[key] = value; } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return config; } }]); }(); var _excluded = ["url", "payload"]; 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), true).forEach(function (r) { _defineProperty(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; } function performRequest(_ref) { var url = _ref.url, payload = _ref.payload, options = _objectWithoutProperties(_ref, _excluded); options = _objectSpread({ hostname: url.host, port: url.port !== '' ? url.port : 443, path: url.pathname }, options); return new Promise(function (resolve, reject) { var request = https.request(options, function (response) { resolve(response); }); request.useChunkedEncodingByDefault = true; request.on('error', function (error) { reject(error); }); if (payload) { request.write(JSON.stringify(payload)); } request.end(); }); } var Client = /*#__PURE__*/function () { function Client() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Client); this.config = new Config(config); this.agent = new https.Agent({ keepAlive: true }); } return _createClass(Client, [{ key: "get", value: function get(endpoint) { return this._request('GET', endpoint); } }, { key: "put", value: function put(endpoint, payload) { return this._request('PUT', endpoint, payload); } }, { key: "post", value: function post(endpoint, payload) { return this._request('POST', endpoint, payload); } }, { key: "delete", value: function _delete(endpoint, payload) { return this._request('DELETE', endpoint, payload); } }, { key: "_request", value: function _request(method, endpoint, payload) { var _this$config = this.config, pushEndpoint = _this$config.pushEndpoint, pushKey = _this$config.pushKey; var agent = this.agent; var url = new URL(endpoint, pushEndpoint); var headers = { Accept: 'application/json', 'Content-Type': 'application/json; charset=UTF-8', Authorization: "Push ".concat(pushKey) }; return performRequest({ method: method, url: url, headers: headers, payload: payload, agent: agent }); } }]); }(); function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: true, configurable: true } }), Object.defineProperty(t, "prototype", { writable: false }), e && _setPrototypeOf(t, e); } var Resource = /*#__PURE__*/_createClass(function Resource() { var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Resource); if (attributes == null || _typeof(attributes) !== 'object') { throw 'Missing required attributes object'; } Object.assign(this, attributes); }); function _callSuper$4(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$4() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct$4() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$4 = function _isNativeReflectConstruct() { return !!t; })(); } var User = /*#__PURE__*/function (_Resource) { function User(attributes) { _classCallCheck(this, User); if (typeof attributes === 'string') { attributes = { identifier: attributes }; } if (attributes == null || _typeof(attributes) !== 'object') { throw 'Missing required attributes object'; } if (attributes.identifier == null && attributes.email == null) { throw 'Missing required attribute identifier or email'; } return _callSuper$4(this, User, [attributes]); } _inherits(User, _Resource); return _createClass(User, null, [{ key: "endpoint", get: function get() { return '/users'; } }]); }(Resource); function _callSuper$3(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$3() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct$3() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$3 = function _isNativeReflectConstruct() { return !!t; })(); } var Company = /*#__PURE__*/function (_Resource) { function Company(attributes) { _classCallCheck(this, Company); if (typeof attributes === 'string') { attributes = { identifier: attributes }; } if (attributes == null || _typeof(attributes) !== 'object') { throw 'Missing required attributes object'; } if (attributes.identifier == null) { throw 'Missing required attribute identifier'; } return _callSuper$3(this, Company, [attributes]); } _inherits(Company, _Resource); return _createClass(Company, null, [{ key: "endpoint", get: function get() { return '/companies'; } }]); }(Resource); function _callSuper$2(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$2() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct$2() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$2 = function _isNativeReflectConstruct() { return !!t; })(); } var Relationship = /*#__PURE__*/function (_Resource) { function Relationship(attributes) { _classCallCheck(this, Relationship); if (attributes == null || _typeof(attributes) !== 'object') { throw 'Missing required attributes object'; } if (attributes.user == null) { throw 'Missing required attribute user'; } if (attributes.company == null) { throw 'Missing required attribute company'; } return _callSuper$2(this, Relationship, [attributes]); } _inherits(Relationship, _Resource); return _createClass(Relationship, null, [{ key: "endpoint", get: function get() { return '/relationships'; } }]); }(Resource); function _callSuper$1(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct$1() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct$1() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct$1 = function _isNativeReflectConstruct() { return !!t; })(); } var Event = /*#__PURE__*/function (_Resource) { function Event(attributes) { _classCallCheck(this, Event); if (attributes == null || _typeof(attributes) !== 'object') { throw 'Missing required attributes object'; } if (attributes.user == null && attributes.company == null) { throw 'Missing required attribute user or company'; } if (attributes.name == null) { throw 'Missing required attribute name'; } return _callSuper$1(this, Event, [attributes]); } _inherits(Event, _Resource); return _createClass(Event, null, [{ key: "endpoint", get: function get() { return '/events'; } }]); }(Resource); function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } var Message = /*#__PURE__*/function (_Resource) { function Message() { _classCallCheck(this, Message); return _callSuper(this, Message, arguments); } _inherits(Message, _Resource); return _createClass(Message, null, [{ key: "endpoint", get: function get() { return '/messages'; } }]); }(Resource); var Userlist = /*#__PURE__*/function () { function Userlist() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Userlist); this.config = new Config(config); this.client = new Client(this.config); this.users = new Relation(this, User); this.companies = new Relation(this, Company); this.relationships = new Relation(this, Relationship); this.events = new Relation(this, Event); this.messages = new Relation(this, Message); this.identify = this.user; this.track = this.event; } return _createClass(Userlist, [{ key: "user", value: function user() { var _this$users; return (_this$users = this.users).push.apply(_this$users, arguments); } }, { key: "company", value: function company() { var _this$companies; return (_this$companies = this.companies).push.apply(_this$companies, arguments); } }, { key: "relationship", value: function relationship() { var _this$relationships; return (_this$relationships = this.relationships).push.apply(_this$relationships, arguments); } }, { key: "event", value: function event() { var _this$events; return (_this$events = this.events).push.apply(_this$events, arguments); } }, { key: "message", value: function message() { var _this$messages; return (_this$messages = this.messages).push.apply(_this$messages, arguments); } }]); }(); module.exports = Userlist;