Skip to content

Commit

Permalink
Improve type safety of FBadgeProgress, add SteamCommunityApiFacade, u…
Browse files Browse the repository at this point in the history
…se BackgroundSender
  • Loading branch information
tfedor committed Apr 10, 2024
1 parent cd7ce49 commit 01a2b6d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Errors, GameId, HTMLParser, LocalStorage} from "../../modulesCore";
import {Api} from "./Api";
import CacheStorage from "./CacheStorage";
import {IndexedDB} from "./IndexedDB";
import {Errors, GameId, HTMLParser, LocalStorage} from "../../../modulesCore";
import {Api} from "../Api";
import CacheStorage from "../CacheStorage";
import {IndexedDB} from "../IndexedDB";
import type {TFetchBadgeInfoResponse} from "./_types";

class SteamCommunityApi extends Api {

Expand All @@ -10,7 +11,7 @@ class SteamCommunityApi extends Api {
* static params = { 'credentials': 'include', };
*/

static cards(steamid, appid) {
static fetchBadgeInfo(steamid: string, appid: number): Promise<TFetchBadgeInfoResponse> {
return SteamCommunityApi.getEndpoint(`/profiles/${steamid}/ajaxgetbadgeinfo/${appid}`);
}

Expand Down
37 changes: 37 additions & 0 deletions src/js/Background/Modules/Community/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

interface TCardInfo {
name: string;
title: string;
imgurl: string;
arturl: string;
owned: 0|1;
markethash: string;
}

export interface TBadgeData {
appid: number;
border: 0|1;
series: number;
level: number;
maxlevel: number;
name: string; // empty if no badge level
xp: number;
nextlevelname: string;
nextlevelxp: number;
iconurl: string;
bMaxed: null|unknown;
rgCards: TCardInfo[]
}

export interface TFetchBadgeInfoMessage {
action: "community.badgeinfo",
params: {
steamId: string,
appid: number
}
}

export interface TFetchBadgeInfoResponse {
eresult: number,
badgedata?: TBadgeData
}
21 changes: 18 additions & 3 deletions src/js/Background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import setup from "../setup";
import {LocalStorage, Permissions, SyncedStorage} from "../modulesCore";
import {ContextMenu} from "./Modules/ContextMenu";
import {IndexedDB} from "./Modules/IndexedDB";
import {SteamCommunityApi} from "./Modules/SteamCommunityApi";
import {SteamCommunityApi} from "./Modules/Community/SteamCommunityApi";
import {SteamStoreApi} from "./Modules/SteamStoreApi";
import {StaticResources} from "./Modules/StaticResources";
import {ITADApi} from "./Modules/IsThereAnyDeal/ITADApi";
Expand All @@ -12,6 +12,7 @@ import CacheStorage from "./Modules/CacheStorage";
import browser, {type Runtime} from "webextension-polyfill";
import type {TFetchPricesMessage} from "./Modules/AugmentedSteam/_types";
import type {TGetStoreListMessage} from "./Modules/IsThereAnyDeal/_types";
import type {TFetchBadgeInfoMessage} from "./Modules/Community/_types";

type MessageSender = Runtime.MessageSender;

Expand Down Expand Up @@ -99,7 +100,7 @@ const actionCallbacks = new Map([
["login", SteamCommunityApi.login],
["logout", SteamCommunityApi.logout],
["storecountry", SteamCommunityApi.storeCountry],
["cards", SteamCommunityApi.cards],
["cards", SteamCommunityApi.fetchBadgeInfo],
["coupon", SteamCommunityApi.getCoupon],
["hasgiftsandpasses", SteamCommunityApi.hasGiftsAndPasses],
["hascoupon", SteamCommunityApi.hasCoupon],
Expand Down Expand Up @@ -136,6 +137,8 @@ type Message =
| TGetStoreListMessage
// AugmentedSteamApi
| TFetchPricesMessage
// SteamCommunityApi
| TFetchBadgeInfoMessage
// old
| GenericMessage;

Expand All @@ -152,12 +155,17 @@ browser.runtime.onMessage.addListener((
return;
}

(async function(): void {
(async function(): Promise<void> {
try {
await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]);

let response: any;

/*
* TODO: (<>message.)params typecast should be needed only until we allow GenericMessage, once we get rid of it,
* remove type cast, which should also ensure better checks
*/

switch (message.action) { // TODO rename to "api"?

case "itad.storelist":
Expand All @@ -169,6 +177,13 @@ browser.runtime.onMessage.addListener((
response = await AugmentedSteamApi.fetchPrices(country, apps, subs, bundles, voucher, shops);
break;
}

case "community.badgeinfo":
const {steamId, appid} = (<TFetchBadgeInfoMessage>message).params;
response = await SteamCommunityApi.fetchBadgeInfo(steamId, appid);
break;


default: {
/*
* TODO deprecated
Expand Down
40 changes: 10 additions & 30 deletions src/js/Content/Features/Store/App/FBadgeProgress.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// @ts-ignore
import self_ from "./FBadgeProgress.svelte";
import {SyncedStorage} from "../../../../modulesCore";
import {Background, Feature, User} from "../../../modulesContent";
import {Feature, User} from "../../../modulesContent";
import type {CApp} from "./CApp";
import type {TFetchBadgeInfoResponse} from "../../../../Background/Modules/Community/_types";
import SteamCommunityApiFacade from "../../../Modules/Facades/SteamCommunityApiFacade";
export class FBadgeProgress extends Feature<CApp> {
Expand All @@ -14,15 +16,12 @@
}
override async apply(): Promise<void> {
let data;
let response: TFetchBadgeInfoResponse = await SteamCommunityApiFacade.fetchBadgeInfo(
User.steamId,
this.context.communityAppid
);
try {
data = await Background.action("cards", User.steamId, this.context.communityAppid);
} catch (err) {
throw new Error("Failed to fetch badges", {"cause": err});
}
data = data.badgedata;
let data = response.badgedata;
// No badge data if game doesn't have cards or not logged in
if (!data) {
Expand All @@ -45,28 +44,9 @@

<script lang="ts">
import {Localization} from "../../../../modulesCore";
import type {TBadgeData} from "../../../../Background/Modules/Community/_types";
export let data: {
appid: number;
border: 0|1;
series: number;
level: number;
maxlevel: number;
name: string; // empty if no badge level
xp: number;
nextlevelname: string;
nextlevelxp: number;
iconurl: string;
bMaxed: null|boolean; // not sure
rgCards: {
name: string;
title: string;
imgurl: string;
arturl: string;
owned: 0|1;
markethash: string;
}[];
};
export let data: TBadgeData;
let cardOwned = data.rgCards.filter(c => c.owned === 1).length;
let cardTotal = data.rgCards.length;
Expand Down
12 changes: 12 additions & 0 deletions src/js/Content/Modules/Facades/SteamCommunityApiFacade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {BackgroundSender} from "../../../Core/BackgroundSimple";
import type {TFetchBadgeInfoMessage, TFetchBadgeInfoResponse} from "../../../Background/Modules/Community/_types";

export default class SteamCommunityApiFacade {

static async fetchBadgeInfo(steamId: string, appid: number): Promise<TFetchBadgeInfoResponse> {
return await BackgroundSender.send<TFetchBadgeInfoMessage, TFetchBadgeInfoResponse>({
action: "community.badgeinfo",
params: {steamId, appid}
});
}
}
5 changes: 4 additions & 1 deletion src/js/Core/BackgroundSimple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import browser from "webextension-polyfill";

/** @deprecated */
class BackgroundSimple {

/** @deprecated */
static message(message) {
return browser.runtime.sendMessage(message);
}

/** @deprecated */
static action(requested, ...params) {
if (!params.length) { return this.message({"action": requested}); }
return this.message({"action": requested, "params": params});
}
}

class BackgroundSender {
static send<T, U>(message: T): Promise<U> {
static send<Request, Response>(message: Request): Promise<Response> {
return browser.runtime.sendMessage(message);
}
}
Expand Down

0 comments on commit 01a2b6d

Please sign in to comment.