api-client: implement individual api clients

This commit is contained in:
dumbmoron 2024-09-11 21:03:11 +00:00
parent a80c7b7a5a
commit 1d30ac0139
No known key found for this signature in database
4 changed files with 48 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import CobaltSessionHandler from "./internal/session";
import BaseCobaltAPI from "./internal/base-api";
import { CobaltRequest } from "./types/request";
import { CobaltAuthError } from "./types/errors";
export default class TurnstileCobaltAPI extends BaseCobaltAPI {
#session: CobaltSessionHandler;
constructor(baseURL: string) {
super(baseURL);
this.#session = new CobaltSessionHandler(baseURL);
}
async request(data: CobaltRequest, turnstileResponse?: string) {
const sessionOrError = await this.#session.getSession(turnstileResponse);
const headers: Record<string, string> = {};
if ("error" in sessionOrError) {
if (sessionOrError.error.code !== CobaltAuthError.NotConfigured) {
return sessionOrError;
}
} else {
headers['Authorization'] = `Bearer ${sessionOrError.token}`;
}
return super._request(data, headers);
}
}

View file

@ -1,4 +1,5 @@
export enum CobaltAuthError { export enum CobaltAuthError {
NotConfigured = 'error.api.auth.not_configured',
JWTMissing = 'error.api.auth.jwt.missing', JWTMissing = 'error.api.auth.jwt.missing',
JWTInvalid = 'error.api.auth.jwt.invalid', JWTInvalid = 'error.api.auth.jwt.invalid',
TurnstileMissing = 'error.api.auth.turnstile.missing', TurnstileMissing = 'error.api.auth.turnstile.missing',

View file

@ -0,0 +1,6 @@
import { CobaltRequest } from "./request";
import { CobaltResponse } from "./response";
export interface CobaltAPIClient {
request(data: CobaltRequest): Promise<CobaltResponse>;
}

View file

@ -0,0 +1,13 @@
import BaseCobaltAPI from "./internal/base-api";
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, {});
}
}