-
Notifications
You must be signed in to change notification settings - Fork 0
/
axiosCalls.js
40 lines (31 loc) · 896 Bytes
/
axiosCalls.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
const axios = require("axios");
const getArticleTitles = async () => {
try {
const url = `https://codaisseur-coders-network.herokuapp.com/posts`;
const response = await axios.get(url);
const { data } = response;
const articles = data.rows;
if (!articles.length) {
return "No articles found";
}
return articles.map((a) => a.title);
} catch (error) {
console.log(error.message);
throw new Error("Something went wrong");
}
};
const getOneArticle = async (id) => {
try {
const response = await axios.get(
`https://codaisseur-coders-network.herokuapp.com/posts/${id}`
);
const article = response.data;
if (!article.title) {
throw new Error("404 article not found");
}
return article;
} catch (e) {
throw new Error("Something went wrong");
}
};
module.exports = { getArticleTitles, getOneArticle };