Skip to content

Commit

Permalink
Add json routes for extensions and themes
Browse files Browse the repository at this point in the history
  • Loading branch information
jschr committed Feb 9, 2024
1 parent fdcd51e commit 9fbdc65
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/routes/e/$extensionSlug/$themeSlug[.json].ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
}
32 changes: 32 additions & 0 deletions app/routes/e/$extensionSlug[.json].ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
}

0 comments on commit 9fbdc65

Please sign in to comment.