From 7bcf16207a0a003645b0bac78c660ccdab0dc69b Mon Sep 17 00:00:00 2001 From: GitPusher99 Date: Thu, 28 Mar 2024 16:08:13 +0800 Subject: [PATCH] feat: adjust keepAlive token --- src/lib/SunoApi.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/lib/SunoApi.ts b/src/lib/SunoApi.ts index d444dfc..0be870d 100644 --- a/src/lib/SunoApi.ts +++ b/src/lib/SunoApi.ts @@ -7,7 +7,7 @@ import { CookieJar } from "tough-cookie"; const logger = pino(); -interface AudioInfo { +export interface AudioInfo { id: string; title?: string; image_url?: string; @@ -63,21 +63,15 @@ class SunoApi { } public async init(): Promise { - const token = await this.getAuthToken(); - this.client.interceptors.request.use(function (config) { - config.headers['Authorization'] = `Bearer ${token}` - return config; - }) + await this.getAuthToken(); + await this.keepAlive(); return this; } - private async getAuthToken(): Promise { + private async getAuthToken() { // 获取会话ID的URL const getSessionUrl = `${SunoApi.CLERK_BASE_URL}/v1/client?_clerk_js_version=4.70.5`; - // 交换令牌的URL模板 - const exchangeTokenUrlTemplate = `${SunoApi.CLERK_BASE_URL}/v1/client/sessions/{sid}/tokens/api?_clerk_js_version=4.70.0`; - // 获取会话ID const sessionResponse = await this.client.get(getSessionUrl); const sid = sessionResponse.data.response['last_active_session_id']; @@ -86,14 +80,9 @@ class SunoApi { } // 保存会话ID以备后用 this.sid = sid; - - // 使用会话ID获取JWT令牌 - const exchangeTokenUrl = exchangeTokenUrlTemplate.replace('{sid}', sid); - const tokenResponse = await this.client.post(exchangeTokenUrl); - return tokenResponse.data['jwt']; } - public async KeepAlive(): Promise { + public async keepAlive(isWait?: boolean): Promise { if (!this.sid) { throw new Error("Session ID is not set. Cannot renew token."); } @@ -102,7 +91,9 @@ class SunoApi { // 续订会话令牌 const renewResponse = await this.client.post(renewUrl); logger.info("KeepAlive...\n"); - await sleep(1, 2); + if (isWait) { + await sleep(1, 2); + } const newToken = renewResponse.data['jwt']; // 更新请求头中的Authorization字段,使用新的JWT令牌 this.client.interceptors.request.use(function (config) { @@ -216,11 +207,11 @@ class SunoApi { } lastResponse = response; await sleep(3, 6); - await this.KeepAlive(); + await this.keepAlive(true); } return lastResponse; } else { - await this.KeepAlive(); + await this.keepAlive(true); return response.data['clips'].map((audio: any) => ({ id: audio.id, title: audio.title,