@blocklet/js-sdk 1.16.28 → 1.16.29-beta-e04c6f40
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 +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.mjs +57 -1
- package/package.json +5 -4
- package/dist/index.cjs +0 -455
- package/dist/index.d.cts +0 -123
package/dist/index.d.mts
CHANGED
|
@@ -5,6 +5,7 @@ type UserPublicInfo = {
|
|
|
5
5
|
avatar: string;
|
|
6
6
|
did: string;
|
|
7
7
|
fullName: string;
|
|
8
|
+
sourceAppPid: string | null;
|
|
8
9
|
};
|
|
9
10
|
type Webhook = {
|
|
10
11
|
type: 'slack' | 'api';
|
|
@@ -105,6 +106,12 @@ declare class UserSessionService {
|
|
|
105
106
|
}): Promise<UserSession[]>;
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
declare class BlockletService {
|
|
110
|
+
getBlocklet(baseUrl: string, force?: boolean): Promise<any>;
|
|
111
|
+
loadBlocklet(): Promise<void>;
|
|
112
|
+
getPrefix(blocklet?: any): any;
|
|
113
|
+
}
|
|
114
|
+
|
|
108
115
|
type RequestParams = {
|
|
109
116
|
lazy?: boolean;
|
|
110
117
|
lazyTime?: number;
|
|
@@ -115,9 +122,10 @@ declare class BlockletSDK {
|
|
|
115
122
|
user: AuthService;
|
|
116
123
|
userSession: UserSessionService;
|
|
117
124
|
token: TokenService;
|
|
125
|
+
blocklet: BlockletService;
|
|
118
126
|
constructor();
|
|
119
127
|
}
|
|
120
128
|
declare function createAxios(config?: AxiosRequestConfig, requestParams?: RequestParams): axios.AxiosInstance;
|
|
121
129
|
declare function createFetch(options?: RequestInit, requestParams?: RequestParams): (input: string | Request | URL, options?: RequestInit) => Promise<Response>;
|
|
122
130
|
|
|
123
|
-
export { AuthService, BlockletSDK, type NotificationConfig, type PrivacyConfig, TokenService, type UserPublicInfo, type UserSession, UserSessionService, type UserSessionUser, type Webhook, createAxios, createFetch };
|
|
131
|
+
export { AuthService, BlockletSDK, BlockletService, type NotificationConfig, type PrivacyConfig, TokenService, type UserPublicInfo, type UserSession, UserSessionService, type UserSessionUser, type Webhook, createAxios, createFetch };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ type UserPublicInfo = {
|
|
|
5
5
|
avatar: string;
|
|
6
6
|
did: string;
|
|
7
7
|
fullName: string;
|
|
8
|
+
sourceAppPid: string | null;
|
|
8
9
|
};
|
|
9
10
|
type Webhook = {
|
|
10
11
|
type: 'slack' | 'api';
|
|
@@ -105,6 +106,12 @@ declare class UserSessionService {
|
|
|
105
106
|
}): Promise<UserSession[]>;
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
declare class BlockletService {
|
|
110
|
+
getBlocklet(baseUrl: string, force?: boolean): Promise<any>;
|
|
111
|
+
loadBlocklet(): Promise<void>;
|
|
112
|
+
getPrefix(blocklet?: any): any;
|
|
113
|
+
}
|
|
114
|
+
|
|
108
115
|
type RequestParams = {
|
|
109
116
|
lazy?: boolean;
|
|
110
117
|
lazyTime?: number;
|
|
@@ -115,9 +122,10 @@ declare class BlockletSDK {
|
|
|
115
122
|
user: AuthService;
|
|
116
123
|
userSession: UserSessionService;
|
|
117
124
|
token: TokenService;
|
|
125
|
+
blocklet: BlockletService;
|
|
118
126
|
constructor();
|
|
119
127
|
}
|
|
120
128
|
declare function createAxios(config?: AxiosRequestConfig, requestParams?: RequestParams): axios.AxiosInstance;
|
|
121
129
|
declare function createFetch(options?: RequestInit, requestParams?: RequestParams): (input: string | Request | URL, options?: RequestInit) => Promise<Response>;
|
|
122
130
|
|
|
123
|
-
export { AuthService, BlockletSDK, type NotificationConfig, type PrivacyConfig, TokenService, type UserPublicInfo, type UserSession, UserSessionService, type UserSessionUser, type Webhook, createAxios, createFetch };
|
|
131
|
+
export { AuthService, BlockletSDK, BlockletService, type NotificationConfig, type PrivacyConfig, TokenService, type UserPublicInfo, type UserSession, UserSessionService, type UserSessionUser, type Webhook, createAxios, createFetch };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { WELLKNOWN_SERVICE_PATH_PREFIX, SESSION_TOKEN_STORAGE_KEY, REFRESH_TOKEN_STORAGE_KEY } from '@abtnode/constant';
|
|
2
2
|
import { withQuery, joinURL } from 'ufo';
|
|
3
3
|
import Cookie from 'js-cookie';
|
|
4
|
+
import QuickLRU from 'quick-lru';
|
|
4
5
|
import axios from 'axios';
|
|
5
6
|
|
|
6
7
|
var __defProp$2 = Object.defineProperty;
|
|
@@ -115,7 +116,56 @@ class UserSessionService {
|
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
const
|
|
119
|
+
const blockletCache = new QuickLRU({ maxSize: 30, maxAge: 60 * 1e3 });
|
|
120
|
+
class BlockletService {
|
|
121
|
+
async getBlocklet(baseUrl, force = false) {
|
|
122
|
+
if (!force && blockletCache.has(baseUrl)) {
|
|
123
|
+
return blockletCache.get(baseUrl);
|
|
124
|
+
}
|
|
125
|
+
const url = withQuery(joinURL(baseUrl, "__blocklet__.js"), {
|
|
126
|
+
type: "json",
|
|
127
|
+
t: Date.now()
|
|
128
|
+
});
|
|
129
|
+
const res = await fetch(url);
|
|
130
|
+
const data = await res.json();
|
|
131
|
+
blockletCache.set(baseUrl, data);
|
|
132
|
+
return data;
|
|
133
|
+
}
|
|
134
|
+
async loadBlocklet() {
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
137
|
+
reject();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const blockletScript = document.createElement("script");
|
|
141
|
+
let basename = "/";
|
|
142
|
+
if (window.blocklet && window.blocklet.prefix) {
|
|
143
|
+
basename = window.blocklet.prefix;
|
|
144
|
+
}
|
|
145
|
+
blockletScript.src = withQuery(joinURL(basename, "__blocklet__.js"), {
|
|
146
|
+
t: Date.now()
|
|
147
|
+
});
|
|
148
|
+
blockletScript.onload = () => {
|
|
149
|
+
resolve();
|
|
150
|
+
};
|
|
151
|
+
blockletScript.onerror = () => {
|
|
152
|
+
reject();
|
|
153
|
+
};
|
|
154
|
+
document.head.append(blockletScript);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
getPrefix(blocklet) {
|
|
158
|
+
if (blocklet) {
|
|
159
|
+
return blocklet?.prefix || "/";
|
|
160
|
+
}
|
|
161
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
return window.blocklet?.prefix || "/";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const version = "1.16.28";
|
|
119
169
|
|
|
120
170
|
const sleep = (time = 0) => {
|
|
121
171
|
return new Promise((resolve) => {
|
|
@@ -377,6 +427,7 @@ class BlockletSDK {
|
|
|
377
427
|
__publicField(this, "user");
|
|
378
428
|
__publicField(this, "userSession");
|
|
379
429
|
__publicField(this, "token");
|
|
430
|
+
__publicField(this, "blocklet");
|
|
380
431
|
const tokenService = new TokenService();
|
|
381
432
|
const internalApi = createRequest$1(
|
|
382
433
|
{
|
|
@@ -400,10 +451,15 @@ class BlockletSDK {
|
|
|
400
451
|
this.userSession = new UserSessionService({ api: internalApi });
|
|
401
452
|
this.token = tokenService;
|
|
402
453
|
this.api = internalApi;
|
|
454
|
+
this.blocklet = new BlockletService();
|
|
403
455
|
}
|
|
404
456
|
}
|
|
405
457
|
function createAxios(config, requestParams) {
|
|
406
458
|
const tokenService = new TokenService();
|
|
459
|
+
const blockletService = new BlockletService();
|
|
460
|
+
if (config.baseURL === void 0) {
|
|
461
|
+
config.baseURL = blockletService.getPrefix();
|
|
462
|
+
}
|
|
407
463
|
return createRequest$1(
|
|
408
464
|
{
|
|
409
465
|
getSessionToken: tokenService.getSessionToken,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/js-sdk",
|
|
3
|
-
"version": "1.16.
|
|
4
|
-
"main": "dist/index.
|
|
3
|
+
"version": "1.16.29-beta-e04c6f40",
|
|
4
|
+
"main": "dist/index.mjs",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
@@ -31,13 +31,14 @@
|
|
|
31
31
|
"build": "unbuild"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@abtnode/constant": "1.16.
|
|
34
|
+
"@abtnode/constant": "1.16.29-beta-e04c6f40",
|
|
35
35
|
"axios": "^1.7.2",
|
|
36
36
|
"js-cookie": "^3.0.5",
|
|
37
|
+
"quick-lru": "^7.0.0",
|
|
37
38
|
"ufo": "^1.5.3"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"unbuild": "^2.0.0"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "f3e8f2ce931215b95ada33e87aee997d8ae52a69"
|
|
43
44
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,455 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const constant = require('@abtnode/constant');
|
|
4
|
-
const ufo = require('ufo');
|
|
5
|
-
const Cookie = require('js-cookie');
|
|
6
|
-
const axios = require('axios');
|
|
7
|
-
|
|
8
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
9
|
-
|
|
10
|
-
const Cookie__default = /*#__PURE__*/_interopDefaultCompat(Cookie);
|
|
11
|
-
const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
|
|
12
|
-
|
|
13
|
-
var __defProp$2 = Object.defineProperty;
|
|
14
|
-
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
15
|
-
var __publicField$2 = (obj, key, value) => {
|
|
16
|
-
__defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
class AuthService {
|
|
20
|
-
constructor({ api }) {
|
|
21
|
-
__publicField$2(this, "api");
|
|
22
|
-
this.api = api;
|
|
23
|
-
}
|
|
24
|
-
async getUserPublicInfo({ did }) {
|
|
25
|
-
const { data } = await this.api.get("/api/user", {
|
|
26
|
-
params: { did }
|
|
27
|
-
});
|
|
28
|
-
return data;
|
|
29
|
-
}
|
|
30
|
-
async getUserPrivacyConfig({ did }) {
|
|
31
|
-
const { data } = await this.api.get("/api/user/privacy/config", {
|
|
32
|
-
params: { did }
|
|
33
|
-
});
|
|
34
|
-
return data;
|
|
35
|
-
}
|
|
36
|
-
async saveUserPrivacyConfig(config) {
|
|
37
|
-
const { data } = await this.api.post("/api/user/privacy/config", config);
|
|
38
|
-
return data;
|
|
39
|
-
}
|
|
40
|
-
async getUserNotificationConfig() {
|
|
41
|
-
const { data } = await this.api.get("/api/user/notification/config");
|
|
42
|
-
return data;
|
|
43
|
-
}
|
|
44
|
-
async saveUserNotificationConfig(config) {
|
|
45
|
-
const { data } = await this.api.post("/api/user/notification/config", config);
|
|
46
|
-
return data;
|
|
47
|
-
}
|
|
48
|
-
async testNotificationWebhook(webhook) {
|
|
49
|
-
const { data } = await this.api.put("/api/user/notification/webhook", webhook);
|
|
50
|
-
return data;
|
|
51
|
-
}
|
|
52
|
-
async getProfileUrl({ did, locale }) {
|
|
53
|
-
const url = `${constant.WELLKNOWN_SERVICE_PATH_PREFIX}/user`;
|
|
54
|
-
return ufo.withQuery(url, {
|
|
55
|
-
did,
|
|
56
|
-
locale
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
async getProfile() {
|
|
60
|
-
const { data } = await this.api.get("/api/user/profile");
|
|
61
|
-
return data;
|
|
62
|
-
}
|
|
63
|
-
async saveProfile({ locale }) {
|
|
64
|
-
const { data } = await this.api.put("/api/user/profile", {
|
|
65
|
-
locale
|
|
66
|
-
});
|
|
67
|
-
return data;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
*
|
|
71
|
-
* 指定要退出登录的设备 id
|
|
72
|
-
* @param {{ visitorId: string }} { visitorId }
|
|
73
|
-
* @return {*} {Promise<void>}
|
|
74
|
-
*/
|
|
75
|
-
async logout({ visitorId }) {
|
|
76
|
-
const { data } = await this.api.post("/api/user/logout", {
|
|
77
|
-
visitorId
|
|
78
|
-
});
|
|
79
|
-
return data;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
class TokenService {
|
|
84
|
-
getSessionToken() {
|
|
85
|
-
return Cookie__default.get(constant.SESSION_TOKEN_STORAGE_KEY);
|
|
86
|
-
}
|
|
87
|
-
setSessionToken(value) {
|
|
88
|
-
Cookie__default.set(constant.SESSION_TOKEN_STORAGE_KEY, value);
|
|
89
|
-
}
|
|
90
|
-
removeSessionToken() {
|
|
91
|
-
Cookie__default.remove(constant.SESSION_TOKEN_STORAGE_KEY);
|
|
92
|
-
}
|
|
93
|
-
getRefreshToken() {
|
|
94
|
-
return localStorage.getItem(constant.REFRESH_TOKEN_STORAGE_KEY);
|
|
95
|
-
}
|
|
96
|
-
setRefreshToken(value) {
|
|
97
|
-
localStorage.setItem(constant.REFRESH_TOKEN_STORAGE_KEY, value);
|
|
98
|
-
}
|
|
99
|
-
removeRefreshToken() {
|
|
100
|
-
localStorage.removeItem(constant.REFRESH_TOKEN_STORAGE_KEY);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
var __defProp$1 = Object.defineProperty;
|
|
105
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
106
|
-
var __publicField$1 = (obj, key, value) => {
|
|
107
|
-
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
108
|
-
return value;
|
|
109
|
-
};
|
|
110
|
-
class UserSessionService {
|
|
111
|
-
constructor({ api }) {
|
|
112
|
-
__publicField$1(this, "api");
|
|
113
|
-
this.api = api;
|
|
114
|
-
}
|
|
115
|
-
async getUserSessions({ did }) {
|
|
116
|
-
const { data } = await this.api.get("/api/user-session", {
|
|
117
|
-
params: {
|
|
118
|
-
userDid: did
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
return data;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const version = "1.16.27";
|
|
126
|
-
|
|
127
|
-
const sleep = (time = 0) => {
|
|
128
|
-
return new Promise((resolve) => {
|
|
129
|
-
setTimeout(() => {
|
|
130
|
-
resolve();
|
|
131
|
-
}, time);
|
|
132
|
-
});
|
|
133
|
-
};
|
|
134
|
-
const getBearerToken = (token) => {
|
|
135
|
-
return `Bearer ${encodeURIComponent(token)}`;
|
|
136
|
-
};
|
|
137
|
-
const visitorIdKey = "__visitor_id";
|
|
138
|
-
const getVisitorId = () => {
|
|
139
|
-
return localStorage.getItem(visitorIdKey);
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
async function sleepForLoading(config, lazyTime = 300) {
|
|
143
|
-
config.metaData.endTime = +/* @__PURE__ */ new Date();
|
|
144
|
-
const { startTime, endTime } = config.metaData;
|
|
145
|
-
const timeDiff = endTime - startTime;
|
|
146
|
-
if (timeDiff < lazyTime)
|
|
147
|
-
await sleep(lazyTime - timeDiff);
|
|
148
|
-
delete config.metaData;
|
|
149
|
-
}
|
|
150
|
-
const createAxios$1 = (options, requestParams) => {
|
|
151
|
-
const headers = {
|
|
152
|
-
...options?.headers,
|
|
153
|
-
"x-blocklet-js-sdk-version": version
|
|
154
|
-
};
|
|
155
|
-
const visitorId = getVisitorId();
|
|
156
|
-
if (![void 0, null].includes(visitorId)) {
|
|
157
|
-
headers["x-blocklet-visitor-id"] = visitorId;
|
|
158
|
-
}
|
|
159
|
-
const instance = axios__default.create({
|
|
160
|
-
...options,
|
|
161
|
-
headers
|
|
162
|
-
});
|
|
163
|
-
if (requestParams?.lazy) {
|
|
164
|
-
instance.interceptors.request.use(
|
|
165
|
-
(config) => {
|
|
166
|
-
config.metaData = { startTime: +/* @__PURE__ */ new Date() };
|
|
167
|
-
return config;
|
|
168
|
-
},
|
|
169
|
-
(err) => Promise.reject(err)
|
|
170
|
-
);
|
|
171
|
-
instance.interceptors.response.use(
|
|
172
|
-
async (res) => {
|
|
173
|
-
await sleepForLoading(res.config, requestParams?.lazyTime);
|
|
174
|
-
return res;
|
|
175
|
-
},
|
|
176
|
-
async (err) => {
|
|
177
|
-
await sleepForLoading(err.response.config, requestParams?.lazyTime);
|
|
178
|
-
return Promise.reject(err);
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
return instance;
|
|
183
|
-
};
|
|
184
|
-
async function renewRefreshToken$1(refreshToken) {
|
|
185
|
-
if (!refreshToken) {
|
|
186
|
-
throw new Error("Refresh token not found");
|
|
187
|
-
}
|
|
188
|
-
const refreshApi = createAxios$1({
|
|
189
|
-
baseURL: constant.WELLKNOWN_SERVICE_PATH_PREFIX,
|
|
190
|
-
timeout: 10 * 1e3,
|
|
191
|
-
headers: {
|
|
192
|
-
authorization: getBearerToken(refreshToken)
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
const { data } = await refreshApi.post("/api/did/refreshSession");
|
|
196
|
-
return data;
|
|
197
|
-
}
|
|
198
|
-
function createRequest$1({
|
|
199
|
-
getSessionToken,
|
|
200
|
-
setSessionToken,
|
|
201
|
-
removeSessionToken,
|
|
202
|
-
getRefreshToken,
|
|
203
|
-
setRefreshToken,
|
|
204
|
-
removeRefreshToken,
|
|
205
|
-
onRefreshTokenError,
|
|
206
|
-
onRefreshTokenSuccess
|
|
207
|
-
}, requestOptions, requestParams) {
|
|
208
|
-
let refreshingTokenRequest = null;
|
|
209
|
-
const service = createAxios$1(
|
|
210
|
-
{
|
|
211
|
-
timeout: 30 * 1e3,
|
|
212
|
-
...requestOptions
|
|
213
|
-
},
|
|
214
|
-
requestParams
|
|
215
|
-
);
|
|
216
|
-
service.interceptors.request.use(
|
|
217
|
-
async (config) => {
|
|
218
|
-
if (!Cookie__default.get(constant.SESSION_TOKEN_STORAGE_KEY)) {
|
|
219
|
-
const token = getSessionToken();
|
|
220
|
-
if (token) {
|
|
221
|
-
config.headers.authorization = getBearerToken(token);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if (refreshingTokenRequest) {
|
|
225
|
-
await refreshingTokenRequest;
|
|
226
|
-
}
|
|
227
|
-
return config;
|
|
228
|
-
},
|
|
229
|
-
(error) => Promise.reject(error)
|
|
230
|
-
);
|
|
231
|
-
service.interceptors.response.use(
|
|
232
|
-
(response) => response,
|
|
233
|
-
async (error) => {
|
|
234
|
-
const originalRequest = error.config;
|
|
235
|
-
originalRequest.headers = { ...originalRequest?.headers };
|
|
236
|
-
if (error?.response?.status === 401 && !originalRequest._retry) {
|
|
237
|
-
originalRequest._retry = true;
|
|
238
|
-
try {
|
|
239
|
-
if (!refreshingTokenRequest) {
|
|
240
|
-
refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
|
|
241
|
-
}
|
|
242
|
-
const tokenData = await refreshingTokenRequest;
|
|
243
|
-
setSessionToken(tokenData.nextToken);
|
|
244
|
-
setRefreshToken(tokenData.nextRefreshToken);
|
|
245
|
-
if (typeof onRefreshTokenSuccess === "function") {
|
|
246
|
-
onRefreshTokenSuccess(tokenData);
|
|
247
|
-
}
|
|
248
|
-
return service(originalRequest);
|
|
249
|
-
} catch (refreshTokenError) {
|
|
250
|
-
removeSessionToken();
|
|
251
|
-
removeRefreshToken();
|
|
252
|
-
if (typeof onRefreshTokenError === "function") {
|
|
253
|
-
onRefreshTokenError(refreshTokenError);
|
|
254
|
-
}
|
|
255
|
-
return Promise.reject(error);
|
|
256
|
-
} finally {
|
|
257
|
-
refreshingTokenRequest = null;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
return Promise.reject(error);
|
|
261
|
-
}
|
|
262
|
-
);
|
|
263
|
-
return service;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function createFetch$1(globalOptions = {}, requestParams) {
|
|
267
|
-
return async (input, options) => {
|
|
268
|
-
const startAt = Date.now();
|
|
269
|
-
const headers = {
|
|
270
|
-
...globalOptions?.headers,
|
|
271
|
-
...options?.headers,
|
|
272
|
-
"x-blocklet-js-sdk-version": version
|
|
273
|
-
};
|
|
274
|
-
const visitorId = getVisitorId();
|
|
275
|
-
if (![void 0, null].includes(visitorId)) {
|
|
276
|
-
headers["x-blocklet-visitor-id"] = visitorId;
|
|
277
|
-
}
|
|
278
|
-
const request = fetch(input, {
|
|
279
|
-
...globalOptions,
|
|
280
|
-
...options,
|
|
281
|
-
headers
|
|
282
|
-
});
|
|
283
|
-
try {
|
|
284
|
-
return request;
|
|
285
|
-
} catch (error) {
|
|
286
|
-
throw error;
|
|
287
|
-
} finally {
|
|
288
|
-
const endAt = Date.now();
|
|
289
|
-
if (requestParams?.lazy) {
|
|
290
|
-
const lazyTime = requestParams?.lazyTime ?? 300;
|
|
291
|
-
const timeDiff = endAt - startAt;
|
|
292
|
-
if (timeDiff < lazyTime)
|
|
293
|
-
await sleep(lazyTime - timeDiff);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
async function renewRefreshToken(refreshToken) {
|
|
299
|
-
if (!refreshToken) {
|
|
300
|
-
throw new Error("Refresh token not found");
|
|
301
|
-
}
|
|
302
|
-
const refreshApi = createFetch$1();
|
|
303
|
-
const res = await refreshApi(ufo.joinURL(constant.WELLKNOWN_SERVICE_PATH_PREFIX, "/api/did/refreshSession"), {
|
|
304
|
-
method: "POST",
|
|
305
|
-
headers: {
|
|
306
|
-
authorization: getBearerToken(refreshToken)
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
const data = await res.json();
|
|
310
|
-
return data;
|
|
311
|
-
}
|
|
312
|
-
function createRequest({
|
|
313
|
-
baseURL,
|
|
314
|
-
getSessionToken,
|
|
315
|
-
setSessionToken,
|
|
316
|
-
removeSessionToken,
|
|
317
|
-
getRefreshToken,
|
|
318
|
-
setRefreshToken,
|
|
319
|
-
removeRefreshToken,
|
|
320
|
-
onRefreshTokenError,
|
|
321
|
-
onRefreshTokenSuccess
|
|
322
|
-
}, requestOptions, requestParams) {
|
|
323
|
-
let refreshingTokenRequest = null;
|
|
324
|
-
const service = createFetch$1(requestOptions, requestParams);
|
|
325
|
-
return async (input, options) => {
|
|
326
|
-
let authorization;
|
|
327
|
-
const finalUrl = typeof input === "string" ? ufo.joinURL(baseURL, input) : input;
|
|
328
|
-
if (!Cookie__default.get(constant.SESSION_TOKEN_STORAGE_KEY)) {
|
|
329
|
-
const token = getSessionToken();
|
|
330
|
-
if (token) {
|
|
331
|
-
authorization = getBearerToken(token);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (refreshingTokenRequest) {
|
|
335
|
-
await refreshingTokenRequest;
|
|
336
|
-
}
|
|
337
|
-
const res = await service(finalUrl, {
|
|
338
|
-
...options,
|
|
339
|
-
headers: {
|
|
340
|
-
...options?.headers,
|
|
341
|
-
authorization
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
if (!res.ok && res.status === 401) {
|
|
345
|
-
refreshingTokenRequest = renewRefreshToken(getRefreshToken());
|
|
346
|
-
try {
|
|
347
|
-
const tokenData = await refreshingTokenRequest;
|
|
348
|
-
setSessionToken(tokenData.nextToken);
|
|
349
|
-
setRefreshToken(tokenData.nextRefreshToken);
|
|
350
|
-
if (typeof onRefreshTokenSuccess === "function") {
|
|
351
|
-
onRefreshTokenSuccess(tokenData);
|
|
352
|
-
}
|
|
353
|
-
return service(finalUrl, {
|
|
354
|
-
...options,
|
|
355
|
-
headers: {
|
|
356
|
-
...options?.headers,
|
|
357
|
-
authorization
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
} catch (error) {
|
|
361
|
-
removeSessionToken();
|
|
362
|
-
removeRefreshToken();
|
|
363
|
-
if (typeof onRefreshTokenError === "function") {
|
|
364
|
-
onRefreshTokenError(error);
|
|
365
|
-
}
|
|
366
|
-
return res;
|
|
367
|
-
} finally {
|
|
368
|
-
refreshingTokenRequest = null;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return res;
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
var __defProp = Object.defineProperty;
|
|
376
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
377
|
-
var __publicField = (obj, key, value) => {
|
|
378
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
379
|
-
return value;
|
|
380
|
-
};
|
|
381
|
-
class BlockletSDK {
|
|
382
|
-
constructor() {
|
|
383
|
-
__publicField(this, "api");
|
|
384
|
-
__publicField(this, "user");
|
|
385
|
-
__publicField(this, "userSession");
|
|
386
|
-
__publicField(this, "token");
|
|
387
|
-
const tokenService = new TokenService();
|
|
388
|
-
const internalApi = createRequest$1(
|
|
389
|
-
{
|
|
390
|
-
getSessionToken: tokenService.getSessionToken,
|
|
391
|
-
setSessionToken: tokenService.setSessionToken,
|
|
392
|
-
removeSessionToken: tokenService.removeSessionToken,
|
|
393
|
-
getRefreshToken: tokenService.getRefreshToken,
|
|
394
|
-
setRefreshToken: tokenService.setRefreshToken,
|
|
395
|
-
removeRefreshToken: tokenService.removeRefreshToken,
|
|
396
|
-
onRefreshTokenError: () => {
|
|
397
|
-
console.error("Failed to refresh token");
|
|
398
|
-
},
|
|
399
|
-
onRefreshTokenSuccess: () => {
|
|
400
|
-
}
|
|
401
|
-
},
|
|
402
|
-
{
|
|
403
|
-
baseURL: constant.WELLKNOWN_SERVICE_PATH_PREFIX
|
|
404
|
-
}
|
|
405
|
-
);
|
|
406
|
-
this.user = new AuthService({ api: internalApi });
|
|
407
|
-
this.userSession = new UserSessionService({ api: internalApi });
|
|
408
|
-
this.token = tokenService;
|
|
409
|
-
this.api = internalApi;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
function createAxios(config, requestParams) {
|
|
413
|
-
const tokenService = new TokenService();
|
|
414
|
-
return createRequest$1(
|
|
415
|
-
{
|
|
416
|
-
getSessionToken: tokenService.getSessionToken,
|
|
417
|
-
setSessionToken: tokenService.setSessionToken,
|
|
418
|
-
removeSessionToken: tokenService.removeSessionToken,
|
|
419
|
-
getRefreshToken: tokenService.getRefreshToken,
|
|
420
|
-
setRefreshToken: tokenService.setRefreshToken,
|
|
421
|
-
removeRefreshToken: tokenService.removeRefreshToken,
|
|
422
|
-
onRefreshTokenError: () => {
|
|
423
|
-
console.error("Failed to refresh token");
|
|
424
|
-
},
|
|
425
|
-
onRefreshTokenSuccess: () => {
|
|
426
|
-
}
|
|
427
|
-
},
|
|
428
|
-
config,
|
|
429
|
-
requestParams
|
|
430
|
-
);
|
|
431
|
-
}
|
|
432
|
-
function createFetch(options, requestParams) {
|
|
433
|
-
const tokenService = new TokenService();
|
|
434
|
-
return createRequest(
|
|
435
|
-
{
|
|
436
|
-
getSessionToken: tokenService.getSessionToken,
|
|
437
|
-
setSessionToken: tokenService.setSessionToken,
|
|
438
|
-
removeSessionToken: tokenService.removeSessionToken,
|
|
439
|
-
getRefreshToken: tokenService.getRefreshToken,
|
|
440
|
-
setRefreshToken: tokenService.setRefreshToken,
|
|
441
|
-
removeRefreshToken: tokenService.removeRefreshToken,
|
|
442
|
-
onRefreshTokenError: () => {
|
|
443
|
-
console.error("Failed to refresh token");
|
|
444
|
-
},
|
|
445
|
-
onRefreshTokenSuccess: () => {
|
|
446
|
-
}
|
|
447
|
-
},
|
|
448
|
-
options,
|
|
449
|
-
requestParams
|
|
450
|
-
);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
exports.BlockletSDK = BlockletSDK;
|
|
454
|
-
exports.createAxios = createAxios;
|
|
455
|
-
exports.createFetch = createFetch;
|
package/dist/index.d.cts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import * as axios from 'axios';
|
|
2
|
-
import { Axios, AxiosRequestConfig } from 'axios';
|
|
3
|
-
|
|
4
|
-
type UserPublicInfo = {
|
|
5
|
-
avatar: string;
|
|
6
|
-
did: string;
|
|
7
|
-
fullName: string;
|
|
8
|
-
};
|
|
9
|
-
type Webhook = {
|
|
10
|
-
type: 'slack' | 'api';
|
|
11
|
-
url: string;
|
|
12
|
-
};
|
|
13
|
-
type NotificationConfig = {
|
|
14
|
-
webhooks?: Webhook[];
|
|
15
|
-
notifications?: {
|
|
16
|
-
email?: boolean;
|
|
17
|
-
wallet?: boolean;
|
|
18
|
-
phone?: boolean;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
type PrivacyConfig = {
|
|
22
|
-
[key: string]: boolean;
|
|
23
|
-
};
|
|
24
|
-
declare class AuthService {
|
|
25
|
-
private api;
|
|
26
|
-
constructor({ api }: {
|
|
27
|
-
api: Axios;
|
|
28
|
-
});
|
|
29
|
-
getUserPublicInfo({ did }: {
|
|
30
|
-
did: string;
|
|
31
|
-
}): Promise<UserPublicInfo>;
|
|
32
|
-
getUserPrivacyConfig({ did }: {
|
|
33
|
-
did: string;
|
|
34
|
-
}): Promise<PrivacyConfig>;
|
|
35
|
-
saveUserPrivacyConfig(config: PrivacyConfig): Promise<PrivacyConfig>;
|
|
36
|
-
getUserNotificationConfig(): Promise<NotificationConfig>;
|
|
37
|
-
saveUserNotificationConfig(config: NotificationConfig): Promise<NotificationConfig>;
|
|
38
|
-
testNotificationWebhook(webhook: Webhook): Promise<{
|
|
39
|
-
success: boolean;
|
|
40
|
-
}>;
|
|
41
|
-
getProfileUrl({ did, locale }: {
|
|
42
|
-
did: string;
|
|
43
|
-
locale: string;
|
|
44
|
-
}): Promise<string>;
|
|
45
|
-
getProfile(): Promise<any>;
|
|
46
|
-
saveProfile({ locale }: {
|
|
47
|
-
locale: string;
|
|
48
|
-
}): Promise<any>;
|
|
49
|
-
/**
|
|
50
|
-
*
|
|
51
|
-
* 指定要退出登录的设备 id
|
|
52
|
-
* @param {{ visitorId: string }} { visitorId }
|
|
53
|
-
* @return {*} {Promise<void>}
|
|
54
|
-
*/
|
|
55
|
-
logout({ visitorId }: {
|
|
56
|
-
visitorId: string;
|
|
57
|
-
}): Promise<void>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
declare class TokenService {
|
|
61
|
-
getSessionToken(): string;
|
|
62
|
-
setSessionToken(value: string): void;
|
|
63
|
-
removeSessionToken(): void;
|
|
64
|
-
getRefreshToken(): string;
|
|
65
|
-
setRefreshToken(value: string): void;
|
|
66
|
-
removeRefreshToken(): void;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
type UserSessionUser = {
|
|
70
|
-
avatar: string;
|
|
71
|
-
did: string;
|
|
72
|
-
email: string;
|
|
73
|
-
fullName: string;
|
|
74
|
-
pk: string;
|
|
75
|
-
remark?: string;
|
|
76
|
-
role: string;
|
|
77
|
-
roleTitle: string;
|
|
78
|
-
sourceAppPid: string | null;
|
|
79
|
-
sourceProvider: 'wallet' | 'auth0' | 'nft';
|
|
80
|
-
};
|
|
81
|
-
type UserSession = {
|
|
82
|
-
appName: string;
|
|
83
|
-
appPid: string;
|
|
84
|
-
extra: {
|
|
85
|
-
walletOS: 'android' | 'ios' | 'web';
|
|
86
|
-
};
|
|
87
|
-
id: string;
|
|
88
|
-
lastLoginIp: string;
|
|
89
|
-
passportId: string | null;
|
|
90
|
-
ua: string;
|
|
91
|
-
createdAt?: string;
|
|
92
|
-
updatedAt: string;
|
|
93
|
-
status?: string;
|
|
94
|
-
user?: UserSessionUser;
|
|
95
|
-
userDid: string;
|
|
96
|
-
visitorId: string;
|
|
97
|
-
};
|
|
98
|
-
declare class UserSessionService {
|
|
99
|
-
private api;
|
|
100
|
-
constructor({ api }: {
|
|
101
|
-
api: Axios;
|
|
102
|
-
});
|
|
103
|
-
getUserSessions({ did }: {
|
|
104
|
-
did: string;
|
|
105
|
-
}): Promise<UserSession[]>;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
type RequestParams = {
|
|
109
|
-
lazy?: boolean;
|
|
110
|
-
lazyTime?: number;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
declare class BlockletSDK {
|
|
114
|
-
api: Axios;
|
|
115
|
-
user: AuthService;
|
|
116
|
-
userSession: UserSessionService;
|
|
117
|
-
token: TokenService;
|
|
118
|
-
constructor();
|
|
119
|
-
}
|
|
120
|
-
declare function createAxios(config?: AxiosRequestConfig, requestParams?: RequestParams): axios.AxiosInstance;
|
|
121
|
-
declare function createFetch(options?: RequestInit, requestParams?: RequestParams): (input: string | Request | URL, options?: RequestInit) => Promise<Response>;
|
|
122
|
-
|
|
123
|
-
export { AuthService, BlockletSDK, type NotificationConfig, type PrivacyConfig, TokenService, type UserPublicInfo, type UserSession, UserSessionService, type UserSessionUser, type Webhook, createAxios, createFetch };
|