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
import { fetch } from "https://esm.town/v/std/fetch";
export const glifJson = async (id: string) => {
const getRandom = (list) => {
const shuffledList = list.sort(function () {
return 0.5 - Math.random();
});
const numToPick = Math.min(20, shuffledList.length);
var randomItems = [];
for (let i = 0; i < numToPick; i++)
randomItems.push(shuffledList[i]);
return randomItems;
};
const url = `https://alpha.glif.xyz/api/glifs?id=${id}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data) {
const filteredInputs = data[0].spellRuns.filter((r) => r.inputs).map(
(r) => r.inputs
);
const randomInputs = getRandom(filteredInputs);
return { data: randomInputs };
}
else {
throw new Error("Invalid data structure");
}
}
catch (error) {
return { error: error.message };
}
};
// Forked from @jamiedubs.glifJson
👆 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.