-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json routes for extensions and themes (#270)
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); | ||
} |