Skip to content

Commit 03728e6

Browse files
authored
Merge pull request #47 from ivanatias/minor_refactors
Minor refactors
2 parents 58f1423 + bfafa92 commit 03728e6

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

server/api/palettes/[hash].get.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { kv } from '~/lib/db'
22
import { log } from '~/lib/logs'
3+
import { type ColorWithRgbAndHex } from '~/types/colors'
34

45
interface Params {
56
hash?: string
67
}
78

8-
interface Color {
9-
hex: string
10-
rgb: number[]
11-
}
12-
139
interface ApiResponse {
1410
imageUrl: string | null
15-
colors: Color[] | null
11+
colors: ColorWithRgbAndHex[] | null
1612
}
1713

1814
export 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...')

server/api/palettes/save.post.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { kv } from '~/lib/db'
33
import { hashImageBase64 } from '~/utils/images'
44
import { log } from '~/lib/logs'
55
import { MAX_BYTES_SIZE } from '~/consts/files'
6+
import { type ColorWithRgbAndHex } from '~/types/colors'
67

78
export default eventHandler(async event => {
89
const formData = await readMultipartFormData(event)
910
const file = formData?.at(0)
1011
const colorsData = formData?.at(1)?.data
11-
const colors =
12-
colorsData != null ? JSON.parse(colorsData.toString()) : undefined
1312

1413
if (file === undefined) {
1514
log('error', '❌ No image provided...')
@@ -19,14 +18,24 @@ export default eventHandler(async event => {
1918
})
2019
}
2120

22-
if (colors === undefined || !Array.isArray(colors)) {
21+
if (colorsData === undefined) {
2322
log('error', '❌ No colors provided...')
2423
throw createError({
2524
statusCode: 400,
2625
statusMessage: 'No colors provided!'
2726
})
2827
}
2928

29+
const colors: ColorWithRgbAndHex[] = JSON.parse(colorsData.toString())
30+
31+
if (!Array.isArray(colors)) {
32+
log('error', '❌ Expected an array of rgb and hex color objects...')
33+
throw createError({
34+
statusCode: 400,
35+
statusMessage: 'Invalid colors format!'
36+
})
37+
}
38+
3039
const fileBytes = file.data.buffer.byteLength
3140

3241
if (fileBytes > MAX_BYTES_SIZE) {
@@ -98,7 +107,7 @@ export default eventHandler(async event => {
98107
imageUrl,
99108
colors
100109
}
101-
} catch (error: unknown) {
110+
} catch (error) {
102111
if (error instanceof Error) {
103112
log(
104113
'error',

0 commit comments

Comments
 (0)