mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-30 02:18:29 +00:00
api-client: migrate session handler and response types
This commit is contained in:
parent
ab8650030b
commit
6cc1227288
59
packages/api-client/src/session.ts
Normal file
59
packages/api-client/src/session.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "./types/response";
|
||||||
|
import { CobaltReachabilityError } from "./types/errors";
|
||||||
|
|
||||||
|
const currentTime = () => Math.floor(new Date().getTime() / 1000);
|
||||||
|
const EXPIRY_THRESHOLD_SECONDS = 2;
|
||||||
|
|
||||||
|
export default class CobaltSessionHandler {
|
||||||
|
#baseURL: string;
|
||||||
|
#session: CobaltSession | undefined;
|
||||||
|
|
||||||
|
constructor(baseURL: string) {
|
||||||
|
this.#baseURL = baseURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
async #requestSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
|
||||||
|
const endpoint = new URL('/session', this.#baseURL);
|
||||||
|
const headers: Record<string, string> = {};
|
||||||
|
|
||||||
|
if (turnstileResponse) {
|
||||||
|
headers['cf-turnstile-response'] = turnstileResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(endpoint, {
|
||||||
|
method: 'POST',
|
||||||
|
redirect: 'manual',
|
||||||
|
signal: AbortSignal.timeout(10000),
|
||||||
|
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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if ('token' in response && 'exp' in response) {
|
||||||
|
this.#session = {
|
||||||
|
token: response.token,
|
||||||
|
exp: currentTime() + response.exp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSession(turnstileResponse?: string): Promise<CobaltSessionResponse> {
|
||||||
|
if (this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime()) {
|
||||||
|
return this.#session;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.#requestSession(turnstileResponse);
|
||||||
|
}
|
||||||
|
};
|
@ -56,7 +56,7 @@ export enum CobaltYouTubeError {
|
|||||||
TokenExpired = 'error.api.youtube.token_expired'
|
TokenExpired = 'error.api.youtube.token_expired'
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CobaltAPIError = CobaltAuthError
|
export type CobaltAPIErrorCode = CobaltAuthError
|
||||||
| CobaltReachabilityError
|
| CobaltReachabilityError
|
||||||
| CobaltGenericError
|
| CobaltGenericError
|
||||||
| CobaltServiceError
|
| CobaltServiceError
|
||||||
|
71
packages/api-client/src/types/response.ts
Normal file
71
packages/api-client/src/types/response.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { CobaltAPIErrorCode } from "./errors";
|
||||||
|
|
||||||
|
export enum CobaltResponseType {
|
||||||
|
Error = 'error',
|
||||||
|
Picker = 'picker',
|
||||||
|
Redirect = 'redirect',
|
||||||
|
Tunnel = 'tunnel',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CobaltErrorResponse = {
|
||||||
|
status: CobaltResponseType.Error,
|
||||||
|
error: {
|
||||||
|
code: CobaltAPIErrorCode,
|
||||||
|
context?: {
|
||||||
|
service?: string,
|
||||||
|
limit?: number,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
type CobaltPartialURLResponse = {
|
||||||
|
url: string,
|
||||||
|
filename: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
type CobaltPickerResponse = {
|
||||||
|
status: CobaltResponseType.Picker
|
||||||
|
picker: {
|
||||||
|
type: 'photo' | 'video' | 'gif',
|
||||||
|
url: string,
|
||||||
|
thumb?: string,
|
||||||
|
}[];
|
||||||
|
audio?: string,
|
||||||
|
audioFilename?: string,
|
||||||
|
};
|
||||||
|
|
||||||
|
type CobaltRedirectResponse = {
|
||||||
|
status: CobaltResponseType.Redirect,
|
||||||
|
} & CobaltPartialURLResponse;
|
||||||
|
|
||||||
|
type CobaltTunnelResponse = {
|
||||||
|
status: CobaltResponseType.Tunnel,
|
||||||
|
} & CobaltPartialURLResponse;
|
||||||
|
|
||||||
|
export type CobaltSession = {
|
||||||
|
token: string,
|
||||||
|
exp: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CobaltServerInfo = {
|
||||||
|
cobalt: {
|
||||||
|
version: string,
|
||||||
|
url: string,
|
||||||
|
startTime: string,
|
||||||
|
durationLimit: number,
|
||||||
|
services: string[]
|
||||||
|
},
|
||||||
|
git: {
|
||||||
|
branch: string,
|
||||||
|
commit: string,
|
||||||
|
remote: string,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CobaltSessionResponse = CobaltSession | CobaltErrorResponse;
|
||||||
|
export type CobaltServerInfoResponse = CobaltServerInfo | CobaltErrorResponse;
|
||||||
|
|
||||||
|
export type CobaltAPIResponse = CobaltErrorResponse
|
||||||
|
| CobaltPickerResponse
|
||||||
|
| CobaltRedirectResponse
|
||||||
|
| CobaltTunnelResponse;
|
Loading…
Reference in New Issue
Block a user