@@ -4,17 +4,17 @@ import { consola } from 'consola'
4
4
import { createError , getQuery , type H3Event } from 'h3'
5
5
import type { NuxtIconRuntimeOptions } from '../../schema-types'
6
6
// @ts -expect-error tsconfig.server has the types
7
- import { useAppConfig , defineCachedEventHandler } from '#imports'
7
+ import { useAppConfig , getRequestURL , defineCachedEventHandler } from '#imports'
8
8
import { collections } from '#nuxt-icon-server-bundle'
9
9
10
10
const warnOnceSet = /* @__PURE__ */ new Set < string > ( )
11
11
12
12
const DEFAULT_ENDPOINT = 'https://api.iconify.design'
13
13
14
14
export default defineCachedEventHandler ( async ( event : H3Event ) => {
15
- const url = event . node . req . url
15
+ const url = getRequestURL ( event ) as URL
16
16
if ( ! url )
17
- return
17
+ return createError ( { status : 400 , message : 'Invalid icon request' } )
18
18
19
19
const options = useAppConfig ( ) . icon as NuxtIconRuntimeOptions
20
20
const collectionName = event . context . params ?. collection ?. replace ( / \. j s o n $ / , '' )
@@ -23,8 +23,7 @@ export default defineCachedEventHandler(async (event: H3Event) => {
23
23
: null
24
24
25
25
const apiEndPoint = options . iconifyApiEndpoint || DEFAULT_ENDPOINT
26
- const apiUrl = new URL ( './' + basename ( url ) , apiEndPoint )
27
- const icons = apiUrl . searchParams . get ( 'icons' ) ?. split ( ',' )
26
+ const icons = url . searchParams . get ( 'icons' ) ?. split ( ',' )
28
27
29
28
if ( collection ) {
30
29
if ( icons ?. length ) {
@@ -48,6 +47,7 @@ export default defineCachedEventHandler(async (event: H3Event) => {
48
47
}
49
48
50
49
if ( options . fallbackToApi === true || options . fallbackToApi === 'server-only' ) {
50
+ const apiUrl = new URL ( './' + basename ( url . pathname ) + url . search , apiEndPoint )
51
51
consola . debug ( `[Icon] fetching ${ ( icons || [ ] ) . map ( i => '`' + collectionName + ':' + i + '`' ) . join ( ',' ) } from iconify api` )
52
52
if ( apiUrl . host !== new URL ( apiEndPoint ) . host ) {
53
53
return createError ( { status : 400 , message : 'Invalid icon request' } )
@@ -56,9 +56,13 @@ export default defineCachedEventHandler(async (event: H3Event) => {
56
56
const data = await $fetch ( apiUrl . href )
57
57
return data
58
58
}
59
- catch ( e ) {
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ catch ( e : any ) {
60
61
console . error ( e )
61
- return createError ( { status : 404 } )
62
+ if ( e . status === 404 )
63
+ return createError ( { status : 404 } )
64
+ else
65
+ return createError ( { status : 500 , message : 'Failed to fetch fallback icon' } )
62
66
}
63
67
}
64
68
return createError ( { status : 404 } )
0 commit comments