154
155 // Serve cached response if less than 1 minute old
156 const imageResponse = await fetch(imageUrl);
157
158 if (imageResponse.ok) {
229
230 try {
231 const response = await fetch(unsplashApiUrl, {
232 headers: {
233 "Accept-Version": "v1",
239 // Include the response body for more detailed error information
240 const errorBody = await response.text();
241 return new Response(`Failed to fetch from Unsplash API: ${errorBody}`, { status: response.status });
242 }
243
247 const imageUrl = `${photo.urls.raw}${additionalParams ? `&${additionalParams}` : ""}`;
248
249 // Fetch the specific image
250 const imageResponse = await fetch(imageUrl);
251
252 if (!imageResponse.ok) {
25
26async function getTopHNStory() {
27 const topStories = await fetch(
28 "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
29 ).then((res) => res.json());
30
31 const id = topStories[0];
32 const story = await fetch(
33 `https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`
34 ).then((res) => res.json());
11 e.preventDefault();
12 try {
13 const result = await fetch("/process", {
14 method: "POST",
15 body: JSON.stringify({ query })
11
12 try {
13 const response = await fetch("/messages", {
14 method: "POST",
15 headers: { "Content-Type": "application/json" },
60});
61
62// HTTP vals expect an exported "fetch handler"
63// This is how you "run the server" in Val Town with Hono
64export default app.fetch;
9 const [messages, setMessages] = React.useState(initialMessages);
10
11 const fetchMessages = async () => {
12 try {
13 const response = await fetch("/messages");
14 const data = await response.json();
15 setMessages(data.reverse());
16 } catch (error) {
17 console.error("Failed to fetch messages", error);
18 }
19 };
23 <h1>💬 Message Board</h1>
24 <MessageList messages={messages} />
25 <MessageInput onSubmit={fetchMessages} />
26 {thisProjectURL
27 ? (
5async function servePublicFile(path: string): Promise<Response> {
6 const url = new URL("./public/" + path, import.meta.url);
7 const text = await (await fetch(url, {
8 headers: {
9 "User-Agent": "", // to transpile TS to JS
181
182 try {
183 const response = await fetch("/", {
184 method: "POST",
185 body: JSON.stringify({
162 const handleVoiceInput = async (transcript) => {
163 try {
164 const response = await fetch("/generate", {
165 method: "POST",
166 body: JSON.stringify({
207 window.speechSynthesis.speak(utterance);
208 } else {
209 fetch("/speak", {
210 method: "POST",
211 body: JSON.stringify({ text }),
318 }`;
319
320 const ttsResponse = await fetch(ttsUrl, {
321 headers: {
322 "User-Agent":
38
39 try {
40 const response = await fetch("/chat", {
41 method: "POST",
42 body: JSON.stringify({ messages: newMessages, mode }),