api-client: move base api and session to internal subfolder
This commit is contained in:
51
packages/api-client/src/internal/base-api.ts
Normal file
51
packages/api-client/src/internal/base-api.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { CobaltResponseType, type CobaltAPIResponse } from "../types/response";
|
||||
import type { CobaltRequest } from "../types/request";
|
||||
import { CobaltReachabilityError } from "../types/errors";
|
||||
|
||||
export default class BaseCobaltAPI {
|
||||
#baseURL: string;
|
||||
#userAgent: string | undefined;
|
||||
|
||||
constructor(baseURL: string, userAgent?: string) {
|
||||
const url = new URL(baseURL);
|
||||
|
||||
if (baseURL !== url.origin && baseURL !== `${url.origin}/`) {
|
||||
throw new Error('Invalid cobalt instance URL');
|
||||
}
|
||||
|
||||
this.#baseURL = url.origin;
|
||||
this.#userAgent = userAgent;
|
||||
}
|
||||
|
||||
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',
|
||||
signal: AbortSignal.timeout(10000),
|
||||
body: JSON.stringify(data),
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
...headers
|
||||
}
|
||||
})
|
||||
.then(r => r.json())
|
||||
.catch((e) => {
|
||||
const timedOut = e?.message?.includes("timed out");
|
||||
return {
|
||||
status: CobaltResponseType.Error,
|
||||
error: {
|
||||
code: timedOut
|
||||
? CobaltReachabilityError.TimedOut
|
||||
: CobaltReachabilityError.Unreachable
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user