🚀 Add server endpoint for transforming SVG to React component

This commit is contained in:
pheralb
2024-04-09 23:05:54 +01:00
parent 79687a4ff1
commit bbaa5cd05d
3 changed files with 127 additions and 10 deletions
+31
View File
@@ -0,0 +1,31 @@
import type { RequestEvent } from '../$types';
import { transform } from '@svgr/core';
import { json } from '@sveltejs/kit';
export const POST = async ({ request }: RequestEvent) => {
try {
const body = await request.json();
const svgCode = body.code;
const typescript = body.typescript;
const name = body.name;
const jsCode = await transform(
svgCode,
{
plugins: ['@svgr/plugin-jsx'],
icon: true,
typescript: typescript
},
{ componentName: name }
);
return json(jsCode, { status: 200 });
} catch (error) {
return json(
{ error: `Error al transformar el SVG a componente React: ${error}` },
{ status: 500 }
);
}
};