api-client: implement individual api clients
This commit is contained in:
parent
a80c7b7a5a
commit
1d30ac0139
4 changed files with 48 additions and 0 deletions
28
packages/api-client/src/turnstile-api.ts
Normal file
28
packages/api-client/src/turnstile-api.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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',
|
||||||
|
|
6
packages/api-client/src/types/interface.ts
Normal file
6
packages/api-client/src/types/interface.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import { CobaltRequest } from "./request";
|
||||||
|
import { CobaltResponse } from "./response";
|
||||||
|
|
||||||
|
export interface CobaltAPIClient {
|
||||||
|
request(data: CobaltRequest): Promise<CobaltResponse>;
|
||||||
|
}
|
13
packages/api-client/src/unauthenticated-api.ts
Normal file
13
packages/api-client/src/unauthenticated-api.ts
Normal 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, {});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue