|
1 |
| -import fs from "fs/promises"; |
2 | 1 | import fz from "fuzzysearch";
|
3 |
| -import { glob } from "glob"; |
4 |
| -import type { NextApiRequest, NextApiResponse } from "next"; |
| 2 | +import type { PageConfig } from "next"; |
| 3 | +import type { NextRequest } from "next/server"; |
5 | 4 |
|
6 |
| -import type { ApiIcon, MetaIcon, ResponseIcon, Source, Sources } from "../../types"; |
| 5 | +import type { ApiIcon, ResponseIcon, Source, Sources } from "../../types"; |
| 6 | + |
| 7 | +export const config: PageConfig = { |
| 8 | + runtime: "edge", |
| 9 | +}; |
7 | 10 |
|
8 | 11 | const getIcons = async () => {
|
9 |
| - const snapshots: Promise<string>[] = await glob("../packages/@chakra-icons/**/snapshot.json").then((maybeSnapshots) => |
10 |
| - maybeSnapshots.map((snapshotPath) => fs.readFile(snapshotPath, { encoding: "utf8" })), |
11 |
| - ); |
12 |
| - const metaIcons = await Promise.all([...snapshots]).then((all) => all.map((j) => JSON.parse(j) as MetaIcon)); |
| 12 | + // const snapshots: Promise<string>[] = await glob("../packages/@chakra-icons/**/snapshot.json").then((maybeSnapshots) => |
| 13 | + // maybeSnapshots.map((snapshotPath) => fs.readFile(snapshotPath, { encoding: "utf8" })), |
| 14 | + // ); |
| 15 | + // const metaIcons = await Promise.all([...snapshots]).then((all) => all.map((j) => JSON.parse(j) as MetaIcon)); |
| 16 | + |
| 17 | + const metaIcons = Array.from(await import("../../snapshots.json")); |
13 | 18 |
|
14 | 19 | return ({ limit, q, qCreator }: { limit?: number; q?: string; qCreator?: string }): [ApiIcon[], number, string[]] => {
|
15 | 20 | const icons = metaIcons.flatMap((metaIcon) =>
|
@@ -52,14 +57,22 @@ export const getData = async (q: string, qCreator: string, limit = 50) => {
|
52 | 57 | };
|
53 | 58 | const toInt = (a: any): number => a | 0; // eslint-disable-line no-bitwise, @typescript-eslint/no-explicit-any
|
54 | 59 |
|
55 |
| -export default async (req: NextApiRequest, res: NextApiResponse) => { |
56 |
| - const { q, qCreator, limit } = req.query; |
| 60 | +export default async (req: NextRequest) => { |
| 61 | + const params = req.nextUrl.searchParams; |
| 62 | + |
| 63 | + const q = params.get("q"); |
| 64 | + const qCreator = params.get("qCreator"); |
| 65 | + const limit = params.get("limit"); |
57 | 66 |
|
58 | 67 | if (!Array.isArray(q) && !Array.isArray(limit) && !Array.isArray(qCreator)) {
|
59 | 68 | const _limit = toInt(limit);
|
60 | 69 | const data = await getData(q ?? "", qCreator ?? "", _limit > 0 ? _limit : 50);
|
61 |
| - if (req.method?.toLowerCase() === "get") { |
62 |
| - res.status(200).json(data); |
| 70 | + if (req.method.toLowerCase() === "get") { |
| 71 | + return new Response(JSON.stringify(data), { |
| 72 | + headers: { |
| 73 | + "content-type": "application/json", |
| 74 | + }, |
| 75 | + }); |
63 | 76 | }
|
64 | 77 | }
|
65 | 78 | };
|
0 commit comments