From aae2d7c2f21c3977e0c4688926925c626155d800 Mon Sep 17 00:00:00 2001 From: Kinggodhoon Date: Thu, 10 Oct 2024 17:50:50 +0900 Subject: [PATCH] feat: add method(Get clerk latest version) in to lib/SunoApi --- src/lib/SunoApi.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lib/SunoApi.ts b/src/lib/SunoApi.ts index a1c6ea1..7299102 100644 --- a/src/lib/SunoApi.ts +++ b/src/lib/SunoApi.ts @@ -30,8 +30,10 @@ export interface AudioInfo { class SunoApi { private static BASE_URL: string = 'https://studio-api.suno.ai'; private static CLERK_BASE_URL: string = 'https://clerk.suno.com'; + private static JSDELIVR_BASE_URL: string = 'https://data.jsdelivr.com'; private readonly client: AxiosInstance; + private clerkVersion?: string; private sid?: string; private currentToken?: string; @@ -55,17 +57,34 @@ class SunoApi { } public async init(): Promise { + await this.getClerkLatestVersion(); await this.getAuthToken(); await this.keepAlive(); return this; } + /** + * Get the clerk package latest version id. + */ + private async getClerkLatestVersion() { + // URL to get clerk version ID + const getClerkVersionUrl = `${SunoApi.JSDELIVR_BASE_URL}/v1/package/npm/@clerk/clerk-js`;  + // Get clerk version ID + const versionListResponse = await this.client.get(getClerkVersionUrl); + if (!versionListResponse?.data?.['tags']['latest']) { + throw new Error("Failed to get clerk version info, Please try again later"); + } + // Save clerk version ID for auth + this.clerkVersion = versionListResponse?.data?.['tags']['latest']; + console.log(this.clerkVersion) + } + /** * Get the session ID and save it for later use. */ private async getAuthToken() { // URL to get session ID - const getSessionUrl = `${SunoApi.CLERK_BASE_URL}/v1/client?_clerk_js_version=4.73.4`;  + const getSessionUrl = `${SunoApi.CLERK_BASE_URL}/v1/client?_clerk_js_version=${this.clerkVersion}`;  // Get session ID const sessionResponse = await this.client.get(getSessionUrl); if (!sessionResponse?.data?.response?.['last_active_session_id']) { @@ -84,7 +103,7 @@ class SunoApi { throw new Error("Session ID is not set. Cannot renew token."); } // URL to renew session token - const renewUrl = `${SunoApi.CLERK_BASE_URL}/v1/client/sessions/${this.sid}/tokens?_clerk_js_version==4.73.4`;  + const renewUrl = `${SunoApi.CLERK_BASE_URL}/v1/client/sessions/${this.sid}/tokens?_clerk_js_version==${this.clerkVersion}`;  // Renew session token const renewResponse = await this.client.post(renewUrl); logger.info("KeepAlive...\n");