-
Notifications
You must be signed in to change notification settings - Fork 6
/
cat_api_fetch.js
96 lines (93 loc) · 1.85 KB
/
cat_api_fetch.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// URL: https://beta.observablehq.com/@saylorsd/cat-api
// Title: Cat API
// Author: Steven Saylor (@saylorsd)
// Version: 189
// Runtime version: 1
const m0 = {
id: "aef306c5255914f4@189",
variables: [
{
inputs: ["md"],
value: (function(md){return(
md`# Cat API
### But this time with fetch!`
)})
},
{
name: "baseURL",
value: (function(){return(
"https://thecatapi.com/"
)})
},
{
name: "endpoint",
value: (function(){return(
"api/images/get"
)})
},
{
name: "requestURL",
inputs: ["baseURL","endpoint"],
value: (function(baseURL,endpoint){return(
baseURL + endpoint
)})
},
{
name: "myBlob",
inputs: ["requestURL"],
value: (async function(requestURL){return(
await fetch(requestURL)
.then(data => data.blob())
.then(blob => blob)
)})
},
{
inputs: ["myBlob"],
value: (function(myBlob)
{
let image = new Image();
image.src = window.webkitURL.createObjectURL( myBlob);
return image
}
)
},
{
inputs: ["requestURL"],
value: (async function(requestURL)
{
let newBlob = await fetch(requestURL)
.then(data => data.blob())
.then(blob => blob);
let image = new Image;
image.src = image.src = window.URL.createObjectURL(newBlob);
return image
}
)
},
{
name: "spaceURL",
inputs: ["requestURL"],
value: (function(requestURL){return(
requestURL + "?category=space&size=large"
)})
},
{
name: "categoryURL",
inputs: ["spaceURL"],
value: (async function(spaceURL)
{
let spaceBlob = await fetch(spaceURL)
.then(data => data.blob());
let image = new Image();
image.src = window.webkitURL.createObjectURL(spaceBlob);
return image
}
)
}
]
};
const notebook = {
id: "aef306c5255914f4@189",
modules: [m0]
};
export default notebook;