@kevisual/api 0.0.54 → 0.0.56
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/query-login-node.d.ts +12 -12
- package/dist/query-login-node.js +35 -14
- package/dist/query-login.d.ts +9 -7
- package/dist/query-login.js +33 -4
- package/package.json +1 -1
- package/query/query-login/login-cache.ts +39 -12
- package/query/query-login/login-node-cache.ts +2 -8
- package/query/query-login/query-login-node.ts +1 -1
- package/query/query-login/query-login.ts +10 -6
|
@@ -39,7 +39,7 @@ type CacheLoginUser = {
|
|
|
39
39
|
type CacheLogin = {
|
|
40
40
|
loginUsers: CacheLoginUser[];
|
|
41
41
|
} & CacheLoginUser;
|
|
42
|
-
type CacheStore<T = Cache> = {
|
|
42
|
+
type CacheStore<T extends Cache = Cache> = {
|
|
43
43
|
name: string;
|
|
44
44
|
/**
|
|
45
45
|
* 缓存数据
|
|
@@ -81,15 +81,16 @@ type CacheStore<T = Cache> = {
|
|
|
81
81
|
getValue(): Promise<CacheLogin>;
|
|
82
82
|
setValue(value: CacheLogin): Promise<CacheLogin>;
|
|
83
83
|
delValue(): Promise<void>;
|
|
84
|
-
init(): Promise<
|
|
84
|
+
init(): Promise<CacheLogin>;
|
|
85
|
+
getIsExpired(): Promise<boolean>;
|
|
85
86
|
};
|
|
86
87
|
|
|
87
|
-
type QueryLoginOpts = {
|
|
88
|
+
type QueryLoginOpts<T extends Cache = Cache> = {
|
|
88
89
|
query?: Query;
|
|
89
90
|
isBrowser?: boolean;
|
|
90
91
|
onLoad?: () => void;
|
|
91
92
|
storage?: Storage;
|
|
92
|
-
cache:
|
|
93
|
+
cache: T;
|
|
93
94
|
};
|
|
94
95
|
type QueryLoginData = {
|
|
95
96
|
username?: string;
|
|
@@ -100,16 +101,16 @@ type QueryLoginResult = {
|
|
|
100
101
|
accessToken: string;
|
|
101
102
|
refreshToken: string;
|
|
102
103
|
};
|
|
103
|
-
declare class QueryLogin extends BaseQuery {
|
|
104
|
+
declare class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
|
104
105
|
/**
|
|
105
106
|
* query login cache, 非实际操作, 一个cache的包裹模块
|
|
106
107
|
*/
|
|
107
|
-
cacheStore: CacheStore
|
|
108
|
+
cacheStore: CacheStore<T>;
|
|
108
109
|
isBrowser: boolean;
|
|
109
110
|
load?: boolean;
|
|
110
111
|
storage: Storage;
|
|
111
112
|
onLoad?: () => void;
|
|
112
|
-
constructor(opts?: QueryLoginOpts);
|
|
113
|
+
constructor(opts?: QueryLoginOpts<T>);
|
|
113
114
|
setQuery(query: Query): void;
|
|
114
115
|
private init;
|
|
115
116
|
post<T = any>(data: any, opts?: DataOpts): Promise<any>;
|
|
@@ -228,6 +229,7 @@ declare class QueryLogin extends BaseQuery {
|
|
|
228
229
|
* @returns
|
|
229
230
|
*/
|
|
230
231
|
checkLocalToken(): Promise<boolean>;
|
|
232
|
+
checkTokenValid(): Promise<boolean>;
|
|
231
233
|
/**
|
|
232
234
|
* 检查本地用户列表
|
|
233
235
|
* @returns
|
|
@@ -299,7 +301,6 @@ declare class StorageNode implements Storage {
|
|
|
299
301
|
cacheData: any;
|
|
300
302
|
filePath: string;
|
|
301
303
|
hostname: string;
|
|
302
|
-
isLoaded: boolean;
|
|
303
304
|
constructor(opts?: {
|
|
304
305
|
baseURL?: string;
|
|
305
306
|
load?: boolean;
|
|
@@ -307,7 +308,7 @@ declare class StorageNode implements Storage {
|
|
|
307
308
|
setHostName(hostname: string, opts?: {
|
|
308
309
|
load?: boolean;
|
|
309
310
|
}): void;
|
|
310
|
-
loadCache(
|
|
311
|
+
loadCache(): void;
|
|
311
312
|
get length(): number;
|
|
312
313
|
getItem(key: string): any;
|
|
313
314
|
setItem(key: string, value: any): void;
|
|
@@ -317,7 +318,6 @@ declare class StorageNode implements Storage {
|
|
|
317
318
|
}
|
|
318
319
|
declare class LoginNodeCache implements Cache {
|
|
319
320
|
filepath: string;
|
|
320
|
-
isLoaded: boolean;
|
|
321
321
|
constructor(opts?: {
|
|
322
322
|
baseURL?: string;
|
|
323
323
|
load?: boolean;
|
|
@@ -325,14 +325,14 @@ declare class LoginNodeCache implements Cache {
|
|
|
325
325
|
get(_key: string): Promise<any>;
|
|
326
326
|
set(_key: string, value: any): Promise<void>;
|
|
327
327
|
del(): Promise<void>;
|
|
328
|
-
loadCache(filePath: string
|
|
328
|
+
loadCache(filePath: string): any;
|
|
329
329
|
init(): any;
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
type QueryLoginNodeOptsWithoutCache = Omit<QueryLoginOpts, 'cache'>;
|
|
333
333
|
|
|
334
334
|
declare const cache: LoginNodeCache;
|
|
335
|
-
declare class QueryLoginNode extends QueryLogin {
|
|
335
|
+
declare class QueryLoginNode extends QueryLogin<LoginNodeCache> {
|
|
336
336
|
storage: StorageNode;
|
|
337
337
|
constructor(opts: QueryLoginNodeOptsWithoutCache);
|
|
338
338
|
}
|
package/dist/query-login-node.js
CHANGED
|
@@ -337,6 +337,13 @@ class LoginCacheStore {
|
|
|
337
337
|
}
|
|
338
338
|
async delValue() {
|
|
339
339
|
await this.cache.del();
|
|
340
|
+
this.cacheData = {
|
|
341
|
+
loginUsers: [],
|
|
342
|
+
user: undefined,
|
|
343
|
+
id: undefined,
|
|
344
|
+
accessToken: undefined,
|
|
345
|
+
refreshToken: undefined
|
|
346
|
+
};
|
|
340
347
|
}
|
|
341
348
|
getValue() {
|
|
342
349
|
return this.cache.get(this.name);
|
|
@@ -344,10 +351,12 @@ class LoginCacheStore {
|
|
|
344
351
|
async init() {
|
|
345
352
|
const defaultData = {
|
|
346
353
|
loginUsers: [],
|
|
347
|
-
user:
|
|
348
|
-
id:
|
|
349
|
-
accessToken:
|
|
350
|
-
refreshToken:
|
|
354
|
+
user: undefined,
|
|
355
|
+
id: undefined,
|
|
356
|
+
accessToken: undefined,
|
|
357
|
+
refreshToken: undefined,
|
|
358
|
+
accessTokenExpiresIn: undefined,
|
|
359
|
+
createdAt: undefined
|
|
351
360
|
};
|
|
352
361
|
if (this.cache.init) {
|
|
353
362
|
try {
|
|
@@ -359,6 +368,7 @@ class LoginCacheStore {
|
|
|
359
368
|
} else {
|
|
360
369
|
this.cacheData = await this.getValue() || defaultData;
|
|
361
370
|
}
|
|
371
|
+
return this.cacheData;
|
|
362
372
|
}
|
|
363
373
|
async setLoginUser(user) {
|
|
364
374
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
@@ -389,6 +399,18 @@ class LoginCacheStore {
|
|
|
389
399
|
const cacheData = this.cacheData;
|
|
390
400
|
return Promise.resolve(cacheData.accessToken || "");
|
|
391
401
|
}
|
|
402
|
+
getIsExpired() {
|
|
403
|
+
const cacheData = this.cacheData;
|
|
404
|
+
if (!cacheData.accessToken) {
|
|
405
|
+
return Promise.resolve(true);
|
|
406
|
+
}
|
|
407
|
+
if (!cacheData.createdAt || !cacheData.accessTokenExpiresIn) {
|
|
408
|
+
return Promise.resolve(false);
|
|
409
|
+
}
|
|
410
|
+
const now = Date.now();
|
|
411
|
+
const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
|
|
412
|
+
return Promise.resolve(now >= expiresIn);
|
|
413
|
+
}
|
|
392
414
|
async clearCurrentUser() {
|
|
393
415
|
const user = await this.getCurrentUser();
|
|
394
416
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
@@ -399,6 +421,8 @@ class LoginCacheStore {
|
|
|
399
421
|
this.cacheData.id = undefined;
|
|
400
422
|
this.cacheData.accessToken = undefined;
|
|
401
423
|
this.cacheData.refreshToken = undefined;
|
|
424
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
425
|
+
this.cacheData.createdAt = undefined;
|
|
402
426
|
await this.setValue(this.cacheData);
|
|
403
427
|
}
|
|
404
428
|
async clearAll() {
|
|
@@ -407,6 +431,8 @@ class LoginCacheStore {
|
|
|
407
431
|
this.cacheData.id = undefined;
|
|
408
432
|
this.cacheData.accessToken = undefined;
|
|
409
433
|
this.cacheData.refreshToken = undefined;
|
|
434
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
435
|
+
this.cacheData.createdAt = undefined;
|
|
410
436
|
await this.setValue(this.cacheData);
|
|
411
437
|
}
|
|
412
438
|
}
|
|
@@ -1063,6 +1089,9 @@ class QueryLogin extends BaseQuery {
|
|
|
1063
1089
|
const token = this.storage.getItem("token");
|
|
1064
1090
|
return !!token;
|
|
1065
1091
|
}
|
|
1092
|
+
async checkTokenValid() {
|
|
1093
|
+
return this.cacheStore.getIsExpired();
|
|
1094
|
+
}
|
|
1066
1095
|
async getToken() {
|
|
1067
1096
|
const token = this.storage.getItem("token");
|
|
1068
1097
|
return token || "";
|
|
@@ -1244,7 +1273,6 @@ class StorageNode {
|
|
|
1244
1273
|
cacheData;
|
|
1245
1274
|
filePath = "";
|
|
1246
1275
|
hostname = "";
|
|
1247
|
-
isLoaded = false;
|
|
1248
1276
|
constructor(opts) {
|
|
1249
1277
|
this.cacheData = {};
|
|
1250
1278
|
const hostname = getHostName(opts?.baseURL);
|
|
@@ -1263,14 +1291,11 @@ class StorageNode {
|
|
|
1263
1291
|
this.loadCache();
|
|
1264
1292
|
}
|
|
1265
1293
|
}
|
|
1266
|
-
loadCache(
|
|
1267
|
-
if (this.isLoaded && !force)
|
|
1268
|
-
return;
|
|
1294
|
+
loadCache() {
|
|
1269
1295
|
const filePath = this.filePath;
|
|
1270
1296
|
try {
|
|
1271
1297
|
const data = readConfigFile(filePath);
|
|
1272
1298
|
this.cacheData = data;
|
|
1273
|
-
this.isLoaded = true;
|
|
1274
1299
|
} catch (error) {
|
|
1275
1300
|
this.cacheData = {};
|
|
1276
1301
|
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
|
|
@@ -1301,7 +1326,6 @@ class StorageNode {
|
|
|
1301
1326
|
|
|
1302
1327
|
class LoginNodeCache {
|
|
1303
1328
|
filepath;
|
|
1304
|
-
isLoaded = false;
|
|
1305
1329
|
constructor(opts) {
|
|
1306
1330
|
this.filepath = join(homedir(), ".config", "envision", "config", `${getHostName(opts?.baseURL)}-login.json`);
|
|
1307
1331
|
fileExists(this.filepath, { isFile: true });
|
|
@@ -1331,13 +1355,10 @@ class LoginNodeCache {
|
|
|
1331
1355
|
async del() {
|
|
1332
1356
|
unlinkSync(this.filepath);
|
|
1333
1357
|
}
|
|
1334
|
-
loadCache(filePath
|
|
1335
|
-
if (this.isLoaded && !force)
|
|
1336
|
-
return;
|
|
1358
|
+
loadCache(filePath) {
|
|
1337
1359
|
try {
|
|
1338
1360
|
const data = readFileSync(filePath, "utf-8");
|
|
1339
1361
|
const jsonData = JSON.parse(data);
|
|
1340
|
-
this.isLoaded = true;
|
|
1341
1362
|
return jsonData;
|
|
1342
1363
|
} catch (error) {
|
|
1343
1364
|
console.log("create new cache file:", filePath);
|
package/dist/query-login.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ type CacheLoginUser = {
|
|
|
39
39
|
type CacheLogin = {
|
|
40
40
|
loginUsers: CacheLoginUser[];
|
|
41
41
|
} & CacheLoginUser;
|
|
42
|
-
type CacheStore<T = Cache> = {
|
|
42
|
+
type CacheStore<T extends Cache = Cache> = {
|
|
43
43
|
name: string;
|
|
44
44
|
/**
|
|
45
45
|
* 缓存数据
|
|
@@ -81,15 +81,16 @@ type CacheStore<T = Cache> = {
|
|
|
81
81
|
getValue(): Promise<CacheLogin>;
|
|
82
82
|
setValue(value: CacheLogin): Promise<CacheLogin>;
|
|
83
83
|
delValue(): Promise<void>;
|
|
84
|
-
init(): Promise<
|
|
84
|
+
init(): Promise<CacheLogin>;
|
|
85
|
+
getIsExpired(): Promise<boolean>;
|
|
85
86
|
};
|
|
86
87
|
|
|
87
|
-
type QueryLoginOpts = {
|
|
88
|
+
type QueryLoginOpts<T extends Cache = Cache> = {
|
|
88
89
|
query?: Query;
|
|
89
90
|
isBrowser?: boolean;
|
|
90
91
|
onLoad?: () => void;
|
|
91
92
|
storage?: Storage;
|
|
92
|
-
cache:
|
|
93
|
+
cache: T;
|
|
93
94
|
};
|
|
94
95
|
type QueryLoginData = {
|
|
95
96
|
username?: string;
|
|
@@ -100,16 +101,16 @@ type QueryLoginResult = {
|
|
|
100
101
|
accessToken: string;
|
|
101
102
|
refreshToken: string;
|
|
102
103
|
};
|
|
103
|
-
declare class QueryLogin extends BaseQuery {
|
|
104
|
+
declare class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
|
104
105
|
/**
|
|
105
106
|
* query login cache, 非实际操作, 一个cache的包裹模块
|
|
106
107
|
*/
|
|
107
|
-
cacheStore: CacheStore
|
|
108
|
+
cacheStore: CacheStore<T>;
|
|
108
109
|
isBrowser: boolean;
|
|
109
110
|
load?: boolean;
|
|
110
111
|
storage: Storage;
|
|
111
112
|
onLoad?: () => void;
|
|
112
|
-
constructor(opts?: QueryLoginOpts);
|
|
113
|
+
constructor(opts?: QueryLoginOpts<T>);
|
|
113
114
|
setQuery(query: Query): void;
|
|
114
115
|
private init;
|
|
115
116
|
post<T = any>(data: any, opts?: DataOpts): Promise<any>;
|
|
@@ -228,6 +229,7 @@ declare class QueryLogin extends BaseQuery {
|
|
|
228
229
|
* @returns
|
|
229
230
|
*/
|
|
230
231
|
checkLocalToken(): Promise<boolean>;
|
|
232
|
+
checkTokenValid(): Promise<boolean>;
|
|
231
233
|
/**
|
|
232
234
|
* 检查本地用户列表
|
|
233
235
|
* @returns
|
package/dist/query-login.js
CHANGED
|
@@ -337,6 +337,13 @@ class LoginCacheStore {
|
|
|
337
337
|
}
|
|
338
338
|
async delValue() {
|
|
339
339
|
await this.cache.del();
|
|
340
|
+
this.cacheData = {
|
|
341
|
+
loginUsers: [],
|
|
342
|
+
user: undefined,
|
|
343
|
+
id: undefined,
|
|
344
|
+
accessToken: undefined,
|
|
345
|
+
refreshToken: undefined
|
|
346
|
+
};
|
|
340
347
|
}
|
|
341
348
|
getValue() {
|
|
342
349
|
return this.cache.get(this.name);
|
|
@@ -344,10 +351,12 @@ class LoginCacheStore {
|
|
|
344
351
|
async init() {
|
|
345
352
|
const defaultData = {
|
|
346
353
|
loginUsers: [],
|
|
347
|
-
user:
|
|
348
|
-
id:
|
|
349
|
-
accessToken:
|
|
350
|
-
refreshToken:
|
|
354
|
+
user: undefined,
|
|
355
|
+
id: undefined,
|
|
356
|
+
accessToken: undefined,
|
|
357
|
+
refreshToken: undefined,
|
|
358
|
+
accessTokenExpiresIn: undefined,
|
|
359
|
+
createdAt: undefined
|
|
351
360
|
};
|
|
352
361
|
if (this.cache.init) {
|
|
353
362
|
try {
|
|
@@ -359,6 +368,7 @@ class LoginCacheStore {
|
|
|
359
368
|
} else {
|
|
360
369
|
this.cacheData = await this.getValue() || defaultData;
|
|
361
370
|
}
|
|
371
|
+
return this.cacheData;
|
|
362
372
|
}
|
|
363
373
|
async setLoginUser(user) {
|
|
364
374
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
@@ -389,6 +399,18 @@ class LoginCacheStore {
|
|
|
389
399
|
const cacheData = this.cacheData;
|
|
390
400
|
return Promise.resolve(cacheData.accessToken || "");
|
|
391
401
|
}
|
|
402
|
+
getIsExpired() {
|
|
403
|
+
const cacheData = this.cacheData;
|
|
404
|
+
if (!cacheData.accessToken) {
|
|
405
|
+
return Promise.resolve(true);
|
|
406
|
+
}
|
|
407
|
+
if (!cacheData.createdAt || !cacheData.accessTokenExpiresIn) {
|
|
408
|
+
return Promise.resolve(false);
|
|
409
|
+
}
|
|
410
|
+
const now = Date.now();
|
|
411
|
+
const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
|
|
412
|
+
return Promise.resolve(now >= expiresIn);
|
|
413
|
+
}
|
|
392
414
|
async clearCurrentUser() {
|
|
393
415
|
const user = await this.getCurrentUser();
|
|
394
416
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
@@ -399,6 +421,8 @@ class LoginCacheStore {
|
|
|
399
421
|
this.cacheData.id = undefined;
|
|
400
422
|
this.cacheData.accessToken = undefined;
|
|
401
423
|
this.cacheData.refreshToken = undefined;
|
|
424
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
425
|
+
this.cacheData.createdAt = undefined;
|
|
402
426
|
await this.setValue(this.cacheData);
|
|
403
427
|
}
|
|
404
428
|
async clearAll() {
|
|
@@ -407,6 +431,8 @@ class LoginCacheStore {
|
|
|
407
431
|
this.cacheData.id = undefined;
|
|
408
432
|
this.cacheData.accessToken = undefined;
|
|
409
433
|
this.cacheData.refreshToken = undefined;
|
|
434
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
435
|
+
this.cacheData.createdAt = undefined;
|
|
410
436
|
await this.setValue(this.cacheData);
|
|
411
437
|
}
|
|
412
438
|
}
|
|
@@ -1063,6 +1089,9 @@ class QueryLogin extends BaseQuery {
|
|
|
1063
1089
|
const token = this.storage.getItem("token");
|
|
1064
1090
|
return !!token;
|
|
1065
1091
|
}
|
|
1092
|
+
async checkTokenValid() {
|
|
1093
|
+
return this.cacheStore.getIsExpired();
|
|
1094
|
+
}
|
|
1066
1095
|
async getToken() {
|
|
1067
1096
|
const token = this.storage.getItem("token");
|
|
1068
1097
|
return token || "";
|
package/package.json
CHANGED
|
@@ -38,7 +38,7 @@ type CacheLogin = {
|
|
|
38
38
|
loginUsers: CacheLoginUser[];
|
|
39
39
|
} & CacheLoginUser;
|
|
40
40
|
|
|
41
|
-
export type CacheStore<T = Cache> = {
|
|
41
|
+
export type CacheStore<T extends Cache = Cache> = {
|
|
42
42
|
name: string;
|
|
43
43
|
/**
|
|
44
44
|
* 缓存数据
|
|
@@ -82,18 +82,19 @@ export type CacheStore<T = Cache> = {
|
|
|
82
82
|
getValue(): Promise<CacheLogin>;
|
|
83
83
|
setValue(value: CacheLogin): Promise<CacheLogin>;
|
|
84
84
|
delValue(): Promise<void>;
|
|
85
|
-
init(): Promise<
|
|
85
|
+
init(): Promise<CacheLogin>;
|
|
86
|
+
getIsExpired(): Promise<boolean>;
|
|
86
87
|
};
|
|
87
88
|
|
|
88
|
-
export type LoginCacheStoreOpts = {
|
|
89
|
+
export type LoginCacheStoreOpts<T extends Cache = Cache> = {
|
|
89
90
|
name: string;
|
|
90
|
-
cache:
|
|
91
|
+
cache: T;
|
|
91
92
|
};
|
|
92
|
-
export class LoginCacheStore implements CacheStore<
|
|
93
|
-
cache:
|
|
93
|
+
export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|
94
|
+
cache: T;
|
|
94
95
|
name: string;
|
|
95
96
|
cacheData: CacheLogin;
|
|
96
|
-
constructor(opts: LoginCacheStoreOpts) {
|
|
97
|
+
constructor(opts: LoginCacheStoreOpts<T>) {
|
|
97
98
|
if (!opts.cache) {
|
|
98
99
|
throw new Error('cache is required');
|
|
99
100
|
}
|
|
@@ -124,6 +125,13 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
124
125
|
*/
|
|
125
126
|
async delValue() {
|
|
126
127
|
await this.cache.del();
|
|
128
|
+
this.cacheData = {
|
|
129
|
+
loginUsers: [],
|
|
130
|
+
user: undefined,
|
|
131
|
+
id: undefined,
|
|
132
|
+
accessToken: undefined,
|
|
133
|
+
refreshToken: undefined,
|
|
134
|
+
};
|
|
127
135
|
}
|
|
128
136
|
getValue(): Promise<CacheLogin> {
|
|
129
137
|
return this.cache.get(this.name);
|
|
@@ -132,12 +140,14 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
132
140
|
* 初始化,设置默认值
|
|
133
141
|
*/
|
|
134
142
|
async init() {
|
|
135
|
-
const defaultData = {
|
|
143
|
+
const defaultData: CacheLogin = {
|
|
136
144
|
loginUsers: [],
|
|
137
|
-
user:
|
|
138
|
-
id:
|
|
139
|
-
accessToken:
|
|
140
|
-
refreshToken:
|
|
145
|
+
user: undefined,
|
|
146
|
+
id: undefined,
|
|
147
|
+
accessToken: undefined,
|
|
148
|
+
refreshToken: undefined,
|
|
149
|
+
accessTokenExpiresIn: undefined,
|
|
150
|
+
createdAt: undefined,
|
|
141
151
|
};
|
|
142
152
|
if (this.cache.init) {
|
|
143
153
|
try {
|
|
@@ -149,6 +159,7 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
149
159
|
} else {
|
|
150
160
|
this.cacheData = (await this.getValue()) || defaultData;
|
|
151
161
|
}
|
|
162
|
+
return this.cacheData;
|
|
152
163
|
}
|
|
153
164
|
/**
|
|
154
165
|
* 设置当前用户
|
|
@@ -184,6 +195,18 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
184
195
|
const cacheData = this.cacheData;
|
|
185
196
|
return Promise.resolve(cacheData.accessToken || '');
|
|
186
197
|
}
|
|
198
|
+
getIsExpired(): Promise<boolean> {
|
|
199
|
+
const cacheData = this.cacheData;
|
|
200
|
+
if (!cacheData.accessToken) {
|
|
201
|
+
return Promise.resolve(true);
|
|
202
|
+
}
|
|
203
|
+
if (!cacheData.createdAt || !cacheData.accessTokenExpiresIn) {
|
|
204
|
+
return Promise.resolve(false);
|
|
205
|
+
}
|
|
206
|
+
const now = Date.now();
|
|
207
|
+
const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
|
|
208
|
+
return Promise.resolve(now >= expiresIn);
|
|
209
|
+
}
|
|
187
210
|
|
|
188
211
|
async clearCurrentUser() {
|
|
189
212
|
const user = await this.getCurrentUser();
|
|
@@ -195,6 +218,8 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
195
218
|
this.cacheData.id = undefined;
|
|
196
219
|
this.cacheData.accessToken = undefined;
|
|
197
220
|
this.cacheData.refreshToken = undefined;
|
|
221
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
222
|
+
this.cacheData.createdAt = undefined;
|
|
198
223
|
await this.setValue(this.cacheData);
|
|
199
224
|
}
|
|
200
225
|
async clearAll() {
|
|
@@ -203,6 +228,8 @@ export class LoginCacheStore implements CacheStore<any> {
|
|
|
203
228
|
this.cacheData.id = undefined;
|
|
204
229
|
this.cacheData.accessToken = undefined;
|
|
205
230
|
this.cacheData.refreshToken = undefined;
|
|
231
|
+
this.cacheData.accessTokenExpiresIn = undefined;
|
|
232
|
+
this.cacheData.createdAt = undefined;
|
|
206
233
|
await this.setValue(this.cacheData);
|
|
207
234
|
}
|
|
208
235
|
}
|
|
@@ -51,7 +51,6 @@ export class StorageNode implements Storage {
|
|
|
51
51
|
cacheData: any;
|
|
52
52
|
filePath: string = '';
|
|
53
53
|
hostname: string = '';
|
|
54
|
-
isLoaded: boolean = false;
|
|
55
54
|
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
|
56
55
|
this.cacheData = {};
|
|
57
56
|
const hostname = getHostName(opts?.baseURL);
|
|
@@ -70,13 +69,11 @@ export class StorageNode implements Storage {
|
|
|
70
69
|
this.loadCache();
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
|
-
loadCache(
|
|
74
|
-
if (this.isLoaded && !force) return;
|
|
72
|
+
loadCache() {
|
|
75
73
|
const filePath = this.filePath;
|
|
76
74
|
try {
|
|
77
75
|
const data = readConfigFile(filePath);
|
|
78
76
|
this.cacheData = data;
|
|
79
|
-
this.isLoaded = true;
|
|
80
77
|
} catch (error) {
|
|
81
78
|
this.cacheData = {};
|
|
82
79
|
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
|
|
@@ -106,7 +103,6 @@ export class StorageNode implements Storage {
|
|
|
106
103
|
}
|
|
107
104
|
export class LoginNodeCache implements Cache {
|
|
108
105
|
filepath: string;
|
|
109
|
-
isLoaded: boolean = false;
|
|
110
106
|
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
|
111
107
|
this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`);
|
|
112
108
|
fileExists(this.filepath, { isFile: true });
|
|
@@ -136,12 +132,10 @@ export class LoginNodeCache implements Cache {
|
|
|
136
132
|
async del() {
|
|
137
133
|
unlinkSync(this.filepath);
|
|
138
134
|
}
|
|
139
|
-
loadCache(filePath: string
|
|
140
|
-
if (this.isLoaded && !force) return;
|
|
135
|
+
loadCache(filePath: string) {
|
|
141
136
|
try {
|
|
142
137
|
const data = readFileSync(filePath, 'utf-8');
|
|
143
138
|
const jsonData = JSON.parse(data);
|
|
144
|
-
this.isLoaded = true;
|
|
145
139
|
return jsonData;
|
|
146
140
|
} catch (error) {
|
|
147
141
|
// console.log('loadCache error', error);
|
|
@@ -3,7 +3,7 @@ import { LoginNodeCache, StorageNode } from './login-node-cache.ts';
|
|
|
3
3
|
type QueryLoginNodeOptsWithoutCache = Omit<QueryLoginOpts, 'cache'>;
|
|
4
4
|
export { StorageNode }
|
|
5
5
|
export const cache = new LoginNodeCache();
|
|
6
|
-
export class QueryLoginNode extends QueryLogin {
|
|
6
|
+
export class QueryLoginNode extends QueryLogin<LoginNodeCache> {
|
|
7
7
|
declare storage: StorageNode;
|
|
8
8
|
constructor(opts: QueryLoginNodeOptsWithoutCache) {
|
|
9
9
|
const baseURL = opts?.query?.baseURL;
|
|
@@ -3,12 +3,12 @@ import type { Result, DataOpts } from '@kevisual/query/query';
|
|
|
3
3
|
import { LoginCacheStore, CacheStore } from './login-cache.ts';
|
|
4
4
|
import { Cache } from './login-cache.ts';
|
|
5
5
|
import { BaseLoad } from '@kevisual/load';
|
|
6
|
-
export type QueryLoginOpts = {
|
|
6
|
+
export type QueryLoginOpts<T extends Cache = Cache> = {
|
|
7
7
|
query?: Query;
|
|
8
8
|
isBrowser?: boolean;
|
|
9
9
|
onLoad?: () => void;
|
|
10
10
|
storage?: Storage;
|
|
11
|
-
cache:
|
|
11
|
+
cache: T;
|
|
12
12
|
};
|
|
13
13
|
export type QueryLoginData = {
|
|
14
14
|
username?: string;
|
|
@@ -20,21 +20,21 @@ export type QueryLoginResult = {
|
|
|
20
20
|
refreshToken: string;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export class QueryLogin extends BaseQuery {
|
|
23
|
+
export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
|
24
24
|
/**
|
|
25
25
|
* query login cache, 非实际操作, 一个cache的包裹模块
|
|
26
26
|
*/
|
|
27
|
-
cacheStore: CacheStore
|
|
27
|
+
cacheStore: CacheStore<T>;
|
|
28
28
|
isBrowser: boolean;
|
|
29
29
|
load?: boolean;
|
|
30
30
|
storage: Storage;
|
|
31
31
|
onLoad?: () => void;
|
|
32
32
|
|
|
33
|
-
constructor(opts?: QueryLoginOpts) {
|
|
33
|
+
constructor(opts?: QueryLoginOpts<T>) {
|
|
34
34
|
super({
|
|
35
35
|
query: opts?.query || new Query(),
|
|
36
36
|
});
|
|
37
|
-
this.cacheStore = new LoginCacheStore({ name: 'login', cache: opts?.cache! });
|
|
37
|
+
this.cacheStore = new LoginCacheStore<T>({ name: 'login', cache: opts?.cache! });
|
|
38
38
|
this.isBrowser = opts?.isBrowser ?? true;
|
|
39
39
|
this.init();
|
|
40
40
|
this.onLoad = opts?.onLoad;
|
|
@@ -305,6 +305,9 @@ export class QueryLogin extends BaseQuery {
|
|
|
305
305
|
const token = this.storage.getItem('token');
|
|
306
306
|
return !!token;
|
|
307
307
|
}
|
|
308
|
+
async checkTokenValid() {
|
|
309
|
+
return this.cacheStore.getIsExpired();
|
|
310
|
+
}
|
|
308
311
|
/**
|
|
309
312
|
* 检查本地用户列表
|
|
310
313
|
* @returns
|
|
@@ -313,6 +316,7 @@ export class QueryLogin extends BaseQuery {
|
|
|
313
316
|
const token = this.storage.getItem('token');
|
|
314
317
|
return token || '';
|
|
315
318
|
}
|
|
319
|
+
|
|
316
320
|
async beforeRequest(opts: any = {}) {
|
|
317
321
|
const token = this.storage.getItem('token');
|
|
318
322
|
if (token) {
|