api-client/turnstile: expose information about client needing session
This commit is contained in:
parent
1d30ac0139
commit
ff57a6a448
2 changed files with 15 additions and 3 deletions
|
@ -49,9 +49,13 @@ export default class CobaltSessionHandler {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasSession() {
|
||||||
|
return this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime();
|
||||||
|
}
|
||||||
|
|
||||||
async getSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
|
async getSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
|
||||||
if (this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime()) {
|
if (this.hasSession()) {
|
||||||
return this.#session;
|
return this.#session!;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.#requestSession(turnstileResponse);
|
return this.#requestSession(turnstileResponse);
|
||||||
|
|
|
@ -2,15 +2,21 @@ import CobaltSessionHandler from "./internal/session";
|
||||||
import BaseCobaltAPI from "./internal/base-api";
|
import BaseCobaltAPI from "./internal/base-api";
|
||||||
import { CobaltRequest } from "./types/request";
|
import { CobaltRequest } from "./types/request";
|
||||||
import { CobaltAuthError } from "./types/errors";
|
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;
|
#session: CobaltSessionHandler;
|
||||||
|
#instanceHasTurnstile = true;
|
||||||
|
|
||||||
constructor(baseURL: string) {
|
constructor(baseURL: string) {
|
||||||
super(baseURL);
|
super(baseURL);
|
||||||
this.#session = new CobaltSessionHandler(baseURL);
|
this.#session = new CobaltSessionHandler(baseURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
needsSession() {
|
||||||
|
return this.#instanceHasTurnstile && !this.#session.hasSession();
|
||||||
|
}
|
||||||
|
|
||||||
async request(data: CobaltRequest, turnstileResponse?: string) {
|
async request(data: CobaltRequest, turnstileResponse?: string) {
|
||||||
const sessionOrError = await this.#session.getSession(turnstileResponse);
|
const sessionOrError = await this.#session.getSession(turnstileResponse);
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
|
@ -18,6 +24,8 @@ export default class TurnstileCobaltAPI extends BaseCobaltAPI {
|
||||||
if ("error" in sessionOrError) {
|
if ("error" in sessionOrError) {
|
||||||
if (sessionOrError.error.code !== CobaltAuthError.NotConfigured) {
|
if (sessionOrError.error.code !== CobaltAuthError.NotConfigured) {
|
||||||
return sessionOrError;
|
return sessionOrError;
|
||||||
|
} else {
|
||||||
|
this.#instanceHasTurnstile = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
headers['Authorization'] = `Bearer ${sessionOrError.token}`;
|
headers['Authorization'] = `Bearer ${sessionOrError.token}`;
|
||||||
|
|
Loading…
Reference in a new issue