mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-01-23 13:34:54 +03:00
feat: create HydraApi
This commit is contained in:
parent
d31d056e5e
commit
81f2e509c5
@ -1,4 +1,3 @@
|
||||
MAIN_VITE_STEAMGRIDDB_API_KEY=YOUR_API_KEY
|
||||
MAIN_VITE_ONLINEFIX_USERNAME=YOUR_USERNAME
|
||||
MAIN_VITE_ONLINEFIX_PASSWORD=YOUR_PASSWORD
|
||||
MAIN_VITE_API_URL=API_URL
|
||||
|
||||
|
@ -37,4 +37,10 @@ export class UserPreferences {
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
accessToken: string;
|
||||
|
||||
@Column("text", { default: "" })
|
||||
refreshToken: string;
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import { RealDebridClient } from "./services/real-debrid";
|
||||
import { fetchDownloadSourcesAndUpdate } from "./helpers";
|
||||
import { publishNewRepacksNotifications } from "./services/notifications";
|
||||
import { MoreThan } from "typeorm";
|
||||
import { HydraApi } from "./services/hydra-api";
|
||||
|
||||
startMainLoop();
|
||||
|
||||
@ -20,6 +21,8 @@ const loadState = async (userPreferences: UserPreferences | null) => {
|
||||
if (userPreferences?.realDebridApiToken)
|
||||
RealDebridClient.authorize(userPreferences?.realDebridApiToken);
|
||||
|
||||
HydraApi.createInstance();
|
||||
|
||||
const [nextQueueItem] = await downloadQueueRepository.find({
|
||||
order: {
|
||||
id: "DESC",
|
||||
|
88
src/main/services/hydra-api.ts
Normal file
88
src/main/services/hydra-api.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import { userPreferencesRepository } from "@main/repository";
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
|
||||
export class HydraApi {
|
||||
private static instance: AxiosInstance;
|
||||
|
||||
static authToken = "";
|
||||
static refreshToken = "";
|
||||
|
||||
static async createInstance() {
|
||||
this.instance = axios.create({
|
||||
baseURL: import.meta.env.MAIN_VITE_API_URL,
|
||||
});
|
||||
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
|
||||
this.authToken = userPreferences?.accessToken ?? "";
|
||||
this.refreshToken = userPreferences?.refreshToken ?? "";
|
||||
|
||||
this.instance.interceptors.request.use(
|
||||
(config) => {
|
||||
config.headers.Authorization = `Bearer ${this.authToken}`;
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
this.instance.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const originalRequest = error.config;
|
||||
if (error.response.status === 401 && !originalRequest._retry) {
|
||||
originalRequest._retry = true;
|
||||
const refreshToken = this.refreshToken;
|
||||
if (refreshToken) {
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${import.meta.env.MAIN_VITE_API_URL}/auth/refresh`,
|
||||
{ refreshToken }
|
||||
);
|
||||
const newAccessToken = response.data.accessToken;
|
||||
this.authToken = newAccessToken;
|
||||
|
||||
userPreferencesRepository.upsert(
|
||||
{
|
||||
id: 1,
|
||||
accessToken: newAccessToken,
|
||||
},
|
||||
["id"]
|
||||
);
|
||||
|
||||
originalRequest.headers.Authorization = `Bearer ${newAccessToken}`;
|
||||
return axios(originalRequest); //recall Api with new token
|
||||
} catch (error) {
|
||||
this.authToken = "";
|
||||
this.refreshToken = "";
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
static async get(url: string) {
|
||||
return this.instance.get(url);
|
||||
}
|
||||
|
||||
static async post(url: string, data?: any) {
|
||||
return this.instance.post(url, data);
|
||||
}
|
||||
|
||||
static async put(url, data?: any) {
|
||||
return this.instance.put(url, data);
|
||||
}
|
||||
|
||||
static async patch(url, data?: any) {
|
||||
return this.instance.patch(url, data);
|
||||
}
|
||||
}
|
3
src/main/vite-env.d.ts
vendored
3
src/main/vite-env.d.ts
vendored
@ -2,8 +2,7 @@
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly MAIN_VITE_STEAMGRIDDB_API_KEY: string;
|
||||
readonly MAIN_VITE_ONLINEFIX_USERNAME: string;
|
||||
readonly MAIN_VITE_ONLINEFIX_PASSWORD: string;
|
||||
readonly MAIN_VITE_API_URL: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
|
Loading…
Reference in New Issue
Block a user