From 00d5754ea8dea2b91a208ef070c5bcfe31f1f201 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sat, 14 Sep 2024 15:21:39 +0000 Subject: [PATCH] api-client: simplify interface, take turnstile object on init --- packages/api-client/package.json | 1 + packages/api-client/src/internal/base-api.ts | 10 ++------- packages/api-client/src/internal/session.ts | 22 ++++++++++--------- packages/api-client/src/turnstile-api.ts | 11 +++++----- .../api-client/src/unauthenticated-api.ts | 6 +---- pnpm-lock.yaml | 3 +++ 6 files changed, 25 insertions(+), 28 deletions(-) diff --git a/packages/api-client/package.json b/packages/api-client/package.json index 2de234a3..8b9dd7d5 100644 --- a/packages/api-client/package.json +++ b/packages/api-client/package.json @@ -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" } diff --git a/packages/api-client/src/internal/base-api.ts b/packages/api-client/src/internal/base-api.ts index ef2c8960..934d1a53 100644 --- a/packages/api-client/src/internal/base-api.ts +++ b/packages/api-client/src/internal/base-api.ts @@ -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) { - if (this.#userAgent) { - headers['user-agent'] = this.#userAgent; - } - + async request(data: CobaltRequest, headers?: Record) { const response: CobaltResponse = await fetch(this.#baseURL, { method: 'POST', redirect: 'manual', diff --git a/packages/api-client/src/internal/session.ts b/packages/api-client/src/internal/session.ts index a4390113..7a709701 100644 --- a/packages/api-client/src/internal/session.ts +++ b/packages/api-client/src/internal/session.ts @@ -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 { + async #requestSession(): Promise { const endpoint = new URL('/session', this.#baseURL); - const headers: Record = {}; - - 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 { + async getSession(): Promise { if (this.hasSession()) { return this.#session!; } - return this.#requestSession(turnstileResponse); + return this.#requestSession(); } }; diff --git a/packages/api-client/src/turnstile-api.ts b/packages/api-client/src/turnstile-api.ts index 1ff0004b..0792c276 100644 --- a/packages/api-client/src/turnstile-api.ts +++ b/packages/api-client/src/turnstile-api.ts @@ -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 = {}; 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); } } diff --git a/packages/api-client/src/unauthenticated-api.ts b/packages/api-client/src/unauthenticated-api.ts index cf656ef8..3fa5dee1 100644 --- a/packages/api-client/src/unauthenticated-api.ts +++ b/packages/api-client/src/unauthenticated-api.ts @@ -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, {}); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0759e255..f6f0dd7d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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