api-client: make base URL reconfigurable
This commit is contained in:
parent
d3e278660d
commit
64ac458941
4 changed files with 30 additions and 13 deletions
|
@ -1,22 +1,30 @@
|
|||
import { CobaltResponseType, type CobaltResponse } from "../types/response";
|
||||
import { CobaltReachabilityError } from "../types/errors";
|
||||
import type { CobaltRequest } from "../types/request";
|
||||
import { CobaltAPIClient } from "../types/interface";
|
||||
|
||||
export default class BaseCobaltAPI {
|
||||
#baseURL: string;
|
||||
export default class CobaltAPI implements CobaltAPIClient {
|
||||
#baseURL: string | undefined;
|
||||
|
||||
constructor(baseURL: string) {
|
||||
getBaseURL() {
|
||||
return this.#baseURL;
|
||||
}
|
||||
|
||||
setBaseURL(baseURL: string) {
|
||||
const url = new URL(baseURL);
|
||||
|
||||
if (baseURL !== url.origin && baseURL !== `${url.origin}/`) {
|
||||
throw new Error('Invalid cobalt instance URL');
|
||||
}
|
||||
|
||||
this.#baseURL = url.origin;
|
||||
return this.#baseURL = url.origin;
|
||||
}
|
||||
|
||||
async request(data: CobaltRequest, headers?: Record<string, string>) {
|
||||
const response: CobaltResponse = await fetch(this.#baseURL, {
|
||||
const baseURL = this.getBaseURL();
|
||||
if (!baseURL) throw "baseURL is undefined";
|
||||
|
||||
const response: CobaltResponse = await fetch(baseURL, {
|
||||
method: 'POST',
|
||||
redirect: 'manual',
|
||||
signal: AbortSignal.timeout(10000),
|
||||
|
|
|
@ -1,23 +1,30 @@
|
|||
import CobaltSessionHandler from "./internal/session";
|
||||
import BaseCobaltAPI from "./internal/base-api";
|
||||
import CobaltAPI 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 {
|
||||
export class TurnstileCobaltAPI extends CobaltAPI {
|
||||
#session: CobaltSessionHandler;
|
||||
#instanceHasTurnstile = true;
|
||||
|
||||
constructor(baseURL: string, turnstile: TurnstileObject) {
|
||||
super(baseURL);
|
||||
this.#session = new CobaltSessionHandler(baseURL, turnstile);
|
||||
constructor(turnstile: TurnstileObject) {
|
||||
super();
|
||||
this.#session = new CobaltSessionHandler(this, turnstile);
|
||||
}
|
||||
|
||||
needsSession() {
|
||||
return this.#instanceHasTurnstile && !this.#session.hasSession();
|
||||
}
|
||||
|
||||
setBaseURL(baseURL: string): string {
|
||||
if (this.#session && super.getBaseURL() !== super.setBaseURL(baseURL)) {
|
||||
this.#session.reset();
|
||||
}
|
||||
|
||||
return super.getBaseURL()!;
|
||||
}
|
||||
|
||||
async request(data: CobaltRequest) {
|
||||
const sessionOrError = await this.#session.getSession();
|
||||
const headers: Record<string, string> = {};
|
||||
|
|
|
@ -3,4 +3,6 @@ import { CobaltResponse } from "./response";
|
|||
|
||||
export interface CobaltAPIClient {
|
||||
request(data: CobaltRequest): Promise<CobaltResponse>;
|
||||
getBaseURL(): string | undefined;
|
||||
setBaseURL(baseURL: string): string;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import BaseCobaltAPI from "./internal/base-api";
|
||||
import CobaltAPI from "./internal/base-api";
|
||||
import { CobaltRequest } from "./types/request";
|
||||
import { CobaltAPIClient } from "./types/interface";
|
||||
|
||||
export default class UnauthenticatedCobaltAPI extends BaseCobaltAPI implements CobaltAPIClient {
|
||||
export class UnauthenticatedCobaltAPI extends CobaltAPI implements CobaltAPIClient {
|
||||
async request(data: CobaltRequest) {
|
||||
return super.request(data, {});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue