feat: 更新版本至 0.0.55,增强类型支持,优化登录缓存相关类

This commit is contained in:
2026-02-21 02:24:07 +08:00
parent 885abd8c46
commit 15fd2d3dc8
5 changed files with 16 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/api", "name": "@kevisual/api",
"version": "0.0.54", "version": "0.0.55",
"description": "", "description": "",
"main": "mod.ts", "main": "mod.ts",
"scripts": { "scripts": {

View File

@@ -38,7 +38,7 @@ type CacheLogin = {
loginUsers: CacheLoginUser[]; loginUsers: CacheLoginUser[];
} & CacheLoginUser; } & CacheLoginUser;
export type CacheStore<T = Cache> = { export type CacheStore<T extends Cache = Cache> = {
name: string; name: string;
/** /**
* 缓存数据 * 缓存数据
@@ -85,15 +85,15 @@ export type CacheStore<T = Cache> = {
init(): Promise<any>; init(): Promise<any>;
}; };
export type LoginCacheStoreOpts = { export type LoginCacheStoreOpts<T extends Cache = Cache> = {
name: string; name: string;
cache: Cache; cache: T;
}; };
export class LoginCacheStore implements CacheStore<any> { export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
cache: Cache; cache: T;
name: string; name: string;
cacheData: CacheLogin; cacheData: CacheLogin;
constructor(opts: LoginCacheStoreOpts) { constructor(opts: LoginCacheStoreOpts<T>) {
if (!opts.cache) { if (!opts.cache) {
throw new Error('cache is required'); throw new Error('cache is required');
} }

View File

@@ -51,7 +51,6 @@ export class StorageNode implements Storage {
cacheData: any; cacheData: any;
filePath: string = ''; filePath: string = '';
hostname: string = ''; hostname: string = '';
isLoaded: boolean = false;
constructor(opts?: { baseURL?: string, load?: boolean }) { constructor(opts?: { baseURL?: string, load?: boolean }) {
this.cacheData = {}; this.cacheData = {};
const hostname = getHostName(opts?.baseURL); const hostname = getHostName(opts?.baseURL);
@@ -70,13 +69,11 @@ export class StorageNode implements Storage {
this.loadCache(); this.loadCache();
} }
} }
loadCache(force?: boolean) { loadCache() {
if (this.isLoaded && !force) return;
const filePath = this.filePath; const filePath = this.filePath;
try { try {
const data = readConfigFile(filePath); const data = readConfigFile(filePath);
this.cacheData = data; this.cacheData = data;
this.isLoaded = true;
} catch (error) { } catch (error) {
this.cacheData = {}; this.cacheData = {};
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2)); writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
@@ -106,7 +103,6 @@ export class StorageNode implements Storage {
} }
export class LoginNodeCache implements Cache { export class LoginNodeCache implements Cache {
filepath: string; filepath: string;
isLoaded: boolean = false;
constructor(opts?: { baseURL?: string, load?: boolean }) { constructor(opts?: { baseURL?: string, load?: boolean }) {
this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`); this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`);
fileExists(this.filepath, { isFile: true }); fileExists(this.filepath, { isFile: true });
@@ -136,12 +132,10 @@ export class LoginNodeCache implements Cache {
async del() { async del() {
unlinkSync(this.filepath); unlinkSync(this.filepath);
} }
loadCache(filePath: string, force?: boolean) { loadCache(filePath: string) {
if (this.isLoaded && !force) return;
try { try {
const data = readFileSync(filePath, 'utf-8'); const data = readFileSync(filePath, 'utf-8');
const jsonData = JSON.parse(data); const jsonData = JSON.parse(data);
this.isLoaded = true;
return jsonData; return jsonData;
} catch (error) { } catch (error) {
// console.log('loadCache error', error); // console.log('loadCache error', error);

View File

@@ -3,7 +3,7 @@ import { LoginNodeCache, StorageNode } from './login-node-cache.ts';
type QueryLoginNodeOptsWithoutCache = Omit<QueryLoginOpts, 'cache'>; type QueryLoginNodeOptsWithoutCache = Omit<QueryLoginOpts, 'cache'>;
export { StorageNode } export { StorageNode }
export const cache = new LoginNodeCache(); export const cache = new LoginNodeCache();
export class QueryLoginNode extends QueryLogin { export class QueryLoginNode extends QueryLogin<LoginNodeCache> {
declare storage: StorageNode; declare storage: StorageNode;
constructor(opts: QueryLoginNodeOptsWithoutCache) { constructor(opts: QueryLoginNodeOptsWithoutCache) {
const baseURL = opts?.query?.baseURL; const baseURL = opts?.query?.baseURL;

View File

@@ -3,12 +3,12 @@ import type { Result, DataOpts } from '@kevisual/query/query';
import { LoginCacheStore, CacheStore } from './login-cache.ts'; import { LoginCacheStore, CacheStore } from './login-cache.ts';
import { Cache } from './login-cache.ts'; import { Cache } from './login-cache.ts';
import { BaseLoad } from '@kevisual/load'; import { BaseLoad } from '@kevisual/load';
export type QueryLoginOpts = { export type QueryLoginOpts<T extends Cache = Cache> = {
query?: Query; query?: Query;
isBrowser?: boolean; isBrowser?: boolean;
onLoad?: () => void; onLoad?: () => void;
storage?: Storage; storage?: Storage;
cache: Cache; cache: T;
}; };
export type QueryLoginData = { export type QueryLoginData = {
username?: string; username?: string;
@@ -20,21 +20,21 @@ export type QueryLoginResult = {
refreshToken: string; refreshToken: string;
}; };
export class QueryLogin extends BaseQuery { export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
/** /**
* query login cache 非实际操作, 一个cache的包裹模块 * query login cache 非实际操作, 一个cache的包裹模块
*/ */
cacheStore: CacheStore; cacheStore: CacheStore<T>;
isBrowser: boolean; isBrowser: boolean;
load?: boolean; load?: boolean;
storage: Storage; storage: Storage;
onLoad?: () => void; onLoad?: () => void;
constructor(opts?: QueryLoginOpts) { constructor(opts?: QueryLoginOpts<T>) {
super({ super({
query: opts?.query || new Query(), query: opts?.query || new Query(),
}); });
this.cacheStore = new LoginCacheStore({ name: 'login', cache: opts?.cache! }); this.cacheStore = new LoginCacheStore<T>({ name: 'login', cache: opts?.cache! });
this.isBrowser = opts?.isBrowser ?? true; this.isBrowser = opts?.isBrowser ?? true;
this.init(); this.init();
this.onLoad = opts?.onLoad; this.onLoad = opts?.onLoad;