aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-07-01 18:06:03 -0500
committermat <github@matdoes.dev>2022-07-01 18:06:03 -0500
commit06071107fd3e2d7cd697a97844fb4520fef55fe0 (patch)
treebe056af8e631329d17e2ee0311c9eb88835561c5 /src
parent55a62be03612553160779d87ea55dce05fc57e8e (diff)
downloadskyblock-api-06071107fd3e2d7cd697a97844fb4520fef55fe0.tar.gz
skyblock-api-06071107fd3e2d7cd697a97844fb4520fef55fe0.tar.bz2
skyblock-api-06071107fd3e2d7cd697a97844fb4520fef55fe0.zip
Pagination
Diffstat (limited to 'src')
-rw-r--r--src/cleaners/skyblock/auctions.ts22
-rw-r--r--src/hypixel.ts13
-rw-r--r--src/index.ts2
3 files changed, 23 insertions, 14 deletions
diff --git a/src/cleaners/skyblock/auctions.ts b/src/cleaners/skyblock/auctions.ts
index 83b77a8..bec922c 100644
--- a/src/cleaners/skyblock/auctions.ts
+++ b/src/cleaners/skyblock/auctions.ts
@@ -3,6 +3,11 @@ import { cleanInventory, Item } from './inventory.js'
import * as cached from '../../hypixelCached.js'
import { CleanPlayer } from '../player.js'
+export interface Auctions {
+ auctions: Auction[]
+ pages: number
+}
+
export interface Auction {
id: string
sellerUuid: string
@@ -16,19 +21,22 @@ export interface Auction {
}
-export async function cleanAuctions(data: typedHypixelApi.SkyBlockRequestAuctionResponse): Promise<Auction[]> {
+export async function cleanAuctions(data: typedHypixelApi.SkyBlockRequestAuctionResponse, page: number): Promise<Auctions> {
const auctionPromises: Promise<Auction>[] = []
- for (const auction of data.auctions) {
+
+ let rawAuctions = data.auctions
+ // sort by newer first
+ rawAuctions.sort((a, b) => b.start - a.start)
+
+ rawAuctions = rawAuctions.slice(page * 10, page * 10 + 10)
+
+ for (const auction of rawAuctions) {
auctionPromises.push(cleanAuction(auction))
}
const auctions = await Promise.all(auctionPromises)
- // sort by newer first
- auctions.sort((a, b) => b.creationTimestamp - a.creationTimestamp)
-
- return auctions
-
+ return { auctions, pages: Math.ceil(rawAuctions.length / 10) }
}
async function cleanAuction(auction: typedHypixelApi.SkyBlockRequestAuctionResponse['auctions'][number]): Promise<Auction> {
diff --git a/src/hypixel.ts b/src/hypixel.ts
index 8080a4f..bb07968 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -50,6 +50,8 @@ export interface ApiOptions {
mainMemberUuid?: string
/** Only get the most basic information, like uuids and names */
basic?: boolean
+ /** Some endpoints have pagination */
+ page?: number
}
/** Sends an API request to Hypixel and returns the response. */
@@ -72,7 +74,7 @@ const cleanResponseFunctions = {
'skyblock/profile': (data: typedHypixelApi.SkyBlockProfileResponse, options) => cleanSkyblockProfileResponse(data.profile, options),
'skyblock/profiles': (data, options) => cleanSkyblockProfilesResponse(data.profiles),
'skyblock/auctions_ended': (data, options) => cleanEndedAuctions(data),
- 'skyblock/auction': (data, options) => cleanAuctions(data),
+ 'skyblock/auction': (data, options) => cleanAuctions(data, options.page ?? 0),
'resources/skyblock/election': (data, options) => cleanElectionResponse(data),
'resources/skyblock/items': (data, options) => cleanItemListResponse(data),
} as const
@@ -484,15 +486,14 @@ async function fetchAuctionItemsUncached() {
return idsToData
}
-export async function fetchPlayerAuctions(user: string): Promise<Auction[] | null> {
+export async function fetchPlayerAuctions(user: string, page: number): Promise<Auction[] | null> {
const playerUuid = await cached.uuidFromUser(user)
if (!playerUuid) return null
const playerAuctions = await sendCleanApiRequest(
'skyblock/auction',
- {
- player: playerUuid
- }
+ { player: playerUuid },
+ { page }
)
- return playerAuctions
+ return playerAuctions.auctions
}
diff --git a/src/index.ts b/src/index.ts
index 4da37d7..857fd88 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -211,7 +211,7 @@ app.get('/auction/:uuid', async (req, res) => {
app.get('/playerauctions/:user', async (req, res) => {
try {
res.json(
- await fetchPlayerAuctions(req.params.user)
+ await fetchPlayerAuctions(req.params.user, parseInt(req.query.page as string) || 0)
)
} catch (err) {
console.error(err)