mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-29 01:48:28 +00:00
api-client/session: pull URL from client
This commit is contained in:
parent
d201deb60a
commit
d3e278660d
@ -1,29 +1,32 @@
|
|||||||
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "../types/response";
|
import { CobaltSession, CobaltResponseType, CobaltSessionResponse } from "../types/response";
|
||||||
import { CobaltReachabilityError } from "../types/errors";
|
import { CobaltReachabilityError } from "../types/errors";
|
||||||
import type { TurnstileObject } from "turnstile-types";
|
import type { TurnstileObject } from "turnstile-types";
|
||||||
|
import { CobaltAPIClient } from "../types/interface";
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
export default class CobaltSessionHandler {
|
export default class CobaltSessionHandler {
|
||||||
#baseURL: string;
|
#client: CobaltAPIClient;
|
||||||
#turnstile: TurnstileObject;
|
#turnstile: TurnstileObject;
|
||||||
#session: CobaltSession | undefined;
|
#session: CobaltSession | undefined;
|
||||||
|
|
||||||
constructor(baseURL: string, turnstile: TurnstileObject) {
|
constructor(client: CobaltAPIClient, turnstile: TurnstileObject) {
|
||||||
this.#baseURL = baseURL;
|
this.#client = client;
|
||||||
this.#turnstile = turnstile;
|
this.#turnstile = turnstile;
|
||||||
}
|
}
|
||||||
|
|
||||||
async #requestSession(): Promise<CobaltSessionResponse> {
|
async #requestSession(): Promise<CobaltSessionResponse> {
|
||||||
const endpoint = new URL('/session', this.#baseURL);
|
const baseURL = this.#client.getBaseURL();
|
||||||
|
if (!baseURL) throw "baseURL is undefined";
|
||||||
|
|
||||||
|
const endpoint = new URL('/session', baseURL);
|
||||||
const response = await fetch(endpoint, {
|
const response = await fetch(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
redirect: 'manual',
|
redirect: 'manual',
|
||||||
signal: AbortSignal.timeout(10000),
|
signal: AbortSignal.timeout(10000),
|
||||||
headers: {
|
headers: {
|
||||||
'cf-turnstile-response': this.#turnstile.getResponse('turnstile-widget')
|
'cf-turnstile-response': this.#turnstile.getResponse('#turnstile-widget')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
@ -46,7 +49,7 @@ export default class CobaltSessionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#turnstile.reset('turnstile-widget');
|
this.#turnstile.reset('#turnstile-widget');
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
@ -55,6 +58,10 @@ export default class CobaltSessionHandler {
|
|||||||
return this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime();
|
return this.#session && this.#session.exp - EXPIRY_THRESHOLD_SECONDS > currentTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.#session = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
async getSession(): Promise<CobaltSessionResponse> {
|
async getSession(): Promise<CobaltSessionResponse> {
|
||||||
if (this.hasSession()) {
|
if (this.hasSession()) {
|
||||||
return this.#session!;
|
return this.#session!;
|
||||||
|
Loading…
Reference in New Issue
Block a user