Skip to content

Commit

Permalink
fix(website): rework snapshots api
Browse files Browse the repository at this point in the history
  • Loading branch information
grikomsn committed Sep 8, 2023
1 parent 70ec6be commit 529763c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ yarn-error.log*

# vercel
.vercel

src/snapshots.json
21 changes: 21 additions & 0 deletions website/scripts/generate-snapshot.ts
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 25 additions & 12 deletions website/src/pages/api/[...icons].ts
Original file line number Diff line number Diff line change
@@ -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<string>[] = 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<string>[] = 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) =>
Expand Down Expand Up @@ -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",
},
});
}
}
};

0 comments on commit 529763c

Please sign in to comment.