api-client: simplify interface, take turnstile object on init

This commit is contained in:
dumbmoron 2024-09-14 15:21:39 +00:00
parent ff57a6a448
commit 00d5754ea8
No known key found for this signature in database
6 changed files with 25 additions and 28 deletions

View file

@ -10,6 +10,7 @@
"devDependencies": {
"prettier": "3.3.3",
"tsup": "^8.2.4",
"turnstile-types": "^1.2.2",
"typescript": "^5.4.5",
"zod": "^3.23.8"
}

View file

@ -4,9 +4,8 @@ import type { CobaltRequest } from "../types/request";
export default class BaseCobaltAPI {
#baseURL: string;
#userAgent: string | undefined;
constructor(baseURL: string, userAgent?: string) {
constructor(baseURL: string) {
const url = new URL(baseURL);
if (baseURL !== url.origin && baseURL !== `${url.origin}/`) {
@ -14,14 +13,9 @@ export default class BaseCobaltAPI {
}
this.#baseURL = url.origin;
this.#userAgent = userAgent;
}
protected async _request(data: CobaltRequest, headers: Record<string, string>) {
if (this.#userAgent) {
headers['user-agent'] = this.#userAgent;
}
async request(data: CobaltRequest, headers?: Record<string, string>) {
const response: CobaltResponse = await fetch(this.#baseURL, {
method: 'POST',
redirect: 'manual',

View file

@ -1,30 +1,30 @@
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "../types/response";
import { CobaltReachabilityError } from "../types/errors";
import type { TurnstileObject } from "turnstile-types";
const currentTime = () => Math.floor(new Date().getTime() / 1000);
const EXPIRY_THRESHOLD_SECONDS = 2;
export default class CobaltSessionHandler {
#baseURL: string;
#turnstile: TurnstileObject;
#session: CobaltSession | undefined;
constructor(baseURL: string) {
constructor(baseURL: string, turnstile: TurnstileObject) {
this.#baseURL = baseURL;
this.#turnstile = turnstile;
}
async #requestSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
async #requestSession(): Promise<CobaltSessionResponse> {
const endpoint = new URL('/session', this.#baseURL);
const headers: Record<string, string> = {};
if (turnstileResponse) {
headers['cf-turnstile-response'] = turnstileResponse;
}
const response = await fetch(endpoint, {
method: 'POST',
redirect: 'manual',
signal: AbortSignal.timeout(10000),
headers
headers: {
'cf-turnstile-response': this.#turnstile.getResponse('turnstile-widget')
}
})
.then(r => r.json())
.catch((e) => {
@ -46,6 +46,8 @@ export default class CobaltSessionHandler {
}
}
this.#turnstile.reset('turnstile-widget');
return response;
}
@ -53,11 +55,11 @@ export default class CobaltSessionHandler {
return this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime();
}
async getSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
async getSession(): Promise<CobaltSessionResponse> {
if (this.hasSession()) {
return this.#session!;
}
return this.#requestSession(turnstileResponse);
return this.#requestSession();
}
};

View file

@ -3,22 +3,23 @@ import BaseCobaltAPI from "./internal/base-api";
import { CobaltRequest } from "./types/request";
import { CobaltAuthError } from "./types/errors";
import { CobaltAPIClient } from "./types/interface";
import type { TurnstileObject } from "turnstile-types";
export default class TurnstileCobaltAPI extends BaseCobaltAPI implements CobaltAPIClient {
#session: CobaltSessionHandler;
#instanceHasTurnstile = true;
constructor(baseURL: string) {
constructor(baseURL: string, turnstile: TurnstileObject) {
super(baseURL);
this.#session = new CobaltSessionHandler(baseURL);
this.#session = new CobaltSessionHandler(baseURL, turnstile);
}
needsSession() {
return this.#instanceHasTurnstile && !this.#session.hasSession();
}
async request(data: CobaltRequest, turnstileResponse?: string) {
const sessionOrError = await this.#session.getSession(turnstileResponse);
async request(data: CobaltRequest) {
const sessionOrError = await this.#session.getSession();
const headers: Record<string, string> = {};
if ("error" in sessionOrError) {
@ -31,6 +32,6 @@ export default class TurnstileCobaltAPI extends BaseCobaltAPI implements CobaltA
headers['Authorization'] = `Bearer ${sessionOrError.token}`;
}
return super._request(data, headers);
return super.request(data, headers);
}
}

View file

@ -3,11 +3,7 @@ import { CobaltRequest } from "./types/request";
import { CobaltAPIClient } from "./types/interface";
export default class UnauthenticatedCobaltAPI extends BaseCobaltAPI implements CobaltAPIClient {
constructor(baseURL: string, userAgent?: string) {
super(baseURL, userAgent);
}
async request(data: CobaltRequest) {
return super._request(data, {});
return super.request(data, {});
}
}

View file

@ -77,6 +77,9 @@ importers:
tsup:
specifier: ^8.2.4
version: 8.2.4(postcss@8.4.40)(typescript@5.5.4)
turnstile-types:
specifier: ^1.2.2
version: 1.2.2
typescript:
specifier: ^5.4.5
version: 5.5.4