diff --git a/package.json b/package.json index 7748f18..e635fe9 100644 --- a/package.json +++ b/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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f7eed51..6db47d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: {} diff --git a/src/index.ts b/src/index.ts index 801a082..342992f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,5 @@ -export * from './auth.ts'; \ No newline at end of file +export * from './auth.ts'; + +export * from './generate.ts'; + +export { AuthQuery } from './query.ts' \ No newline at end of file diff --git a/src/query.ts b/src/query.ts new file mode 100644 index 0000000..9c8fac3 --- /dev/null +++ b/src/query.ts @@ -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; + constructor(opts: AuthQueryOpts = {}) { + if (!opts.url) { + opts.url = 'https://kevisual.cn/api/router/'; + } + super(opts); + this.cache = new LRUCache({ + max: 10000, // 最大缓存数量 + ttl: 1000 * 60 * 60 * 2, // 缓存过期时间,单位为毫秒,这里设置为2小时 + }); + } + getTokenUser = async (token: string): Promise> => { + const res = await this.post({ + path: 'user', + key: 'me', + token: token, + }); + return res; + } + getTokenUserCache = async (token: string): Promise> => { + 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; + } +}