diff --git a/packages/api-client/src/internal/session.ts b/packages/api-client/src/internal/session.ts index b486b8f7..a4390113 100644 --- a/packages/api-client/src/internal/session.ts +++ b/packages/api-client/src/internal/session.ts @@ -49,9 +49,13 @@ export default class CobaltSessionHandler { return response; } + hasSession() { + return this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime(); + } + async getSession(turnstileResponse?: string): Promise { - if (this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime()) { - return this.#session; + if (this.hasSession()) { + return this.#session!; } return this.#requestSession(turnstileResponse); diff --git a/packages/api-client/src/turnstile-api.ts b/packages/api-client/src/turnstile-api.ts index 9a7cabb7..1ff0004b 100644 --- a/packages/api-client/src/turnstile-api.ts +++ b/packages/api-client/src/turnstile-api.ts @@ -2,15 +2,21 @@ import CobaltSessionHandler from "./internal/session"; import BaseCobaltAPI from "./internal/base-api"; import { CobaltRequest } from "./types/request"; import { CobaltAuthError } from "./types/errors"; +import { CobaltAPIClient } from "./types/interface"; -export default class TurnstileCobaltAPI extends BaseCobaltAPI { +export default class TurnstileCobaltAPI extends BaseCobaltAPI implements CobaltAPIClient { #session: CobaltSessionHandler; + #instanceHasTurnstile = true; constructor(baseURL: string) { super(baseURL); this.#session = new CobaltSessionHandler(baseURL); } + needsSession() { + return this.#instanceHasTurnstile && !this.#session.hasSession(); + } + async request(data: CobaltRequest, turnstileResponse?: string) { const sessionOrError = await this.#session.getSession(turnstileResponse); const headers: Record = {}; @@ -18,6 +24,8 @@ export default class TurnstileCobaltAPI extends BaseCobaltAPI { if ("error" in sessionOrError) { if (sessionOrError.error.code !== CobaltAuthError.NotConfigured) { return sessionOrError; + } else { + this.#instanceHasTurnstile = false; } } else { headers['Authorization'] = `Bearer ${sessionOrError.token}`;