feat: add If-Modified-Since header to get-game-achievement-data

This commit is contained in:
Zamitto
2025-06-06 16:52:40 -03:00
parent 0b2d4e2ba0
commit 100ddd79aa
4 changed files with 34 additions and 13 deletions

View File

@@ -3,6 +3,9 @@ import type { GameShop, SteamAchievement } from "@types";
import { UserNotLoggedInError } from "@shared";
import { logger } from "../logger";
import { db, gameAchievementsSublevel, levelKeys } from "@main/level";
import { AxiosError } from "axios";
const LOCAL_CACHE_EXPIRATION = 1000 * 60 * 30; // 30 minutes
export const getGameAchievementData = async (
objectId: string,
@@ -18,7 +21,7 @@ export const getGameAchievementData = async (
if (
cachedAchievements?.achievements &&
Date.now() < (cachedAchievements.cacheExpiresTimestamp ?? 0)
Date.now() < (cachedAchievements.updatedAt ?? 0) + LOCAL_CACHE_EXPIRATION
) {
return cachedAchievements.achievements;
}
@@ -29,18 +32,24 @@ export const getGameAchievementData = async (
})
.then((language) => language || "en");
return HydraApi.get<SteamAchievement[]>("/games/achievements", {
shop,
objectId,
language,
})
return HydraApi.get<SteamAchievement[]>(
"/games/achievements",
{
shop,
objectId,
language,
},
{
ifModifiedSince: cachedAchievements?.updatedAt
? new Date(cachedAchievements?.updatedAt)
: undefined,
}
)
.then(async (achievements) => {
await gameAchievementsSublevel.put(gameKey, {
unlockedAchievements: cachedAchievements?.unlockedAchievements ?? [],
achievements,
cacheExpiresTimestamp: achievements.length
? Date.now() + 1000 * 60 * 30 // 30 minutes
: undefined,
updatedAt: Date.now() + LOCAL_CACHE_EXPIRATION,
});
return achievements;
@@ -50,6 +59,12 @@ export const getGameAchievementData = async (
throw err;
}
const isNotModified = (err as AxiosError)?.response?.status === 304;
if (isNotModified) {
return cachedAchievements?.achievements ?? [];
}
logger.error("Failed to get game achievements for", objectId, err);
return [];

View File

@@ -36,7 +36,7 @@ const saveAchievementsOnLocal = async (
await gameAchievementsSublevel.put(levelKey, {
achievements: gameAchievement?.achievements ?? [],
unlockedAchievements: unlockedAchievements,
cacheExpiresTimestamp: gameAchievement?.cacheExpiresTimestamp,
updatedAt: gameAchievement?.updatedAt,
});
if (!sendUpdateEvent) return;

View File

@@ -1,4 +1,4 @@
import axios, { AxiosError, AxiosInstance } from "axios";
import axios, { AxiosError, AxiosHeaders, AxiosInstance } from "axios";
import { WindowManager } from "./window-manager";
import url from "url";
import { uploadGamesBatch } from "./library-sync";
@@ -16,6 +16,7 @@ import { WSClient } from "./ws/ws-client";
interface HydraApiOptions {
needsAuth?: boolean;
needsSubscription?: boolean;
ifModifiedSince?: Date;
}
interface HydraApiUserAuth {
@@ -337,8 +338,13 @@ export class HydraApi {
) {
await this.validateOptions(options);
const headers = {
...this.getAxiosConfig().headers,
"If-Modified-Since": options?.ifModifiedSince?.toUTCString(),
};
return this.instance
.get<T>(url, { params, ...this.getAxiosConfig() })
.get<T>(url, { params, ...this.getAxiosConfig(), headers })
.then((response) => response.data)
.catch(this.handleUnauthorizedError);
}

View File

@@ -68,7 +68,7 @@ export interface Download {
export interface GameAchievement {
achievements: SteamAchievement[];
unlockedAchievements: UnlockedAchievement[];
cacheExpiresTimestamp: number | undefined;
updatedAt: number | undefined;
}
export type AchievementCustomNotificationPosition =