From 13edd232459c9c3343b9bd35c867e381f4a08cb4 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sat, 8 Nov 2025 18:02:35 +0800 Subject: [PATCH] feat(protocol): register, refactor: authOrThrow --- client-protocol/LingChairClient.ts | 58 ++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/client-protocol/LingChairClient.ts b/client-protocol/LingChairClient.ts index cd663a8..e977ef8 100644 --- a/client-protocol/LingChairClient.ts +++ b/client-protocol/LingChairClient.ts @@ -79,6 +79,19 @@ export default class LingChairClient { access_token?: string, account?: string, password?: string, + }) { + try { + this.authOrThrow(args) + return true + } catch (_) { + return false + } + } + async authOrThrow(args: { + refresh_token?: string, + access_token?: string, + account?: string, + password?: string, }) { if ((!args.access_token && !args.refresh_token) && (!args.account && !args.password)) throw new Error('Access/Refresh token or account & password required') @@ -90,9 +103,10 @@ export default class LingChairClient { const re = await this.invoke('User.refreshAccessToken', { refresh_token: args.refresh_token, }) - if (re.code == 200) { + if (re.code == 200) access_token = re.data!.access_token as string | undefined - } else return re + else + throw new CallbackError(re) } if (!access_token && (args.account && args.password)) { @@ -100,15 +114,47 @@ export default class LingChairClient { account: args.account, password: crypto.createHash('sha256').update(args.password).digest('hex'), }) - if (re.code == 200) { + if (re.code == 200) access_token = re.data!.access_token as string | undefined - } else return re + else + throw new CallbackError(re) } const re = await this.invoke('User.auth', { access_token: access_token }) - if (re.code == 200) this.access_token = access_token as string - return re + if (re.code == 200) + this.access_token = access_token as string + else + throw new CallbackError(re) + } + async register(args: { + nickname: string, + username?: string, + password: string, + }) { + try { + this.registerOrThrow(args) + return true + } catch (_) { + return false + } + } + async registerOrThrow({ + nickname, + username, + password, + }: { + nickname: string, + username?: string, + password: string, + }) { + const re = await this.invoke('User.register', { + nickname, + username, + password, + }) + if (re.code != 200) + throw new CallbackError(re) } }