更新版本至2.0.2,添加lru-cache依赖,重构AuthQuery类以支持缓存功能
This commit is contained in:
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/auth",
|
||||
"version": "2.0.1",
|
||||
"version": "2.0.2",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"build": "bun run bun.config.ts"
|
||||
@@ -14,16 +14,16 @@
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.28.1",
|
||||
"type": "module",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@kevisual/types": "^0.0.12",
|
||||
"@kevisual/use-config": "^1.0.28",
|
||||
"@kevisual/query": "^0.0.38",
|
||||
"@kevisual/router": "^0.0.60",
|
||||
"@kevisual/types": "^0.0.12",
|
||||
"@kevisual/use-config": "^1.0.28",
|
||||
"@types/bun": "^1.3.6",
|
||||
"@types/node": "^25.0.10",
|
||||
"es-toolkit": "^1.44.0",
|
||||
"jose": "^6.1.3"
|
||||
"jose": "^6.1.3",
|
||||
"lru-cache": "^11.2.4"
|
||||
},
|
||||
"exports": {
|
||||
".": "./dist/app.js",
|
||||
|
||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@@ -32,6 +32,9 @@ importers:
|
||||
jose:
|
||||
specifier: ^6.1.3
|
||||
version: 6.1.3
|
||||
lru-cache:
|
||||
specifier: ^11.2.4
|
||||
version: 11.2.4
|
||||
|
||||
packages:
|
||||
|
||||
@@ -78,6 +81,10 @@ packages:
|
||||
jose@6.1.3:
|
||||
resolution: {integrity: sha512-0TpaTfihd4QMNwrz/ob2Bp7X04yuxJkjRGi4aKmOqwhov54i6u79oCv7T+C7lo70MKH6BesI3vscD1yb/yzKXQ==}
|
||||
|
||||
lru-cache@11.2.4:
|
||||
resolution: {integrity: sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg==}
|
||||
engines: {node: 20 || >=22}
|
||||
|
||||
tslib@2.8.1:
|
||||
resolution: {integrity: sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==}
|
||||
|
||||
@@ -127,6 +134,8 @@ snapshots:
|
||||
|
||||
jose@6.1.3: {}
|
||||
|
||||
lru-cache@11.2.4: {}
|
||||
|
||||
tslib@2.8.1: {}
|
||||
|
||||
undici-types@7.16.0: {}
|
||||
|
||||
@@ -1 +1,5 @@
|
||||
export * from './auth.ts';
|
||||
export * from './auth.ts';
|
||||
|
||||
export * from './generate.ts';
|
||||
|
||||
export { AuthQuery } from './query.ts'
|
||||
53
src/query.ts
Normal file
53
src/query.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { Query, Result } from "@kevisual/query/query";
|
||||
import { LRUCache } from 'lru-cache'
|
||||
|
||||
type TokenUser = {
|
||||
id: string;
|
||||
username: string;
|
||||
nickname: string;
|
||||
description: string;
|
||||
/** user or org */
|
||||
type: string;
|
||||
avatar?: string;
|
||||
orgs?: string[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
type AuthQueryOpts = {
|
||||
url?: string;
|
||||
}
|
||||
export class AuthQuery extends Query {
|
||||
cache: LRUCache<string, any>;
|
||||
constructor(opts: AuthQueryOpts = {}) {
|
||||
if (!opts.url) {
|
||||
opts.url = 'https://kevisual.cn/api/router/';
|
||||
}
|
||||
super(opts);
|
||||
this.cache = new LRUCache<string, any>({
|
||||
max: 10000, // 最大缓存数量
|
||||
ttl: 1000 * 60 * 60 * 2, // 缓存过期时间,单位为毫秒,这里设置为2小时
|
||||
});
|
||||
}
|
||||
getTokenUser = async (token: string): Promise<Result<TokenUser>> => {
|
||||
const res = await this.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
token: token,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
getTokenUserCache = async (token: string): Promise<Result<TokenUser>> => {
|
||||
const tokenUser = await this.cache.get(token);
|
||||
if (tokenUser) {
|
||||
return {
|
||||
code: 200,
|
||||
data: tokenUser,
|
||||
};
|
||||
}
|
||||
const res = await this.getTokenUser(token);
|
||||
if (res.code === 200) {
|
||||
this.cache.set(token, res.data);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user