qiscus-sdk-core 2.16.0 → 2.16.1
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/qiscus-sdk-core.js +24475 -34766
- package/dist/qiscus-sdk-core.js.map +1 -1
- package/dist/qiscus-sdk-core.min.js +24 -8
- package/dist/qiscus-sdk-core.min.js.map +1 -1
- package/lib/index.js +1847 -2705
- package/lib/lib/Comment.js +89 -142
- package/lib/lib/Room.js +52 -94
- package/lib/lib/adapters/auth.js +42 -69
- package/lib/lib/adapters/custom-event.js +26 -35
- package/lib/lib/adapters/expired-token.js +86 -113
- package/lib/lib/adapters/hook.js +15 -19
- package/lib/lib/adapters/http.js +129 -253
- package/lib/lib/adapters/mqtt.js +422 -648
- package/lib/lib/adapters/room.js +121 -147
- package/lib/lib/adapters/sync.js +233 -421
- package/lib/lib/adapters/user.js +175 -210
- package/lib/lib/is.js +13 -47
- package/lib/lib/match.js +49 -92
- package/lib/lib/url-builder.js +7 -18
- package/lib/lib/util.js +6 -20
- package/lib/lib/utils.js +52 -97
- package/package.json +21 -55
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
26
|
+
class ExpiredTokenAdapter {
|
|
16
27
|
/**
|
|
17
28
|
* @constructor
|
|
18
29
|
*
|
|
@@ -25,129 +36,91 @@ var ExpiredTokenAdapter = exports.ExpiredTokenAdapter = /*#__PURE__*/function ()
|
|
|
25
36
|
* getAuthenticationStatus: () => boolean,
|
|
26
37
|
* }} param
|
|
27
38
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
constructor({
|
|
40
|
+
httpAdapter,
|
|
41
|
+
refreshToken,
|
|
42
|
+
expiredAt,
|
|
43
|
+
userId,
|
|
44
|
+
onTokenRefreshed,
|
|
45
|
+
getAuthenticationStatus
|
|
46
|
+
}) {
|
|
36
47
|
/** @type {boolean} */
|
|
37
|
-
(
|
|
48
|
+
__publicField(this, "_isExpiredTokenEnabled", false);
|
|
38
49
|
/** @type {string | null} */
|
|
39
|
-
(
|
|
50
|
+
__publicField(this, "_refreshToken", null);
|
|
40
51
|
/** @type {Date | null} */
|
|
41
|
-
(
|
|
52
|
+
__publicField(this, "_expiredAt", null);
|
|
42
53
|
/** @type {import('./http').default} */
|
|
43
|
-
(
|
|
54
|
+
__publicField(this, "_http");
|
|
44
55
|
/** @type {(token: string, refreshToken: string, expiredAt: Date, oldToken: string) => void | undefined} */
|
|
45
|
-
(
|
|
56
|
+
__publicField(this, "_onTokenRefreshed");
|
|
46
57
|
/** @type {any} */
|
|
47
|
-
(
|
|
58
|
+
__publicField(this, "_timerId");
|
|
48
59
|
/** @type {() => boolean} */
|
|
49
|
-
(
|
|
60
|
+
__publicField(this, "_getAuthenticationStatus");
|
|
50
61
|
this._http = httpAdapter;
|
|
51
62
|
this._refreshToken = refreshToken;
|
|
52
|
-
// this._expiredAt = expiredAt == null ? null : new Date(expiredAt)
|
|
53
63
|
this._userId = userId;
|
|
54
64
|
this._onTokenRefreshed = onTokenRefreshed;
|
|
55
65
|
this._getAuthenticationStatus = getAuthenticationStatus;
|
|
56
|
-
if (this._refreshToken != null && this._refreshToken ===
|
|
66
|
+
if (this._refreshToken != null && this._refreshToken === "") {
|
|
57
67
|
this._refreshToken = null;
|
|
58
68
|
}
|
|
59
|
-
if (expiredAt != null && expiredAt !==
|
|
69
|
+
if (expiredAt != null && expiredAt !== "") {
|
|
60
70
|
this._expiredAt = new Date(expiredAt);
|
|
61
71
|
}
|
|
62
72
|
this._isExpiredTokenEnabled = this._refreshToken != null && this._expiredAt != null;
|
|
63
73
|
this._setTimer(this._expiredAt);
|
|
64
74
|
}
|
|
65
|
-
|
|
66
75
|
/**
|
|
67
76
|
* @param {Date | null} expiredAt
|
|
68
77
|
*
|
|
69
78
|
* Sets a timer to refresh the authentication token when it expires.
|
|
70
79
|
* If the token is already expired, it will immediately trigger the refresh.
|
|
71
80
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (this._timerId != null) {
|
|
78
|
-
clearTimeout(this._timerId);
|
|
79
|
-
this._timerId = null;
|
|
80
|
-
}
|
|
81
|
-
var delay = Math.floor(((_expiredAt$getTime = expiredAt === null || expiredAt === void 0 ? void 0 : expiredAt.getTime()) !== null && _expiredAt$getTime !== void 0 ? _expiredAt$getTime : NaN) - Date.now());
|
|
82
|
-
if (!isNaN(delay) && delay > 0) {
|
|
83
|
-
this._timerId = setTimeout(function () {
|
|
84
|
-
_this.refreshAuthToken();
|
|
85
|
-
}, delay);
|
|
86
|
-
}
|
|
81
|
+
_setTimer(expiredAt) {
|
|
82
|
+
var _a;
|
|
83
|
+
if (this._timerId != null) {
|
|
84
|
+
clearTimeout(this._timerId);
|
|
85
|
+
this._timerId = null;
|
|
87
86
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
return _context.abrupt("return");
|
|
101
|
-
case 1:
|
|
102
|
-
return _context.abrupt("return", this._http.post('api/v2/sdk/refresh_user_token', {
|
|
103
|
-
user_id: this._userId,
|
|
104
|
-
refresh_token: this._refreshToken
|
|
105
|
-
}).then(function (r) {
|
|
106
|
-
var _this2$_onTokenRefres;
|
|
107
|
-
var res = r.body.results;
|
|
108
|
-
var token = res.token;
|
|
109
|
-
_this2._refreshToken = res.refresh_token;
|
|
110
|
-
// @ts-ignore
|
|
111
|
-
(_this2$_onTokenRefres = _this2._onTokenRefreshed) === null || _this2$_onTokenRefres === void 0 || _this2$_onTokenRefres.call(_this2, token, _this2._refreshToken, _this2._expiredAt, _this2._http.token);
|
|
112
|
-
_this2._http.setToken(res.token);
|
|
113
|
-
if (res.token_expires_at != null) {
|
|
114
|
-
_this2._expiredAt = new Date(res.token_expires_at);
|
|
115
|
-
}
|
|
116
|
-
_this2._setTimer(_this2._expiredAt);
|
|
117
|
-
return res;
|
|
118
|
-
}));
|
|
119
|
-
case 2:
|
|
120
|
-
case "end":
|
|
121
|
-
return _context.stop();
|
|
122
|
-
}
|
|
123
|
-
}, _callee, this);
|
|
124
|
-
}));
|
|
125
|
-
function refreshAuthToken() {
|
|
126
|
-
return _refreshAuthToken.apply(this, arguments);
|
|
127
|
-
}
|
|
128
|
-
return refreshAuthToken;
|
|
129
|
-
}()
|
|
130
|
-
}, {
|
|
131
|
-
key: "logout",
|
|
132
|
-
value: function () {
|
|
133
|
-
var _logout = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
|
|
134
|
-
return _regenerator["default"].wrap(function (_context2) {
|
|
135
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
136
|
-
case 0:
|
|
137
|
-
return _context2.abrupt("return", this._http.post('api/v2/sdk/logout', {
|
|
138
|
-
user_id: this._userId,
|
|
139
|
-
token: this._http.token
|
|
140
|
-
}));
|
|
141
|
-
case 1:
|
|
142
|
-
case "end":
|
|
143
|
-
return _context2.stop();
|
|
144
|
-
}
|
|
145
|
-
}, _callee2, this);
|
|
146
|
-
}));
|
|
147
|
-
function logout() {
|
|
148
|
-
return _logout.apply(this, arguments);
|
|
87
|
+
const delay = Math.floor(((_a = expiredAt == null ? void 0 : expiredAt.getTime()) != null ? _a : NaN) - Date.now());
|
|
88
|
+
if (!isNaN(delay) && delay > 0) {
|
|
89
|
+
this._timerId = setTimeout(() => {
|
|
90
|
+
this.refreshAuthToken();
|
|
91
|
+
}, delay);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
refreshAuthToken() {
|
|
95
|
+
return __async(this, null, function* () {
|
|
96
|
+
if (this._getAuthenticationStatus() == false || this._refreshToken == null) {
|
|
97
|
+
return;
|
|
149
98
|
}
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}()
|
|
99
|
+
return this._http.post("api/v2/sdk/refresh_user_token", {
|
|
100
|
+
user_id: this._userId,
|
|
101
|
+
refresh_token: this._refreshToken
|
|
102
|
+
}).then((r) => {
|
|
103
|
+
var _a;
|
|
104
|
+
let res = r.body.results;
|
|
105
|
+
let token = res.token;
|
|
106
|
+
this._refreshToken = res.refresh_token;
|
|
107
|
+
(_a = this._onTokenRefreshed) == null ? void 0 : _a.call(this, token, this._refreshToken, this._expiredAt, this._http.token);
|
|
108
|
+
this._http.setToken(res.token);
|
|
109
|
+
if (res.token_expires_at != null) {
|
|
110
|
+
this._expiredAt = new Date(res.token_expires_at);
|
|
111
|
+
}
|
|
112
|
+
this._setTimer(this._expiredAt);
|
|
113
|
+
return res;
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
logout() {
|
|
118
|
+
return __async(this, null, function* () {
|
|
119
|
+
return this._http.post("api/v2/sdk/logout", {
|
|
120
|
+
user_id: this._userId,
|
|
121
|
+
token: this._http.token
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.ExpiredTokenAdapter = ExpiredTokenAdapter;
|
package/lib/lib/adapters/hook.js
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.Hooks = void 0;
|
|
7
|
-
exports.hookAdapterFactory = hookAdapterFactory;
|
|
8
|
-
var Hooks = exports.Hooks = {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const Hooks = {
|
|
9
4
|
MESSAGE_BEFORE_SENT: "message::before-sent",
|
|
10
5
|
MESSAGE_BEFORE_RECEIVED: "message::before-received"
|
|
11
6
|
};
|
|
12
7
|
function hookAdapterFactory() {
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
const hooks = {};
|
|
9
|
+
const get = (key) => {
|
|
15
10
|
if (!Array.isArray(hooks[key])) hooks[key] = [];
|
|
16
11
|
return hooks[key];
|
|
17
12
|
};
|
|
18
13
|
function intercept(hook, callback) {
|
|
19
14
|
get(hook).push(callback);
|
|
20
|
-
|
|
21
|
-
return
|
|
22
|
-
return get(hook).splice(index, 1);
|
|
23
|
-
};
|
|
15
|
+
const index = get(hook).length;
|
|
16
|
+
return () => get(hook).splice(index, 1);
|
|
24
17
|
}
|
|
25
18
|
function trigger(hook, payload) {
|
|
26
|
-
return get(hook).reduce(
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
return get(hook).reduce(
|
|
20
|
+
(acc, fn) => Promise.resolve(acc).then(fn),
|
|
21
|
+
Promise.resolve(payload)
|
|
22
|
+
);
|
|
29
23
|
}
|
|
30
24
|
return {
|
|
31
|
-
trigger
|
|
32
|
-
intercept
|
|
25
|
+
trigger,
|
|
26
|
+
intercept
|
|
33
27
|
};
|
|
34
|
-
}
|
|
28
|
+
}
|
|
29
|
+
exports.Hooks = Hooks;
|
|
30
|
+
exports.hookAdapterFactory = hookAdapterFactory;
|