Back

Version 0

5/28/2024
/** @jsxImportSource npm:preact **/
import codeOnValTown from "https://esm.town/v/andreterron/codeOnValTown?v=50";
import { render } from "npm:preact-render-to-string";

const githubQuery = (query: string) => encodeURIComponent(`${query} repo:pomdtr/val-town-mirror path:vals/`);

async function handler(req: Request) {
const url = new URL(req.url);

if (url.pathname == "/opensearch.xml") {
return new Response(
`<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Val Town Search</ShortName>
<Description>Search for vals using the Github API</Description>
<Url type="text/html" method="get" template="https://val-town-search.pomdtr.me/search?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" template="https://val-town-search.pomdtr.me/opensearch.xml"/>
<Image height="16" width="16" type="image/png">https://pomdtr-favicons.web.val.run/val-town</Image>
<moz:SearchForm>https://val-town-search.pomdtr.me/search</moz:SearchForm>
</OpenSearchDescription>`,
{
headers: {
"Content-Type": "application/opensearchdescription+xml",
},
},
);
}

if (url.pathname == "/search") {
const query = url.searchParams.get("q");
const resp = await fetch(`https://api.github.com/search/code?q=${githubQuery(query)}`, {
headers: {
"Accept": "application/vnd.github.text-match+json",
"Authorization": `Bearer ${Deno.env.get("GH_TOKEN")}`,
},
});