Doing both lookups in parallel saves around 1 ms in the happy case (20% of baseline time), and in case nonce does not match, the error is simply raised as per usual with the inventory request being lightweight enough to be negligible. Noteworthy that this reasoning doesn't really work for other controllers because in the error case, the inventory request would still be quite significant, even if the HTTP request itself would still finish quickly. Reviewed-on: #2359 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { RequestHandler } from "express";
|
|
import { config } from "@/src/services/configService";
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
|
import { getInventory } from "@/src/services/inventoryService";
|
|
|
|
export const creditsController: RequestHandler = async (req, res) => {
|
|
const inventory = (
|
|
await Promise.all([
|
|
getAccountIdForRequest(req),
|
|
getInventory(
|
|
req.query.accountId as string,
|
|
"RegularCredits TradesRemaining PremiumCreditsFree PremiumCredits"
|
|
)
|
|
])
|
|
)[1];
|
|
|
|
const response = {
|
|
RegularCredits: inventory.RegularCredits,
|
|
TradesRemaining: inventory.TradesRemaining,
|
|
PremiumCreditsFree: inventory.PremiumCreditsFree,
|
|
PremiumCredits: inventory.PremiumCredits
|
|
};
|
|
|
|
if (config.infiniteCredits) {
|
|
response.RegularCredits = 999999999;
|
|
}
|
|
if (config.infinitePlatinum) {
|
|
response.PremiumCreditsFree = 0;
|
|
response.PremiumCredits = 999999999;
|
|
}
|
|
|
|
res.json(response);
|
|
};
|