Skip to content

Commit 529763c

Browse files
committed
fix(website): rework snapshots api
1 parent 70ec6be commit 529763c

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

website/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ yarn-error.log*
3232

3333
# vercel
3434
.vercel
35+
36+
src/snapshots.json

website/scripts/generate-snapshot.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as fs from "fs/promises";
2+
import { glob } from "glob";
3+
import * as path from "path";
4+
5+
import type { MetaIcon } from "../src/types";
6+
7+
const generateSnapshot = async () => {
8+
const snapshotPaths = await glob("./node_modules/@chakra-icons/*/snapshot.json");
9+
10+
const snapshots = await Promise.all(
11+
snapshotPaths.map(async (snapshotPath) => {
12+
const content = await fs.readFile(snapshotPath, { encoding: "utf8" });
13+
return JSON.parse(content) as MetaIcon;
14+
}),
15+
);
16+
17+
const dest = path.resolve("./src/snapshots.json");
18+
await fs.writeFile(dest, JSON.stringify(snapshots), { encoding: "utf-8" });
19+
};
20+
21+
void generateSnapshot();

website/src/pages/api/[...icons].ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import fs from "fs/promises";
21
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";
54

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+
};
710

811
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"));
1318

1419
return ({ limit, q, qCreator }: { limit?: number; q?: string; qCreator?: string }): [ApiIcon[], number, string[]] => {
1520
const icons = metaIcons.flatMap((metaIcon) =>
@@ -52,14 +57,22 @@ export const getData = async (q: string, qCreator: string, limit = 50) => {
5257
};
5358
const toInt = (a: any): number => a | 0; // eslint-disable-line no-bitwise, @typescript-eslint/no-explicit-any
5459

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");
5766

5867
if (!Array.isArray(q) && !Array.isArray(limit) && !Array.isArray(qCreator)) {
5968
const _limit = toInt(limit);
6069
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+
});
6376
}
6477
}
6578
};

0 commit comments

Comments
 (0)