|
| 1 | +import { NextRequest } from "next/server"; |
| 2 | +import { stringify } from "superjson"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { parseSearchParams } from "zod-search-params"; |
| 5 | + |
| 6 | +import { CLUSTER_NAMES, toCluster } from "../../../../services/pyth"; |
| 7 | +import { getFeeds } from "../../../../services/pyth/get-feeds"; |
| 8 | + |
| 9 | +export const GET = async (request: NextRequest) => { |
| 10 | + // get cluster from query params |
| 11 | + const searchParams = request.nextUrl.searchParams; |
| 12 | + const parsedSearchParams = parseSearchParams(queryParamsSchema, searchParams); |
| 13 | + |
| 14 | + if (!parsedSearchParams) { |
| 15 | + return new Response("Invalid params", { |
| 16 | + status: 400, |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + const { excludePriceComponents, cluster } = parsedSearchParams; |
| 21 | + |
| 22 | + const feeds = await getFeeds(cluster); |
| 23 | + const filteredFeeds = excludePriceComponents |
| 24 | + ? feeds.map((feed) => { |
| 25 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 26 | + const { price, ...rest } = feed; |
| 27 | + return rest; |
| 28 | + }) |
| 29 | + : feeds; |
| 30 | + |
| 31 | + return new Response(stringify(filteredFeeds), { |
| 32 | + headers: { |
| 33 | + "Content-Type": "application/json", |
| 34 | + }, |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +const queryParamsSchema = z.object({ |
| 39 | + cluster: z.enum(CLUSTER_NAMES).transform((value) => toCluster(value)), |
| 40 | + excludePriceComponents: z.boolean(), |
| 41 | +}); |
0 commit comments