qiscus-sdk-core 2.14.2 → 2.15.0

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.
@@ -90,15 +90,15 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
90
90
  value: function () {
91
91
  var _refreshAuthToken = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
92
92
  var _this2 = this;
93
- return _regenerator["default"].wrap(function _callee$(_context) {
93
+ return _regenerator["default"].wrap(function (_context) {
94
94
  while (1) switch (_context.prev = _context.next) {
95
95
  case 0:
96
96
  if (!(this._getAuthenticationStatus() == false || this._refreshToken == null)) {
97
- _context.next = 2;
97
+ _context.next = 1;
98
98
  break;
99
99
  }
100
100
  return _context.abrupt("return");
101
- case 2:
101
+ case 1:
102
102
  return _context.abrupt("return", this._http.post('api/v2/sdk/refresh_user_token', {
103
103
  user_id: this._userId,
104
104
  refresh_token: this._refreshToken
@@ -116,7 +116,7 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
116
116
  _this2._setTimer(_this2._expiredAt);
117
117
  return res;
118
118
  }));
119
- case 3:
119
+ case 2:
120
120
  case "end":
121
121
  return _context.stop();
122
122
  }
@@ -131,7 +131,7 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
131
131
  key: "logout",
132
132
  value: function () {
133
133
  var _logout = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
134
- return _regenerator["default"].wrap(function _callee2$(_context2) {
134
+ return _regenerator["default"].wrap(function (_context2) {
135
135
  while (1) switch (_context2.prev = _context2.next) {
136
136
  case 0:
137
137
  return _context2.abrupt("return", this._http.post('api/v2/sdk/logout', {
@@ -41,14 +41,14 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
41
41
  var options = arguments.length > 2 ? arguments[2] : undefined;
42
42
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
43
43
  var req;
44
- return _regenerator["default"].wrap(function _callee$(_context) {
44
+ return _regenerator["default"].wrap(function (_context) {
45
45
  while (1) switch (_context.prev = _context.next) {
46
46
  case 0:
47
47
  req = _superagent["default"].get("".concat(_this.baseURL, "/").concat(path));
48
48
  if (options && options.baseURL) req = _superagent["default"].get("".concat(options.baseURL, "/").concat(path));
49
49
  req = _this.setupHeaders(req, headers);
50
50
  return _context.abrupt("return", req);
51
- case 4:
51
+ case 1:
52
52
  case "end":
53
53
  return _context.stop();
54
54
  }
@@ -62,13 +62,13 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
62
62
  var _this2 = this;
63
63
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
64
64
  var req;
65
- return _regenerator["default"].wrap(function _callee2$(_context2) {
65
+ return _regenerator["default"].wrap(function (_context2) {
66
66
  while (1) switch (_context2.prev = _context2.next) {
67
67
  case 0:
68
68
  req = _superagent["default"].get("".concat(_this2.baseURL, "/").concat(path));
69
69
  req = _this2.setupHeaders(req, {});
70
70
  return _context2.abrupt("return", req);
71
- case 3:
71
+ case 1:
72
72
  case "end":
73
73
  return _context2.stop();
74
74
  }
@@ -83,13 +83,13 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
83
83
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
84
84
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
85
85
  var req;
86
- return _regenerator["default"].wrap(function _callee3$(_context3) {
86
+ return _regenerator["default"].wrap(function (_context3) {
87
87
  while (1) switch (_context3.prev = _context3.next) {
88
88
  case 0:
89
89
  req = _superagent["default"].post("".concat(_this3.baseURL, "/").concat(path));
90
90
  req = _this3.setupHeaders(req, headers);
91
91
  return _context3.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
92
- case 3:
92
+ case 1:
93
93
  case "end":
94
94
  return _context3.stop();
95
95
  }
@@ -106,14 +106,14 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
106
106
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
107
107
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
108
108
  var req;
109
- return _regenerator["default"].wrap(function _callee4$(_context4) {
109
+ return _regenerator["default"].wrap(function (_context4) {
110
110
  while (1) switch (_context4.prev = _context4.next) {
111
111
  case 0:
112
112
  req = _superagent["default"].post("".concat(_this4.baseURL, "/").concat(path));
113
113
  req = _this4.setupHeaders(req, headers);
114
114
  req.send(body);
115
115
  return _context4.abrupt("return", req);
116
- case 4:
116
+ case 1:
117
117
  case "end":
118
118
  return _context4.stop();
119
119
  }
@@ -128,13 +128,13 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
128
128
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
129
129
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee5() {
130
130
  var req;
131
- return _regenerator["default"].wrap(function _callee5$(_context5) {
131
+ return _regenerator["default"].wrap(function (_context5) {
132
132
  while (1) switch (_context5.prev = _context5.next) {
133
133
  case 0:
134
134
  req = _superagent["default"].put("".concat(_this5.baseURL, "/").concat(path));
135
135
  req = _this5.setupHeaders(req, headers);
136
136
  return _context5.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
137
- case 3:
137
+ case 1:
138
138
  case "end":
139
139
  return _context5.stop();
140
140
  }
@@ -149,13 +149,13 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
149
149
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
150
150
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee6() {
151
151
  var req;
152
- return _regenerator["default"].wrap(function _callee6$(_context6) {
152
+ return _regenerator["default"].wrap(function (_context6) {
153
153
  while (1) switch (_context6.prev = _context6.next) {
154
154
  case 0:
155
155
  req = _superagent["default"].patch("".concat(_this6.baseURL, "/").concat(path));
156
156
  req = _this6.setupHeaders(req, headers);
157
157
  return _context6.abrupt("return", req.send(body).set('Content-Type', 'application/x-www-form-urlencoded'));
158
- case 3:
158
+ case 1:
159
159
  case "end":
160
160
  return _context6.stop();
161
161
  }
@@ -170,13 +170,13 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
170
170
  var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
171
171
  return this._retryHelper(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee7() {
172
172
  var req;
173
- return _regenerator["default"].wrap(function _callee7$(_context7) {
173
+ return _regenerator["default"].wrap(function (_context7) {
174
174
  while (1) switch (_context7.prev = _context7.next) {
175
175
  case 0:
176
176
  req = _superagent["default"].del("".concat(_this7.baseURL, "/").concat(path));
177
177
  req = _this7.setupHeaders(req, headers);
178
178
  return _context7.abrupt("return", req.send(body).set('Content-Type', 'application/json'));
179
- case 3:
179
+ case 1:
180
180
  case "end":
181
181
  return _context7.stop();
182
182
  }
@@ -226,38 +226,38 @@ var HttpAdapter = exports["default"] = /*#__PURE__*/function () {
226
226
  key: "_retryHelper",
227
227
  value: (function () {
228
228
  var _retryHelper2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee8(fn) {
229
- var _err$response, _err$response2, _body$error, status, body;
230
- return _regenerator["default"].wrap(function _callee8$(_context8) {
229
+ var _err$response, _err$response2, _body$error, status, body, _t;
230
+ return _regenerator["default"].wrap(function (_context8) {
231
231
  while (1) switch (_context8.prev = _context8.next) {
232
232
  case 0:
233
233
  _context8.prev = 0;
234
- _context8.next = 3;
234
+ _context8.next = 1;
235
235
  return fn();
236
- case 3:
236
+ case 1:
237
237
  return _context8.abrupt("return", _context8.sent);
238
- case 6:
239
- _context8.prev = 6;
240
- _context8.t0 = _context8["catch"](0);
241
- status = _context8.t0 === null || _context8.t0 === void 0 || (_err$response = _context8.t0.response) === null || _err$response === void 0 ? void 0 : _err$response.status;
242
- body = _context8.t0 === null || _context8.t0 === void 0 || (_err$response2 = _context8.t0.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.body;
238
+ case 2:
239
+ _context8.prev = 2;
240
+ _t = _context8["catch"](0);
241
+ status = _t === null || _t === void 0 || (_err$response = _t.response) === null || _err$response === void 0 ? void 0 : _err$response.status;
242
+ body = _t === null || _t === void 0 || (_err$response2 = _t.response) === null || _err$response2 === void 0 ? void 0 : _err$response2.body;
243
243
  if (!(status === 403 && ((_body$error = body.error) === null || _body$error === void 0 || (_body$error = _body$error.message) === null || _body$error === void 0 ? void 0 : _body$error.toLowerCase()) === 'unauthorized. token is expired')) {
244
- _context8.next = 18;
244
+ _context8.next = 5;
245
245
  break;
246
246
  }
247
- _context8.next = 13;
247
+ _context8.next = 3;
248
248
  return this._expiredTokenAdapterGetter().refreshAuthToken();
249
- case 13:
250
- _context8.next = 15;
249
+ case 3:
250
+ _context8.next = 4;
251
251
  return fn();
252
- case 15:
252
+ case 4:
253
253
  return _context8.abrupt("return", _context8.sent);
254
- case 18:
255
- throw _context8.t0;
256
- case 19:
254
+ case 5:
255
+ throw _t;
256
+ case 6:
257
257
  case "end":
258
258
  return _context8.stop();
259
259
  }
260
- }, _callee8, this, [[0, 6]]);
260
+ }, _callee8, this, [[0, 2]]);
261
261
  }));
262
262
  function _retryHelper(_x) {
263
263
  return _retryHelper2.apply(this, arguments);
@@ -44,7 +44,9 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
44
44
  shouldConnect = _ref$shouldConnect === void 0 ? true : _ref$shouldConnect,
45
45
  brokerLbUrl = _ref.brokerLbUrl,
46
46
  enableLb = _ref.enableLb,
47
- getClientId = _ref.getClientId;
47
+ getClientId = _ref.getClientId,
48
+ mqttUsername = _ref.mqttUsername,
49
+ mqttPassword = _ref.mqttPassword;
48
50
  (0, _classCallCheck2["default"])(this, MqttAdapter);
49
51
  (0, _defineProperty2["default"])(this, "_getClientId", function () {
50
52
  if (_this.getClientId == null) return "".concat(_this.core.AppId, "_").concat(_this.core.user_id, "_").concat(Date.now());
@@ -85,6 +87,8 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
85
87
  // reconnectPeriod: 0,
86
88
  // connectTimeout: 1 * 1000,
87
89
  };
90
+ if (_this.mqttUsername != null) opts.username = _this.mqttUsername;
91
+ if (_this.mqttPassword != null) opts.password = _this.mqttPassword;
88
92
  if (brokerUrl == null) brokerUrl = _this.cacheRealtimeURL;
89
93
  if (_this.mqtt != null) {
90
94
  var _topics = Object.keys(_this.mqtt._resubscribeTopics);
@@ -112,7 +116,7 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
112
116
  });
113
117
  (0, _defineProperty2["default"])(this, "_on_close_handler", (0, _lodash["default"])(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
114
118
  var shouldReconnect, _yield$wrapP, _yield$wrapP2, url, err;
115
- return _regenerator["default"].wrap(function _callee$(_context) {
119
+ return _regenerator["default"].wrap(function (_context) {
116
120
  while (1) switch (_context.prev = _context.next) {
117
121
  case 0:
118
122
  shouldReconnect = _this.enableLb === true &&
@@ -132,15 +136,15 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
132
136
  console.groupEnd();
133
137
  }
134
138
  if (shouldReconnect) {
135
- _context.next = 4;
139
+ _context.next = 1;
136
140
  break;
137
141
  }
138
142
  return _context.abrupt("return");
139
- case 4:
143
+ case 1:
140
144
  _this.willConnectToRealtime = true;
141
- _context.next = 7;
145
+ _context.next = 2;
142
146
  return (0, _util.wrapP)(_this.getMqttNode());
143
- case 7:
147
+ case 2:
144
148
  _yield$wrapP = _context.sent;
145
149
  _yield$wrapP2 = (0, _slicedToArray2["default"])(_yield$wrapP, 2);
146
150
  url = _yield$wrapP2[0];
@@ -154,7 +158,7 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
154
158
  _this.mqtt = _this.__mqtt_conneck(url);
155
159
  }
156
160
  _this.willConnectToRealtime = false;
157
- case 13:
161
+ case 3:
158
162
  case "end":
159
163
  return _context.stop();
160
164
  }
@@ -191,7 +195,10 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
191
195
  }), (0, _match2.when)(), function (topic) {
192
196
  return _this.logger('topic not handled', topic);
193
197
  }));
198
+ this.mqttUsername = mqttUsername;
199
+ this.mqttPassword = mqttPassword;
194
200
  var _mqtt = this.__mqtt_conneck(_url);
201
+ this.cacheRealtimeURL = _url;
195
202
  this.mqtt = _mqtt;
196
203
 
197
204
  // if appConfig set realtimeEnabled to false,
@@ -208,6 +215,11 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
208
215
  // })
209
216
  }
210
217
  return (0, _createClass2["default"])(MqttAdapter, [{
218
+ key: "http",
219
+ get: function get() {
220
+ return this.core.HTTPAdapter;
221
+ }
222
+ }, {
211
223
  key: "cacheRealtimeURL",
212
224
  get: function get() {
213
225
  return this.core.mqttURL;
@@ -228,9 +240,16 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
228
240
  key: "openConnection",
229
241
  value: (function () {
230
242
  var _openConnection = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
231
- return _regenerator["default"].wrap(function _callee2$(_context2) {
243
+ return _regenerator["default"].wrap(function (_context2) {
232
244
  while (1) switch (_context2.prev = _context2.next) {
233
245
  case 0:
246
+ if (!(this.mqtt != null)) {
247
+ _context2.next = 1;
248
+ break;
249
+ }
250
+ _context2.next = 1;
251
+ return this.closeConnection();
252
+ case 1:
234
253
  this.shouldConnect = true;
235
254
  this.mqtt = this.__mqtt_conneck();
236
255
  case 2:
@@ -253,17 +272,24 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
253
272
  value: (function () {
254
273
  var _closeConnection = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
255
274
  var _this2 = this;
256
- return _regenerator["default"].wrap(function _callee3$(_context3) {
275
+ return _regenerator["default"].wrap(function (_context3) {
257
276
  while (1) switch (_context3.prev = _context3.next) {
258
277
  case 0:
259
278
  this.shouldConnect = false;
260
- this.mqtt.end(true, function (err) {
261
- if (err) {
262
- _this2.logger('error when close connection', err.message);
263
- }
279
+ _context3.next = 1;
280
+ return new Promise(function (resolve, reject) {
281
+ _this2.mqtt.end(true, null, function (err) {
282
+ if (err) {
283
+ _this2.logger('error when close connection', err.message);
284
+ reject(err);
285
+ } else {
286
+ resolve();
287
+ }
288
+ });
264
289
  });
290
+ case 1:
265
291
  this.mqtt = null;
266
- case 3:
292
+ case 2:
267
293
  case "end":
268
294
  return _context3.stop();
269
295
  }
@@ -278,18 +304,21 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
278
304
  key: "getMqttNode",
279
305
  value: function () {
280
306
  var _getMqttNode = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
281
- var res, url, port;
282
- return _regenerator["default"].wrap(function _callee4$(_context4) {
307
+ var res, mqttUsername, mqttPassword, mqttURL;
308
+ return _regenerator["default"].wrap(function (_context4) {
283
309
  while (1) switch (_context4.prev = _context4.next) {
284
310
  case 0:
285
- _context4.next = 2;
286
- return _superagent["default"].get(this.brokerLbUrl);
287
- case 2:
311
+ _context4.next = 1;
312
+ return this.http.get('api/v2/sdk/mqtt_config');
313
+ case 1:
288
314
  res = _context4.sent;
289
- url = res.body.data.url;
290
- port = res.body.data.wss_port;
291
- return _context4.abrupt("return", "wss://".concat(url, ":").concat(port, "/mqtt"));
292
- case 6:
315
+ mqttUsername = res.body.results.username;
316
+ mqttPassword = res.body.results.password;
317
+ mqttURL = "wss://".concat(res.body.results.url, ":1886/mqtt");
318
+ this.mqttUsername = mqttUsername;
319
+ this.mqttPassword = mqttPassword;
320
+ return _context4.abrupt("return", mqttURL);
321
+ case 2:
293
322
  case "end":
294
323
  return _context4.stop();
295
324
  }
@@ -381,7 +410,7 @@ var MqttAdapter = exports["default"] = /*#__PURE__*/function () {
381
410
  }, {
382
411
  key: "logger",
383
412
  get: function get() {
384
- if (!this.core.debugMQTTMode) return this.noop;
413
+ if (!this.logEnabled) return this.noop;
385
414
  return console.log.bind(console, 'QRealtime ->');
386
415
  }
387
416