Skip to content

Commit a02f7f9

Browse files
committed
refactor: update version handling and improve category retrieval logic
1 parent 632e34a commit a02f7f9

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/routes/v1_categories.openapi.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { createRoute, z } from "@hono/zod-openapi";
2-
import { ApiErrorSchema, EmojiVersion } from "../schemas";
2+
import { ApiErrorSchema, EmojiVersionSchema } from "../schemas";
33

4+
const VERSION_PATH_PARAMETER = {
5+
in: "path" as const,
6+
name: "version",
7+
required: true,
8+
example: "latest",
9+
schema: {
10+
type: "string" as const,
11+
},
12+
};
413
export const ALL_CATEGORIES_ROUTE = createRoute({
514
method: "get",
615
path: "/",
716
tags: ["Categories"],
817
parameters: [
9-
{
10-
in: "path",
11-
name: "version",
12-
required: true,
13-
example: "latest",
14-
schema: {
15-
type: "string",
16-
},
17-
},
18+
VERSION_PATH_PARAMETER,
1819
],
1920
responses: {
2021
200: {
2122
content: {
2223
"application/json": {
23-
schema: z.array(EmojiVersion),
24+
schema: z.array(z.object({})),
2425
},
2526
},
26-
description: "Retrieve a list of all emoji versions available",
27+
description: "Retrieve a list of all emoji categories available for the specified version",
2728
},
2829
500: {
2930
content: {
@@ -41,10 +42,12 @@ export const GET_CATEGORY_ROUTE = createRoute({
4142
path: "/{category}",
4243
tags: ["Categories"],
4344
parameters: [
45+
VERSION_PATH_PARAMETER,
4446
{
4547
in: "path",
46-
name: "version",
48+
name: "category",
4749
required: true,
50+
example: "smileys",
4851
schema: {
4952
type: "string",
5053
},
@@ -54,10 +57,10 @@ export const GET_CATEGORY_ROUTE = createRoute({
5457
200: {
5558
content: {
5659
"application/json": {
57-
schema: z.array(EmojiVersion),
60+
schema: z.object({}),
5861
},
5962
},
60-
description: "Retrieve a list of all emoji versions available",
63+
description: "Retrieve the information for the specified emoji category",
6164
},
6265
500: {
6366
content: {

src/routes/v1_categories.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HonoContext } from "../types";
22
import { OpenAPIHono } from "@hono/zod-openapi";
3-
import { createError } from "../utils";
3+
import { createError, getAvailableVersions } from "../utils";
44
import { ALL_CATEGORIES_ROUTE } from "./v1_categories.openapi";
55

66
export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/v1/categories/:version");
@@ -12,20 +12,28 @@ V1_CATEGORIES_ROUTER.use(async (c, next) => {
1212
return createError(c, 400, "missing version");
1313
}
1414

15-
// const availableVersions = await getAvailableVersions();
15+
const availableVersions = await getAvailableVersions();
1616

17-
// if (!availableVersions.includes(version) && version !== "latest") {
18-
// return createError(c, 400, "invalid version");
19-
// }
17+
if (version !== "latest" && !availableVersions.some((v) => v.emoji_version === version)) {
18+
return createError(c, 404, "version not found");
19+
}
2020

2121
await next();
2222
});
2323

2424
V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
25-
const _version = c.req.param("version");
25+
const version = c.req.param("version");
26+
27+
const res = await fetch(`https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${version}/groups.json`);
28+
29+
if (!res.ok) {
30+
return createError(c, 500, "failed to fetch categories");
31+
}
32+
33+
const categories = await res.json();
2634

2735
return c.json(
28-
[],
36+
categories as [],
2937
200,
3038
);
3139
});

0 commit comments

Comments
 (0)