mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-12 19:22:28 +00:00
feat: refactor hydra api
This commit is contained in:
parent
dd23358a95
commit
9e11d6c098
15 changed files with 55 additions and 76 deletions
|
@ -19,8 +19,8 @@ const removeGameFromLibrary = async (
|
|||
const removeRemoveGameFromLibrary = async (gameId: number) => {
|
||||
const game = await gameRepository.findOne({ where: { id: gameId } });
|
||||
|
||||
if (game?.remoteId && HydraApi.isLoggedIn()) {
|
||||
HydraApi.delete(`/games/${game.remoteId}`);
|
||||
if (game?.remoteId) {
|
||||
HydraApi.delete(`/games/${game.remoteId}`).catch();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
|
||||
const isUserLoggedIn = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
return HydraApi.isLoggedIn();
|
||||
};
|
||||
|
||||
registerEvent("isUserLoggedIn", isUserLoggedIn);
|
|
@ -1,15 +1,12 @@
|
|||
import { registerEvent } from "../register-event";
|
||||
import * as Sentry from "@sentry/electron/main";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { UserProfile } from "@types";
|
||||
import { UserNotLoggedInError, UserProfile } from "@types";
|
||||
import { userAuthRepository } from "@main/repository";
|
||||
import { logger } from "@main/services";
|
||||
|
||||
const getMe = async (
|
||||
_event: Electron.IpcMainInvokeEvent
|
||||
): Promise<UserProfile | null> => {
|
||||
if (!HydraApi.isLoggedIn()) return null;
|
||||
|
||||
return HydraApi.get(`/profile/me`)
|
||||
.then((response) => {
|
||||
const me = response.data;
|
||||
|
@ -29,7 +26,10 @@ const getMe = async (
|
|||
return me;
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error("getMe", err.message);
|
||||
if (err instanceof UserNotLoggedInError) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return userAuthRepository.findOne({ where: { id: 1 } });
|
||||
});
|
||||
};
|
||||
|
|
|
@ -26,9 +26,11 @@ const updateProfile = async (
|
|||
_event: Electron.IpcMainInvokeEvent,
|
||||
displayName: string,
|
||||
newProfileImagePath: string | null
|
||||
): Promise<UserProfile> => {
|
||||
) => {
|
||||
if (!newProfileImagePath) {
|
||||
return (await patchUserProfile(displayName)).data;
|
||||
return patchUserProfile(displayName).then(
|
||||
(response) => response.data as UserProfile
|
||||
);
|
||||
}
|
||||
|
||||
const stats = fs.statSync(newProfileImagePath);
|
||||
|
@ -51,11 +53,11 @@ const updateProfile = async (
|
|||
});
|
||||
return profileImageUrl;
|
||||
})
|
||||
.catch(() => {
|
||||
return undefined;
|
||||
});
|
||||
.catch(() => undefined);
|
||||
|
||||
return (await patchUserProfile(displayName, profileImageUrl)).data;
|
||||
return patchUserProfile(displayName, profileImageUrl).then(
|
||||
(response) => response.data as UserProfile
|
||||
);
|
||||
};
|
||||
|
||||
registerEvent("updateProfile", updateProfile);
|
||||
|
|
|
@ -9,8 +9,6 @@ const getUser = async (
|
|||
_event: Electron.IpcMainInvokeEvent,
|
||||
userId: string
|
||||
): Promise<UserProfile | null> => {
|
||||
if (!HydraApi.isLoggedIn()) return null;
|
||||
|
||||
try {
|
||||
const response = await HydraApi.get(`/user/${userId}`);
|
||||
const profile = response.data;
|
||||
|
|
|
@ -22,8 +22,8 @@ const loadState = async (userPreferences: UserPreferences | null) => {
|
|||
if (userPreferences?.realDebridApiToken)
|
||||
RealDebridClient.authorize(userPreferences?.realDebridApiToken);
|
||||
|
||||
HydraApi.setupApi().then(async () => {
|
||||
if (HydraApi.isLoggedIn()) uploadGamesBatch();
|
||||
HydraApi.setupApi().then(() => {
|
||||
uploadGamesBatch();
|
||||
});
|
||||
|
||||
const [nextQueueItem] = await downloadQueueRepository.find({
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -3,23 +3,23 @@ import { HydraApi } from "../hydra-api";
|
|||
import { gameRepository } from "@main/repository";
|
||||
|
||||
export const createGame = async (game: Game) => {
|
||||
if (!HydraApi.isLoggedIn()) return;
|
||||
|
||||
HydraApi.post(`/games`, {
|
||||
objectId: game.objectID,
|
||||
playTimeInMilliseconds: Math.trunc(game.playTimeInMilliseconds),
|
||||
shop: game.shop,
|
||||
lastTimePlayed: game.lastTimePlayed,
|
||||
}).then((response) => {
|
||||
const {
|
||||
id: remoteId,
|
||||
playTimeInMilliseconds,
|
||||
lastTimePlayed,
|
||||
} = response.data;
|
||||
})
|
||||
.then((response) => {
|
||||
const {
|
||||
id: remoteId,
|
||||
playTimeInMilliseconds,
|
||||
lastTimePlayed,
|
||||
} = response.data;
|
||||
|
||||
gameRepository.update(
|
||||
{ objectID: game.objectID },
|
||||
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
||||
);
|
||||
});
|
||||
gameRepository.update(
|
||||
{ objectID: game.objectID },
|
||||
{ remoteId, playTimeInMilliseconds, lastTimePlayed }
|
||||
);
|
||||
})
|
||||
.catch();
|
||||
};
|
||||
|
|
|
@ -2,8 +2,6 @@ import { gameRepository } from "@main/repository";
|
|||
import { HydraApi } from "../hydra-api";
|
||||
import { steamGamesWorker } from "@main/workers";
|
||||
import { getSteamAppAsset } from "@main/helpers";
|
||||
import { logger } from "../logger";
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
export const mergeWithRemoteGames = async () => {
|
||||
try {
|
||||
|
@ -62,11 +60,5 @@ export const mergeWithRemoteGames = async () => {
|
|||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof AxiosError) {
|
||||
logger.error("getRemoteGames", err.message);
|
||||
} else {
|
||||
logger.error("getRemoteGames", err);
|
||||
}
|
||||
}
|
||||
} catch (err) {}
|
||||
};
|
||||
|
|
|
@ -6,10 +6,8 @@ export const updateGamePlaytime = async (
|
|||
deltaInMillis: number,
|
||||
lastTimePlayed: Date
|
||||
) => {
|
||||
if (!HydraApi.isLoggedIn()) return;
|
||||
|
||||
HydraApi.put(`/games/${game.remoteId}`, {
|
||||
playTimeDeltaInSeconds: Math.trunc(deltaInMillis / 1000),
|
||||
lastTimePlayed,
|
||||
});
|
||||
}).catch();
|
||||
};
|
||||
|
|
|
@ -2,9 +2,6 @@ import { gameRepository } from "@main/repository";
|
|||
import { chunk } from "lodash-es";
|
||||
import { IsNull } from "typeorm";
|
||||
import { HydraApi } from "../hydra-api";
|
||||
import { logger } from "../logger";
|
||||
import { AxiosError } from "axios";
|
||||
|
||||
import { mergeWithRemoteGames } from "./merge-with-remote-games";
|
||||
import { WindowManager } from "../window-manager";
|
||||
|
||||
|
@ -27,18 +24,12 @@ export const uploadGamesBatch = async () => {
|
|||
lastTimePlayed: game.lastTimePlayed,
|
||||
};
|
||||
})
|
||||
);
|
||||
).catch();
|
||||
}
|
||||
|
||||
await mergeWithRemoteGames();
|
||||
|
||||
if (WindowManager.mainWindow)
|
||||
WindowManager.mainWindow.webContents.send("on-library-batch-complete");
|
||||
} catch (err) {
|
||||
if (err instanceof AxiosError) {
|
||||
logger.error("uploadGamesBatch", err.response, err.message);
|
||||
} else {
|
||||
logger.error("uploadGamesBatch", err);
|
||||
}
|
||||
}
|
||||
} catch (err) {}
|
||||
};
|
||||
|
|
|
@ -112,7 +112,6 @@ contextBridge.exposeInMainWorld("electron", {
|
|||
getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"),
|
||||
isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"),
|
||||
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
|
||||
isUserLoggedIn: () => ipcRenderer.invoke("isUserLoggedIn"),
|
||||
showOpenDialog: (options: Electron.OpenDialogOptions) =>
|
||||
ipcRenderer.invoke("showOpenDialog", options),
|
||||
platform: process.platform,
|
||||
|
|
|
@ -93,14 +93,8 @@ export function App() {
|
|||
dispatch(setProfileBackground(profileBackground));
|
||||
}
|
||||
|
||||
window.electron.isUserLoggedIn().then((isLoggedIn) => {
|
||||
if (isLoggedIn) {
|
||||
fetchUserDetails().then((response) => {
|
||||
if (response) updateUserDetails(response);
|
||||
});
|
||||
} else {
|
||||
clearUserDetails();
|
||||
}
|
||||
fetchUserDetails().then((response) => {
|
||||
if (response) updateUserDetails(response);
|
||||
});
|
||||
}, [fetchUserDetails, updateUserDetails, dispatch]);
|
||||
|
||||
|
|
1
src/renderer/src/declaration.d.ts
vendored
1
src/renderer/src/declaration.d.ts
vendored
|
@ -100,7 +100,6 @@ declare global {
|
|||
|
||||
/* Misc */
|
||||
openExternal: (src: string) => Promise<void>;
|
||||
isUserLoggedIn: () => Promise<boolean>;
|
||||
getVersion: () => Promise<string>;
|
||||
ping: () => string;
|
||||
getDefaultDownloadsPath: () => Promise<string>;
|
||||
|
|
|
@ -290,3 +290,10 @@ export interface DownloadSource {
|
|||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export class UserNotLoggedInError extends Error {
|
||||
constructor() {
|
||||
super("user not logged in");
|
||||
this.name = "UserNotLoggedInError";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue