mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-29 18:08:28 +00:00
api-client: move base api and session to internal subfolder
This commit is contained in:
parent
4755787b69
commit
afe9917169
@ -1,11 +1,12 @@
|
|||||||
import { CobaltResponseType, type CobaltAPIResponse } from "./types/response";
|
import { CobaltResponseType, type CobaltAPIResponse } from "../types/response";
|
||||||
import type { CobaltRequest } from "./types/request";
|
import type { CobaltRequest } from "../types/request";
|
||||||
import { CobaltReachabilityError } from "./types/errors";
|
import { CobaltReachabilityError } from "../types/errors";
|
||||||
|
|
||||||
export default class CobaltAPI {
|
export default class BaseCobaltAPI {
|
||||||
#baseURL: string;
|
#baseURL: string;
|
||||||
|
#userAgent: string | undefined;
|
||||||
|
|
||||||
constructor(baseURL: string) {
|
constructor(baseURL: string, userAgent?: string) {
|
||||||
const url = new URL(baseURL);
|
const url = new URL(baseURL);
|
||||||
|
|
||||||
if (baseURL !== url.origin && baseURL !== `${url.origin}/`) {
|
if (baseURL !== url.origin && baseURL !== `${url.origin}/`) {
|
||||||
@ -13,9 +14,14 @@ export default class CobaltAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.#baseURL = url.origin;
|
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, {
|
const response: CobaltAPIResponse = await fetch(this.#baseURL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
redirect: 'manual',
|
redirect: 'manual',
|
@ -1,5 +1,5 @@
|
|||||||
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "./types/response";
|
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "../types/response";
|
||||||
import { CobaltReachabilityError } from "./types/errors";
|
import { CobaltReachabilityError } from "../types/errors";
|
||||||
|
|
||||||
const currentTime = () => Math.floor(new Date().getTime() / 1000);
|
const currentTime = () => Math.floor(new Date().getTime() / 1000);
|
||||||
const EXPIRY_THRESHOLD_SECONDS = 2;
|
const EXPIRY_THRESHOLD_SECONDS = 2;
|
Loading…
Reference in New Issue
Block a user