File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -155,4 +155,28 @@ app.get('/category/:category', async (c) => {
155155 return c . json ( categorySvgs ) ;
156156} ) ;
157157
158+ // 🌱 GET: "/svg/:filename" - Return the SVG file by filename:
159+ app . get ( '/svg/:filename' , async ( c ) => {
160+ const fileName = c . req . param ( 'filename' ) as string ;
161+ const svgLibrary = 'https://svgl.app/library/' ;
162+
163+ const ratelimit = c . get ( 'ratelimit' ) ;
164+ const ip = c . req . raw . headers . get ( 'CF-Connecting-IP' ) ;
165+ const { success } = await ratelimit . limit ( ip ?? 'anonymous' ) ;
166+
167+ if ( ! success ) {
168+ return c . json ( { error : '🛑 Too many request' } , 429 ) ;
169+ }
170+
171+ try {
172+ const svg = await fetch ( `${ svgLibrary } ${ fileName } ` ) . then ( ( res ) => {
173+ if ( ! res . ok ) throw new Error ( 'Network response was not ok' ) ;
174+ return res . text ( ) ;
175+ } ) ;
176+ return c . body ( svg , 200 ) ;
177+ } catch ( err ) {
178+ return c . json ( { error : 'not found' } , 404 ) ;
179+ }
180+ } ) ;
181+
158182export default app ;
You can’t perform that action at this time.
0 commit comments