feat: refactor hydra api

This commit is contained in:
Zamitto 2024-07-02 14:55:38 -03:00
parent dd23358a95
commit 9e11d6c098
15 changed files with 55 additions and 76 deletions

View file

@ -5,6 +5,7 @@ import url from "url";
import { uploadGamesBatch } from "./library-sync";
import { clearGamesRemoteIds } from "./library-sync/clear-games-remote-id";
import { logger } from "./logger";
import { UserNotLoggedInError } from "@types";
export class HydraApi {
private static instance: AxiosInstance;
@ -19,7 +20,7 @@ export class HydraApi {
expirationTimestamp: 0,
};
static isLoggedIn() {
private static isLoggedIn() {
return this.userAuth.authToken !== "";
}
@ -127,10 +128,6 @@ export class HydraApi {
}
private static async revalidateAccessTokenIfExpired() {
if (!this.isLoggedIn()) {
throw new Error("user is not logged in");
}
const now = new Date();
if (this.userAuth.expirationTimestamp < now.getTime()) {
@ -188,6 +185,8 @@ export class HydraApi {
};
static async get(url: string) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired();
return this.instance
.get(url, this.getAxiosConfig())
@ -195,6 +194,8 @@ export class HydraApi {
}
static async post(url: string, data?: any) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired();
return this.instance
.post(url, data, this.getAxiosConfig())
@ -202,6 +203,8 @@ export class HydraApi {
}
static async put(url: string, data?: any) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired();
return this.instance
.put(url, data, this.getAxiosConfig())
@ -209,6 +212,8 @@ export class HydraApi {
}
static async patch(url: string, data?: any) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired();
return this.instance
.patch(url, data, this.getAxiosConfig())
@ -216,6 +221,8 @@ export class HydraApi {
}
static async delete(url: string) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired();
return this.instance
.delete(url, this.getAxiosConfig())