api-client: move base api and session to internal subfolder

This commit is contained in:
dumbmoron 2024-09-11 21:01:41 +00:00
parent 4755787b69
commit afe9917169
No known key found for this signature in database
2 changed files with 14 additions and 8 deletions

View file

@ -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<string, string>) {
protected async _request(data: CobaltRequest, headers: Record<string, string>) {
if (this.#userAgent) {
headers['user-agent'] = this.#userAgent;
}
const response: CobaltAPIResponse = await fetch(this.#baseURL, {
method: 'POST',
redirect: 'manual',

View file

@ -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;