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