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
31
32
33
34
35
36
37
38
39
40
41
42
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const variablePower = (request, response) => {
const stylesheet = `
<style>
ul {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
}
ul > li {
flex-basis: 200px;
}
ul li img {
object-fit: cover;
max-width: 100%;
height: auto;
vertical-align: middle;
border-radius: 5px;
}
</style>
`;
fetchJSON("https://api.magicthegathering.io/v1/cards?power=*")
.then(({ cards }) => {
const names = new Set();
const isNewCard = ({ name }) => names.size + 1 == names.add(name).size;
const sortByName = (a, b) => (a.name < b.name ? -1 : 1);
const renderCard = ({ name, imageUrl, set, number }) =>
`<li><a href="https://scryfall.com/card/${set.toLowerCase()}/${number}">${name} <img src=${imageUrl} /></a>`;
const cardlist = cards.sort(sortByName).filter(isNewCard).map(renderCard);
const ul = (children) => `<ul>${children.join("\n")}</ul>`;
const html = (children) =>
[`<!doctype html>`, stylesheet, ul(cardlist)].join("\n");
response.set("Content-Type", "text/html");
response.send(html);
});
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.