1
- import { Accepts } from " https://deno.land/x/[email protected] /mod.ts" ;
2
- import type { Context } from " netlify:edge" ;
1
+ import { Accepts } from ' https://deno.land/x/[email protected] /mod.ts'
2
+ import type { Context } from ' netlify:edge'
3
3
// Available at build time
4
- import imageconfig from "./imageconfig.json" assert {
5
- type : "json" ,
6
- } ;
4
+ import imageconfig from './imageconfig.json' assert { type : 'json' }
7
5
8
- const defaultFormat = " webp"
6
+ const defaultFormat = ' webp'
9
7
10
8
interface ImageConfig extends Record < string , unknown > {
11
- formats ?: string [ ] ;
9
+ formats ?: string [ ]
12
10
}
13
11
14
12
/**
@@ -17,41 +15,38 @@ interface ImageConfig extends Record<string, unknown> {
17
15
18
16
// deno-lint-ignore require-await
19
17
const handler = async ( req : Request , context : Context ) => {
20
- const { searchParams } = new URL ( req . url ) ;
21
- const accept = new Accepts ( req . headers ) ;
22
- const { formats = [ defaultFormat ] } = imageconfig as ImageConfig ;
18
+ const { searchParams } = new URL ( req . url )
19
+ const accept = new Accepts ( req . headers )
20
+ const { formats = [ defaultFormat ] } = imageconfig as ImageConfig
23
21
if ( formats . length === 0 ) {
24
- formats . push ( defaultFormat ) ;
22
+ formats . push ( defaultFormat )
25
23
}
26
- let type = accept . types ( formats ) || defaultFormat ;
27
- if ( Array . isArray ( type ) ) {
28
- type = type [ 0 ] ;
24
+ let type = accept . types ( formats ) || defaultFormat
25
+ if ( Array . isArray ( type ) ) {
26
+ type = type [ 0 ]
29
27
}
30
28
31
-
32
- const source = searchParams . get ( "url" ) ;
33
- const width = searchParams . get ( "w" ) ;
34
- const quality = searchParams . get ( "q" ) ?? 75 ;
29
+ const source = searchParams . get ( 'url' )
30
+ const width = searchParams . get ( 'w' )
31
+ const quality = searchParams . get ( 'q' ) ?? 75
35
32
36
33
if ( ! source || ! width ) {
37
- return new Response ( " Invalid request" , {
34
+ return new Response ( ' Invalid request' , {
38
35
status : 400 ,
39
- } ) ;
36
+ } )
40
37
}
41
38
42
- const modifiers = [ `w_${ width } ` , `q_${ quality } ` ] ;
39
+ const modifiers = [ `w_${ width } ` , `q_${ quality } ` ]
43
40
44
41
if ( type ) {
45
- if ( type . includes ( '/' ) ) {
42
+ if ( type . includes ( '/' ) ) {
46
43
// If this is a mimetype, strip "image/"
47
- type = type . split ( '/' ) [ 1 ] ;
44
+ type = type . split ( '/' ) [ 1 ]
48
45
}
49
- modifiers . push ( `f_${ type } ` ) ;
46
+ modifiers . push ( `f_${ type } ` )
50
47
}
51
- const target = `/_ipx/${ modifiers . join ( "," ) } /${ encodeURIComponent ( source ) } ` ;
52
- return context . rewrite (
53
- target ,
54
- ) ;
55
- } ;
48
+ const target = `/_ipx/${ modifiers . join ( ',' ) } /${ encodeURIComponent ( source ) } `
49
+ return context . rewrite ( target )
50
+ }
56
51
57
- export default handler ;
52
+ export default handler
0 commit comments