1Migrated from folder: a2_shared/fetch_example2
1import { email } from "https://esm.town/v/std/email?v=9";
2
3// Fetches a random joke.
4async function fetchRandomJoke() {
5 const response = await fetch(
6 "https://official-joke-api.appspot.com/random_joke",
7 );
9}
10
11const randomJoke = await fetchRandomJoke();
12const setup = randomJoke.setup;
13const punchline = randomJoke.punchline;
2import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
3import { createInstanceActor } from "https://esm.town/v/vladimyr/activityPubInstanceActor";
4import { createSignedFetch } from "https://esm.town/v/vladimyr/signedFetch";
5import { privateKeyPem, publicKeyPem } from "https://esm.town/v/vladimyr/signRequest_testdata";
6
12export { id, publicKeyId };
13
14const signedFetch = createSignedFetch({
15 pkcs8: privateKeyPem,
16 publicKeyId,
21 const query = Object.fromEntries(searchParams.entries());
22 if (pathname === "/" && query.url) {
23 const resp = await signedFetch(query.url, {
24 headers: { accept: "application/activity+json" },
25 });
1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
2
3export const gsheet_call = async (service_account, sheet_id, method, action, data) => {
9 };
10 const token = await getToken(service_account, googleAuthOptions);
11 const result = fetchJSON(
12 `https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/${action}`,
13 {
36});
37
38export async function fetchNewestVals({ page = 0 } = {}) {
39 const url = new URL("https://www.val.town/newest");
40 url.searchParams.set("_data", "routes/_app.newest");
24});
25
26export async function fetchVal(author: string, name: string) {
27 const prefixUrl = new URL("/v1/alias", API_URL);
28 const resp = await ky.get(`${author}/${name}`, { prefixUrl }).json();
1import { normalizeURL } from "https://esm.town/v/stevekrouse/normalizeURL";
2
3export const fetchText = async (
4 url: string | URL,
5 options?: RequestInit & {
6 bearer?: string;
7 fetch?: typeof fetch;
8 },
9) => {
13 headers.set("authorization", `Bearer ${options.bearer}`);
14 }
15 let fetch = options?.fetch ?? globalThis.fetch;
16 let resp = await fetch(url, {
17 redirect: "follow",
18 ...options,
1import { fetchText } from "https://esm.town/v/stevekrouse/fetchText";
2
3export const fetchJSON = async (
4 url: string | URL,
5 options?: RequestInit & {
6 bearer?: string;
7 fetch?: typeof fetch;
8 },
9) => {
10 let headers = new Headers(options?.headers ?? {});
11 headers.set("accept", "application/json");
12 let text = await fetchText(url, {
13 ...options,
14 headers,
17 return JSON.parse(text);
18 } catch (e) {
19 throw new Error(`fetchJSON error: ${e.message} in ${url}\n\n"${text}"`);
20 }
21};
1import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
2
3export const gsheet_call = async (service_account, sheet_id, method, action, data) => {
9 };
10 const token = await getToken(service_account, googleAuthOptions);
11 const result = fetchJSON(
12 `https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/${action}`,
13 {
1/**
2 * Fetches JSON data from a URL.
3 * @async
4 * @param {string | URL} url - The URL to fetch JSON data from.
5 * @param {RequestInit & { bearer?: string; fetch?: typeof fetch }} [options] - Additional options for the fetch.
6 * @returns {Promise<any>} - A Promise that resolves to the fetched JSON data.
7 */
8export const fetchJSON = async (
9 url: string | URL,
10 options?: RequestInit & { bearer?: string; fetch?: typeof fetch },
11) => {
12 let headers = new Headers(options?.headers ?? {});
15 headers.set("authorization", `Bearer ${options.bearer}`);
16 }
17 let fetch = options?.fetch ?? globalThis.fetch;
18 let resp = await fetch(normalizeURL(url), {
19 redirect: "follow",
20 ...options,
26 }
27 catch (e) {
28 throw new Error(`fetchJSON error: ${e.message} in ${url}\n\n"${text}"`);
29 }
30};