Skip to content

Commit ad0768d

Browse files
committed
🌱 Add new route to extract svg code directly (preview)
1 parent 82fd3de commit ad0768d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

api-routes/src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
158182
export default app;

0 commit comments

Comments
 (0)