@blocklet/js-sdk 1.17.8-beta-20260119-102944-6ba93a16 → 1.17.8-beta-20260125-093329-64b43854
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/index.d.mts +391 -304
- package/dist/index.mjs +704 -850
- package/dist/index.mjs.map +1 -0
- package/package.json +20 -13
- package/dist/index.d.ts +0 -360
package/dist/index.mjs
CHANGED
|
@@ -1,902 +1,756 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import Cookie from
|
|
4
|
-
import QuickLRU from
|
|
5
|
-
import isEmpty from
|
|
6
|
-
import axios from
|
|
7
|
-
import Keyv from
|
|
8
|
-
import { KeyvLocalStorage } from
|
|
9
|
-
import isNumber from
|
|
10
|
-
import omit from
|
|
11
|
-
import isObject from
|
|
12
|
-
import stableStringify from
|
|
13
|
-
import
|
|
14
|
-
import { toTypeInfo } from '@arcblock/did';
|
|
15
|
-
import isUrl from 'is-url';
|
|
1
|
+
import { REFRESH_TOKEN_STORAGE_KEY, SESSION_TOKEN_STORAGE_KEY, WELLKNOWN_SERVICE_PATH_PREFIX } from "@abtnode/constant";
|
|
2
|
+
import { joinURL, withQuery } from "ufo";
|
|
3
|
+
import Cookie from "js-cookie";
|
|
4
|
+
import QuickLRU from "quick-lru";
|
|
5
|
+
import isEmpty from "lodash/isEmpty";
|
|
6
|
+
import axios from "axios";
|
|
7
|
+
import Keyv from "keyv";
|
|
8
|
+
import { KeyvLocalStorage } from "keyv-browser";
|
|
9
|
+
import isNumber from "lodash/isNumber";
|
|
10
|
+
import omit from "lodash/omit";
|
|
11
|
+
import isObject from "lodash/isObject";
|
|
12
|
+
import stableStringify from "json-stable-stringify";
|
|
13
|
+
import isUrl from "is-url";
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
//#region src/services/auth.ts
|
|
16
|
+
var AuthService = class {
|
|
17
|
+
constructor({ api, token }) {
|
|
18
|
+
this.api = api;
|
|
19
|
+
this.token = token;
|
|
20
|
+
}
|
|
21
|
+
async getUserPublicInfo({ did, name }) {
|
|
22
|
+
const { data } = await this.api.get("/api/user", { params: {
|
|
23
|
+
did,
|
|
24
|
+
name
|
|
25
|
+
} });
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
async getUserPrivacyConfig({ did, name }) {
|
|
29
|
+
const { data } = await this.api.get("/api/user/privacy/config", { params: {
|
|
30
|
+
did,
|
|
31
|
+
name
|
|
32
|
+
} });
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
async saveUserPrivacyConfig(config) {
|
|
36
|
+
const { data } = await this.api.post("/api/user/privacy/config", config);
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
async getUserNotificationConfig() {
|
|
40
|
+
const { data } = await this.api.get("/api/user/notification/config");
|
|
41
|
+
return data;
|
|
42
|
+
}
|
|
43
|
+
async saveUserNotificationConfig(config) {
|
|
44
|
+
const { data } = await this.api.post("/api/user/notification/config", config);
|
|
45
|
+
return data;
|
|
46
|
+
}
|
|
47
|
+
async testNotificationWebhook(webhook) {
|
|
48
|
+
const { data } = await this.api.put("/api/user/notification/webhook", webhook);
|
|
49
|
+
return data;
|
|
50
|
+
}
|
|
51
|
+
async getProfileUrl({ did, locale }) {
|
|
52
|
+
return withQuery(`${WELLKNOWN_SERVICE_PATH_PREFIX}/user`, {
|
|
53
|
+
did,
|
|
54
|
+
locale
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async getProfile() {
|
|
58
|
+
const { data } = await this.api.get("/api/user/profile");
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
async refreshProfile() {
|
|
62
|
+
await this.api.put("/api/user/refreshProfile");
|
|
63
|
+
}
|
|
64
|
+
async followUser({ userDid }) {
|
|
65
|
+
await this.api.post(`/api/user/follow/${userDid}`);
|
|
66
|
+
}
|
|
67
|
+
async unfollowUser({ userDid }) {
|
|
68
|
+
await this.api.delete(`/api/user/follow/${userDid}`);
|
|
69
|
+
}
|
|
70
|
+
async isFollowingUser({ userDid }) {
|
|
71
|
+
const { data } = await this.api.get(`/api/user/follow/${userDid}`);
|
|
72
|
+
return data.isFollowing;
|
|
73
|
+
}
|
|
74
|
+
async saveProfile({ locale, inviter, metadata, address }) {
|
|
75
|
+
const { data } = await this.api.put("/api/user/profile", {
|
|
76
|
+
locale,
|
|
77
|
+
inviter,
|
|
78
|
+
metadata,
|
|
79
|
+
address
|
|
80
|
+
});
|
|
81
|
+
return data;
|
|
82
|
+
}
|
|
83
|
+
async updateDidSpace({ spaceGateway }) {
|
|
84
|
+
await this.api.put("/api/user/updateDidSpace", { spaceGateway });
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 指定要退出登录的设备 id
|
|
88
|
+
* 指定要退出登录的会话状态
|
|
89
|
+
* @param {{ visitorId: string, status: string }} { visitorId, status }
|
|
90
|
+
* @return {Promise<void>}
|
|
91
|
+
*/
|
|
92
|
+
async logout({ visitorId, status, includeFederated }) {
|
|
93
|
+
const refreshToken = this.token.getRefreshToken();
|
|
94
|
+
const { data } = await this.api.post("/api/user/logout", {
|
|
95
|
+
visitorId,
|
|
96
|
+
status,
|
|
97
|
+
includeFederated,
|
|
98
|
+
refreshToken
|
|
99
|
+
});
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 删除当前登录用户
|
|
104
|
+
* @return {Promise<{did: string}>}
|
|
105
|
+
*/
|
|
106
|
+
async destroyMyself() {
|
|
107
|
+
const { data } = await this.api.delete("/api/user");
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* org 相关
|
|
112
|
+
*/
|
|
113
|
+
async getOrgs({ search = "", type = "", page = 1, pageSize = 20 }) {
|
|
114
|
+
const { data } = await this.api.get("/api/user/orgs", { params: {
|
|
115
|
+
search,
|
|
116
|
+
page,
|
|
117
|
+
pageSize,
|
|
118
|
+
type
|
|
119
|
+
} });
|
|
120
|
+
return data;
|
|
121
|
+
}
|
|
122
|
+
async getOrg(orgId) {
|
|
123
|
+
const { data } = await this.api.get(`/api/user/orgs/${orgId}`);
|
|
124
|
+
return data;
|
|
125
|
+
}
|
|
126
|
+
async createOrg(org) {
|
|
127
|
+
const { data } = await this.api.post("/api/user/orgs", { org });
|
|
128
|
+
return data;
|
|
129
|
+
}
|
|
130
|
+
async getRole(name) {
|
|
131
|
+
const { data } = await this.api.get("/api/user/role", { params: { name } });
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
async addResourceToOrg({ orgId, resourceId, type, metadata }) {
|
|
135
|
+
const { data } = await this.api.post(`/api/user/orgs/${orgId}/resources`, {
|
|
136
|
+
resourceId,
|
|
137
|
+
type,
|
|
138
|
+
metadata
|
|
139
|
+
});
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
async migrateResourceToOrg({ form, to, resourceId }) {
|
|
143
|
+
const { data } = await this.api.put(`/api/user/orgs/${form}/resources`, {
|
|
144
|
+
to,
|
|
145
|
+
resourceId
|
|
146
|
+
});
|
|
147
|
+
return data;
|
|
148
|
+
}
|
|
22
149
|
};
|
|
23
|
-
class AuthService {
|
|
24
|
-
constructor({ api, token }) {
|
|
25
|
-
__publicField$4(this, "api");
|
|
26
|
-
__publicField$4(this, "token");
|
|
27
|
-
this.api = api;
|
|
28
|
-
this.token = token;
|
|
29
|
-
}
|
|
30
|
-
async getUserPublicInfo({ did }) {
|
|
31
|
-
const { data } = await this.api.get("/api/user", {
|
|
32
|
-
params: { did }
|
|
33
|
-
});
|
|
34
|
-
return data;
|
|
35
|
-
}
|
|
36
|
-
async getUserPrivacyConfig({ did }) {
|
|
37
|
-
const { data } = await this.api.get("/api/user/privacy/config", {
|
|
38
|
-
params: { did }
|
|
39
|
-
});
|
|
40
|
-
return data;
|
|
41
|
-
}
|
|
42
|
-
async saveUserPrivacyConfig(config) {
|
|
43
|
-
const { data } = await this.api.post("/api/user/privacy/config", config);
|
|
44
|
-
return data;
|
|
45
|
-
}
|
|
46
|
-
async getUserNotificationConfig() {
|
|
47
|
-
const { data } = await this.api.get("/api/user/notification/config");
|
|
48
|
-
return data;
|
|
49
|
-
}
|
|
50
|
-
async saveUserNotificationConfig(config) {
|
|
51
|
-
const { data } = await this.api.post("/api/user/notification/config", config);
|
|
52
|
-
return data;
|
|
53
|
-
}
|
|
54
|
-
async testNotificationWebhook(webhook) {
|
|
55
|
-
const { data } = await this.api.put("/api/user/notification/webhook", webhook);
|
|
56
|
-
return data;
|
|
57
|
-
}
|
|
58
|
-
// eslint-disable-next-line require-await
|
|
59
|
-
async getProfileUrl({ did, locale }) {
|
|
60
|
-
const url = `${WELLKNOWN_SERVICE_PATH_PREFIX}/user`;
|
|
61
|
-
return withQuery(url, {
|
|
62
|
-
did,
|
|
63
|
-
locale
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
async getProfile() {
|
|
67
|
-
const { data } = await this.api.get("/api/user/profile");
|
|
68
|
-
return data;
|
|
69
|
-
}
|
|
70
|
-
async refreshProfile() {
|
|
71
|
-
await this.api.put("/api/user/refreshProfile");
|
|
72
|
-
}
|
|
73
|
-
async followUser({ userDid }) {
|
|
74
|
-
await this.api.post(`/api/user/follow/${userDid}`);
|
|
75
|
-
}
|
|
76
|
-
async unfollowUser({ userDid }) {
|
|
77
|
-
await this.api.delete(`/api/user/follow/${userDid}`);
|
|
78
|
-
}
|
|
79
|
-
async isFollowingUser({ userDid }) {
|
|
80
|
-
const { data } = await this.api.get(`/api/user/follow/${userDid}`);
|
|
81
|
-
return data.isFollowing;
|
|
82
|
-
}
|
|
83
|
-
async saveProfile({
|
|
84
|
-
locale,
|
|
85
|
-
inviter,
|
|
86
|
-
metadata,
|
|
87
|
-
address
|
|
88
|
-
}) {
|
|
89
|
-
const { data } = await this.api.put("/api/user/profile", { locale, inviter, metadata, address });
|
|
90
|
-
return data;
|
|
91
|
-
}
|
|
92
|
-
async updateDidSpace({ spaceGateway }) {
|
|
93
|
-
await this.api.put("/api/user/updateDidSpace", { spaceGateway });
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* 指定要退出登录的设备 id
|
|
97
|
-
* 指定要退出登录的会话状态
|
|
98
|
-
* @param {{ visitorId: string, status: string }} { visitorId, status }
|
|
99
|
-
* @return {Promise<void>}
|
|
100
|
-
*/
|
|
101
|
-
async logout({
|
|
102
|
-
visitorId,
|
|
103
|
-
status,
|
|
104
|
-
includeFederated
|
|
105
|
-
}) {
|
|
106
|
-
const refreshToken = this.token.getRefreshToken();
|
|
107
|
-
const { data } = await this.api.post("/api/user/logout", {
|
|
108
|
-
visitorId,
|
|
109
|
-
status,
|
|
110
|
-
includeFederated,
|
|
111
|
-
refreshToken
|
|
112
|
-
});
|
|
113
|
-
return data;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* 删除当前登录用户
|
|
117
|
-
* @return {Promise<{did: string}>}
|
|
118
|
-
*/
|
|
119
|
-
async destroyMyself() {
|
|
120
|
-
const { data } = await this.api.delete("/api/user");
|
|
121
|
-
return data;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* org 相关
|
|
125
|
-
*/
|
|
126
|
-
async getOrgs({
|
|
127
|
-
search = "",
|
|
128
|
-
type = "",
|
|
129
|
-
page = 1,
|
|
130
|
-
pageSize = 20
|
|
131
|
-
}) {
|
|
132
|
-
const { data } = await this.api.get("/api/user/orgs", { params: { search, page, pageSize, type } });
|
|
133
|
-
return data;
|
|
134
|
-
}
|
|
135
|
-
// 根据 orgId 查询一个 org 的信息
|
|
136
|
-
async getOrg(orgId) {
|
|
137
|
-
const { data } = await this.api.get(`/api/user/orgs/${orgId}`);
|
|
138
|
-
return data;
|
|
139
|
-
}
|
|
140
|
-
async createOrg(org) {
|
|
141
|
-
const { data } = await this.api.post("/api/user/orgs", { org });
|
|
142
|
-
return data;
|
|
143
|
-
}
|
|
144
|
-
// 查询一个role 的信息用于获取是否属于一个 org
|
|
145
|
-
async getRole(name) {
|
|
146
|
-
const { data } = await this.api.get("/api/user/role", { params: { name } });
|
|
147
|
-
return data;
|
|
148
|
-
}
|
|
149
|
-
async addResourceToOrg({
|
|
150
|
-
orgId,
|
|
151
|
-
resourceId,
|
|
152
|
-
type,
|
|
153
|
-
metadata
|
|
154
|
-
}) {
|
|
155
|
-
const { data } = await this.api.post(`/api/user/orgs/${orgId}/resources`, { resourceId, type, metadata });
|
|
156
|
-
return data;
|
|
157
|
-
}
|
|
158
|
-
async migrateResourceToOrg({ form, to, resourceId }) {
|
|
159
|
-
const { data } = await this.api.put(`/api/user/orgs/${form}/resources`, { to, resourceId });
|
|
160
|
-
return data;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
150
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
}
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/services/token.ts
|
|
153
|
+
var TokenService = class {
|
|
154
|
+
getSessionToken(config) {
|
|
155
|
+
if (Cookie.get(SESSION_TOKEN_STORAGE_KEY)) return Cookie.get(SESSION_TOKEN_STORAGE_KEY);
|
|
156
|
+
if (config.sessionTokenKey) return window.localStorage.getItem(config.sessionTokenKey);
|
|
157
|
+
return "";
|
|
158
|
+
}
|
|
159
|
+
setSessionToken(value) {
|
|
160
|
+
Cookie.set(SESSION_TOKEN_STORAGE_KEY, value);
|
|
161
|
+
}
|
|
162
|
+
removeSessionToken() {
|
|
163
|
+
Cookie.remove(SESSION_TOKEN_STORAGE_KEY);
|
|
164
|
+
}
|
|
165
|
+
getRefreshToken() {
|
|
166
|
+
return localStorage.getItem(REFRESH_TOKEN_STORAGE_KEY);
|
|
167
|
+
}
|
|
168
|
+
setRefreshToken(value) {
|
|
169
|
+
localStorage.setItem(REFRESH_TOKEN_STORAGE_KEY, value);
|
|
170
|
+
}
|
|
171
|
+
removeRefreshToken() {
|
|
172
|
+
localStorage.removeItem(REFRESH_TOKEN_STORAGE_KEY);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
190
175
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (!force && blockletCache.has(baseUrl)) {
|
|
201
|
-
return blockletCache.get(baseUrl);
|
|
202
|
-
}
|
|
203
|
-
const url = withQuery(joinURL(baseUrl, "__blocklet__.js"), {
|
|
204
|
-
type: "json",
|
|
205
|
-
t: Date.now()
|
|
206
|
-
});
|
|
207
|
-
return new Promise(async (resolve) => {
|
|
208
|
-
const res = await fetch(url);
|
|
209
|
-
const data = await res.json();
|
|
210
|
-
blockletCache.set(baseUrl, data);
|
|
211
|
-
resolve(data);
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
loadBlocklet() {
|
|
215
|
-
return new Promise((resolve, reject) => {
|
|
216
|
-
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
217
|
-
reject();
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
const blockletScript = document.createElement("script");
|
|
221
|
-
let basename = "/";
|
|
222
|
-
if (window.blocklet && window.blocklet.prefix) {
|
|
223
|
-
basename = window.blocklet.prefix;
|
|
224
|
-
}
|
|
225
|
-
blockletScript.src = withQuery(joinURL(basename, "__blocklet__.js"), {
|
|
226
|
-
t: Date.now()
|
|
227
|
-
});
|
|
228
|
-
blockletScript.onload = () => {
|
|
229
|
-
resolve();
|
|
230
|
-
};
|
|
231
|
-
blockletScript.onerror = () => {
|
|
232
|
-
reject();
|
|
233
|
-
};
|
|
234
|
-
document.head.append(blockletScript);
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
getPrefix(blocklet) {
|
|
238
|
-
if (blocklet) {
|
|
239
|
-
return blocklet?.prefix || "/";
|
|
240
|
-
}
|
|
241
|
-
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
242
|
-
return null;
|
|
243
|
-
}
|
|
244
|
-
return window.blocklet?.prefix || "/";
|
|
245
|
-
}
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/services/blocklet.ts
|
|
178
|
+
let blockletCache;
|
|
179
|
+
function getBlockletCache() {
|
|
180
|
+
if (!blockletCache) blockletCache = new QuickLRU({
|
|
181
|
+
maxSize: 30,
|
|
182
|
+
maxAge: 60 * 1e3
|
|
183
|
+
});
|
|
184
|
+
return blockletCache;
|
|
246
185
|
}
|
|
186
|
+
var BlockletService = class {
|
|
187
|
+
getBlocklet(baseUrl, force = false) {
|
|
188
|
+
if (!baseUrl) {
|
|
189
|
+
if (typeof window === "undefined" || typeof document === "undefined") throw new Error("Cannot get blocklet in server side without baseUrl");
|
|
190
|
+
return window.blocklet;
|
|
191
|
+
}
|
|
192
|
+
const cache = getBlockletCache();
|
|
193
|
+
if (!force && cache.has(baseUrl)) return cache.get(baseUrl);
|
|
194
|
+
const url = withQuery(joinURL(baseUrl, "__blocklet__.js"), {
|
|
195
|
+
type: "json",
|
|
196
|
+
t: Date.now()
|
|
197
|
+
});
|
|
198
|
+
return new Promise(async (resolve) => {
|
|
199
|
+
const data = await (await fetch(url)).json();
|
|
200
|
+
cache.set(baseUrl, data);
|
|
201
|
+
resolve(data);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
loadBlocklet() {
|
|
205
|
+
return new Promise((resolve, reject) => {
|
|
206
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
207
|
+
reject();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const blockletScript = document.createElement("script");
|
|
211
|
+
let basename = "/";
|
|
212
|
+
if (window.blocklet && window.blocklet.prefix) basename = window.blocklet.prefix;
|
|
213
|
+
blockletScript.src = withQuery(joinURL(basename, "__blocklet__.js"), { t: Date.now() });
|
|
214
|
+
blockletScript.onload = () => {
|
|
215
|
+
resolve();
|
|
216
|
+
};
|
|
217
|
+
blockletScript.onerror = () => {
|
|
218
|
+
reject();
|
|
219
|
+
};
|
|
220
|
+
document.head.append(blockletScript);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
getPrefix(blocklet) {
|
|
224
|
+
if (blocklet) return blocklet?.prefix || "/";
|
|
225
|
+
if (typeof window === "undefined" || typeof document === "undefined") return null;
|
|
226
|
+
return window.blocklet?.prefix || "/";
|
|
227
|
+
}
|
|
228
|
+
};
|
|
247
229
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
var
|
|
251
|
-
|
|
252
|
-
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/services/user-session.ts
|
|
232
|
+
var UserSessionService = class {
|
|
233
|
+
constructor({ api, blocklet }) {
|
|
234
|
+
this.api = api;
|
|
235
|
+
this.blocklet = blocklet || new BlockletService();
|
|
236
|
+
}
|
|
237
|
+
getBaseUrl(appUrl) {
|
|
238
|
+
return appUrl ? joinURL(appUrl, WELLKNOWN_SERVICE_PATH_PREFIX) : void 0;
|
|
239
|
+
}
|
|
240
|
+
async getUserSessions({ did, appUrl }) {
|
|
241
|
+
const baseURL = this.getBaseUrl(appUrl);
|
|
242
|
+
const blocklet = await this.blocklet.getBlocklet();
|
|
243
|
+
const { data } = await this.api.get("/api/user-session", {
|
|
244
|
+
baseURL,
|
|
245
|
+
params: {
|
|
246
|
+
userDid: did,
|
|
247
|
+
appPid: blocklet.appPid
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
return data;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 获取个人的所有登录会话
|
|
254
|
+
*/
|
|
255
|
+
async getMyLoginSessions({ appUrl } = {}, params = {
|
|
256
|
+
page: 1,
|
|
257
|
+
pageSize: 10
|
|
258
|
+
}) {
|
|
259
|
+
const baseURL = this.getBaseUrl(appUrl);
|
|
260
|
+
const { data } = await this.api.get("/api/user-session/myself", {
|
|
261
|
+
baseURL,
|
|
262
|
+
params
|
|
263
|
+
});
|
|
264
|
+
return data;
|
|
265
|
+
}
|
|
266
|
+
async loginByUserSession({ id, appPid, userDid, passportId, appUrl }) {
|
|
267
|
+
const baseURL = this.getBaseUrl(appUrl);
|
|
268
|
+
const { data } = await this.api.post("/api/user-session/login", {
|
|
269
|
+
id,
|
|
270
|
+
appPid,
|
|
271
|
+
userDid,
|
|
272
|
+
passportId
|
|
273
|
+
}, { baseURL });
|
|
274
|
+
return data;
|
|
275
|
+
}
|
|
253
276
|
};
|
|
254
|
-
class UserSessionService {
|
|
255
|
-
constructor({ api, blocklet }) {
|
|
256
|
-
__publicField$3(this, "api");
|
|
257
|
-
__publicField$3(this, "blocklet");
|
|
258
|
-
this.api = api;
|
|
259
|
-
this.blocklet = blocklet || new BlockletService();
|
|
260
|
-
}
|
|
261
|
-
getBaseUrl(appUrl) {
|
|
262
|
-
return appUrl ? joinURL(appUrl, WELLKNOWN_SERVICE_PATH_PREFIX) : void 0;
|
|
263
|
-
}
|
|
264
|
-
async getUserSessions({ did, appUrl }) {
|
|
265
|
-
const baseURL = this.getBaseUrl(appUrl);
|
|
266
|
-
const blocklet = await this.blocklet.getBlocklet();
|
|
267
|
-
const { data } = await this.api.get("/api/user-session", {
|
|
268
|
-
baseURL,
|
|
269
|
-
params: {
|
|
270
|
-
userDid: did,
|
|
271
|
-
appPid: blocklet.appPid
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
return data;
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* 获取个人的所有登录会话
|
|
278
|
-
*/
|
|
279
|
-
async getMyLoginSessions({ appUrl } = {}, params = { page: 1, pageSize: 10 }) {
|
|
280
|
-
const baseURL = this.getBaseUrl(appUrl);
|
|
281
|
-
const { data } = await this.api.get("/api/user-session/myself", { baseURL, params });
|
|
282
|
-
return data;
|
|
283
|
-
}
|
|
284
|
-
async loginByUserSession({
|
|
285
|
-
id,
|
|
286
|
-
appPid,
|
|
287
|
-
userDid,
|
|
288
|
-
passportId,
|
|
289
|
-
appUrl
|
|
290
|
-
}) {
|
|
291
|
-
const baseURL = this.getBaseUrl(appUrl);
|
|
292
|
-
const { data } = await this.api.post(
|
|
293
|
-
"/api/user-session/login",
|
|
294
|
-
{
|
|
295
|
-
id,
|
|
296
|
-
appPid,
|
|
297
|
-
userDid,
|
|
298
|
-
passportId
|
|
299
|
-
},
|
|
300
|
-
{ baseURL }
|
|
301
|
-
);
|
|
302
|
-
return data;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
277
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
var
|
|
309
|
-
|
|
310
|
-
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/services/component.ts
|
|
280
|
+
var ComponentService = class {
|
|
281
|
+
constructor({ blocklet = window?.blocklet } = {}) {
|
|
282
|
+
this.blocklet = blocklet;
|
|
283
|
+
}
|
|
284
|
+
getComponent(name) {
|
|
285
|
+
return (this.blocklet?.componentMountPoints || []).find((x) => [
|
|
286
|
+
x.title,
|
|
287
|
+
x.name,
|
|
288
|
+
x.did
|
|
289
|
+
].includes(name));
|
|
290
|
+
}
|
|
291
|
+
getComponentMountPoint(name) {
|
|
292
|
+
return this.getComponent(name)?.mountPoint || "";
|
|
293
|
+
}
|
|
294
|
+
getUrl(name, ...parts) {
|
|
295
|
+
const mountPoint = this.getComponentMountPoint(name);
|
|
296
|
+
return joinURL(this.blocklet?.appUrl || "", mountPoint, ...parts);
|
|
297
|
+
}
|
|
311
298
|
};
|
|
312
|
-
class ComponentService {
|
|
313
|
-
constructor({ blocklet = window.blocklet } = {}) {
|
|
314
|
-
__publicField$2(this, "blocklet");
|
|
315
|
-
this.blocklet = blocklet;
|
|
316
|
-
}
|
|
317
|
-
getComponent(name) {
|
|
318
|
-
const componentMountPoints = this.blocklet?.componentMountPoints || [];
|
|
319
|
-
const item = componentMountPoints.find((x) => [x.title, x.name, x.did].includes(name));
|
|
320
|
-
return item;
|
|
321
|
-
}
|
|
322
|
-
getComponentMountPoint(name) {
|
|
323
|
-
const component = this.getComponent(name);
|
|
324
|
-
return component?.mountPoint || "";
|
|
325
|
-
}
|
|
326
|
-
getUrl(name, ...parts) {
|
|
327
|
-
const mountPoint = this.getComponentMountPoint(name);
|
|
328
|
-
const appUrl = this.blocklet?.appUrl || "";
|
|
329
|
-
return joinURL(appUrl, mountPoint, ...parts);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
299
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
var
|
|
336
|
-
|
|
337
|
-
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region src/services/federated.ts
|
|
302
|
+
var FederatedService = class {
|
|
303
|
+
constructor({ api, blocklet }) {
|
|
304
|
+
this.blockletDataCache = {};
|
|
305
|
+
this.api = api;
|
|
306
|
+
this.blocklet = blocklet || new BlockletService();
|
|
307
|
+
}
|
|
308
|
+
async getTrustedDomains() {
|
|
309
|
+
const { data } = await this.api.get("/api/federated/getTrustedDomains");
|
|
310
|
+
return data;
|
|
311
|
+
}
|
|
312
|
+
getMaster(blocklet = this.blocklet.getBlocklet()) {
|
|
313
|
+
return (blocklet?.settings?.federated)?.master;
|
|
314
|
+
}
|
|
315
|
+
getConfig(blocklet = this.blocklet.getBlocklet()) {
|
|
316
|
+
return (blocklet?.settings?.federated)?.config;
|
|
317
|
+
}
|
|
318
|
+
getFederatedEnabled(blocklet = this.blocklet.getBlocklet()) {
|
|
319
|
+
return this.getConfig(blocklet)?.status === "approved";
|
|
320
|
+
}
|
|
321
|
+
getSourceAppPid(blocklet = this.blocklet.getBlocklet()) {
|
|
322
|
+
return this.getMaster(blocklet)?.appPid;
|
|
323
|
+
}
|
|
324
|
+
getFederatedApp(blocklet = this.blocklet.getBlocklet()) {
|
|
325
|
+
const master = this.getMaster(blocklet);
|
|
326
|
+
if (!!isEmpty(master)) return null;
|
|
327
|
+
return {
|
|
328
|
+
appId: master.appId,
|
|
329
|
+
appName: master.appName,
|
|
330
|
+
appDescription: master.appDescription,
|
|
331
|
+
appLogo: master.appLogo,
|
|
332
|
+
appPid: master.appPid,
|
|
333
|
+
appUrl: master.appUrl,
|
|
334
|
+
version: master.version,
|
|
335
|
+
sourceAppPid: master.appPid,
|
|
336
|
+
provider: "wallet"
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
getCurrentApp(blocklet = this.blocklet.getBlocklet()) {
|
|
340
|
+
if (blocklet) return {
|
|
341
|
+
appId: blocklet.appId,
|
|
342
|
+
appName: blocklet.appName,
|
|
343
|
+
appDescription: blocklet.appDescription,
|
|
344
|
+
appLogo: blocklet.appLogo,
|
|
345
|
+
appPid: blocklet.appPid,
|
|
346
|
+
appUrl: blocklet.appUrl,
|
|
347
|
+
version: blocklet.version,
|
|
348
|
+
sourceAppPid: null,
|
|
349
|
+
provider: "wallet"
|
|
350
|
+
};
|
|
351
|
+
if (window.env) {
|
|
352
|
+
const server = window.env;
|
|
353
|
+
return {
|
|
354
|
+
appId: server.appId,
|
|
355
|
+
appName: server.appName,
|
|
356
|
+
appDescription: server.appDescription,
|
|
357
|
+
appUrl: server.baseUrl,
|
|
358
|
+
sourceAppPid: null,
|
|
359
|
+
provider: "wallet",
|
|
360
|
+
type: "server"
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
getApps(blocklet = this.blocklet.getBlocklet()) {
|
|
366
|
+
const appList = [];
|
|
367
|
+
const masterApp = this.getFederatedApp(blocklet);
|
|
368
|
+
const currentApp = this.getCurrentApp(blocklet);
|
|
369
|
+
const federatedEnabled = this.getFederatedEnabled(blocklet);
|
|
370
|
+
if (currentApp) appList.push(currentApp);
|
|
371
|
+
if (masterApp && masterApp?.appId !== currentApp?.appId && federatedEnabled) appList.push(masterApp);
|
|
372
|
+
return appList.reverse();
|
|
373
|
+
}
|
|
374
|
+
async getBlockletData(appUrl, force = false) {
|
|
375
|
+
if (!force && this.blockletDataCache[appUrl]) return this.blockletDataCache[appUrl];
|
|
376
|
+
try {
|
|
377
|
+
const url = new URL("__blocklet__.js", appUrl);
|
|
378
|
+
url.searchParams.set("type", "json");
|
|
379
|
+
const jsonData = await (await fetch(url.href)).json();
|
|
380
|
+
this.blockletDataCache[appUrl] = jsonData;
|
|
381
|
+
return jsonData;
|
|
382
|
+
} catch (err) {
|
|
383
|
+
console.error(`Failed to get blocklet data: ${appUrl}`, err);
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
338
387
|
};
|
|
339
|
-
class FederatedService {
|
|
340
|
-
constructor({ api, blocklet }) {
|
|
341
|
-
__publicField$1(this, "api");
|
|
342
|
-
__publicField$1(this, "blocklet");
|
|
343
|
-
__publicField$1(this, "blockletDataCache", {});
|
|
344
|
-
this.api = api;
|
|
345
|
-
this.blocklet = blocklet || new BlockletService();
|
|
346
|
-
}
|
|
347
|
-
async getTrustedDomains() {
|
|
348
|
-
const { data } = await this.api.get("/api/federated/getTrustedDomains");
|
|
349
|
-
return data;
|
|
350
|
-
}
|
|
351
|
-
getMaster(blocklet = this.blocklet.getBlocklet()) {
|
|
352
|
-
const federated = blocklet?.settings?.federated;
|
|
353
|
-
return federated?.master;
|
|
354
|
-
}
|
|
355
|
-
getConfig(blocklet = this.blocklet.getBlocklet()) {
|
|
356
|
-
const federated = blocklet?.settings?.federated;
|
|
357
|
-
return federated?.config;
|
|
358
|
-
}
|
|
359
|
-
getFederatedEnabled(blocklet = this.blocklet.getBlocklet()) {
|
|
360
|
-
const config = this.getConfig(blocklet);
|
|
361
|
-
return config?.status === "approved";
|
|
362
|
-
}
|
|
363
|
-
getSourceAppPid(blocklet = this.blocklet.getBlocklet()) {
|
|
364
|
-
const master = this.getMaster(blocklet);
|
|
365
|
-
return master?.appPid;
|
|
366
|
-
}
|
|
367
|
-
getFederatedApp(blocklet = this.blocklet.getBlocklet()) {
|
|
368
|
-
const master = this.getMaster(blocklet);
|
|
369
|
-
const isFederatedMode = !isEmpty(master);
|
|
370
|
-
if (!isFederatedMode) {
|
|
371
|
-
return null;
|
|
372
|
-
}
|
|
373
|
-
return {
|
|
374
|
-
appId: master.appId,
|
|
375
|
-
appName: master.appName,
|
|
376
|
-
appDescription: master.appDescription,
|
|
377
|
-
appLogo: master.appLogo,
|
|
378
|
-
appPid: master.appPid,
|
|
379
|
-
appUrl: master.appUrl,
|
|
380
|
-
version: master.version,
|
|
381
|
-
sourceAppPid: master.appPid,
|
|
382
|
-
provider: "wallet"
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
getCurrentApp(blocklet = this.blocklet.getBlocklet()) {
|
|
386
|
-
if (blocklet) {
|
|
387
|
-
return {
|
|
388
|
-
appId: blocklet.appId,
|
|
389
|
-
appName: blocklet.appName,
|
|
390
|
-
appDescription: blocklet.appDescription,
|
|
391
|
-
appLogo: blocklet.appLogo,
|
|
392
|
-
appPid: blocklet.appPid,
|
|
393
|
-
appUrl: blocklet.appUrl,
|
|
394
|
-
version: blocklet.version,
|
|
395
|
-
// NOTICE: null 代表该值置空
|
|
396
|
-
sourceAppPid: null,
|
|
397
|
-
provider: "wallet"
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
if (window.env) {
|
|
401
|
-
const server = window.env;
|
|
402
|
-
return {
|
|
403
|
-
appId: server.appId,
|
|
404
|
-
appName: server.appName,
|
|
405
|
-
appDescription: server.appDescription,
|
|
406
|
-
appUrl: server.baseUrl,
|
|
407
|
-
// NOTICE: null 代表该值置空
|
|
408
|
-
sourceAppPid: null,
|
|
409
|
-
provider: "wallet",
|
|
410
|
-
type: "server"
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
return null;
|
|
414
|
-
}
|
|
415
|
-
getApps(blocklet = this.blocklet.getBlocklet()) {
|
|
416
|
-
const appList = [];
|
|
417
|
-
const masterApp = this.getFederatedApp(blocklet);
|
|
418
|
-
const currentApp = this.getCurrentApp(blocklet);
|
|
419
|
-
const federatedEnabled = this.getFederatedEnabled(blocklet);
|
|
420
|
-
if (currentApp) {
|
|
421
|
-
appList.push(currentApp);
|
|
422
|
-
}
|
|
423
|
-
if (masterApp && masterApp?.appId !== currentApp?.appId && federatedEnabled) {
|
|
424
|
-
appList.push(masterApp);
|
|
425
|
-
}
|
|
426
|
-
return appList.reverse();
|
|
427
|
-
}
|
|
428
|
-
async getBlockletData(appUrl, force = false) {
|
|
429
|
-
if (!force && this.blockletDataCache[appUrl]) {
|
|
430
|
-
return this.blockletDataCache[appUrl];
|
|
431
|
-
}
|
|
432
|
-
try {
|
|
433
|
-
const url = new URL("__blocklet__.js", appUrl);
|
|
434
|
-
url.searchParams.set("type", "json");
|
|
435
|
-
const res = await fetch(url.href);
|
|
436
|
-
const jsonData = await res.json();
|
|
437
|
-
this.blockletDataCache[appUrl] = jsonData;
|
|
438
|
-
return jsonData;
|
|
439
|
-
} catch (err) {
|
|
440
|
-
console.error(`Failed to get blocklet data: ${appUrl}`, err);
|
|
441
|
-
return null;
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
388
|
|
|
446
|
-
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region package.json
|
|
391
|
+
var version = "1.17.7";
|
|
447
392
|
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/libs/utils.ts
|
|
448
395
|
const sleep = (time = 0) => {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
396
|
+
return new Promise((resolve) => {
|
|
397
|
+
setTimeout(() => {
|
|
398
|
+
resolve();
|
|
399
|
+
}, time);
|
|
400
|
+
});
|
|
454
401
|
};
|
|
455
402
|
const getBearerToken = (token) => {
|
|
456
|
-
|
|
403
|
+
return `Bearer ${encodeURIComponent(token)}`;
|
|
457
404
|
};
|
|
458
405
|
const visitorIdKey = "vid";
|
|
459
406
|
const visitorIdKeyLegacy = "__visitor_id";
|
|
460
407
|
const getVisitorId = () => {
|
|
461
|
-
|
|
408
|
+
return Cookie.get(visitorIdKey) || localStorage.getItem(visitorIdKeyLegacy);
|
|
462
409
|
};
|
|
463
|
-
const verifyResponse = async (response, onInvalid) => {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
410
|
+
const verifyResponse = async (response, onInvalid, verifyFn) => {
|
|
411
|
+
if (isObject(response.data) && response.status >= 200 && response.status < 300 && typeof window !== "undefined" && window.blocklet?.appId && window.blocklet?.appPk) {
|
|
412
|
+
if (!verifyFn) return response;
|
|
413
|
+
if (!response.data.$signature) {
|
|
414
|
+
onInvalid();
|
|
415
|
+
throw new Error("Invalid response");
|
|
416
|
+
}
|
|
417
|
+
const { appId, appPk } = window.blocklet;
|
|
418
|
+
if (await verifyFn(stableStringify(omit(response.data, ["$signature"])), response.data.$signature, appPk, appId) === false) {
|
|
419
|
+
onInvalid();
|
|
420
|
+
throw new Error("Invalid response");
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return response;
|
|
477
424
|
};
|
|
478
425
|
|
|
426
|
+
//#endregion
|
|
427
|
+
//#region src/libs/csrf.ts
|
|
479
428
|
function getCSRFToken() {
|
|
480
|
-
|
|
429
|
+
return Cookie.get("x-csrf-token");
|
|
481
430
|
}
|
|
482
431
|
function getLoginToken() {
|
|
483
|
-
|
|
432
|
+
return Cookie.get("login_token");
|
|
484
433
|
}
|
|
485
434
|
async function getCSRFTokenByLoginToken() {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
loginToken: getLoginToken(),
|
|
499
|
-
csrfToken: null
|
|
500
|
-
};
|
|
501
|
-
}
|
|
435
|
+
const csrfToken = getCSRFToken();
|
|
436
|
+
try {
|
|
437
|
+
const url = joinURL(window.location.origin, WELLKNOWN_SERVICE_PATH_PREFIX, "/api/did/csrfToken");
|
|
438
|
+
const { data } = await axios.get(url, { headers: { "x-csrf-token": csrfToken } });
|
|
439
|
+
return data;
|
|
440
|
+
} catch (error) {
|
|
441
|
+
console.error(error);
|
|
442
|
+
return {
|
|
443
|
+
loginToken: getLoginToken(),
|
|
444
|
+
csrfToken: null
|
|
445
|
+
};
|
|
446
|
+
}
|
|
502
447
|
}
|
|
503
448
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
449
|
+
//#endregion
|
|
450
|
+
//#region src/libs/request/adapters/axios.ts
|
|
451
|
+
let csrfTokenCache;
|
|
452
|
+
function getCsrfTokenCache() {
|
|
453
|
+
if (!csrfTokenCache) {
|
|
454
|
+
const cacheTtl = window?.blocklet?.settings?.session?.cacheTtl;
|
|
455
|
+
csrfTokenCache = new Keyv({
|
|
456
|
+
store: new KeyvLocalStorage(),
|
|
457
|
+
ttl: isNumber(cacheTtl) ? cacheTtl * 1e3 : 1e3 * 60 * 60
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return csrfTokenCache;
|
|
461
|
+
}
|
|
509
462
|
async function sleepForLoading(config, lazyTime = 300) {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
delete config.metaData;
|
|
463
|
+
config.metaData.endTime = +/* @__PURE__ */ new Date();
|
|
464
|
+
const { startTime, endTime } = config.metaData;
|
|
465
|
+
const timeDiff = endTime - startTime;
|
|
466
|
+
if (timeDiff < lazyTime) await sleep(lazyTime - timeDiff);
|
|
467
|
+
delete config.metaData;
|
|
516
468
|
}
|
|
517
469
|
const createAxios$1 = (options, requestParams) => {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
config.headers["x-csrf-token"] = csrfToken;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
if (config.headers["x-csrf-token"] && config.headers["x-csrf-token"] !== getCSRFToken()) {
|
|
572
|
-
Cookie.set("x-csrf-token", config.headers["x-csrf-token"], {
|
|
573
|
-
sameSite: "strict",
|
|
574
|
-
secure: true
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
const visitorId = getVisitorId();
|
|
579
|
-
if (![void 0, null].includes(visitorId)) {
|
|
580
|
-
config.headers["x-blocklet-visitor-id"] = visitorId;
|
|
581
|
-
}
|
|
582
|
-
return config;
|
|
583
|
-
},
|
|
584
|
-
(error) => Promise.reject(error)
|
|
585
|
-
);
|
|
586
|
-
return instance;
|
|
470
|
+
const headers = {
|
|
471
|
+
...options?.headers,
|
|
472
|
+
"x-blocklet-js-sdk-version": version
|
|
473
|
+
};
|
|
474
|
+
const componentService = new ComponentService();
|
|
475
|
+
const instance = axios.create({
|
|
476
|
+
...options,
|
|
477
|
+
headers
|
|
478
|
+
});
|
|
479
|
+
if (requestParams?.lazy) {
|
|
480
|
+
instance.interceptors.request.use((config) => {
|
|
481
|
+
config.metaData = { startTime: +/* @__PURE__ */ new Date() };
|
|
482
|
+
return config;
|
|
483
|
+
}, (err) => Promise.reject(err));
|
|
484
|
+
instance.interceptors.response.use(async (res) => {
|
|
485
|
+
if (res.config) await sleepForLoading(res.config, requestParams?.lazyTime);
|
|
486
|
+
return res;
|
|
487
|
+
}, async (err) => {
|
|
488
|
+
if (err.response) await sleepForLoading(err.response.config, requestParams?.lazyTime);
|
|
489
|
+
return Promise.reject(err);
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
instance.interceptors.request.use(async (config) => {
|
|
493
|
+
const componentDid = requestParams?.componentDid ?? window.blocklet?.componentId?.split("/").pop();
|
|
494
|
+
config.baseURL = config.baseURL || componentService.getComponentMountPoint(componentDid);
|
|
495
|
+
config.timeout = config.timeout || 20 * 1e3;
|
|
496
|
+
const loginToken = getLoginToken();
|
|
497
|
+
const csrfToken = getCSRFToken();
|
|
498
|
+
if (loginToken && csrfToken) {
|
|
499
|
+
const loginTokenKey = loginToken.slice(-32);
|
|
500
|
+
const cache = getCsrfTokenCache();
|
|
501
|
+
const csrfTokenFromCache = await cache.get(loginTokenKey);
|
|
502
|
+
if (csrfTokenFromCache) config.headers["x-csrf-token"] = csrfTokenFromCache;
|
|
503
|
+
else {
|
|
504
|
+
const { loginToken: newLoginToken, csrfToken: newCsrfToken } = await getCSRFTokenByLoginToken();
|
|
505
|
+
if (newCsrfToken) {
|
|
506
|
+
await cache.set(newLoginToken.slice(-32), newCsrfToken);
|
|
507
|
+
config.headers["x-csrf-token"] = newCsrfToken;
|
|
508
|
+
} else config.headers["x-csrf-token"] = csrfToken;
|
|
509
|
+
}
|
|
510
|
+
if (config.headers["x-csrf-token"] && config.headers["x-csrf-token"] !== getCSRFToken()) Cookie.set("x-csrf-token", config.headers["x-csrf-token"], {
|
|
511
|
+
sameSite: "strict",
|
|
512
|
+
secure: true
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
const visitorId = getVisitorId();
|
|
516
|
+
if (![void 0, null].includes(visitorId)) config.headers["x-blocklet-visitor-id"] = visitorId;
|
|
517
|
+
return config;
|
|
518
|
+
}, (error) => Promise.reject(error));
|
|
519
|
+
return instance;
|
|
587
520
|
};
|
|
588
521
|
async function renewRefreshToken$1(refreshToken) {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
authorization: getBearerToken(refreshToken)
|
|
598
|
-
}
|
|
599
|
-
});
|
|
600
|
-
const { data } = await refreshApi.post("/api/did/refreshSession");
|
|
601
|
-
return data;
|
|
522
|
+
if (!refreshToken) throw new Error("Refresh token not found");
|
|
523
|
+
const { data } = await createAxios$1({
|
|
524
|
+
baseURL: WELLKNOWN_SERVICE_PATH_PREFIX,
|
|
525
|
+
timeout: 10 * 1e3,
|
|
526
|
+
secure: true,
|
|
527
|
+
headers: { authorization: getBearerToken(refreshToken) }
|
|
528
|
+
}).post("/api/did/refreshSession");
|
|
529
|
+
return data;
|
|
602
530
|
}
|
|
603
|
-
function createRequest$1({
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
if (originalRequest) {
|
|
650
|
-
originalRequest.headers = originalRequest?.headers ? { ...originalRequest.headers } : {};
|
|
651
|
-
if (error?.response?.status === 401 && !originalRequest._retry) {
|
|
652
|
-
originalRequest._retry = true;
|
|
653
|
-
try {
|
|
654
|
-
if (!refreshingTokenRequest) {
|
|
655
|
-
refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
|
|
656
|
-
}
|
|
657
|
-
const tokenData = await refreshingTokenRequest;
|
|
658
|
-
setSessionToken(tokenData.nextToken);
|
|
659
|
-
setRefreshToken(tokenData.nextRefreshToken);
|
|
660
|
-
if (typeof onRefreshTokenSuccess === "function") {
|
|
661
|
-
onRefreshTokenSuccess(tokenData);
|
|
662
|
-
}
|
|
663
|
-
return service(originalRequest);
|
|
664
|
-
} catch (refreshTokenError) {
|
|
665
|
-
removeSessionToken();
|
|
666
|
-
removeRefreshToken();
|
|
667
|
-
if (typeof onRefreshTokenError === "function") {
|
|
668
|
-
onRefreshTokenError(refreshTokenError);
|
|
669
|
-
}
|
|
670
|
-
return Promise.reject(error);
|
|
671
|
-
} finally {
|
|
672
|
-
refreshingTokenRequest = null;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
return Promise.reject(error);
|
|
677
|
-
}
|
|
678
|
-
);
|
|
679
|
-
return service;
|
|
531
|
+
function createRequest$1({ getSessionToken, setSessionToken, removeSessionToken, getRefreshToken, setRefreshToken, removeRefreshToken, onRefreshTokenError, onRefreshTokenSuccess }, requestOptions, requestParams) {
|
|
532
|
+
let refreshingTokenRequest = null;
|
|
533
|
+
const service = createAxios$1({
|
|
534
|
+
timeout: 30 * 1e3,
|
|
535
|
+
...requestOptions
|
|
536
|
+
}, requestParams);
|
|
537
|
+
service.interceptors.request.use(async (config) => {
|
|
538
|
+
if (!Cookie.get(SESSION_TOKEN_STORAGE_KEY)) {
|
|
539
|
+
const token = getSessionToken(config);
|
|
540
|
+
if (token) config.headers.authorization = getBearerToken(token);
|
|
541
|
+
}
|
|
542
|
+
if (refreshingTokenRequest) await refreshingTokenRequest;
|
|
543
|
+
return config;
|
|
544
|
+
}, (error) => Promise.reject(error));
|
|
545
|
+
service.interceptors.response.use(async (response) => {
|
|
546
|
+
if (response.config?.secure) return await verifyResponse(response, () => {
|
|
547
|
+
removeSessionToken();
|
|
548
|
+
removeRefreshToken();
|
|
549
|
+
}, requestParams?.verifyFn);
|
|
550
|
+
return response;
|
|
551
|
+
}, async (error) => {
|
|
552
|
+
const originalRequest = error.config;
|
|
553
|
+
if (originalRequest) {
|
|
554
|
+
originalRequest.headers = originalRequest?.headers ? { ...originalRequest.headers } : {};
|
|
555
|
+
if (error?.response?.status === 401 && !originalRequest._retry) {
|
|
556
|
+
originalRequest._retry = true;
|
|
557
|
+
try {
|
|
558
|
+
if (!refreshingTokenRequest) refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
|
|
559
|
+
const tokenData = await refreshingTokenRequest;
|
|
560
|
+
setSessionToken(tokenData.nextToken);
|
|
561
|
+
setRefreshToken(tokenData.nextRefreshToken);
|
|
562
|
+
if (typeof onRefreshTokenSuccess === "function") onRefreshTokenSuccess(tokenData);
|
|
563
|
+
return service(originalRequest);
|
|
564
|
+
} catch (refreshTokenError) {
|
|
565
|
+
removeSessionToken();
|
|
566
|
+
removeRefreshToken();
|
|
567
|
+
if (typeof onRefreshTokenError === "function") onRefreshTokenError(refreshTokenError);
|
|
568
|
+
return Promise.reject(error);
|
|
569
|
+
} finally {
|
|
570
|
+
refreshingTokenRequest = null;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return Promise.reject(error);
|
|
575
|
+
});
|
|
576
|
+
return service;
|
|
680
577
|
}
|
|
681
578
|
|
|
579
|
+
//#endregion
|
|
580
|
+
//#region src/libs/request/adapters/fetch.ts
|
|
682
581
|
function createFetch$1(globalOptions = {}, requestParams) {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
};
|
|
582
|
+
return async (input, options) => {
|
|
583
|
+
const startAt = Date.now();
|
|
584
|
+
const headers = {
|
|
585
|
+
...globalOptions?.headers,
|
|
586
|
+
...options?.headers,
|
|
587
|
+
"x-csrf-token": getCSRFToken(),
|
|
588
|
+
"x-blocklet-js-sdk-version": version
|
|
589
|
+
};
|
|
590
|
+
const visitorId = getVisitorId();
|
|
591
|
+
if (![void 0, null].includes(visitorId)) headers["x-blocklet-visitor-id"] = visitorId;
|
|
592
|
+
const request = fetch(input, {
|
|
593
|
+
...globalOptions,
|
|
594
|
+
...options,
|
|
595
|
+
headers
|
|
596
|
+
});
|
|
597
|
+
try {
|
|
598
|
+
return request;
|
|
599
|
+
} catch (error) {
|
|
600
|
+
throw error;
|
|
601
|
+
} finally {
|
|
602
|
+
const endAt = Date.now();
|
|
603
|
+
if (requestParams?.lazy) {
|
|
604
|
+
const lazyTime = requestParams?.lazyTime ?? 300;
|
|
605
|
+
const timeDiff = endAt - startAt;
|
|
606
|
+
if (timeDiff < lazyTime) await sleep(lazyTime - timeDiff);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
714
610
|
}
|
|
715
611
|
async function renewRefreshToken(refreshToken) {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
method: "POST",
|
|
722
|
-
headers: {
|
|
723
|
-
authorization: getBearerToken(refreshToken)
|
|
724
|
-
}
|
|
725
|
-
});
|
|
726
|
-
const data = await res.json();
|
|
727
|
-
return data;
|
|
612
|
+
if (!refreshToken) throw new Error("Refresh token not found");
|
|
613
|
+
return await (await createFetch$1()(joinURL(WELLKNOWN_SERVICE_PATH_PREFIX, "/api/did/refreshSession"), {
|
|
614
|
+
method: "POST",
|
|
615
|
+
headers: { authorization: getBearerToken(refreshToken) }
|
|
616
|
+
})).json();
|
|
728
617
|
}
|
|
729
|
-
function createRequest({
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
}
|
|
788
|
-
});
|
|
789
|
-
} catch (error) {
|
|
790
|
-
removeSessionToken();
|
|
791
|
-
removeRefreshToken();
|
|
792
|
-
if (typeof onRefreshTokenError === "function") {
|
|
793
|
-
onRefreshTokenError(error);
|
|
794
|
-
}
|
|
795
|
-
return res;
|
|
796
|
-
} finally {
|
|
797
|
-
refreshingTokenRequest = null;
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
if (res.ok && options?.secure) {
|
|
801
|
-
await verifyResponse({ status: res.status, data: await res.json() }, () => {
|
|
802
|
-
removeSessionToken();
|
|
803
|
-
removeRefreshToken();
|
|
804
|
-
});
|
|
805
|
-
}
|
|
806
|
-
return res;
|
|
807
|
-
};
|
|
618
|
+
function createRequest({ baseURL, getSessionToken, setSessionToken, removeSessionToken, getRefreshToken, setRefreshToken, removeRefreshToken, onRefreshTokenError, onRefreshTokenSuccess }, requestOptions, requestParams) {
|
|
619
|
+
let refreshingTokenRequest = null;
|
|
620
|
+
const service = createFetch$1(requestOptions, requestParams);
|
|
621
|
+
const componentService = new ComponentService();
|
|
622
|
+
return async (input, options) => {
|
|
623
|
+
let authorization;
|
|
624
|
+
let finalUrl = input;
|
|
625
|
+
if (typeof input === "string") {
|
|
626
|
+
if (!isUrl(input)) if (baseURL) finalUrl = joinURL(baseURL, input);
|
|
627
|
+
else {
|
|
628
|
+
const componentDid = requestParams?.componentDid ?? window.blocklet?.componentId?.split("/").pop();
|
|
629
|
+
finalUrl = joinURL(componentService.getComponentMountPoint(componentDid), input);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (!Cookie.get(SESSION_TOKEN_STORAGE_KEY)) {
|
|
633
|
+
const token = getSessionToken(requestOptions);
|
|
634
|
+
if (token) authorization = getBearerToken(token);
|
|
635
|
+
}
|
|
636
|
+
if (refreshingTokenRequest) await refreshingTokenRequest;
|
|
637
|
+
const res = await service(finalUrl, {
|
|
638
|
+
...options,
|
|
639
|
+
headers: {
|
|
640
|
+
...options?.headers,
|
|
641
|
+
authorization
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
if (!res.ok && res.status === 401) {
|
|
645
|
+
refreshingTokenRequest = renewRefreshToken(getRefreshToken());
|
|
646
|
+
try {
|
|
647
|
+
const tokenData = await refreshingTokenRequest;
|
|
648
|
+
setSessionToken(tokenData.nextToken);
|
|
649
|
+
setRefreshToken(tokenData.nextRefreshToken);
|
|
650
|
+
if (typeof onRefreshTokenSuccess === "function") onRefreshTokenSuccess(tokenData);
|
|
651
|
+
return service(finalUrl, {
|
|
652
|
+
...options,
|
|
653
|
+
headers: {
|
|
654
|
+
...options?.headers,
|
|
655
|
+
authorization
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
} catch (error) {
|
|
659
|
+
removeSessionToken();
|
|
660
|
+
removeRefreshToken();
|
|
661
|
+
if (typeof onRefreshTokenError === "function") onRefreshTokenError(error);
|
|
662
|
+
return res;
|
|
663
|
+
} finally {
|
|
664
|
+
refreshingTokenRequest = null;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (res.ok && options?.secure) await verifyResponse({
|
|
668
|
+
status: res.status,
|
|
669
|
+
data: await res.json()
|
|
670
|
+
}, () => {
|
|
671
|
+
removeSessionToken();
|
|
672
|
+
removeRefreshToken();
|
|
673
|
+
}, requestParams?.verifyFn);
|
|
674
|
+
return res;
|
|
675
|
+
};
|
|
808
676
|
}
|
|
809
677
|
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
var
|
|
813
|
-
|
|
814
|
-
|
|
678
|
+
//#endregion
|
|
679
|
+
//#region src/index.ts
|
|
680
|
+
var BlockletSDK = class {
|
|
681
|
+
constructor(options) {
|
|
682
|
+
const tokenService = new TokenService();
|
|
683
|
+
const internalApi = createRequest$1({
|
|
684
|
+
getSessionToken: tokenService.getSessionToken,
|
|
685
|
+
setSessionToken: tokenService.setSessionToken,
|
|
686
|
+
removeSessionToken: tokenService.removeSessionToken,
|
|
687
|
+
getRefreshToken: tokenService.getRefreshToken,
|
|
688
|
+
setRefreshToken: tokenService.setRefreshToken,
|
|
689
|
+
removeRefreshToken: tokenService.removeRefreshToken,
|
|
690
|
+
onRefreshTokenError: () => {
|
|
691
|
+
console.error("Failed to refresh token");
|
|
692
|
+
},
|
|
693
|
+
onRefreshTokenSuccess: () => {}
|
|
694
|
+
}, { baseURL: WELLKNOWN_SERVICE_PATH_PREFIX }, { verifyFn: options?.verifyFn });
|
|
695
|
+
const blocklet = new BlockletService();
|
|
696
|
+
this.user = new AuthService({
|
|
697
|
+
api: internalApi,
|
|
698
|
+
token: tokenService
|
|
699
|
+
});
|
|
700
|
+
this.federated = new FederatedService({
|
|
701
|
+
api: internalApi,
|
|
702
|
+
blocklet
|
|
703
|
+
});
|
|
704
|
+
this.userSession = new UserSessionService({
|
|
705
|
+
api: internalApi,
|
|
706
|
+
blocklet
|
|
707
|
+
});
|
|
708
|
+
this.token = tokenService;
|
|
709
|
+
this.blocklet = blocklet;
|
|
710
|
+
/**
|
|
711
|
+
* @deprecated this is deprecated, please use createAxios replace this
|
|
712
|
+
*/
|
|
713
|
+
this.api = internalApi;
|
|
714
|
+
}
|
|
815
715
|
};
|
|
816
|
-
class BlockletSDK {
|
|
817
|
-
constructor() {
|
|
818
|
-
__publicField(this, "api");
|
|
819
|
-
__publicField(this, "user");
|
|
820
|
-
__publicField(this, "userSession");
|
|
821
|
-
__publicField(this, "token");
|
|
822
|
-
__publicField(this, "blocklet");
|
|
823
|
-
__publicField(this, "federated");
|
|
824
|
-
const tokenService = new TokenService();
|
|
825
|
-
const internalApi = createRequest$1(
|
|
826
|
-
{
|
|
827
|
-
getSessionToken: tokenService.getSessionToken,
|
|
828
|
-
setSessionToken: tokenService.setSessionToken,
|
|
829
|
-
removeSessionToken: tokenService.removeSessionToken,
|
|
830
|
-
getRefreshToken: tokenService.getRefreshToken,
|
|
831
|
-
setRefreshToken: tokenService.setRefreshToken,
|
|
832
|
-
removeRefreshToken: tokenService.removeRefreshToken,
|
|
833
|
-
onRefreshTokenError: () => {
|
|
834
|
-
console.error("Failed to refresh token");
|
|
835
|
-
},
|
|
836
|
-
onRefreshTokenSuccess: () => {
|
|
837
|
-
}
|
|
838
|
-
},
|
|
839
|
-
{
|
|
840
|
-
baseURL: WELLKNOWN_SERVICE_PATH_PREFIX
|
|
841
|
-
}
|
|
842
|
-
);
|
|
843
|
-
const blocklet = new BlockletService();
|
|
844
|
-
this.user = new AuthService({ api: internalApi, token: tokenService });
|
|
845
|
-
this.federated = new FederatedService({ api: internalApi, blocklet });
|
|
846
|
-
this.userSession = new UserSessionService({ api: internalApi, blocklet });
|
|
847
|
-
this.token = tokenService;
|
|
848
|
-
this.blocklet = blocklet;
|
|
849
|
-
this.api = internalApi;
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
716
|
function createAxios(config = {}, requestParams = {}) {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
}
|
|
867
|
-
},
|
|
868
|
-
config,
|
|
869
|
-
requestParams
|
|
870
|
-
);
|
|
717
|
+
const tokenService = new TokenService();
|
|
718
|
+
return createRequest$1({
|
|
719
|
+
getSessionToken: tokenService.getSessionToken,
|
|
720
|
+
setSessionToken: tokenService.setSessionToken,
|
|
721
|
+
removeSessionToken: tokenService.removeSessionToken,
|
|
722
|
+
getRefreshToken: tokenService.getRefreshToken,
|
|
723
|
+
setRefreshToken: tokenService.setRefreshToken,
|
|
724
|
+
removeRefreshToken: tokenService.removeRefreshToken,
|
|
725
|
+
onRefreshTokenError: () => {
|
|
726
|
+
console.error("Failed to refresh token");
|
|
727
|
+
},
|
|
728
|
+
onRefreshTokenSuccess: () => {}
|
|
729
|
+
}, config, requestParams);
|
|
871
730
|
}
|
|
872
731
|
function createFetch(options, requestParams) {
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
}
|
|
887
|
-
},
|
|
888
|
-
options,
|
|
889
|
-
requestParams
|
|
890
|
-
);
|
|
732
|
+
const tokenService = new TokenService();
|
|
733
|
+
return createRequest({
|
|
734
|
+
getSessionToken: tokenService.getSessionToken,
|
|
735
|
+
setSessionToken: tokenService.setSessionToken,
|
|
736
|
+
removeSessionToken: tokenService.removeSessionToken,
|
|
737
|
+
getRefreshToken: tokenService.getRefreshToken,
|
|
738
|
+
setRefreshToken: tokenService.setRefreshToken,
|
|
739
|
+
removeRefreshToken: tokenService.removeRefreshToken,
|
|
740
|
+
onRefreshTokenError: () => {
|
|
741
|
+
console.error("Failed to refresh token");
|
|
742
|
+
},
|
|
743
|
+
onRefreshTokenSuccess: () => {}
|
|
744
|
+
}, options, requestParams);
|
|
891
745
|
}
|
|
892
|
-
const getBlockletSDK =
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
return instance;
|
|
899
|
-
};
|
|
746
|
+
const getBlockletSDK = (() => {
|
|
747
|
+
let instance;
|
|
748
|
+
return (options) => {
|
|
749
|
+
if (!instance) instance = new BlockletSDK(options);
|
|
750
|
+
return instance;
|
|
751
|
+
};
|
|
900
752
|
})();
|
|
901
753
|
|
|
754
|
+
//#endregion
|
|
902
755
|
export { BlockletSDK, createAxios, createFetch, getBlockletSDK, getCSRFToken, getCSRFTokenByLoginToken, getLoginToken };
|
|
756
|
+
//# sourceMappingURL=index.mjs.map
|