Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export const nasaImageDetails = async () => {
const nasaAPOD = await fetchJSON("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
let nasaImageHtml = nasaAPOD.hdurl
? `<img width="100%" src="${nasaAPOD.hdurl}"/>`
: nasaAPOD.media_type === "video"
? `<iframe width="100%" height="100%" src="${nasaAPOD.url}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
: `<p>Unknown Type</p><br/><code><pre>${JSON.stringify(nasaAPOD, null, 2)}</pre></code>`;
nasaImageHtml += `<p>${nasaAPOD.explanation}</p>`;
nasaImageHtml += "<p>"
+ (nasaAPOD.media_type === "video"
? `<small>Video Url: ${nasaAPOD.url}</small>`
: `<p><small>Image Url: ${nasaAPOD.hdurl}</small>`)
+ "</p>";
return { html: nasaImageHtml, nasaAPOD };
};
August 25, 2024