From 5101e436bd2acfb6efdffe5c8c723511ab735153 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 17 May 2022 17:43:30 +0000 Subject: improve auctions api --- src/index.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'src/index.ts') diff --git a/src/index.ts b/src/index.ts index 59e7761..1dcb523 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, deleteSession, fetchPaginatedItemsAuctions } from './database.js' -import { fetchElection, fetchItemList, fetchMemberProfile, fetchUser } from './hypixel.js' +import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount, deleteSession, fetchPaginatedItemsAuctions, fetchItemsAuctions } from './database.js' +import { fetchAuctionUncached, fetchElection, fetchItemList, fetchMemberProfile, fetchUser } from './hypixel.js' import rateLimit from 'express-rate-limit' import * as constants from './constants.js' import * as discord from './discord.js' @@ -155,9 +155,11 @@ app.get('/election', async (req, res) => { app.get('/items', async (req, res) => { try { - res.json( - await fetchItemList() - ) + res + .setHeader('Cache-Control', 'public, max-age=600') + .json( + await fetchItemList() + ) } catch (err) { console.error(err) res.json({ ok: false }) @@ -166,11 +168,17 @@ app.get('/items', async (req, res) => { app.get('/auctionprices', async (req, res) => { + const itemIds = typeof req.query.items === 'string' ? req.query.items.split(',') : null + if (itemIds && itemIds.length > 100) + return res.json({ + ok: false, + error: 'More than 100 items in the items query parameter.' + }) try { res .setHeader('Cache-Control', 'public, max-age=600') .json( - await fetchPaginatedItemsAuctions(0, 100) + itemIds ? await fetchItemsAuctions(itemIds) : await fetchPaginatedItemsAuctions(0, 100) ) } catch (err) { console.error(err) @@ -178,6 +186,20 @@ app.get('/auctionprices', async (req, res) => { } }) +app.get('/auction/:uuid', async (req, res) => { + console.log('fetching auction', req.params.uuid) + const auction = await fetchAuctionUncached(req.params.uuid) + console.log('fetched auction', req.params.uuid) + try { + res + // .setHeader('Cache-Control', 'public, max-age=600') + .json(auction) + } catch (err) { + console.error(err) + res.json({ ok: false }) + } +}) + app.post('/accounts/createsession', async (req, res) => { try { const { code } = req.body -- cgit