@kevisual/api 0.0.59 → 0.0.60

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.
@@ -312,6 +312,16 @@ class BaseQuery {
312
312
  }
313
313
 
314
314
  // query/query-login/login-cache.ts
315
+ var defaultCacheData = {
316
+ loginUsers: [],
317
+ user: undefined,
318
+ id: undefined,
319
+ accessToken: undefined,
320
+ refreshToken: undefined,
321
+ accessTokenExpiresIn: undefined,
322
+ createdAt: undefined
323
+ };
324
+
315
325
  class LoginCacheStore {
316
326
  cache;
317
327
  name;
@@ -321,13 +331,7 @@ class LoginCacheStore {
321
331
  throw new Error("cache is required");
322
332
  }
323
333
  this.cache = opts.cache;
324
- this.cacheData = {
325
- loginUsers: [],
326
- user: undefined,
327
- id: undefined,
328
- accessToken: undefined,
329
- refreshToken: undefined
330
- };
334
+ this.cacheData = { ...defaultCacheData };
331
335
  this.name = opts.name;
332
336
  }
333
337
  async setValue(value) {
@@ -337,27 +341,13 @@ class LoginCacheStore {
337
341
  }
338
342
  async delValue() {
339
343
  await this.cache.del();
340
- this.cacheData = {
341
- loginUsers: [],
342
- user: undefined,
343
- id: undefined,
344
- accessToken: undefined,
345
- refreshToken: undefined
346
- };
344
+ this.cacheData = { ...defaultCacheData };
347
345
  }
348
346
  getValue() {
349
347
  return this.cache.get(this.name);
350
348
  }
351
349
  async init() {
352
- const defaultData = {
353
- loginUsers: [],
354
- user: undefined,
355
- id: undefined,
356
- accessToken: undefined,
357
- refreshToken: undefined,
358
- accessTokenExpiresIn: undefined,
359
- createdAt: undefined
360
- };
350
+ const defaultData = { ...this.cacheData };
361
351
  if (this.cache.init) {
362
352
  try {
363
353
  const cacheData = await this.cache.init();
@@ -370,18 +360,18 @@ class LoginCacheStore {
370
360
  }
371
361
  return this.cacheData;
372
362
  }
373
- async setLoginUser(user) {
374
- const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
363
+ async setLoginUser(loginUser) {
364
+ const has = this.cacheData.loginUsers.find((u) => u.id === loginUser.id);
375
365
  if (has) {
376
- this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
377
- }
378
- this.cacheData.loginUsers.push(user);
379
- this.cacheData.user = user.user;
380
- this.cacheData.id = user.id;
381
- this.cacheData.accessToken = user.accessToken;
382
- this.cacheData.refreshToken = user.refreshToken;
383
- this.cacheData.accessTokenExpiresIn = user.accessTokenExpiresIn;
384
- this.cacheData.createdAt = user.createdAt;
366
+ this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== loginUser.id);
367
+ }
368
+ this.cacheData.loginUsers.push(loginUser);
369
+ this.cacheData.user = loginUser.user;
370
+ this.cacheData.id = loginUser.id;
371
+ this.cacheData.accessToken = loginUser.accessToken;
372
+ this.cacheData.refreshToken = loginUser.refreshToken;
373
+ this.cacheData.accessTokenExpiresIn = loginUser.accessTokenExpiresIn;
374
+ this.cacheData.createdAt = loginUser.createdAt;
385
375
  await this.setValue(this.cacheData);
386
376
  }
387
377
  getCurrentUser() {
@@ -417,22 +407,22 @@ class LoginCacheStore {
417
407
  if (has) {
418
408
  this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
419
409
  }
420
- this.cacheData.user = undefined;
421
- this.cacheData.id = undefined;
422
- this.cacheData.accessToken = undefined;
423
- this.cacheData.refreshToken = undefined;
424
- this.cacheData.accessTokenExpiresIn = undefined;
425
- this.cacheData.createdAt = undefined;
410
+ const hasOther = this.cacheData.loginUsers.length > 0;
411
+ const current = this.cacheData.loginUsers[this.cacheData.loginUsers.length - 1];
412
+ if (hasOther && current) {
413
+ this.cacheData.user = current.user;
414
+ this.cacheData.id = current.id;
415
+ this.cacheData.accessToken = current.accessToken;
416
+ this.cacheData.refreshToken = current.refreshToken;
417
+ this.cacheData.accessTokenExpiresIn = current.accessTokenExpiresIn;
418
+ this.cacheData.createdAt = current.createdAt;
419
+ } else {
420
+ this.cacheData = { ...defaultCacheData };
421
+ }
426
422
  await this.setValue(this.cacheData);
427
423
  }
428
424
  async clearAll() {
429
- this.cacheData.loginUsers = [];
430
- this.cacheData.user = undefined;
431
- this.cacheData.id = undefined;
432
- this.cacheData.accessToken = undefined;
433
- this.cacheData.refreshToken = undefined;
434
- this.cacheData.accessTokenExpiresIn = undefined;
435
- this.cacheData.createdAt = undefined;
425
+ this.cacheData = { ...defaultCacheData };
436
426
  await this.setValue(this.cacheData);
437
427
  }
438
428
  }
@@ -1023,7 +1013,7 @@ class QueryLogin extends BaseQuery {
1023
1013
  async queryRefreshToken(opts) {
1024
1014
  const refreshToken = opts?.refreshToken;
1025
1015
  let accessToken = opts?.accessToken;
1026
- const _refreshToken = refreshToken || await this.cacheStore.getRefreshToken();
1016
+ const _refreshToken = refreshToken ?? await this.cacheStore.getRefreshToken();
1027
1017
  let data = {};
1028
1018
  if (accessToken) {
1029
1019
  data.accessToken = accessToken;
@@ -1134,6 +1124,7 @@ class QueryLogin extends BaseQuery {
1134
1124
  }
1135
1125
  const isExpired = await this.cacheStore.getIsExpired();
1136
1126
  if (isExpired) {
1127
+ console.log("token过期,正在刷新token", this.cacheStore.cacheData);
1137
1128
  const res = await this.refreshLoginUser();
1138
1129
  if (res.code === 200) {
1139
1130
  return res.data?.accessToken || null;
@@ -312,6 +312,16 @@ class BaseQuery {
312
312
  }
313
313
 
314
314
  // query/query-login/login-cache.ts
315
+ var defaultCacheData = {
316
+ loginUsers: [],
317
+ user: undefined,
318
+ id: undefined,
319
+ accessToken: undefined,
320
+ refreshToken: undefined,
321
+ accessTokenExpiresIn: undefined,
322
+ createdAt: undefined
323
+ };
324
+
315
325
  class LoginCacheStore {
316
326
  cache;
317
327
  name;
@@ -321,13 +331,7 @@ class LoginCacheStore {
321
331
  throw new Error("cache is required");
322
332
  }
323
333
  this.cache = opts.cache;
324
- this.cacheData = {
325
- loginUsers: [],
326
- user: undefined,
327
- id: undefined,
328
- accessToken: undefined,
329
- refreshToken: undefined
330
- };
334
+ this.cacheData = { ...defaultCacheData };
331
335
  this.name = opts.name;
332
336
  }
333
337
  async setValue(value) {
@@ -337,27 +341,13 @@ class LoginCacheStore {
337
341
  }
338
342
  async delValue() {
339
343
  await this.cache.del();
340
- this.cacheData = {
341
- loginUsers: [],
342
- user: undefined,
343
- id: undefined,
344
- accessToken: undefined,
345
- refreshToken: undefined
346
- };
344
+ this.cacheData = { ...defaultCacheData };
347
345
  }
348
346
  getValue() {
349
347
  return this.cache.get(this.name);
350
348
  }
351
349
  async init() {
352
- const defaultData = {
353
- loginUsers: [],
354
- user: undefined,
355
- id: undefined,
356
- accessToken: undefined,
357
- refreshToken: undefined,
358
- accessTokenExpiresIn: undefined,
359
- createdAt: undefined
360
- };
350
+ const defaultData = { ...this.cacheData };
361
351
  if (this.cache.init) {
362
352
  try {
363
353
  const cacheData = await this.cache.init();
@@ -370,18 +360,18 @@ class LoginCacheStore {
370
360
  }
371
361
  return this.cacheData;
372
362
  }
373
- async setLoginUser(user) {
374
- const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
363
+ async setLoginUser(loginUser) {
364
+ const has = this.cacheData.loginUsers.find((u) => u.id === loginUser.id);
375
365
  if (has) {
376
- this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
377
- }
378
- this.cacheData.loginUsers.push(user);
379
- this.cacheData.user = user.user;
380
- this.cacheData.id = user.id;
381
- this.cacheData.accessToken = user.accessToken;
382
- this.cacheData.refreshToken = user.refreshToken;
383
- this.cacheData.accessTokenExpiresIn = user.accessTokenExpiresIn;
384
- this.cacheData.createdAt = user.createdAt;
366
+ this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== loginUser.id);
367
+ }
368
+ this.cacheData.loginUsers.push(loginUser);
369
+ this.cacheData.user = loginUser.user;
370
+ this.cacheData.id = loginUser.id;
371
+ this.cacheData.accessToken = loginUser.accessToken;
372
+ this.cacheData.refreshToken = loginUser.refreshToken;
373
+ this.cacheData.accessTokenExpiresIn = loginUser.accessTokenExpiresIn;
374
+ this.cacheData.createdAt = loginUser.createdAt;
385
375
  await this.setValue(this.cacheData);
386
376
  }
387
377
  getCurrentUser() {
@@ -417,22 +407,22 @@ class LoginCacheStore {
417
407
  if (has) {
418
408
  this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
419
409
  }
420
- this.cacheData.user = undefined;
421
- this.cacheData.id = undefined;
422
- this.cacheData.accessToken = undefined;
423
- this.cacheData.refreshToken = undefined;
424
- this.cacheData.accessTokenExpiresIn = undefined;
425
- this.cacheData.createdAt = undefined;
410
+ const hasOther = this.cacheData.loginUsers.length > 0;
411
+ const current = this.cacheData.loginUsers[this.cacheData.loginUsers.length - 1];
412
+ if (hasOther && current) {
413
+ this.cacheData.user = current.user;
414
+ this.cacheData.id = current.id;
415
+ this.cacheData.accessToken = current.accessToken;
416
+ this.cacheData.refreshToken = current.refreshToken;
417
+ this.cacheData.accessTokenExpiresIn = current.accessTokenExpiresIn;
418
+ this.cacheData.createdAt = current.createdAt;
419
+ } else {
420
+ this.cacheData = { ...defaultCacheData };
421
+ }
426
422
  await this.setValue(this.cacheData);
427
423
  }
428
424
  async clearAll() {
429
- this.cacheData.loginUsers = [];
430
- this.cacheData.user = undefined;
431
- this.cacheData.id = undefined;
432
- this.cacheData.accessToken = undefined;
433
- this.cacheData.refreshToken = undefined;
434
- this.cacheData.accessTokenExpiresIn = undefined;
435
- this.cacheData.createdAt = undefined;
425
+ this.cacheData = { ...defaultCacheData };
436
426
  await this.setValue(this.cacheData);
437
427
  }
438
428
  }
@@ -1023,7 +1013,7 @@ class QueryLogin extends BaseQuery {
1023
1013
  async queryRefreshToken(opts) {
1024
1014
  const refreshToken = opts?.refreshToken;
1025
1015
  let accessToken = opts?.accessToken;
1026
- const _refreshToken = refreshToken || await this.cacheStore.getRefreshToken();
1016
+ const _refreshToken = refreshToken ?? await this.cacheStore.getRefreshToken();
1027
1017
  let data = {};
1028
1018
  if (accessToken) {
1029
1019
  data.accessToken = accessToken;
@@ -1134,6 +1124,7 @@ class QueryLogin extends BaseQuery {
1134
1124
  }
1135
1125
  const isExpired = await this.cacheStore.getIsExpired();
1136
1126
  if (isExpired) {
1127
+ console.log("token过期,正在刷新token", this.cacheStore.cacheData);
1137
1128
  const res = await this.refreshLoginUser();
1138
1129
  if (res.code === 200) {
1139
1130
  return res.data?.accessToken || null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/api",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "description": "",
5
5
  "main": "mod.ts",
6
6
  "scripts": {
@@ -90,6 +90,15 @@ export type LoginCacheStoreOpts<T extends Cache = Cache> = {
90
90
  name: string;
91
91
  cache: T;
92
92
  };
93
+ const defaultCacheData: CacheLogin = {
94
+ loginUsers: [],
95
+ user: undefined,
96
+ id: undefined,
97
+ accessToken: undefined,
98
+ refreshToken: undefined,
99
+ accessTokenExpiresIn: undefined,
100
+ createdAt: undefined,
101
+ }
93
102
  export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
94
103
  cache: T;
95
104
  name: string;
@@ -100,12 +109,16 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
100
109
  }
101
110
  // @ts-ignore
102
111
  this.cache = opts.cache;
103
- this.cacheData = {
104
- loginUsers: [],
105
- user: undefined,
106
- id: undefined,
107
- accessToken: undefined,
108
- refreshToken: undefined,
112
+ this.cacheData = { ...defaultCacheData };
113
+ this.name = opts.name;
114
+ }
115
+ /**
116
+ * 设置缓存
117
+ * @param key
118
+ * @param value
119
+ * @returns
120
+ accessTokenExpiresIn: undefined,
121
+ createdAt: undefined,
109
122
  };
110
123
  this.name = opts.name;
111
124
  }
@@ -125,13 +138,7 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
125
138
  */
126
139
  async delValue() {
127
140
  await this.cache.del();
128
- this.cacheData = {
129
- loginUsers: [],
130
- user: undefined,
131
- id: undefined,
132
- accessToken: undefined,
133
- refreshToken: undefined,
134
- };
141
+ this.cacheData = { ...defaultCacheData };
135
142
  }
136
143
  getValue(): Promise<CacheLogin> {
137
144
  return this.cache.get(this.name);
@@ -140,15 +147,7 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
140
147
  * 初始化,设置默认值
141
148
  */
142
149
  async init() {
143
- const defaultData: CacheLogin = {
144
- loginUsers: [],
145
- user: undefined,
146
- id: undefined,
147
- accessToken: undefined,
148
- refreshToken: undefined,
149
- accessTokenExpiresIn: undefined,
150
- createdAt: undefined,
151
- };
150
+ const defaultData: CacheLogin = { ...this.cacheData };
152
151
  if (this.cache.init) {
153
152
  try {
154
153
  const cacheData = await this.cache.init();
@@ -165,18 +164,18 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
165
164
  * 设置当前用户
166
165
  * @param user
167
166
  */
168
- async setLoginUser(user: CacheLoginUser) {
169
- const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
167
+ async setLoginUser(loginUser: CacheLoginUser) {
168
+ const has = this.cacheData.loginUsers.find((u) => u.id === loginUser.id);
170
169
  if (has) {
171
- this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
170
+ this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== loginUser.id);
172
171
  }
173
- this.cacheData.loginUsers.push(user);
174
- this.cacheData.user = user.user;
175
- this.cacheData.id = user.id;
176
- this.cacheData.accessToken = user.accessToken;
177
- this.cacheData.refreshToken = user.refreshToken;
178
- this.cacheData.accessTokenExpiresIn = user.accessTokenExpiresIn;
179
- this.cacheData.createdAt = user.createdAt;
172
+ this.cacheData.loginUsers.push(loginUser);
173
+ this.cacheData.user = loginUser.user;
174
+ this.cacheData.id = loginUser.id;
175
+ this.cacheData.accessToken = loginUser.accessToken;
176
+ this.cacheData.refreshToken = loginUser.refreshToken;
177
+ this.cacheData.accessTokenExpiresIn = loginUser.accessTokenExpiresIn;
178
+ this.cacheData.createdAt = loginUser.createdAt;
180
179
  await this.setValue(this.cacheData);
181
180
  }
182
181
 
@@ -214,22 +213,22 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
214
213
  if (has) {
215
214
  this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
216
215
  }
217
- this.cacheData.user = undefined;
218
- this.cacheData.id = undefined;
219
- this.cacheData.accessToken = undefined;
220
- this.cacheData.refreshToken = undefined;
221
- this.cacheData.accessTokenExpiresIn = undefined;
222
- this.cacheData.createdAt = undefined;
216
+ const hasOther = this.cacheData.loginUsers.length > 0;
217
+ const current = this.cacheData.loginUsers[this.cacheData.loginUsers.length - 1];
218
+ if (hasOther && current) {
219
+ this.cacheData.user = current.user;
220
+ this.cacheData.id = current.id;
221
+ this.cacheData.accessToken = current.accessToken;
222
+ this.cacheData.refreshToken = current.refreshToken;
223
+ this.cacheData.accessTokenExpiresIn = current.accessTokenExpiresIn;
224
+ this.cacheData.createdAt = current.createdAt;
225
+ } else {
226
+ this.cacheData = { ...defaultCacheData };
227
+ }
223
228
  await this.setValue(this.cacheData);
224
229
  }
225
230
  async clearAll() {
226
- this.cacheData.loginUsers = [];
227
- this.cacheData.user = undefined;
228
- this.cacheData.id = undefined;
229
- this.cacheData.accessToken = undefined;
230
- this.cacheData.refreshToken = undefined;
231
- this.cacheData.accessTokenExpiresIn = undefined;
232
- this.cacheData.createdAt = undefined;
231
+ this.cacheData = { ...defaultCacheData };
233
232
  await this.setValue(this.cacheData);
234
233
  }
235
234
  }
@@ -196,7 +196,7 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
196
196
  async queryRefreshToken(opts?: { refreshToken?: string, accessToken?: string }) {
197
197
  const refreshToken = opts?.refreshToken;
198
198
  let accessToken = opts?.accessToken;
199
- const _refreshToken = refreshToken || (await this.cacheStore.getRefreshToken());
199
+ const _refreshToken = refreshToken ?? (await this.cacheStore.getRefreshToken());
200
200
  let data: any = {};
201
201
  if (accessToken) {
202
202
  data.accessToken = accessToken;
@@ -360,6 +360,7 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
360
360
  }
361
361
  const isExpired = await this.cacheStore.getIsExpired();
362
362
  if (isExpired) {
363
+ console.log('token过期,正在刷新token', this.cacheStore.cacheData);
363
364
  const res = await this.refreshLoginUser()
364
365
  if (res.code === 200) {
365
366
  // 刷新成功,返回新的token
@@ -0,0 +1,10 @@
1
+ const cacheData = {
2
+ accessTokenExpiresIn: 604800,
3
+ createdAt: 1771926793545
4
+ };
5
+
6
+ const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
7
+ console.log('expiresIn', expiresIn);
8
+ const now = Date.now();
9
+ console.log('now', now);
10
+ console.log('isExpired', now >= expiresIn);