chore: turn getJSONfromString into a template/generic function #836

Merged
Sainan merged 1 commits from json-template into main 2025-01-24 05:27:10 -08:00
28 changed files with 36 additions and 36 deletions

View File

@ -9,7 +9,7 @@ import { ExportUpgrades } from "warframe-public-export-plus";
export const activateRandomModController: RequestHandler = async (req, res) => { export const activateRandomModController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const request = getJSONfromString(String(req.body)) as IActiveRandomModRequest; const request = getJSONfromString<IActiveRandomModRequest>(String(req.body));
addMods(inventory, [ addMods(inventory, [
{ {
ItemType: request.ItemType, ItemType: request.ItemType,

View File

@ -6,7 +6,7 @@ import { getInventory } from "@/src/services/inventoryService";
const addFriendImageController: RequestHandler = async (req, res) => { const addFriendImageController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const json = getJSONfromString(String(req.body)) as IUpdateGlyphRequest; const json = getJSONfromString<IUpdateGlyphRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
inventory.ActiveAvatarImageType = json.AvatarImageType; inventory.ActiveAvatarImageType = json.AvatarImageType;
await inventory.save(); await inventory.save();

View File

@ -6,7 +6,7 @@ import { IOid } from "@/src/types/commonTypes";
export const arcaneCommonController: RequestHandler = async (req, res) => { export const arcaneCommonController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const json = getJSONfromString(String(req.body)) as IArcaneCommonRequest; const json = getJSONfromString<IArcaneCommonRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const upgrade = inventory.Upgrades.id(json.arcane.ItemId.$oid); const upgrade = inventory.Upgrades.id(json.arcane.ItemId.$oid);
if (json.newRank == -1) { if (json.newRank == -1) {

View File

@ -7,7 +7,7 @@ import { config } from "@/src/services/configService";
export const artifactsController: RequestHandler = async (req, res) => { export const artifactsController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const artifactsData = getJSONfromString(String(req.body)) as IArtifactsRequest; const artifactsData = getJSONfromString<IArtifactsRequest>(String(req.body));
const { Upgrade, LevelDiff, Cost, FusionPointCost } = artifactsData; const { Upgrade, LevelDiff, Cost, FusionPointCost } = artifactsData;

View File

@ -21,7 +21,7 @@ export interface IClaimCompletedRecipeRequest {
} }
export const claimCompletedRecipeController: RequestHandler = async (req, res) => { export const claimCompletedRecipeController: RequestHandler = async (req, res) => {
const claimCompletedRecipeRequest = getJSONfromString(String(req.body)) as IClaimCompletedRecipeRequest; const claimCompletedRecipeRequest = getJSONfromString<IClaimCompletedRecipeRequest>(String(req.body));
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
if (!accountId) throw new Error("no account id"); if (!accountId) throw new Error("no account id");

View File

@ -6,7 +6,7 @@ import { Guild } from "@/src/models/guildModel";
export const createGuildController: RequestHandler = async (req, res) => { export const createGuildController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const payload = getJSONfromString(String(req.body)) as ICreateGuildRequest; const payload = getJSONfromString<ICreateGuildRequest>(String(req.body));
// Create guild on database // Create guild on database
const guild = new Guild({ const guild = new Guild({

View File

@ -7,7 +7,7 @@ import { TEndlessXpCategory } from "@/src/types/inventoryTypes/inventoryTypes";
export const endlessXpController: RequestHandler = async (req, res) => { export const endlessXpController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const payload = getJSONfromString(String(req.body)) as IEndlessXpRequest; const payload = getJSONfromString<IEndlessXpRequest>(String(req.body));
inventory.EndlessXP ??= []; inventory.EndlessXP ??= [];
const entry = inventory.EndlessXP.find(x => x.Category == payload.Category); const entry = inventory.EndlessXP.find(x => x.Category == payload.Category);

View File

@ -8,7 +8,7 @@ import { EquipmentFeatures } from "@/src/types/inventoryTypes/commonInventoryTyp
export const evolveWeaponController: RequestHandler = async (req, res) => { export const evolveWeaponController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const payload = getJSONfromString(String(req.body)) as IEvolveWeaponRequest; const payload = getJSONfromString<IEvolveWeaponRequest>(String(req.body));
const recipe = getRecipe(payload.Recipe)!; const recipe = getRecipe(payload.Recipe)!;
if (payload.Action == "EWA_INSTALL") { if (payload.Action == "EWA_INSTALL") {

View File

@ -9,7 +9,7 @@ import { ExportResources, ExportSyndicates } from "warframe-public-export-plus";
export const fishmongerController: RequestHandler = async (req, res) => { export const fishmongerController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const body = getJSONfromString(String(req.body)) as IFishmongerRequest; const body = getJSONfromString<IFishmongerRequest>(String(req.body));
const miscItemChanges: IMiscItem[] = []; const miscItemChanges: IMiscItem[] = [];
let syndicateTag: string | undefined; let syndicateTag: string | undefined;
let gainedStanding = 0; let gainedStanding = 0;

View File

@ -9,7 +9,7 @@ import { IGenericUpdate } from "@/src/types/genericUpdate";
const genericUpdateController: RequestHandler = async (request, response) => { const genericUpdateController: RequestHandler = async (request, response) => {
const accountId = await getAccountIdForRequest(request); const accountId = await getAccountIdForRequest(request);
const update = getJSONfromString(String(request.body)) as IGenericUpdate; const update = getJSONfromString<IGenericUpdate>(String(request.body));
await updateGeneric(update, accountId); await updateGeneric(update, accountId);
response.json(update); response.json(update);
}; };

View File

@ -10,7 +10,7 @@ import { ExportRelics, ExportRewards, TRarity } from "warframe-public-export-plu
export const getVoidProjectionRewardsController: RequestHandler = async (req, res) => { export const getVoidProjectionRewardsController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const data = getJSONfromString(String(req.body)) as IVoidProjectionRewardRequest; const data = getJSONfromString<IVoidProjectionRewardRequest>(String(req.body));
const response: IVoidProjectionRewardResponse = { const response: IVoidProjectionRewardResponse = {
CurrentWave: data.CurrentWave, CurrentWave: data.CurrentWave,
ParticipantInfo: data.ParticipantInfo, ParticipantInfo: data.ParticipantInfo,

View File

@ -26,7 +26,7 @@ interface IGildWeaponRequest {
export const gildWeaponController: RequestHandler = async (req, res) => { export const gildWeaponController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const data = getJSONfromString(String(req.body)) as IGildWeaponRequest; const data = getJSONfromString<IGildWeaponRequest>(String(req.body));
data.ItemId = String(req.query.ItemId); data.ItemId = String(req.query.ItemId);
if (!modularWeaponCategory.includes(req.query.Category as WeaponTypeInternal | "Hoverboards")) { if (!modularWeaponCategory.includes(req.query.Category as WeaponTypeInternal | "Hoverboards")) {
throw new Error(`Unknown modular weapon Category: ${String(req.query.Category)}`); throw new Error(`Unknown modular weapon Category: ${String(req.query.Category)}`);

View File

@ -5,9 +5,9 @@ import { addKeyChainItems, getInventory } from "@/src/services/inventoryService"
export const giveKeyChainTriggeredItemsController: RequestHandler = async (req, res) => { export const giveKeyChainTriggeredItemsController: RequestHandler = async (req, res) => {
const accountId = parseString(req.query.accountId); const accountId = parseString(req.query.accountId);
const keyChainTriggeredItemsRequest = getJSONfromString( const keyChainTriggeredItemsRequest = getJSONfromString<IGiveKeyChainTriggeredItemsRequest>(
(req.body as string).toString() (req.body as string).toString()
) as IGiveKeyChainTriggeredItemsRequest; );
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const inventoryChanges = await addKeyChainItems(inventory, keyChainTriggeredItemsRequest); const inventoryChanges = await addKeyChainItems(inventory, keyChainTriggeredItemsRequest);

View File

@ -22,7 +22,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
switch (req.query.mode) { switch (req.query.mode) {
case "s": { case "s": {
// shard installation // shard installation
const request = getJSONfromString(String(req.body)) as IShardInstallRequest; const request = getJSONfromString<IShardInstallRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!;
if (!suit.ArchonCrystalUpgrades || suit.ArchonCrystalUpgrades.length != 5) { if (!suit.ArchonCrystalUpgrades || suit.ArchonCrystalUpgrades.length != 5) {
@ -50,7 +50,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
case "x": { case "x": {
// shard removal // shard removal
const request = getJSONfromString(String(req.body)) as IShardUninstallRequest; const request = getJSONfromString<IShardUninstallRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!;
@ -88,7 +88,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
case "n": { case "n": {
// name the beast // name the beast
const request = getJSONfromString(String(req.body)) as IHelminthNameRequest; const request = getJSONfromString<IHelminthNameRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry ??= {};
inventory.InfestedFoundry.Name = request.newName; inventory.InfestedFoundry.Name = request.newName;
@ -105,7 +105,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
case "c": { case "c": {
// consume items // consume items
const request = getJSONfromString(String(req.body)) as IHelminthFeedRequest; const request = getJSONfromString<IHelminthFeedRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry ??= {};
inventory.InfestedFoundry.Resources ??= []; inventory.InfestedFoundry.Resources ??= [];
@ -201,7 +201,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
case "o": { case "o": {
// offerings update // offerings update
const request = getJSONfromString(String(req.body)) as IHelminthOfferingsUpdate; const request = getJSONfromString<IHelminthOfferingsUpdate>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry ??= {};
inventory.InfestedFoundry.InvigorationIndex = request.OfferingsIndex; inventory.InfestedFoundry.InvigorationIndex = request.OfferingsIndex;
@ -220,7 +220,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
case "a": { case "a": {
// subsume warframe // subsume warframe
const request = getJSONfromString(String(req.body)) as IHelminthSubsumeRequest; const request = getJSONfromString<IHelminthSubsumeRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const recipe = getRecipe(request.Recipe)!; const recipe = getRecipe(request.Recipe)!;
for (const ingredient of recipe.secretIngredients!) { for (const ingredient of recipe.secretIngredients!) {
@ -283,7 +283,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => {
} }
case "u": { case "u": {
const request = getJSONfromString(String(req.body)) as IHelminthInvigorationRequest; const request = getJSONfromString<IHelminthInvigorationRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const suit = inventory.Suits.id(request.SuitId.$oid)!; const suit = inventory.Suits.id(request.SuitId.$oid)!;
const upgradesExpiry = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000); const upgradesExpiry = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000);

View File

@ -52,7 +52,7 @@ import { getInventory } from "@/src/services/inventoryService";
export const missionInventoryUpdateController: RequestHandler = async (req, res): Promise<void> => { export const missionInventoryUpdateController: RequestHandler = async (req, res): Promise<void> => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const missionReport = getJSONfromString((req.body as string).toString()) as IMissionInventoryUpdateRequest; const missionReport = getJSONfromString<IMissionInventoryUpdateRequest>((req.body as string).toString());
if (missionReport.MissionStatus !== "GS_SUCCESS") { if (missionReport.MissionStatus !== "GS_SUCCESS") {
console.log(`Mission failed: ${missionReport.RewardInfo?.node}`); console.log(`Mission failed: ${missionReport.RewardInfo?.node}`);

View File

@ -29,7 +29,7 @@ interface IModularCraftRequest {
export const modularWeaponCraftingController: RequestHandler = async (req, res) => { export const modularWeaponCraftingController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const data = getJSONfromString(String(req.body)) as IModularCraftRequest; const data = getJSONfromString<IModularCraftRequest>(String(req.body));
if (!(data.WeaponType in modularWeaponTypes)) { if (!(data.WeaponType in modularWeaponTypes)) {
throw new Error(`unknown modular weapon type: ${data.WeaponType}`); throw new Error(`unknown modular weapon type: ${data.WeaponType}`);
} }

View File

@ -11,7 +11,7 @@ interface INameWeaponRequest {
export const nameWeaponController: RequestHandler = async (req, res) => { export const nameWeaponController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const body = getJSONfromString(String(req.body)) as INameWeaponRequest; const body = getJSONfromString<INameWeaponRequest>(String(req.body));
const item = inventory[req.query.Category as string as TEquipmentKey].find( const item = inventory[req.query.Category as string as TEquipmentKey].find(
item => item._id.toString() == (req.query.ItemId as string) item => item._id.toString() == (req.query.ItemId as string)
)!; )!;

View File

@ -7,7 +7,7 @@ import { RequestHandler } from "express";
export const playerSkillsController: RequestHandler = async (req, res) => { export const playerSkillsController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const request = getJSONfromString(String(req.body)) as IPlayerSkillsRequest; const request = getJSONfromString<IPlayerSkillsRequest>(String(req.body));
const oldRank: number = inventory.PlayerSkills[request.Skill as keyof IPlayerSkills]; const oldRank: number = inventory.PlayerSkills[request.Skill as keyof IPlayerSkills];
const cost = (request.Pool == "LPP_DRIFTER" ? drifterCosts[oldRank] : 1 << oldRank) * 1000; const cost = (request.Pool == "LPP_DRIFTER" ? drifterCosts[oldRank] : 1 << oldRank) * 1000;

View File

@ -7,7 +7,7 @@ import { getRandomElement } from "@/src/services/rngService";
export const rerollRandomModController: RequestHandler = async (req, res) => { export const rerollRandomModController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const request = getJSONfromString(String(req.body)) as RerollRandomModRequest; const request = getJSONfromString<RerollRandomModRequest>(String(req.body));
if ("ItemIds" in request) { if ("ItemIds" in request) {
const inventory = await getInventory(accountId, "Upgrades MiscItems"); const inventory = await getInventory(accountId, "Upgrades MiscItems");
const upgrade = inventory.Upgrades.id(request.ItemIds[0])!; const upgrade = inventory.Upgrades.id(request.ItemIds[0])!;

View File

@ -6,7 +6,7 @@ import { getJSONfromString } from "@/src/helpers/stringHelpers";
export const setEquippedInstrumentController: RequestHandler = async (req, res) => { export const setEquippedInstrumentController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const body = getJSONfromString(String(req.body)) as ISetEquippedInstrumentRequest; const body = getJSONfromString<ISetEquippedInstrumentRequest>(String(req.body));
inventory.EquippedInstrument = body.Instrument; inventory.EquippedInstrument = body.Instrument;
await inventory.save(); await inventory.save();
res.end(); res.end();

View File

@ -7,7 +7,7 @@ import { WeaponTypeInternal } from "@/src/services/itemDataService";
export const setWeaponSkillTreeController: RequestHandler = async (req, res) => { export const setWeaponSkillTreeController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const payload = getJSONfromString(String(req.body)) as ISetWeaponSkillTreeRequest; const payload = getJSONfromString<ISetWeaponSkillTreeRequest>(String(req.body));
const item = inventory[req.query.Category as WeaponTypeInternal].find( const item = inventory[req.query.Category as WeaponTypeInternal].find(
item => item._id.toString() == (req.query.ItemId as string) item => item._id.toString() == (req.query.ItemId as string)

View File

@ -14,7 +14,7 @@ interface IStartRecipeRequest {
} }
export const startRecipeController: RequestHandler = async (req, res) => { export const startRecipeController: RequestHandler = async (req, res) => {
const startRecipeRequest = getJSONfromString(String(req.body)) as IStartRecipeRequest; const startRecipeRequest = getJSONfromString<IStartRecipeRequest>(String(req.body));
logger.debug("StartRecipe Request", { startRecipeRequest }); logger.debug("StartRecipe Request", { startRecipeRequest });
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);

View File

@ -9,7 +9,7 @@ import { IInventoryChanges } from "@/src/types/purchaseTypes";
export const syndicateSacrificeController: RequestHandler = async (request, response) => { export const syndicateSacrificeController: RequestHandler = async (request, response) => {
const accountId = await getAccountIdForRequest(request); const accountId = await getAccountIdForRequest(request);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
const data = getJSONfromString(String(request.body)) as ISyndicateSacrificeRequest; const data = getJSONfromString<ISyndicateSacrificeRequest>(String(request.body));
let syndicate = inventory.Affiliations.find(x => x.Tag == data.AffiliationTag); let syndicate = inventory.Affiliations.find(x => x.Tag == data.AffiliationTag);
if (!syndicate) { if (!syndicate) {

View File

@ -19,7 +19,7 @@ interface ITrainingResultsResponse {
const trainingResultController: RequestHandler = async (req, res): Promise<void> => { const trainingResultController: RequestHandler = async (req, res): Promise<void> => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const trainingResults = getJSONfromString(String(req.body)) as ITrainingResultsRequest; const trainingResults = getJSONfromString<ITrainingResultsRequest>(String(req.body));
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);

View File

@ -5,7 +5,7 @@ import { updateChallengeProgress } from "@/src/services/inventoryService";
import { IUpdateChallengeProgressRequest } from "@/src/types/requestTypes"; import { IUpdateChallengeProgressRequest } from "@/src/types/requestTypes";
const updateChallengeProgressController: RequestHandler = async (req, res) => { const updateChallengeProgressController: RequestHandler = async (req, res) => {
const payload = getJSONfromString(String(req.body)) as IUpdateChallengeProgressRequest; const payload = getJSONfromString<IUpdateChallengeProgressRequest>(String(req.body));
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
await updateChallengeProgress(payload, accountId); await updateChallengeProgress(payload, accountId);

View File

@ -9,7 +9,7 @@ import { addItem, combineInventoryChanges, getInventory } from "@/src/services/i
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
export const updateQuestController: RequestHandler = async (req, res) => { export const updateQuestController: RequestHandler = async (req, res) => {
const accountId = parseString(req.query.accountId); const accountId = parseString(req.query.accountId);
const updateQuestRequest = getJSONfromString((req.body as string).toString()) as IUpdateQuestRequest; const updateQuestRequest = getJSONfromString<IUpdateQuestRequest>((req.body as string).toString());
// updates should be made only to one quest key per request // updates should be made only to one quest key per request
if (updateQuestRequest.QuestKeys.length > 1) { if (updateQuestRequest.QuestKeys.length > 1) {

View File

@ -9,7 +9,7 @@ const updateThemeController: RequestHandler = async (request, response) => {
const body = String(request.body); const body = String(request.body);
try { try {
const json = getJSONfromString(body) as IThemeUpdateRequest; const json = getJSONfromString<IThemeUpdateRequest>(body);
if (typeof json !== "object") { if (typeof json !== "object") {
throw new Error("Invalid data format"); throw new Error("Invalid data format");
} }

View File

@ -1,6 +1,6 @@
export const getJSONfromString = (str: string): any => { export const getJSONfromString = <T>(str: string): T => {
const jsonSubstring = str.substring(0, str.lastIndexOf("}") + 1); const jsonSubstring = str.substring(0, str.lastIndexOf("}") + 1);
return JSON.parse(jsonSubstring); return JSON.parse(jsonSubstring) as T;
}; };
export const getSubstringFromKeyword = (str: string, keyword: string): string => { export const getSubstringFromKeyword = (str: string, keyword: string): string => {