-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCat.js
More file actions
44 lines (41 loc) · 1.12 KB
/
Copy pathgetCat.js
File metadata and controls
44 lines (41 loc) · 1.12 KB
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
const axios = require("axios");
const fs = require("fs");
/**
* Get a random cat image URL from the Cat API
* @see https://thecatapi.com/
* @see https://axios-http.com/docs/
* @returns {Promise<string>} URL of a random cat image
*/
async function getRandomCatURL() {
const url = "https://api.thecatapi.com/v1/images/search";
try {
const { data } = await axios.get(url, {
withCredentials: true,
headers: {
"X-API-KEY": process.env.THE_CAT_API_KEY,
},
timeout: 5000,
});
return data[0].url;
} catch (err) {
console.log(err);
}
}
/**
* Get a cat image file from the Cat API
* @see https://axios-http.com/docs/
* @param {string} url URL of a cat image
* @returns {Promise<void>} A cat image file
*/
async function getCatImageFile(url) {
axios({ url, responseType: "stream", timeout: 5000 }).then(
(response) =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream("tmpcat.png"))
.on("finish", () => resolve())
.on("error", (e) => reject(e));
})
);
}
module.exports = { getRandomCatURL, getCatImageFile };