From 37281232c01017ea0085c0b8e4eae6c22752d023 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sun, 23 Nov 2025 14:37:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client-protocol/LingChairClient.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/client-protocol/LingChairClient.ts b/client-protocol/LingChairClient.ts index c1fd29b..5b720fc 100644 --- a/client-protocol/LingChairClient.ts +++ b/client-protocol/LingChairClient.ts @@ -7,6 +7,7 @@ import User from "./User.ts" import UserMySelf from "./UserMySelf.ts" import CallbackError from "./CallbackError.ts" import Chat from "./Chat.ts" +import { CallableMethodBeforeAuth } from "lingchair-internal-shared" export { User, @@ -18,11 +19,20 @@ export default class LingChairClient { declare client: Socket declare access_token: string declare refresh_token?: string + declare auto_fresh_token: boolean + declare auth_cache: { + refresh_token?: string, + access_token?: string, + account?: string, + password?: string, + } constructor(args: { server_url: string device_id: string, io?: Partial + auto_fresh_token: boolean }) { + this.auto_fresh_token = args.auto_fresh_token || false this.client = io(args.server_url, { transports: ["polling", "websocket", "webtransport"], ...args.io, @@ -59,6 +69,15 @@ export default class LingChairClient { code: -1, msg: err.message, }) + if (CallableMethodBeforeAuth.indexOf(method) == -1 && res.code == 401 && this.auto_fresh_token) { + if (this.auth_cache) + this.auth(this.auth_cache).then((re) => { + if (!re) resolve(res) + this.invoke(method, args, timeout).then((re) => resolve(re)) + }) + else + resolve(res) + } else resolve(res) }) }) @@ -99,6 +118,8 @@ export default class LingChairClient { if ((!args.access_token && !args.refresh_token) && (!args.account && !args.password)) throw new Error('Access/Refresh token or account & password required') + this.auth_cache = args + this.refresh_token = args.refresh_token let access_token = args.access_token