roboto-js 1.6.11 → 1.6.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +37 -2
- package/dist/cjs/rbt_metrics_api.cjs +152 -0
- package/dist/esm/index.js +35 -2
- package/dist/esm/rbt_metrics_api.js +85 -0
- package/package.json +1 -1
- package/src/index.js +33 -2
- package/src/rbt_api.js +1 -0
- package/src/rbt_metrics_api.js +90 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -25,6 +25,7 @@ exports["default"] = void 0;
|
|
|
25
25
|
var _rbt_api = _interopRequireDefault(require("./rbt_api.cjs"));
|
|
26
26
|
var _rbt_object = _interopRequireDefault(require("./rbt_object.cjs"));
|
|
27
27
|
var _rbt_file = _interopRequireDefault(require("./rbt_file.cjs"));
|
|
28
|
+
var _rbt_metrics_api = _interopRequireDefault(require("./rbt_metrics_api.cjs"));
|
|
28
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
29
30
|
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); }
|
|
30
31
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
@@ -41,7 +42,8 @@ var Roboto = exports["default"] = /*#__PURE__*/function () {
|
|
|
41
42
|
accessKey = _ref.accessKey,
|
|
42
43
|
localStorageAdaptor = _ref.localStorageAdaptor,
|
|
43
44
|
_ref$disableWebSocket = _ref.disableWebSocket,
|
|
44
|
-
disableWebSocket = _ref$disableWebSocket === void 0 ? false : _ref$disableWebSocket
|
|
45
|
+
disableWebSocket = _ref$disableWebSocket === void 0 ? false : _ref$disableWebSocket,
|
|
46
|
+
metricsHost = _ref.metricsHost;
|
|
45
47
|
var proxyReq = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
46
48
|
_classCallCheck(this, Roboto);
|
|
47
49
|
if (Roboto.instance && !proxyReq) {
|
|
@@ -82,12 +84,31 @@ var Roboto = exports["default"] = /*#__PURE__*/function () {
|
|
|
82
84
|
if (isBrowser) {
|
|
83
85
|
this.api.initLocalDb();
|
|
84
86
|
}
|
|
87
|
+
|
|
88
|
+
// METRICS API instance (separate host or same)
|
|
89
|
+
if (metricsHost && metricsHost !== host) {
|
|
90
|
+
var metricsUrl = metricsHost.startsWith('http') ? metricsHost : "https://".concat(metricsHost);
|
|
91
|
+
// You can customize headers if needed
|
|
92
|
+
this.metrics = new _rbt_metrics_api["default"]({
|
|
93
|
+
config: {
|
|
94
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
95
|
+
headers: {
|
|
96
|
+
accesskey: accessKey
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
// Use the same axios instance as main API
|
|
102
|
+
this.metrics = new _rbt_metrics_api["default"]({
|
|
103
|
+
axiosInstance: this.api.axios
|
|
104
|
+
});
|
|
105
|
+
}
|
|
85
106
|
Roboto.instance = this;
|
|
86
107
|
}
|
|
87
108
|
_createClass(Roboto, [{
|
|
88
109
|
key: "getVersion",
|
|
89
110
|
value: function getVersion() {
|
|
90
|
-
return '1.6.
|
|
111
|
+
return '1.6.12';
|
|
91
112
|
}
|
|
92
113
|
}, {
|
|
93
114
|
key: "setSiteEnv",
|
|
@@ -117,6 +138,20 @@ var Roboto = exports["default"] = /*#__PURE__*/function () {
|
|
|
117
138
|
host = this._stripHttpsForDomains(host, ['localhost']);
|
|
118
139
|
this.api.setAppServiceHost(host);
|
|
119
140
|
}
|
|
141
|
+
}, {
|
|
142
|
+
key: "setMetricsHost",
|
|
143
|
+
value: function setMetricsHost(host) {
|
|
144
|
+
this.metricsHost = host;
|
|
145
|
+
var metricsUrl = host.startsWith('http') ? host : "https://".concat(host);
|
|
146
|
+
this.metrics = new _rbt_metrics_api["default"]({
|
|
147
|
+
config: {
|
|
148
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
149
|
+
headers: {
|
|
150
|
+
accesskey: this.config.accessKey
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
120
155
|
}, {
|
|
121
156
|
key: "registerTypedefs",
|
|
122
157
|
value: function registerTypedefs(typedefs) {
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
8
|
+
var _eventemitter = _interopRequireDefault(require("eventemitter3"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
11
|
+
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); }
|
|
12
|
+
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; }
|
|
13
|
+
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) { _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; }
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
16
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
17
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
18
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
19
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
21
|
+
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 || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
|
|
23
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
24
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
25
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
26
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
27
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
28
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
29
|
+
function getOrCreateDeviceId() {
|
|
30
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
31
|
+
var deviceId = localStorage.getItem('robotoDeviceId');
|
|
32
|
+
if (!deviceId) {
|
|
33
|
+
// Modern browsers support crypto.randomUUID()
|
|
34
|
+
deviceId = crypto && crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
35
|
+
localStorage.setItem('robotoDeviceId', deviceId);
|
|
36
|
+
}
|
|
37
|
+
return deviceId;
|
|
38
|
+
}
|
|
39
|
+
// On server/Node.cjs, return undefined (or you can fallback to some other logic)
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
var RbtMetricsApi = exports["default"] = /*#__PURE__*/function (_EventEmitter) {
|
|
43
|
+
_inherits(RbtMetricsApi, _EventEmitter);
|
|
44
|
+
/**
|
|
45
|
+
* You can pass either:
|
|
46
|
+
* - axiosInstance: an existing axios instance (recommended for same-host)
|
|
47
|
+
* - config: { baseUrl, headers, ... } to create a new instance (for different host)
|
|
48
|
+
*/
|
|
49
|
+
function RbtMetricsApi() {
|
|
50
|
+
var _this;
|
|
51
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
52
|
+
axiosInstance = _ref.axiosInstance,
|
|
53
|
+
config = _ref.config;
|
|
54
|
+
_classCallCheck(this, RbtMetricsApi);
|
|
55
|
+
_this = _callSuper(this, RbtMetricsApi);
|
|
56
|
+
if (axiosInstance) {
|
|
57
|
+
_this._axios = axiosInstance;
|
|
58
|
+
} else if (config) {
|
|
59
|
+
_this._axios = _axios["default"].create(config);
|
|
60
|
+
} else {
|
|
61
|
+
throw new Error("RbtMetricsApi requires either axiosInstance or config");
|
|
62
|
+
}
|
|
63
|
+
return _this;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Send an event to the metrics API.
|
|
68
|
+
* Usage:
|
|
69
|
+
* sendEvent('UIMain.userAction.login', { param1: 1, param2: 2 });
|
|
70
|
+
* sendEvent({ event: 'foo', ... });
|
|
71
|
+
*
|
|
72
|
+
roboto.metrics.sendEvent("UIMain.userAction.login", { foo: 123, bar: "baz" });
|
|
73
|
+
This will POST a payload like:
|
|
74
|
+
{
|
|
75
|
+
"event": "UIMain.userAction.login",
|
|
76
|
+
"category": "UIMain",
|
|
77
|
+
"action": "userAction",
|
|
78
|
+
"label": "login",
|
|
79
|
+
--
|
|
80
|
+
"foo": 123,
|
|
81
|
+
"bar": "baz"
|
|
82
|
+
}
|
|
83
|
+
*/
|
|
84
|
+
_createClass(RbtMetricsApi, [{
|
|
85
|
+
key: "sendEvent",
|
|
86
|
+
value: (function () {
|
|
87
|
+
var _sendEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(eventOrObj) {
|
|
88
|
+
var paramsObj,
|
|
89
|
+
payload,
|
|
90
|
+
parts,
|
|
91
|
+
deviceId,
|
|
92
|
+
res,
|
|
93
|
+
_args = arguments;
|
|
94
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
95
|
+
while (1) switch (_context.prev = _context.next) {
|
|
96
|
+
case 0:
|
|
97
|
+
paramsObj = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
98
|
+
if (!(typeof eventOrObj === 'string')) {
|
|
99
|
+
_context.next = 6;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
parts = eventOrObj.split('.');
|
|
103
|
+
payload = _objectSpread({
|
|
104
|
+
event: eventOrObj,
|
|
105
|
+
category: parts[0] || undefined,
|
|
106
|
+
action: parts[1] || undefined,
|
|
107
|
+
label: parts[2] || undefined
|
|
108
|
+
}, paramsObj);
|
|
109
|
+
_context.next = 11;
|
|
110
|
+
break;
|
|
111
|
+
case 6:
|
|
112
|
+
if (!(_typeof(eventOrObj) === 'object' && eventOrObj !== null)) {
|
|
113
|
+
_context.next = 10;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
payload = eventOrObj;
|
|
117
|
+
_context.next = 11;
|
|
118
|
+
break;
|
|
119
|
+
case 10:
|
|
120
|
+
throw new Error("Invalid arguments to sendEvent");
|
|
121
|
+
case 11:
|
|
122
|
+
// Add deviceId (only if available)
|
|
123
|
+
deviceId = getOrCreateDeviceId();
|
|
124
|
+
if (deviceId) {
|
|
125
|
+
payload.deviceId = deviceId;
|
|
126
|
+
}
|
|
127
|
+
_context.prev = 13;
|
|
128
|
+
_context.next = 16;
|
|
129
|
+
return this._axios.post('/event', payload);
|
|
130
|
+
case 16:
|
|
131
|
+
res = _context.sent;
|
|
132
|
+
this.emit('sent', payload, res.data);
|
|
133
|
+
return _context.abrupt("return", res.data);
|
|
134
|
+
case 21:
|
|
135
|
+
_context.prev = 21;
|
|
136
|
+
_context.t0 = _context["catch"](13);
|
|
137
|
+
this.emit('error', _context.t0, payload);
|
|
138
|
+
throw _context.t0;
|
|
139
|
+
case 25:
|
|
140
|
+
case "end":
|
|
141
|
+
return _context.stop();
|
|
142
|
+
}
|
|
143
|
+
}, _callee, this, [[13, 21]]);
|
|
144
|
+
}));
|
|
145
|
+
function sendEvent(_x) {
|
|
146
|
+
return _sendEvent.apply(this, arguments);
|
|
147
|
+
}
|
|
148
|
+
return sendEvent;
|
|
149
|
+
}())
|
|
150
|
+
}]);
|
|
151
|
+
return RbtMetricsApi;
|
|
152
|
+
}(_eventemitter["default"]);
|
package/dist/esm/index.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import RbtApi from './rbt_api.js';
|
|
2
2
|
import RbtObject from './rbt_object.js';
|
|
3
3
|
import RbtFile from './rbt_file.js';
|
|
4
|
+
import RbtMetricsApi from './rbt_metrics_api.js';
|
|
4
5
|
export { RbtApi, RbtObject, RbtFile
|
|
5
6
|
//User,
|
|
6
7
|
//Site
|
|
7
8
|
};
|
|
8
9
|
export default class Roboto {
|
|
9
10
|
getVersion() {
|
|
10
|
-
return '1.6.
|
|
11
|
+
return '1.6.12';
|
|
11
12
|
}
|
|
12
13
|
constructor({
|
|
13
14
|
host,
|
|
14
15
|
accessKey,
|
|
15
16
|
localStorageAdaptor,
|
|
16
|
-
disableWebSocket = false
|
|
17
|
+
disableWebSocket = false,
|
|
18
|
+
metricsHost
|
|
17
19
|
}, proxyReq = null) {
|
|
18
20
|
if (Roboto.instance && !proxyReq) {
|
|
19
21
|
// if on client, there can only be one instance
|
|
@@ -53,6 +55,25 @@ export default class Roboto {
|
|
|
53
55
|
if (isBrowser) {
|
|
54
56
|
this.api.initLocalDb();
|
|
55
57
|
}
|
|
58
|
+
|
|
59
|
+
// METRICS API instance (separate host or same)
|
|
60
|
+
if (metricsHost && metricsHost !== host) {
|
|
61
|
+
const metricsUrl = metricsHost.startsWith('http') ? metricsHost : `https://${metricsHost}`;
|
|
62
|
+
// You can customize headers if needed
|
|
63
|
+
this.metrics = new RbtMetricsApi({
|
|
64
|
+
config: {
|
|
65
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
66
|
+
headers: {
|
|
67
|
+
accesskey: accessKey
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
} else {
|
|
72
|
+
// Use the same axios instance as main API
|
|
73
|
+
this.metrics = new RbtMetricsApi({
|
|
74
|
+
axiosInstance: this.api.axios
|
|
75
|
+
});
|
|
76
|
+
}
|
|
56
77
|
Roboto.instance = this;
|
|
57
78
|
}
|
|
58
79
|
setSiteEnv(siteEnv) {
|
|
@@ -72,6 +93,18 @@ export default class Roboto {
|
|
|
72
93
|
host = this._stripHttpsForDomains(host, ['localhost']);
|
|
73
94
|
this.api.setAppServiceHost(host);
|
|
74
95
|
}
|
|
96
|
+
setMetricsHost(host) {
|
|
97
|
+
this.metricsHost = host;
|
|
98
|
+
const metricsUrl = host.startsWith('http') ? host : `https://${host}`;
|
|
99
|
+
this.metrics = new RbtMetricsApi({
|
|
100
|
+
config: {
|
|
101
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
102
|
+
headers: {
|
|
103
|
+
accesskey: this.config.accessKey
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
75
108
|
registerTypedefs(typedefs) {
|
|
76
109
|
this.api.registerTypedefs(typedefs);
|
|
77
110
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import EventEmitter from 'eventemitter3';
|
|
3
|
+
function getOrCreateDeviceId() {
|
|
4
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
5
|
+
let deviceId = localStorage.getItem('robotoDeviceId');
|
|
6
|
+
if (!deviceId) {
|
|
7
|
+
// Modern browsers support crypto.randomUUID()
|
|
8
|
+
deviceId = crypto && crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
9
|
+
localStorage.setItem('robotoDeviceId', deviceId);
|
|
10
|
+
}
|
|
11
|
+
return deviceId;
|
|
12
|
+
}
|
|
13
|
+
// On server/Node.js, return undefined (or you can fallback to some other logic)
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
export default class RbtMetricsApi extends EventEmitter {
|
|
17
|
+
/**
|
|
18
|
+
* You can pass either:
|
|
19
|
+
* - axiosInstance: an existing axios instance (recommended for same-host)
|
|
20
|
+
* - config: { baseUrl, headers, ... } to create a new instance (for different host)
|
|
21
|
+
*/
|
|
22
|
+
constructor({
|
|
23
|
+
axiosInstance,
|
|
24
|
+
config
|
|
25
|
+
} = {}) {
|
|
26
|
+
super();
|
|
27
|
+
if (axiosInstance) {
|
|
28
|
+
this._axios = axiosInstance;
|
|
29
|
+
} else if (config) {
|
|
30
|
+
this._axios = axios.create(config);
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error("RbtMetricsApi requires either axiosInstance or config");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Send an event to the metrics API.
|
|
38
|
+
* Usage:
|
|
39
|
+
* sendEvent('UIMain.userAction.login', { param1: 1, param2: 2 });
|
|
40
|
+
* sendEvent({ event: 'foo', ... });
|
|
41
|
+
*
|
|
42
|
+
roboto.metrics.sendEvent("UIMain.userAction.login", { foo: 123, bar: "baz" });
|
|
43
|
+
This will POST a payload like:
|
|
44
|
+
{
|
|
45
|
+
"event": "UIMain.userAction.login",
|
|
46
|
+
"category": "UIMain",
|
|
47
|
+
"action": "userAction",
|
|
48
|
+
"label": "login",
|
|
49
|
+
--
|
|
50
|
+
"foo": 123,
|
|
51
|
+
"bar": "baz"
|
|
52
|
+
}
|
|
53
|
+
*/
|
|
54
|
+
async sendEvent(eventOrObj, paramsObj = {}) {
|
|
55
|
+
let payload;
|
|
56
|
+
if (typeof eventOrObj === 'string') {
|
|
57
|
+
const parts = eventOrObj.split('.');
|
|
58
|
+
payload = {
|
|
59
|
+
event: eventOrObj,
|
|
60
|
+
category: parts[0] || undefined,
|
|
61
|
+
action: parts[1] || undefined,
|
|
62
|
+
label: parts[2] || undefined,
|
|
63
|
+
...paramsObj
|
|
64
|
+
};
|
|
65
|
+
} else if (typeof eventOrObj === 'object' && eventOrObj !== null) {
|
|
66
|
+
payload = eventOrObj;
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error("Invalid arguments to sendEvent");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Add deviceId (only if available)
|
|
72
|
+
const deviceId = getOrCreateDeviceId();
|
|
73
|
+
if (deviceId) {
|
|
74
|
+
payload.deviceId = deviceId;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const res = await this._axios.post('/event', payload);
|
|
78
|
+
this.emit('sent', payload, res.data);
|
|
79
|
+
return res.data;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
this.emit('error', err, payload);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import RbtApi from './rbt_api.js';
|
|
3
3
|
import RbtObject from './rbt_object.js';
|
|
4
4
|
import RbtFile from './rbt_file.js';
|
|
5
|
+
import RbtMetricsApi from './rbt_metrics_api.js';
|
|
5
6
|
|
|
6
7
|
export {
|
|
7
8
|
RbtApi,
|
|
@@ -14,10 +15,10 @@ export {
|
|
|
14
15
|
export default class Roboto{
|
|
15
16
|
|
|
16
17
|
getVersion(){
|
|
17
|
-
return '1.6.
|
|
18
|
+
return '1.6.12';
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
constructor({ host, accessKey, localStorageAdaptor, disableWebSocket = false }, proxyReq = null) {
|
|
21
|
+
constructor({ host, accessKey, localStorageAdaptor, disableWebSocket = false, metricsHost }, proxyReq = null) {
|
|
21
22
|
|
|
22
23
|
if (Roboto.instance && !proxyReq) {
|
|
23
24
|
// if on client, there can only be one instance
|
|
@@ -59,6 +60,22 @@ export default class Roboto{
|
|
|
59
60
|
this.api.initLocalDb();
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
// METRICS API instance (separate host or same)
|
|
64
|
+
if (metricsHost && metricsHost !== host) {
|
|
65
|
+
const metricsUrl = metricsHost.startsWith('http') ? metricsHost : `https://${metricsHost}`;
|
|
66
|
+
// You can customize headers if needed
|
|
67
|
+
this.metrics = new RbtMetricsApi({
|
|
68
|
+
config: {
|
|
69
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
70
|
+
headers: { accesskey: accessKey }
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
// Use the same axios instance as main API
|
|
75
|
+
this.metrics = new RbtMetricsApi({ axiosInstance: this.api.axios });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
62
79
|
Roboto.instance = this;
|
|
63
80
|
|
|
64
81
|
}
|
|
@@ -94,6 +111,18 @@ export default class Roboto{
|
|
|
94
111
|
|
|
95
112
|
}
|
|
96
113
|
|
|
114
|
+
|
|
115
|
+
setMetricsHost(host) {
|
|
116
|
+
this.metricsHost = host;
|
|
117
|
+
const metricsUrl = host.startsWith('http') ? host : `https://${host}`;
|
|
118
|
+
this.metrics = new RbtMetricsApi({
|
|
119
|
+
config: {
|
|
120
|
+
baseURL: this._stripHttpsForDomains(metricsUrl, ['localhost']),
|
|
121
|
+
headers: { accesskey: this.config.accessKey }
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
97
126
|
registerTypedefs(typedefs){
|
|
98
127
|
|
|
99
128
|
this.api.registerTypedefs(typedefs);
|
|
@@ -206,5 +235,7 @@ export default class Roboto{
|
|
|
206
235
|
async post(endpoint, data){
|
|
207
236
|
return this.api.post(endpoint, data);
|
|
208
237
|
}
|
|
238
|
+
|
|
239
|
+
|
|
209
240
|
}
|
|
210
241
|
|
package/src/rbt_api.js
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import EventEmitter from 'eventemitter3';
|
|
4
|
+
|
|
5
|
+
function getOrCreateDeviceId() {
|
|
6
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
7
|
+
let deviceId = localStorage.getItem('robotoDeviceId');
|
|
8
|
+
if (!deviceId) {
|
|
9
|
+
// Modern browsers support crypto.randomUUID()
|
|
10
|
+
deviceId = (crypto && crypto.randomUUID)
|
|
11
|
+
? crypto.randomUUID()
|
|
12
|
+
: Math.random().toString(36).slice(2) + Date.now().toString(36);
|
|
13
|
+
localStorage.setItem('robotoDeviceId', deviceId);
|
|
14
|
+
}
|
|
15
|
+
return deviceId;
|
|
16
|
+
}
|
|
17
|
+
// On server/Node.js, return undefined (or you can fallback to some other logic)
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default class RbtMetricsApi extends EventEmitter {
|
|
22
|
+
/**
|
|
23
|
+
* You can pass either:
|
|
24
|
+
* - axiosInstance: an existing axios instance (recommended for same-host)
|
|
25
|
+
* - config: { baseUrl, headers, ... } to create a new instance (for different host)
|
|
26
|
+
*/
|
|
27
|
+
constructor({ axiosInstance, config } = {}) {
|
|
28
|
+
super();
|
|
29
|
+
if (axiosInstance) {
|
|
30
|
+
this._axios = axiosInstance;
|
|
31
|
+
} else if (config) {
|
|
32
|
+
this._axios = axios.create(config);
|
|
33
|
+
} else {
|
|
34
|
+
throw new Error("RbtMetricsApi requires either axiosInstance or config");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Send an event to the metrics API.
|
|
40
|
+
* Usage:
|
|
41
|
+
* sendEvent('UIMain.userAction.login', { param1: 1, param2: 2 });
|
|
42
|
+
* sendEvent({ event: 'foo', ... });
|
|
43
|
+
*
|
|
44
|
+
roboto.metrics.sendEvent("UIMain.userAction.login", { foo: 123, bar: "baz" });
|
|
45
|
+
This will POST a payload like:
|
|
46
|
+
{
|
|
47
|
+
"event": "UIMain.userAction.login",
|
|
48
|
+
"category": "UIMain",
|
|
49
|
+
"action": "userAction",
|
|
50
|
+
"label": "login",
|
|
51
|
+
--
|
|
52
|
+
"foo": 123,
|
|
53
|
+
"bar": "baz"
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
*/
|
|
57
|
+
async sendEvent(eventOrObj, paramsObj = {}) {
|
|
58
|
+
let payload;
|
|
59
|
+
|
|
60
|
+
if (typeof eventOrObj === 'string') {
|
|
61
|
+
const parts = eventOrObj.split('.');
|
|
62
|
+
payload = {
|
|
63
|
+
event: eventOrObj,
|
|
64
|
+
category: parts[0] || undefined,
|
|
65
|
+
action: parts[1] || undefined,
|
|
66
|
+
label: parts[2] || undefined,
|
|
67
|
+
...paramsObj,
|
|
68
|
+
};
|
|
69
|
+
} else if (typeof eventOrObj === 'object' && eventOrObj !== null) {
|
|
70
|
+
payload = eventOrObj;
|
|
71
|
+
} else {
|
|
72
|
+
throw new Error("Invalid arguments to sendEvent");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Add deviceId (only if available)
|
|
76
|
+
const deviceId = getOrCreateDeviceId();
|
|
77
|
+
if (deviceId) {
|
|
78
|
+
payload.deviceId = deviceId;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const res = await this._axios.post('/event', payload);
|
|
83
|
+
this.emit('sent', payload, res.data);
|
|
84
|
+
return res.data;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
this.emit('error', err, payload);
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|