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",
"version": "0.0.54",
"version": "0.0.55",
"description": "",
"main": "mod.ts",
"scripts": {

View File

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

View File

@@ -51,7 +51,6 @@ export class StorageNode implements Storage {
cacheData: any;
filePath: string = '';
hostname: string = '';
isLoaded: boolean = false;
constructor(opts?: { baseURL?: string, load?: boolean }) {
this.cacheData = {};
const hostname = getHostName(opts?.baseURL);
@@ -70,13 +69,11 @@ export class StorageNode implements Storage {
this.loadCache();
}
}
loadCache(force?: boolean) {
if (this.isLoaded && !force) return;
loadCache() {
const filePath = this.filePath;
try {
const data = readConfigFile(filePath);
this.cacheData = data;
this.isLoaded = true;
} catch (error) {
this.cacheData = {};
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
@@ -106,7 +103,6 @@ export class StorageNode implements Storage {
}
export class LoginNodeCache implements Cache {
filepath: string;
isLoaded: boolean = false;
constructor(opts?: { baseURL?: string, load?: boolean }) {
this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`);
fileExists(this.filepath, { isFile: true });
@@ -136,12 +132,10 @@ export class LoginNodeCache implements Cache {
async del() {
unlinkSync(this.filepath);
}
loadCache(filePath: string, force?: boolean) {
if (this.isLoaded && !force) return;
loadCache(filePath: string) {
try {
const data = readFileSync(filePath, 'utf-8');
const jsonData = JSON.parse(data);
this.isLoaded = true;
return jsonData;
} catch (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'>;
export { StorageNode }
export const cache = new LoginNodeCache();
export class QueryLoginNode extends QueryLogin {
export class QueryLoginNode extends QueryLogin<LoginNodeCache> {
declare storage: StorageNode;
constructor(opts: QueryLoginNodeOptsWithoutCache) {
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 { Cache } from './login-cache.ts';
import { BaseLoad } from '@kevisual/load';
export type QueryLoginOpts = {
export type QueryLoginOpts<T extends Cache = Cache> = {
query?: Query;
isBrowser?: boolean;
onLoad?: () => void;
storage?: Storage;
cache: Cache;
cache: T;
};
export type QueryLoginData = {
username?: string;
@@ -20,21 +20,21 @@ export type QueryLoginResult = {
refreshToken: string;
};
export class QueryLogin extends BaseQuery {
export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
/**
* query login cache 非实际操作, 一个cache的包裹模块
*/
cacheStore: CacheStore;
cacheStore: CacheStore<T>;
isBrowser: boolean;
load?: boolean;
storage: Storage;
onLoad?: () => void;
constructor(opts?: QueryLoginOpts) {
constructor(opts?: QueryLoginOpts<T>) {
super({
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.init();
this.onLoad = opts?.onLoad;