11import type { OpenAPIHono } from "@hono/zod-openapi" ;
2+ import type { UnicodeFileTree } from "@ucdjs/schemas" ;
23import type { HonoEnv } from "../../types" ;
34import { createRoute } from "@hono/zod-openapi" ;
45import { dedent } from "@luxass/utils" ;
5- import { UnicodeTreeSchema , UnicodeVersionDetailsSchema } from "@ucdjs/schemas" ;
6+ import { UnicodeFileTreeNodeSchema , UnicodeFileTreeSchema , UnicodeVersionDetailsSchema } from "@ucdjs/schemas" ;
67import {
78 hasUCDFolderPath ,
89 resolveUCDVersion ,
@@ -108,7 +109,7 @@ export const GET_VERSION_FILE_TREE_ROUTE = createRoute({
108109 200 : {
109110 content : {
110111 "application/json" : {
111- schema : UnicodeTreeSchema ,
112+ schema : UnicodeFileTreeSchema ,
112113 examples : {
113114 default : {
114115 summary : "File tree for a Unicode version" ,
@@ -208,19 +209,32 @@ export function registerGetVersionRoute(router: OpenAPIHono<HonoEnv>) {
208209
209210 // Try to get statistics from bucket if available
210211 const bucket = c . env . UCD_BUCKET ;
211- let statistics = null ;
212+ let statistics = {
213+ newBlocks : 0 ,
214+ newCharacters : 0 ,
215+ newScripts : 0 ,
216+ totalBlocks : 0 ,
217+ totalCharacters : 0 ,
218+ totalScripts : 0 ,
219+ } ;
220+
221+ // This is so bad.... but we have to do it for now.
212222 if ( bucket ) {
213- statistics = await calculateStatistics ( bucket , version ) ;
223+ const tmp = await calculateStatistics ( bucket , version ) ;
224+ if ( tmp ) {
225+ statistics = tmp ;
226+ }
214227 }
215228
216229 return c . json ( {
217230 ...versionInfo ,
218- statistics : statistics ?? undefined ,
231+ statistics,
219232 } , 200 ) ;
220233 } ) ;
221234}
222235
223236export function registerVersionFileTreeRoute ( router : OpenAPIHono < HonoEnv > ) {
237+ router . openAPIRegistry . register ( "UnicodeFileTreeNode" , UnicodeFileTreeNodeSchema ) ;
224238 router . openapi ( GET_VERSION_FILE_TREE_ROUTE , async ( c ) => {
225239 try {
226240 let version = c . req . param ( "version" ) ;
@@ -243,7 +257,11 @@ export function registerVersionFileTreeRoute(router: OpenAPIHono<HonoEnv>) {
243257 format : "F2" ,
244258 } ) ;
245259
246- return c . json ( result , 200 ) ;
260+ // We cast the result to UnicodeFileTree because the traverse function
261+ // returns entries that uses lastModified as `number | undefined`.
262+ // But we can't use the `number | undefined` type in the API schema.
263+ // So we need to return lastModified as `number | null` always.
264+ return c . json ( result as UnicodeFileTree , 200 ) ;
247265 } catch ( error ) {
248266 console . error ( "Error processing directory:" , error ) ;
249267 return internalServerError ( c , {
0 commit comments