From 529763c4d7b37400ef55b5f97786b0aeec6672b6 Mon Sep 17 00:00:00 2001 From: Griko Nibras Date: Sat, 9 Sep 2023 05:10:47 +0700 Subject: [PATCH] fix(website): rework snapshots api --- website/.gitignore | 2 ++ website/scripts/generate-snapshot.ts | 21 ++++++++++++++++ website/src/pages/api/[...icons].ts | 37 +++++++++++++++++++--------- 3 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 website/scripts/generate-snapshot.ts diff --git a/website/.gitignore b/website/.gitignore index 1437c53f..cd529b13 100644 --- a/website/.gitignore +++ b/website/.gitignore @@ -32,3 +32,5 @@ yarn-error.log* # vercel .vercel + +src/snapshots.json diff --git a/website/scripts/generate-snapshot.ts b/website/scripts/generate-snapshot.ts new file mode 100644 index 00000000..dce8ee8d --- /dev/null +++ b/website/scripts/generate-snapshot.ts @@ -0,0 +1,21 @@ +import * as fs from "fs/promises"; +import { glob } from "glob"; +import * as path from "path"; + +import type { MetaIcon } from "../src/types"; + +const generateSnapshot = async () => { + const snapshotPaths = await glob("./node_modules/@chakra-icons/*/snapshot.json"); + + const snapshots = await Promise.all( + snapshotPaths.map(async (snapshotPath) => { + const content = await fs.readFile(snapshotPath, { encoding: "utf8" }); + return JSON.parse(content) as MetaIcon; + }), + ); + + const dest = path.resolve("./src/snapshots.json"); + await fs.writeFile(dest, JSON.stringify(snapshots), { encoding: "utf-8" }); +}; + +void generateSnapshot(); diff --git a/website/src/pages/api/[...icons].ts b/website/src/pages/api/[...icons].ts index 5520630a..d35c738a 100644 --- a/website/src/pages/api/[...icons].ts +++ b/website/src/pages/api/[...icons].ts @@ -1,15 +1,20 @@ -import fs from "fs/promises"; import fz from "fuzzysearch"; -import { glob } from "glob"; -import type { NextApiRequest, NextApiResponse } from "next"; +import type { PageConfig } from "next"; +import type { NextRequest } from "next/server"; -import type { ApiIcon, MetaIcon, ResponseIcon, Source, Sources } from "../../types"; +import type { ApiIcon, ResponseIcon, Source, Sources } from "../../types"; + +export const config: PageConfig = { + runtime: "edge", +}; const getIcons = async () => { - const snapshots: Promise[] = await glob("../packages/@chakra-icons/**/snapshot.json").then((maybeSnapshots) => - maybeSnapshots.map((snapshotPath) => fs.readFile(snapshotPath, { encoding: "utf8" })), - ); - const metaIcons = await Promise.all([...snapshots]).then((all) => all.map((j) => JSON.parse(j) as MetaIcon)); + // const snapshots: Promise[] = await glob("../packages/@chakra-icons/**/snapshot.json").then((maybeSnapshots) => + // maybeSnapshots.map((snapshotPath) => fs.readFile(snapshotPath, { encoding: "utf8" })), + // ); + // const metaIcons = await Promise.all([...snapshots]).then((all) => all.map((j) => JSON.parse(j) as MetaIcon)); + + const metaIcons = Array.from(await import("../../snapshots.json")); return ({ limit, q, qCreator }: { limit?: number; q?: string; qCreator?: string }): [ApiIcon[], number, string[]] => { const icons = metaIcons.flatMap((metaIcon) => @@ -52,14 +57,22 @@ export const getData = async (q: string, qCreator: string, limit = 50) => { }; const toInt = (a: any): number => a | 0; // eslint-disable-line no-bitwise, @typescript-eslint/no-explicit-any -export default async (req: NextApiRequest, res: NextApiResponse) => { - const { q, qCreator, limit } = req.query; +export default async (req: NextRequest) => { + const params = req.nextUrl.searchParams; + + const q = params.get("q"); + const qCreator = params.get("qCreator"); + const limit = params.get("limit"); if (!Array.isArray(q) && !Array.isArray(limit) && !Array.isArray(qCreator)) { const _limit = toInt(limit); const data = await getData(q ?? "", qCreator ?? "", _limit > 0 ? _limit : 50); - if (req.method?.toLowerCase() === "get") { - res.status(200).json(data); + if (req.method.toLowerCase() === "get") { + return new Response(JSON.stringify(data), { + headers: { + "content-type": "application/json", + }, + }); } } };