1
1
import type { HonoContext } from "../types" ;
2
2
import { OpenAPIHono , z } from "@hono/zod-openapi" ;
3
+ import { HTTPException } from "hono/http-exception" ;
3
4
import { versionMiddleware } from "../middlewares/version" ;
4
5
import { EmojiCategorySchema } from "../schemas" ;
5
- import { createError } from "../utils" ;
6
+ import { cache , createError } from "../utils" ;
6
7
import { ALL_CATEGORIES_ROUTE , GET_CATEGORY_ROUTE } from "./v1_categories.openapi" ;
7
8
8
9
export const V1_CATEGORIES_ROUTER = new OpenAPIHono < HonoContext > ( ) . basePath ( "/api/v1/categories/:version" ) ;
@@ -12,10 +13,12 @@ V1_CATEGORIES_ROUTER.use(versionMiddleware);
12
13
V1_CATEGORIES_ROUTER . openapi ( ALL_CATEGORIES_ROUTE , async ( c ) => {
13
14
const version = c . req . param ( "version" ) ;
14
15
15
- const res = await fetch ( `https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/ v${ version } /groups.json`) ;
16
+ const res = await c . env . EMOJI_DATA . get ( ` v${ version } /groups.json`) ;
16
17
17
- if ( ! res . ok ) {
18
- return createError ( c , 500 , "failed to fetch categories" ) ;
18
+ if ( res == null ) {
19
+ throw new HTTPException ( 500 , {
20
+ message : "failed to fetch categories" ,
21
+ } ) ;
19
22
}
20
23
21
24
const data = await res . json ( ) ;
@@ -28,6 +31,8 @@ V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
28
31
29
32
const categories = result . data ;
30
33
34
+ cache ( c , 3600 , true ) ;
35
+
31
36
return c . json (
32
37
categories ,
33
38
200 ,
@@ -38,10 +43,12 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
38
43
const version = c . req . param ( "version" ) ;
39
44
const categorySlug = c . req . param ( "category" ) ;
40
45
41
- const res = await fetch ( `https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/ v${ version } /groups.json`) ;
46
+ const res = await c . env . EMOJI_DATA . get ( ` v${ version } /groups.json`) ;
42
47
43
- if ( ! res . ok ) {
44
- return createError ( c , 500 , "failed to fetch categories" ) ;
48
+ if ( res == null ) {
49
+ throw new HTTPException ( 500 , {
50
+ message : "failed to fetch categories" ,
51
+ } ) ;
45
52
}
46
53
47
54
const data = await res . json ( ) ;
@@ -54,6 +61,8 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
54
61
55
62
const category = result . data . find ( ( c ) => c . slug === categorySlug ) ;
56
63
64
+ cache ( c , 3600 , true ) ;
65
+
57
66
return c . json (
58
67
category ,
59
68
200 ,
0 commit comments