113. 🤣🤣🤣🤣
12
13## API
14
15This val uses the [icanhazdadjoke API](https://icanhazdadjoke.com/api). You can find [more docs here](https://github.com/15Dkatz/official_joke_api), such as how to [filter by type](https://github.com/15Dkatz/official_joke_api?tab=readme-ov-file#grab-jokes-by-type).
3
4export async function dailyDadJoke() {
5 let { setup, punchline } = await fetchJSON("https://official-joke-api.appspot.com/random_joke");
6 return email({
7 text: punchline,
2 const url = new URL(request.url);
3
4 if (url.pathname === "/api/files") {
5 const user = url.searchParams.get("user");
6 const repo = url.searchParams.get("repo");
10 }
11
12 const apiUrl = `https://api.github.com/repos/${user}/${repo}/contents`;
13 const response = await fetch(apiUrl);
14 const contents = await response.json();
15
56 result.innerHTML = 'Loading...';
57
58 const response = await fetch(\`/api/files?user=\${user}&repo=\${repo}\`);
59 const html = await response.text();
60 result.innerHTML = html;
8 ...params: Parameters<ServiceWorkerRegistration["showNotification"]>
9) => {
10 webPush.setVapidDetails(url, pubKey, privKey);
11 if (!subscription) throw new Error("subscription is falsy");
12 await webPush.sendNotification(
1import * as webpush from "jsr:@negrel/webpush@^0.3.0";
2
3// https://github.com/web-push-libs/web-push/blob/v3.4.4/README.md#using-vapid-key-for-applicationserverkey
4function urlBase64ToUint8Array(b64) {
5 const padding = "=".repeat((4 - (b64.length % 4)) % 4);
44}
45
46const vapidKeysAlgo = {
47 name: "ECDSA",
48 namedCurve: "P-256",
49};
50
51async function importVapidKeys(
52 exportedKeys: { pubKey: string; privKey: string },
53 { crypto = globalThis.crypto.subtle, extractable = false }: {
60 "raw",
61 urlBase64ToUint8Array(exportedKeys.pubKey),
62 vapidKeysAlgo,
63 true,
64 ["verify"],
67 "jwk",
68 getPrivKeyJWK(exportedKeys),
69 vapidKeysAlgo,
70 extractable,
71 ["sign"],
81 ...params: PushParams
82) => {
83 const vapidKeys = await importVapidKeys({ pubKey, privKey }, {
84 extractable: false,
85 });
87 const appServer = await webpush.ApplicationServer.new({
88 contactInformation: url,
89 vapidKeys,
90 });
91
5console.info("Launching browser...");
6
7// Use Deno.env to get the API key
8const apiKey = Deno.env.get("BROWSERBASE_API_KEY");
9if (!apiKey) {
10 console.error("BROWSERBASE_API_KEY environment variable is not set");
11 Deno.exit(1);
12}
14try {
15 const browser = await chromium.connectOverCDP(
16 `wss://connect.browserbase.com?apiKey=${apiKey}`,
17 );
18 console.info("Connected!");
58 });
59
60 const res = await fetch("https://api.pinata.cloud/pinning/pinJSONToIPFS", {
61 method: "POST",
62 headers: {
24 }
25 }
26 const resp = await fetch(`https://api.val.town/v1/alias/${author}/${name}`, {
27 headers,
28 });
6* Fork this val to your own profile.
7* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.
8* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).
9* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).
10* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
11
12<img width=500 src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/7077d1b5-1fa7-4a9b-4b93-f8d01d3e4f00/public"/>
6* Fork this val to your own profile.
7* Make a folder for the temporary vals that get generated, take the ID from the URL, and put it in `tempValsParentFolderId`.
8* If you want to use OpenAI models you need to set the `OPENAI_API_KEY` [env var](https://www.val.town/settings/environment-variables).
9* If you want to use Anthropic models you need to set the `ANTHROPIC_API_KEY` [env var](https://www.val.town/settings/environment-variables).
10* Create a [Val Town API token](https://www.val.town/settings/api), open the browser preview of this val, and use the API token as the password to log in.
11
12<img width=500 src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/7077d1b5-1fa7-4a9b-4b93-f8d01d3e4f00/public"/>