Public
HTTP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { fetchNftMetadata } from "https://esm.town/v/jamiedubs/nftMetadata";
export const nftImage = async (req: Request) => {
const searchParams = new URL(req.url).searchParams;
const contractAddress = searchParams.get("contractAddress");
const tokenId = searchParams.get("tokenId");
const format = searchParams.get("format")?.toLowerCase() ?? "text";
console.log("nftImage", { contractAddress, tokenId });
const json = await fetchNftMetadata(contractAddress, tokenId);
console.log("nftMetadata response =>", json);
const imageUrl = json["metadata"] && json["metadata"]["image"];
if (format == "json") {
return Response.json({ imageUrl });
}
else if (format == "html") {
return new Response(`<img src="${imageUrl}"/>`, {
headers: {
"Content-Type": "text/html",
},
});
}
else {
return new Response(imageUrl, {
headers: {
"Content-Type": "text/plain",
},
});
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v29
December 8, 2023