forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.js
43 lines (37 loc) · 1.09 KB
/
blog.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
import { blogAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Fetchs posts from wordpress API.
*
* @param {Object} params - params sent to the API.
* @param {Object} headers - headers sent to the API.
* @returns {Object[]} array of parsed posts.
*/
export const fetchPosts = (params = {}, headers = {}) => {
logger.info('fetches posts from blog');
return blogAPI.get('/posts', {
headers: {
...blogAPI.defaults.headers,
...headers
},
params: { ...params }
})
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
logger.error('Error fetching posts from blog:', `${status}: ${statusText}`);
throw new Error(statusText);
}
return data;
})
.catch(({ response }) => {
const { data } = response;
const {
message,
data: { status }
} = data;
logger.error(`Error fetching posts from blog: ${status}: ${message}`);
throw new Error(`Error fetching posts from blog: ${status}: ${message}`);
});
};
export default { fetchPosts };