-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
353 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,59 @@ | ||
import { fetchChatEvents } from "@/queries/market"; | ||
import { fetchChatEvents, tryFetchFirstChatEvent } from "@/queries/market"; | ||
import { Parcel } from "lib/parcel"; | ||
import type { NextRequest } from "next/server"; | ||
import { stringifyJSON } from "utils"; | ||
import { isNumber } from "utils"; | ||
|
||
type ChatSearchParams = { | ||
marketID: string | null; | ||
fromMarketNonce: string | null; | ||
toMarketNonce: string | null; | ||
}; | ||
|
||
export type ValidChatSearchParams = { | ||
marketID: string; | ||
fromMarketNonce: string; | ||
toMarketNonce: string; | ||
}; | ||
|
||
const isNumber = (s: string) => !isNaN(parseInt(s)); | ||
|
||
const isValidChatSearchParams = (params: ChatSearchParams): params is ValidChatSearchParams => { | ||
const { marketID, fromMarketNonce } = params; | ||
const { marketID, toMarketNonce } = params; | ||
// prettier-ignore | ||
return ( | ||
marketID !== null && isNumber(marketID) && | ||
fromMarketNonce !== null && isNumber(fromMarketNonce) | ||
toMarketNonce !== null && isNumber(toMarketNonce) | ||
); | ||
}; | ||
|
||
type Chat = Awaited<ReturnType<typeof fetchChatEvents>>[number]; | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const params: ChatSearchParams = { | ||
marketID: searchParams.get("marketID"), | ||
fromMarketNonce: searchParams.get("fromMarketNonce"), | ||
toMarketNonce: searchParams.get("toMarketNonce"), | ||
}; | ||
|
||
if (!isValidChatSearchParams(params)) { | ||
return new Response("Invalid chat search params.", { status: 400 }); | ||
} | ||
|
||
const marketID = Number(params.marketID); | ||
const fromMarketNonce = Number(params.fromMarketNonce); | ||
|
||
const res = await fetchChatEvents({ marketID, fromMarketNonce }); | ||
|
||
return new Response(stringifyJSON(res)); | ||
const toMarketNonce = Number(params.toMarketNonce); | ||
|
||
const queryHelper = new Parcel<Chat, { marketID: number }>({ | ||
parcelSize: 20, | ||
normalRevalidate: 5, | ||
historicRevalidate: 365 * 24 * 60 * 60, | ||
fetchHistoricThreshold: (query) => | ||
fetchChatEvents({ marketID: query.marketID, amount: 1 }).then((r) => | ||
Number(r[0].market.marketNonce) | ||
), | ||
fetchFirst: (query) => tryFetchFirstChatEvent(query.marketID), | ||
cacheKey: "chats", | ||
getKey: (s) => Number(s.market.marketNonce), | ||
fetchFn: ({ to, count }, { marketID }) => | ||
fetchChatEvents({ marketID, toMarketNonce: to, amount: count }), | ||
}); | ||
|
||
const res = await queryHelper.getUnparsedData(toMarketNonce, 50, { marketID }); | ||
|
||
return new Response(res); | ||
} |
Oops, something went wrong.