From afe9917169736965f3af2b86e5f6343b47589d93 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Wed, 11 Sep 2024 21:01:41 +0000 Subject: [PATCH] api-client: move base api and session to internal subfolder --- .../src/{api.ts => internal/base-api.ts} | 18 ++++++++++++------ .../api-client/src/{ => internal}/session.ts | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) rename packages/api-client/src/{api.ts => internal/base-api.ts} (67%) rename packages/api-client/src/{ => internal}/session.ts (95%) diff --git a/packages/api-client/src/api.ts b/packages/api-client/src/internal/base-api.ts similarity index 67% rename from packages/api-client/src/api.ts rename to packages/api-client/src/internal/base-api.ts index 2e746963..321f5487 100644 --- a/packages/api-client/src/api.ts +++ b/packages/api-client/src/internal/base-api.ts @@ -1,11 +1,12 @@ -import { CobaltResponseType, type CobaltAPIResponse } from "./types/response"; -import type { CobaltRequest } from "./types/request"; -import { CobaltReachabilityError } from "./types/errors"; +import { CobaltResponseType, type CobaltAPIResponse } from "../types/response"; +import type { CobaltRequest } from "../types/request"; +import { CobaltReachabilityError } from "../types/errors"; -export default class CobaltAPI { +export default class BaseCobaltAPI { #baseURL: string; + #userAgent: string | undefined; - constructor(baseURL: string) { + constructor(baseURL: string, userAgent?: string) { const url = new URL(baseURL); if (baseURL !== url.origin && baseURL !== `${url.origin}/`) { @@ -13,9 +14,14 @@ export default class CobaltAPI { } this.#baseURL = url.origin; + this.#userAgent = userAgent; } - async request(data: CobaltRequest, headers: Record) { + protected async _request(data: CobaltRequest, headers: Record) { + if (this.#userAgent) { + headers['user-agent'] = this.#userAgent; + } + const response: CobaltAPIResponse = await fetch(this.#baseURL, { method: 'POST', redirect: 'manual', diff --git a/packages/api-client/src/session.ts b/packages/api-client/src/internal/session.ts similarity index 95% rename from packages/api-client/src/session.ts rename to packages/api-client/src/internal/session.ts index aab7cf86..b486b8f7 100644 --- a/packages/api-client/src/session.ts +++ b/packages/api-client/src/internal/session.ts @@ -1,5 +1,5 @@ -import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "./types/response"; -import { CobaltReachabilityError } from "./types/errors"; +import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "../types/response"; +import { CobaltReachabilityError } from "../types/errors"; const currentTime = () => Math.floor(new Date().getTime() / 1000); const EXPIRY_THRESHOLD_SECONDS = 2;