1# Framer Fetch: Basic
2
3A basic example of an API endpoint to use with Framer Fetch. CORS headers are permissive by default on Val Town, so no need to set them.
53 const imageApiEndpoint = record?.["rdf:RDF"]?.["svcs:Service"]?.[0]?.$?.["rdf:about"];
54 if (imageApiEndpoint) {
55 return fetch(imageApiEndpoint)
56 .then(resp => resp.json())
57 .catch((err) => console.log(`Error fetching Image Information from ${imageApiEndpoint}`, err));
58 } else return null;
59}
62 const uri = part.$["rdf:resource"];
63 const id = uri.split("/").pop();
64 return fetchJson(id)
65 .then(getImageInformation)
66 .catch((err) => console.log(`Error fetching more images from ${id}`, err));
67}
68
142}
143
144async function fetchJson(id: string) {
145 const headers = new Headers([
146 ["Accept-Profile", "edm"],
149 const url = new URL(apiBase + id);
150 const parser = new Parser();
151 return fetch(url, { headers })
152 .then(resp => resp.text())
153 .then(text => parser.parseStringPromise(text))
164 });
165 }
166 const resp = await fetchJson(id.toString());
167 if (resp.error || params.get("format") === "edm") {
168 return Response.json(resp);
36
37 useEffect(() => {
38 async function fetchStats() {
39 const response = await fetch("/dashboard-stats");
40 const data = await response.json();
41 setStats(data);
42 }
43 fetchStats();
44 }, []);
45
146
147 try {
148 const response = await fetch("/", {
149 method: "POST",
150 body: JSON.stringify({
20 for (const topic of KEYWORDS) {
21 const results = await Promise.allSettled([
22 fetchHackerNewsResults(topic),
23 fetchTwitterResults(topic),
24 fetchRedditResults(topic),
25 ]);
26
49}
50
51// Fetch Hacker news, Twitter, and Reddit results
52async function fetchHackerNewsResults(topic: string): Promise<Website[]> {
53 return hackerNewsSearch({
54 query: topic,
58}
59
60async function fetchTwitterResults(topic: string): Promise<Website[]> {
61 return twitterSearch({
62 query: topic,
67}
68
69async function fetchRedditResults(topic: string): Promise<Website[]> {
70 return redditSearch({ query: topic });
71}
84 }
85
86 const response = await fetch(slackWebhookUrl, {
87 method: "POST",
88 headers: { "Content-Type": "application/json" },
90
91 try {
92 const response = await fetch("/ask", {
93 method: "POST",
94 headers: {
87
88 try {
89 const response = await fetch("/summarize", {
90 method: "POST",
91 body: JSON.stringify({ url }),
224 const { url } = await request.json();
225
226 // Fetch webpage content
227 const webpageResponse = await fetch(url);
228 const webpageText = await webpageResponse.text();
229
145
146 try {
147 const response = await fetch("/ask", {
148 method: "POST",
149 headers: {
8 }
9
10 const response = await fetch(`https://ntfy.sh/${ntfyChannel}`, {
11 method: "POST",
12 body: `What did you throw out today?`,
35}, { concurrency: 3 })).filter(Boolean);
36
37// fetch text from endpoint
38const vals3 = await pMap(vals2, async (v) => {
39 if (!v.links.endpoint) return v;
40 const text = await (await fetch(v.links.endpoint)).text();
41 return { ...v, endpointText: text };
42}, { concurrency: 3 });
69}
70
71// fetch ai summary of val
72const vals4 = await pMap(vals3, async (v) => {
73 const descriptions = await getDescriptions(v);
76
77async function getScreenshot(url: string) {
78 const resp = await fetch(
79 "https://api.apiflash.com/v1/urltoimage?" + new URLSearchParams({
80 access_key: Deno.env.get("API_FLASH"),
88export type WideApi = typeof routes;
89
90export default app.fetch.bind(app);