Merge pull request #169 from Kinggodhoon/feat/get-latest-clerk-version
Add method(Get clerk latest version) in to lib/SunoApi
This commit is contained in:
commit
6c49aec4b2
@ -31,8 +31,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;
|
||||
|
||||
@ -56,17 +58,33 @@ class SunoApi {
|
||||
}
|
||||
|
||||
public async init(): Promise<SunoApi> {
|
||||
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'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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']) {
|
||||
@ -85,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");
|
||||
|
Loading…
Reference in New Issue
Block a user