From bbaa5cd05d828bbd4371aaa45d01d1deba6fd7b9 Mon Sep 17 00:00:00 2001 From: pheralb Date: Tue, 9 Apr 2024 23:05:54 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20server=20endpoint=20for=20?= =?UTF-8?q?transforming=20SVG=20to=20React=20component?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/copySvg.svelte | 90 +++++++++++++++++++++++++---- src/components/reactIcon.svelte | 16 +++++ src/routes/api/svgs/svgr/+server.ts | 31 ++++++++++ 3 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 src/components/reactIcon.svelte create mode 100644 src/routes/api/svgs/svgr/+server.ts diff --git a/src/components/copySvg.svelte b/src/components/copySvg.svelte index e612247..672fd61 100644 --- a/src/components/copySvg.svelte +++ b/src/components/copySvg.svelte @@ -1,12 +1,16 @@ - + (optionsOpen = isOpen)}> + + {#if optionsOpen} + + {:else} + + {/if} + + + + + + + diff --git a/src/components/reactIcon.svelte b/src/components/reactIcon.svelte new file mode 100644 index 0000000..4cf812a --- /dev/null +++ b/src/components/reactIcon.svelte @@ -0,0 +1,16 @@ + + + + diff --git a/src/routes/api/svgs/svgr/+server.ts b/src/routes/api/svgs/svgr/+server.ts new file mode 100644 index 0000000..703c4ad --- /dev/null +++ b/src/routes/api/svgs/svgr/+server.ts @@ -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 } + ); + } +};