@blocklet/js-sdk 1.16.26-beta-cca12425 → 1.16.26-beta-c8902aec

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.cjs CHANGED
@@ -56,6 +56,12 @@ class AuthService {
56
56
  locale
57
57
  });
58
58
  }
59
+ /**
60
+ *
61
+ * 指定要退出登录的设备 id
62
+ * @param {{ visitorId: string }} { visitorId }
63
+ * @return {*} {Promise<void>}
64
+ */
59
65
  async logout({ visitorId }) {
60
66
  const { data } = await this.api.post("/api/user/logout", {
61
67
  visitorId
@@ -118,6 +124,10 @@ const sleep = (time = 0) => {
118
124
  const getBearerToken = (token) => {
119
125
  return `Bearer ${encodeURIComponent(token)}`;
120
126
  };
127
+ const visitorIdKey = "__visitor_id";
128
+ const getVisitorId = () => {
129
+ return localStorage.getItem(visitorIdKey);
130
+ };
121
131
 
122
132
  async function sleepForLoading(config, lazyTime = 300) {
123
133
  config.metaData.endTime = +/* @__PURE__ */ new Date();
@@ -128,12 +138,17 @@ async function sleepForLoading(config, lazyTime = 300) {
128
138
  delete config.metaData;
129
139
  }
130
140
  const createAxios$1 = (options, requestParams) => {
141
+ const headers = {
142
+ ...options?.headers,
143
+ "x-blocklet-js-sdk-version": version
144
+ };
145
+ const visitorId = getVisitorId();
146
+ if (![void 0, null].includes(visitorId)) {
147
+ headers["x-blocklet-visitor-id"] = visitorId;
148
+ }
131
149
  const instance = axios__default.create({
132
150
  ...options,
133
- headers: {
134
- ...options?.headers,
135
- "x-blocklet-js-sdk-version": version
136
- }
151
+ headers
137
152
  });
138
153
  if (requestParams?.lazy) {
139
154
  instance.interceptors.request.use(
@@ -210,10 +225,10 @@ function createRequest$1({
210
225
  originalRequest.headers = { ...originalRequest?.headers };
211
226
  if (error?.response?.status === 401 && !originalRequest._retry) {
212
227
  originalRequest._retry = true;
213
- if (!refreshingTokenRequest) {
214
- refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
215
- }
216
228
  try {
229
+ if (!refreshingTokenRequest) {
230
+ refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
231
+ }
217
232
  const tokenData = await refreshingTokenRequest;
218
233
  setSessionToken(tokenData.nextToken);
219
234
  setRefreshToken(tokenData.nextRefreshToken);
@@ -221,13 +236,13 @@ function createRequest$1({
221
236
  onRefreshTokenSuccess(tokenData);
222
237
  }
223
238
  return service(originalRequest);
224
- } catch (error2) {
239
+ } catch (refreshTokenError) {
225
240
  removeSessionToken();
226
241
  removeRefreshToken();
227
242
  if (typeof onRefreshTokenError === "function") {
228
- onRefreshTokenError(error2);
243
+ onRefreshTokenError(refreshTokenError);
229
244
  }
230
- return Promise.reject(error2);
245
+ return Promise.reject(error);
231
246
  } finally {
232
247
  refreshingTokenRequest = null;
233
248
  }
@@ -241,14 +256,19 @@ function createRequest$1({
241
256
  function createFetch$1(globalOptions = {}, requestParams) {
242
257
  return async (input, options) => {
243
258
  const startAt = Date.now();
259
+ const headers = {
260
+ ...globalOptions?.headers,
261
+ ...options?.headers,
262
+ "x-blocklet-js-sdk-version": version
263
+ };
264
+ const visitorId = getVisitorId();
265
+ if (![void 0, null].includes(visitorId)) {
266
+ headers["x-blocklet-visitor-id"] = visitorId;
267
+ }
244
268
  const request = fetch(input, {
245
269
  ...globalOptions,
246
270
  ...options,
247
- headers: {
248
- ...globalOptions?.headers,
249
- ...options?.headers,
250
- "x-blocklet-js-sdk-version": version
251
- }
271
+ headers
252
272
  });
253
273
  try {
254
274
  return request;
@@ -333,7 +353,7 @@ function createRequest({
333
353
  if (typeof onRefreshTokenError === "function") {
334
354
  onRefreshTokenError(error);
335
355
  }
336
- return Promise.reject(error);
356
+ return res;
337
357
  } finally {
338
358
  refreshingTokenRequest = null;
339
359
  }
package/dist/index.d.cts CHANGED
@@ -42,6 +42,12 @@ declare class AuthService {
42
42
  did: string;
43
43
  locale: string;
44
44
  }): Promise<string>;
45
+ /**
46
+ *
47
+ * 指定要退出登录的设备 id
48
+ * @param {{ visitorId: string }} { visitorId }
49
+ * @return {*} {Promise<void>}
50
+ */
45
51
  logout({ visitorId }: {
46
52
  visitorId: string;
47
53
  }): Promise<void>;
package/dist/index.d.mts CHANGED
@@ -42,6 +42,12 @@ declare class AuthService {
42
42
  did: string;
43
43
  locale: string;
44
44
  }): Promise<string>;
45
+ /**
46
+ *
47
+ * 指定要退出登录的设备 id
48
+ * @param {{ visitorId: string }} { visitorId }
49
+ * @return {*} {Promise<void>}
50
+ */
45
51
  logout({ visitorId }: {
46
52
  visitorId: string;
47
53
  }): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -42,6 +42,12 @@ declare class AuthService {
42
42
  did: string;
43
43
  locale: string;
44
44
  }): Promise<string>;
45
+ /**
46
+ *
47
+ * 指定要退出登录的设备 id
48
+ * @param {{ visitorId: string }} { visitorId }
49
+ * @return {*} {Promise<void>}
50
+ */
45
51
  logout({ visitorId }: {
46
52
  visitorId: string;
47
53
  }): Promise<void>;
package/dist/index.mjs CHANGED
@@ -49,6 +49,12 @@ class AuthService {
49
49
  locale
50
50
  });
51
51
  }
52
+ /**
53
+ *
54
+ * 指定要退出登录的设备 id
55
+ * @param {{ visitorId: string }} { visitorId }
56
+ * @return {*} {Promise<void>}
57
+ */
52
58
  async logout({ visitorId }) {
53
59
  const { data } = await this.api.post("/api/user/logout", {
54
60
  visitorId
@@ -111,6 +117,10 @@ const sleep = (time = 0) => {
111
117
  const getBearerToken = (token) => {
112
118
  return `Bearer ${encodeURIComponent(token)}`;
113
119
  };
120
+ const visitorIdKey = "__visitor_id";
121
+ const getVisitorId = () => {
122
+ return localStorage.getItem(visitorIdKey);
123
+ };
114
124
 
115
125
  async function sleepForLoading(config, lazyTime = 300) {
116
126
  config.metaData.endTime = +/* @__PURE__ */ new Date();
@@ -121,12 +131,17 @@ async function sleepForLoading(config, lazyTime = 300) {
121
131
  delete config.metaData;
122
132
  }
123
133
  const createAxios$1 = (options, requestParams) => {
134
+ const headers = {
135
+ ...options?.headers,
136
+ "x-blocklet-js-sdk-version": version
137
+ };
138
+ const visitorId = getVisitorId();
139
+ if (![void 0, null].includes(visitorId)) {
140
+ headers["x-blocklet-visitor-id"] = visitorId;
141
+ }
124
142
  const instance = axios.create({
125
143
  ...options,
126
- headers: {
127
- ...options?.headers,
128
- "x-blocklet-js-sdk-version": version
129
- }
144
+ headers
130
145
  });
131
146
  if (requestParams?.lazy) {
132
147
  instance.interceptors.request.use(
@@ -203,10 +218,10 @@ function createRequest$1({
203
218
  originalRequest.headers = { ...originalRequest?.headers };
204
219
  if (error?.response?.status === 401 && !originalRequest._retry) {
205
220
  originalRequest._retry = true;
206
- if (!refreshingTokenRequest) {
207
- refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
208
- }
209
221
  try {
222
+ if (!refreshingTokenRequest) {
223
+ refreshingTokenRequest = renewRefreshToken$1(getRefreshToken());
224
+ }
210
225
  const tokenData = await refreshingTokenRequest;
211
226
  setSessionToken(tokenData.nextToken);
212
227
  setRefreshToken(tokenData.nextRefreshToken);
@@ -214,13 +229,13 @@ function createRequest$1({
214
229
  onRefreshTokenSuccess(tokenData);
215
230
  }
216
231
  return service(originalRequest);
217
- } catch (error2) {
232
+ } catch (refreshTokenError) {
218
233
  removeSessionToken();
219
234
  removeRefreshToken();
220
235
  if (typeof onRefreshTokenError === "function") {
221
- onRefreshTokenError(error2);
236
+ onRefreshTokenError(refreshTokenError);
222
237
  }
223
- return Promise.reject(error2);
238
+ return Promise.reject(error);
224
239
  } finally {
225
240
  refreshingTokenRequest = null;
226
241
  }
@@ -234,14 +249,19 @@ function createRequest$1({
234
249
  function createFetch$1(globalOptions = {}, requestParams) {
235
250
  return async (input, options) => {
236
251
  const startAt = Date.now();
252
+ const headers = {
253
+ ...globalOptions?.headers,
254
+ ...options?.headers,
255
+ "x-blocklet-js-sdk-version": version
256
+ };
257
+ const visitorId = getVisitorId();
258
+ if (![void 0, null].includes(visitorId)) {
259
+ headers["x-blocklet-visitor-id"] = visitorId;
260
+ }
237
261
  const request = fetch(input, {
238
262
  ...globalOptions,
239
263
  ...options,
240
- headers: {
241
- ...globalOptions?.headers,
242
- ...options?.headers,
243
- "x-blocklet-js-sdk-version": version
244
- }
264
+ headers
245
265
  });
246
266
  try {
247
267
  return request;
@@ -326,7 +346,7 @@ function createRequest({
326
346
  if (typeof onRefreshTokenError === "function") {
327
347
  onRefreshTokenError(error);
328
348
  }
329
- return Promise.reject(error);
349
+ return res;
330
350
  } finally {
331
351
  refreshingTokenRequest = null;
332
352
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/js-sdk",
3
- "version": "1.16.26-beta-cca12425",
3
+ "version": "1.16.26-beta-c8902aec",
4
4
  "main": "dist/index.cjs",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "build": "unbuild"
32
32
  },
33
33
  "dependencies": {
34
- "@abtnode/constant": "1.16.26-beta-cca12425",
34
+ "@abtnode/constant": "1.16.26-beta-c8902aec",
35
35
  "axios": "^0.27.2",
36
36
  "js-cookie": "^3.0.5",
37
37
  "ufo": "^1.3.2"
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "unbuild": "^2.0.0"
41
41
  },
42
- "gitHead": "fc2804fa8010d4311f1c74e65fb98d117598df2a"
42
+ "gitHead": "2b74f442ee04e89901d8f5d67a4017dea2b9f2ac"
43
43
  }