7import { render, React } from "https://esm.town/v/jxnblk/ReactStream";
8
9function App() {
10 const [count, setCount] = React.useState(0);
11 return (
62```tsx
63// example middleware
64async function api (req: Request, res: Response, next): Promise<Response> {
65 if (req.pathname !== "/api") return next();
66 if (req.method === "POST") {
78```tsx
79// example middleware
80async function getInitialProps (req: Request, res: Response, next) {
81 // fetch data or do async work to pass as props to the component
82 req.data = {
95import { render } from "https://esm.town/v/jxnblk/ReactStream";
96
97function App () {
98 return (
99 <html>
1export default async function getValsContextWindow(model: any) {
2 const readmeVals: any = await (await fetch("https://janpaul123-readmevals.web.val.run/")).json();
3
183 import { render } from "npm:preact-render-to-string";
184
185 export default async function(req: Request) {
186 return new Response(
187 render(
212 const FLAT_TEMPLATES = sections.flatMap((section) => section.templates);
213
214 function tsResponse(code) {
215 return "```ts\n" + code + "\n```";
216 }
383 ### Utilities
384
385 Our Blob SDK also includes some utility functions to make working with blobs easier.
386
387 ##### Copy
507 role: "assistant",
508 content:
509 "\n```ts\nexport default async function (req: Request): Promise<Response> {\n return Response.json(\"Hello world\")\n}\n```",
510 },
511 {
516 role: "assistant",
517 content:
518 "\n```ts\nexport default async function(req: Request): Promise<Response> {\n const query = new URL(req.url).searchParams;\n\n // Read name from the querystring or body. Defaults to \"you\" if not present.\n const name = query.get(\"name\") || (await req.json().catch(() => ({}))).name || \"you\";\n\n // Returns the HTML response\n return new Response(`<h1>Hi ${name}!</h1>`, {\n headers: { \"Content-Type\": \"text/html\" },\n });\n}\n```",
519 },
520 {
533 role: "assistant",
534 content:
535 "```ts\nexport default async function(req: Request): Promise<Response> {\n const apiKey = Deno.env.get(\"METEO_KEY\");\n const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=40.6782&longitude=-73.9442&hourly=temperature_2m¤t_weather=true&apikey=${apiKey}`;\n\n const response = await fetch(apiUrl);\n const weatherData = await response.json();\n\n return new Response(JSON.stringify(weatherData), { headers: { \"Content-Type\": \"application/json\" } });\n}\n```",
536 },
537 {
35// Database querying.
36
37function parseResultSet<T>(row: ResultSet): T[] {
38 return row.rows.map((r) => Object.fromEntries(r.map((c, i) => [row.columns[i], c]))) as T[];
39}
67};
68
69// Client side javascript and template functions.
70
71const clientJavascript = async () => {
135 });
136
137 function createHeartBurst(event) {
138 const button = event.currentTarget;
139 const buttonContainer = button.closest(".heart-button-container");
174};
175
176function escapeHTML(html: string): string {
177 const escapeChars: { [char: string]: string } = {
178 "&": "&",
408 let encoder = new TextEncoder();
409
410 function isValidSVG(svgString) {
411 const parser = new DOMParser({
412 errorHandler: {
488
489app.get("/:id/img.svg", async (c) => {
490 function addSVGNamespace(svgString) {
491 if (!/^<\?xml/.test(svgString)) svgString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + svgString;
492 if (!/xmlns="http:\/\/www.w3.org\/2000\/svg"/.test(svgString))
9 * @param val Define which val should open. Defaults to the root reference
10 */
11export function modifyHtmlString(
12 bodyText: string,
13 { val, style }: { val?: ValRef; style?: string } = {},
40 * @param val Define which val should open
41 */
42export function modifyFetchHandler(
43 handler: (req: Request) => Response | Promise<Response>,
44 { val, style }: { val?: ValRef; style?: string } = {},
49### Linking to the val
50
51These functions infer the val using the call stack or the request URL. If the inference isn't working, or if you want to ensure it links to a specific val, pass the `val` argument:
52
53- `modifyFetchHandler(handler, {val: { handle: "andre", name: "foo" }})`
3const query = "\"val.town\" OR \"val town\" -_ValTown_ -is:retweet"
4
5export async function twitterAlert({ lastRunAt }: Interval) {
6 const results = await twitterSearch({
7 query,
9const padLeft = 48;
10
11export function Badge({
12 scale = 1,
13 label = "View source on",
66}
67
68export default async function(req: Request): Promise<Response> {
69 const svg = renderToStaticMarkup(
70 <Badge scale={1} />,
16}
17
18// Function to fetch package info from NPM registry
19async function fetchPackageInfo(packageName: string) {
20 const url = `https://registry.npmjs.org/${packageName}`;
21 const response = await fetch(url);
26}
27
28// Function to extract GitHub repo URL from package info
29function extractGitHubRepoUrl(packageInfo: any): string | null {
30 const repository = packageInfo.repository;
31 if (repository && repository.url) {
38}
39
40// Function to get the GitHub repository path (owner/repo) from the URL
41function getGithubRepoPath(repoUrl: string): string | null {
42 const match = repoUrl.match(/github\.com\/([^\/]+\/[^\/]+)/);
43 return match ? match[1] : null;
44}
45
46async function getGithubReleases(repo: string): Promise<GithubRelease[]> {
47 const response = await fetch(`https://api.github.com/repos/${repo}/releases`);
48 if (!response.ok) {
52}
53
54function compareVersions(version1: string, version2: string): number {
55 const v1 = version1.replace(/^v/, "").split(".").map(Number);
56 const v2 = version2.replace(/^v/, "").split(".").map(Number);
63}
64
65async function getReleaseNotesBetweenVersions(repo: string, currentVersion: string): Promise<GithubRelease[]> {
66 const releases = await getGithubReleases(repo);
67
74}
75
76async function checkNpmUpdates(packageJsonUrl: string): Promise<string> {
77 try {
78 const response = await fetch(packageJsonUrl);
1import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
2
3export function stripHTML(html: string) {
4 const doc = new DOMParser().parseFromString(html, "text/html");
5 return doc.body.textContent;
4const exa = new Exa(exaSecret);
5
6const similarSites = async function(url: string) {
7 const results = await exa.findSimilar(url);
8 if (results.results) {