11import { kv } from '~/lib/db'
22import { log } from '~/lib/logs'
3+ import { type ColorWithRgbAndHex } from '~/types/colors'
34
45interface Params {
56 hash ?: string
67}
78
8- interface Color {
9- hex : string
10- rgb : number [ ]
11- }
12-
139interface ApiResponse {
1410 imageUrl : string | null
15- colors : Color [ ] | null
11+ colors : ColorWithRgbAndHex [ ] | null
1612}
1713
1814export default eventHandler ( async ( event ) : Promise < ApiResponse > => {
@@ -21,15 +17,17 @@ export default eventHandler(async (event): Promise<ApiResponse> => {
2117
2218 const storedImageUrl : string | null =
2319 (
24- await kv . getItem ( params ?. hash ?? '' ) . catch ( ( ) => {
25- log (
26- 'error' ,
27- '❌ Something went wrong retrieving the image from KV store...'
28- )
29- throw createError ( {
30- statusCode : 500 ,
31- statusMessage : 'Something went wrong... Try again!'
32- } )
20+ await kv . getItem ( params ?. hash ?? '' ) . catch ( error => {
21+ if ( error instanceof Error ) {
22+ log (
23+ 'error' ,
24+ `❌ Something went wrong retrieving the image from KV store...[${ error . message . toUpperCase ( ) } ]`
25+ )
26+ throw createError ( {
27+ statusCode : 500 ,
28+ statusMessage : 'Something went wrong... Try again!'
29+ } )
30+ }
3331 } )
3432 ) ?. toString ( ) ?? null
3533
@@ -52,15 +50,17 @@ export default eventHandler(async (event): Promise<ApiResponse> => {
5250 } )
5351 ) ?. toString ( ) ?? null
5452
55- let colors : Color [ ] | null = null
53+ let colors : ColorWithRgbAndHex [ ] | null = null
5654 if ( storedColorsString != null ) {
57- colors = storedColorsString . split ( ';' ) . map ( ( colorStr ) : Color => {
58- const [ hex , rgbStr ] = colorStr . split ( '_' )
59- const rgb : number [ ] = rgbStr
60- . split ( '-' )
61- . map ( ( num ) : number => parseInt ( num , 10 ) )
62- return { hex, rgb }
63- } )
55+ colors = storedColorsString
56+ . split ( ';' )
57+ . map ( ( colorStr ) : ColorWithRgbAndHex => {
58+ const [ hex , rgbStr ] = colorStr . split ( '_' )
59+ const rgb : number [ ] = rgbStr
60+ . split ( '-' )
61+ . map ( ( num ) : number => parseInt ( num , 10 ) )
62+ return { hex, rgb }
63+ } )
6464 }
6565
6666 log ( 'info' , '✅ Image and colors found. Retrieving from KV store...' )
0 commit comments