-
Notifications
You must be signed in to change notification settings - Fork 19
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
7 changed files
with
437 additions
and
204 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { fetchChatEvents, tryFetchFirstChatEvent } from "@/queries/market"; | ||
import { Parcel } from "lib/parcel"; | ||
import type { NextRequest } from "next/server"; | ||
import { isNumber } from "utils"; | ||
|
||
type ChatSearchParams = { | ||
marketID: string | null; | ||
toMarketNonce: string | null; | ||
}; | ||
|
||
export type ValidChatSearchParams = { | ||
marketID: string; | ||
toMarketNonce: string; | ||
}; | ||
|
||
const isValidChatSearchParams = (params: ChatSearchParams): params is ValidChatSearchParams => { | ||
const { marketID, toMarketNonce } = params; | ||
// prettier-ignore | ||
return ( | ||
marketID !== null && isNumber(marketID) && | ||
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"), | ||
toMarketNonce: searchParams.get("toMarketNonce"), | ||
}; | ||
|
||
if (!isValidChatSearchParams(params)) { | ||
return new Response("Invalid chat search params.", { status: 400 }); | ||
} | ||
|
||
const marketID = Number(params.marketID); | ||
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); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { fetchSwapEvents, tryFetchFirstSwapEvent } from "@/queries/market"; | ||
import { Parcel } from "lib/parcel"; | ||
import type { NextRequest } from "next/server"; | ||
import { isNumber } from "utils"; | ||
|
||
type SwapSearchParams = { | ||
marketID: string | null; | ||
toMarketNonce: string | null; | ||
}; | ||
|
||
export type ValidSwapSearchParams = { | ||
marketID: string; | ||
toMarketNonce: string; | ||
}; | ||
|
||
const isValidSwapSearchParams = (params: SwapSearchParams): params is ValidSwapSearchParams => { | ||
const { marketID, toMarketNonce } = params; | ||
// prettier-ignore | ||
return ( | ||
marketID !== null && isNumber(marketID) && | ||
toMarketNonce !== null && isNumber(toMarketNonce) | ||
); | ||
}; | ||
|
||
type Swap = Awaited<ReturnType<typeof fetchSwapEvents>>[number]; | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const params: SwapSearchParams = { | ||
marketID: searchParams.get("marketID"), | ||
toMarketNonce: searchParams.get("toMarketNonce"), | ||
}; | ||
|
||
if (!isValidSwapSearchParams(params)) { | ||
return new Response("Invalid swap search params.", { status: 400 }); | ||
} | ||
|
||
const marketID = Number(params.marketID); | ||
const toMarketNonce = Number(params.toMarketNonce); | ||
|
||
const queryHelper = new Parcel<Swap, { marketID: number }>({ | ||
parcelSize: 20, | ||
normalRevalidate: 5, | ||
historicRevalidate: 365 * 24 * 60 * 60, | ||
fetchHistoricThreshold: (query) => | ||
fetchSwapEvents({ marketID: query.marketID, amount: 1 }).then((r) => | ||
Number(r[0].market.marketNonce) | ||
), | ||
fetchFirst: (query) => tryFetchFirstSwapEvent(query.marketID), | ||
cacheKey: "swaps", | ||
getKey: (s) => Number(s.market.marketNonce), | ||
fetchFn: ({ to, count }, { marketID }) => | ||
fetchSwapEvents({ marketID, toMarketNonce: to, amount: count }), | ||
}); | ||
|
||
const res = await queryHelper.getUnparsedData(toMarketNonce, 20, { marketID }); | ||
|
||
return new Response(res); | ||
} |
Oops, something went wrong.