5It doesn't do as much as other transpilers (such as esm.sh, such as rewriting `npm:` imports, etc). We may add that capability in the future. For now, if you want your npm imports to run in the browser, use `https://esm.sh/package` instead of `npm:package`.
6
7The below script demonstrates this transiplation behavior by fetching its own source code (`import.meta.url`) with the user agent of a browser. You can uncoment the line setting the browser agent if you want to see the difference in the output. Or you could just load this val's module URL in your browser to see the untranspiled TS.
8
9As of July 23, 2024, this is the code that determines when esm.town transpiles or not:
90});
91
92export default app.fetch;
3
4/**
5 * @param handler Fetch handler
6 */
7export default function openInValTown(
1import { email } from "https://esm.town/v/std/email";
2import { fetch } from "https://esm.town/v/std/fetch";
3
4export const isMyWebsiteDown = async () => {
8 let reason: string;
9 try {
10 const res = await fetch(URL);
11 if (res.status !== 200) {
12 reason = `(status code: ${res.status})`;
14 }
15 } catch (e) {
16 reason = `couldn't fetch: ${e}`;
17 ok = false;
18 }
3
4export async function flydotioRSS(req: Request): Promise<Response> {
5 const response = await fetch("https://fly.io/blog/");
6 const body = await response.text();
7 const $ = cheerio.load(body);
8}
9
10export default app.fetch;
11
1fetch contents of a specific wikipedia page using `?title=`param. will return nothing if page doesn't exactly match. example: https://jamiedubs-wikipediapage.web.val.run/?title=Berlin
2
3for a more search-oriented approach, try https://www.val.town/v/jamiedubs/searchWikipedia
1export const fetchWikipediaContent = async (req: Request) => {
2 const url = new URL(req.url);
3 const title = url.searchParams.get("title");
15 encodeURIComponent(title)
16 }`;
17 const response = await fetch(apiUrl);
18 if (!response.ok) {
19 throw new Error(`HTTP error! status: ${response.status}`);
33 });
34 } catch (error) {
35 return new Response(`Error fetching Wikipedia content: ${error.message}`, {
36 status: 500, // Internal Server Error
37 headers: { "Content-Type": "text/plain" },
1import { fetch } from "https://esm.town/v/std/fetch";
2import process from "node:process";
3import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
4
5// I <3 ether.actor, very convenient for fetching on-chain data
6// it's a little slow - Alchemy or another RPC/API provider would be faster
7const url = `https://ether.actor/0x869ad3dfb0f9acb9094ba85228008981be6dbdde/tokenURI`;
11// const svg = `<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 350 350"><style>.base { fill: white; font-family: serif; font-size: 14px; }</style><rect width="100%" height="100%" fill="black" /><text x="10" y="20" class="base">Grave Wand</text><text x="10" y="40" class="base">Ornate Chestplate</text><text x="10" y="60" class="base">Dragon's Crown of Protection</text><text x="10" y="80" class="base">War Belt</text><text x="10" y="100" class="base">"Fate Sun" Divine Slippers of Power</text><text x="10" y="120" class="base">Silk Gloves</text><text x="10" y="140" class="base">Necklace</text><text x="10" y="160" class="base">Bronze Ring</text></svg>`;
12
13async function fetchAndParseSvgFromJson(account: string) {
14 try {
15 const response = await fetch(`${url}/${account}`);
16 const dataUri = await response.text();
17
31 return decodedSvg; // or manipulate as needed
32 } catch (error) {
33 console.error("Error fetching or decoding SVG from JSON:", error);
34 }
35}
68 }
69
70 const svg = await fetchAndParseSvgFromJson(account);
71 const elements = parseElementsFromSvg(svg);
72 // const elements = parseElementsFromSvgUsingRegex(svg);
1// Stripping HTML from the final output before sending the response
2import { fetch } from "https://esm.town/v/std/fetch";
3
4export const searchClosestWiki = async (req: Request) => {
16 encodeURIComponent(searchQuery)
17 }&srwhat=text&format=json&utf8=1&origin=*`;
18 const searchResponse = await fetch(searchUrl);
19 const searchData = await searchResponse.json();
20
30 encodeURIComponent(pageTitle)
31 }&format=json&utf8=1&origin=*`;
32 const contentResponse = await fetch(contentUrl);
33 const contentData = await contentResponse.json();
34 const pageId = Object.keys(contentData.query.pages)[0];
43 });
44 } catch (err) {
45 return new Response(`Error fetching Wikipedia data: ${err.message}`, {
46 status: 500, // Internal Server Error
47 headers: { "Content-Type": "text/plain" },