1
1
import type { HonoContext } from "../types" ;
2
2
import { OpenAPIHono , z } from "@hono/zod-openapi" ;
3
+ import { cache } from "hono/cache" ;
4
+ import { HTTPException } from "hono/http-exception" ;
3
5
import { versionMiddleware } from "../middlewares/version" ;
4
6
import { EmojiCategorySchema } from "../schemas" ;
5
7
import { createError } from "../utils" ;
@@ -9,19 +11,24 @@ export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/ap
9
11
10
12
V1_CATEGORIES_ROUTER . use ( versionMiddleware ) ;
11
13
14
+ V1_CATEGORIES_ROUTER . get ( "*" , cache ( {
15
+ cacheName : "v1-categories" ,
16
+ cacheControl : "max-age=3600, immutable" ,
17
+ } ) ) ;
18
+
12
19
V1_CATEGORIES_ROUTER . openapi ( ALL_CATEGORIES_ROUTE , async ( c ) => {
13
20
const version = c . req . param ( "version" ) ;
14
21
15
- const res = await fetch ( `https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${ version } /groups.json` ) ;
16
-
17
- if ( ! res . ok ) {
18
- return createError ( c , 500 , "failed to fetch categories" ) ;
22
+ const res = await c . env . EMOJI_DATA . get ( `v${ version } /groups.json` ) ;
23
+ if ( res == null ) {
24
+ throw new HTTPException ( 500 , {
25
+ message : "failed to fetch categories" ,
26
+ } ) ;
19
27
}
20
28
21
29
const data = await res . json ( ) ;
22
30
23
31
const result = z . array ( EmojiCategorySchema ) . safeParse ( data ) ;
24
-
25
32
if ( ! result . success ) {
26
33
return createError ( c , 500 , "failed to parse categories" ) ;
27
34
}
@@ -38,10 +45,12 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
38
45
const version = c . req . param ( "version" ) ;
39
46
const categorySlug = c . req . param ( "category" ) ;
40
47
41
- const res = await fetch ( `https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/ v${ version } /groups.json`) ;
48
+ const res = await c . env . EMOJI_DATA . get ( ` v${ version } /groups.json`) ;
42
49
43
- if ( ! res . ok ) {
44
- return createError ( c , 500 , "failed to fetch categories" ) ;
50
+ if ( res == null ) {
51
+ throw new HTTPException ( 500 , {
52
+ message : "failed to fetch categories" ,
53
+ } ) ;
45
54
}
46
55
47
56
const data = await res . json ( ) ;
0 commit comments