diff --git a/app/routes/e/$extensionSlug/$themeSlug[.json].ts b/app/routes/e/$extensionSlug/$themeSlug[.json].ts new file mode 100644 index 0000000..dd077e8 --- /dev/null +++ b/app/routes/e/$extensionSlug/$themeSlug[.json].ts @@ -0,0 +1,47 @@ +import type { LoaderArgs } from '@remix-run/cloudflare'; +import { renderers } from '@vscodethemes/utilities'; +import { getQueryParam } from '~/utilities/requests'; +import kv from '~/clients/kv'; + +const parseQuery = (request: Request) => { + const url = new URL(request.url); + const params = new URLSearchParams(url.search); + const language = getQueryParam(params, 'language') ?? 'javascript'; + return { language }; +}; + +export async function loader({ request, params }: LoaderArgs) { + const { extensionSlug, themeSlug } = params; + + if (!extensionSlug) { + throw new Error('Missing extension'); + } + if (!themeSlug) { + throw new Error('Missing theme'); + } + + const query = parseQuery(request); + const result = await kv.getExtensionWithTokens(extensionSlug, query.language); + if (!result) { + throw new Response('Not Found', { status: 404 }); + } + + const themeMatch = result.themes[themeSlug]; + if (!themeMatch) { + throw new Response('Not Found', { status: 404 }); + } + + const svg = renderers.renderThemePreviewSvg({ + theme: themeMatch.theme, + tokens: themeMatch.tokens, + language: query.language, + rounded: true, + }); + + return new Response(JSON.stringify(themeMatch), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); +} diff --git a/app/routes/e/$extensionSlug[.json].ts b/app/routes/e/$extensionSlug[.json].ts new file mode 100644 index 0000000..73ca994 --- /dev/null +++ b/app/routes/e/$extensionSlug[.json].ts @@ -0,0 +1,32 @@ +import type { LoaderArgs } from '@remix-run/cloudflare'; +import { renderers } from '@vscodethemes/utilities'; +import { getQueryParam } from '~/utilities/requests'; +import kv from '~/clients/kv'; + +const parseQuery = (request: Request) => { + const url = new URL(request.url); + const params = new URLSearchParams(url.search); + const language = getQueryParam(params, 'language') ?? 'javascript'; + return { language }; +}; + +export async function loader({ request, params }: LoaderArgs) { + const { extensionSlug } = params; + + if (!extensionSlug) { + throw new Error('Missing extension'); + } + + const query = parseQuery(request); + const result = await kv.getExtensionWithTokens(extensionSlug, query.language); + if (!result) { + throw new Response('Not Found', { status: 404 }); + } + + return new Response(JSON.stringify(result), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); +}