forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopics.js
169 lines (158 loc) · 5.13 KB
/
topics.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Fetches topics according to params.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#getting-all-topics|here}
* @param {Object} params - params sent to the API.
* @param {Object} headers- headers sent to the API.
* @returns {Object[]} array of serialized topics.
*/
export const fetchTopics = (params = {}, headers = {}) => {
logger.info('Fetch topics');
return WRIAPI.get('topic', {
headers: {
...WRIAPI.defaults.headers,
...headers,
// TO-DO: forces the API to not cache, this should be removed at some point
'Upgrade-Insecure-Requests': 1
},
params: {
...params,
env: process.env.API_ENV,
application: process.env.APPLICATIONS
}
}).then((response) => {
const { status, statusText, data } = response;
logger.debug(`Topics fetch returned with code ${status}`);
if (status >= 300) {
logger.error('Error fetching topics:', `${status}: ${statusText}`);
throw new Error(statusText);
}
return WRISerializer(data);
}).catch(({ response }) => {
const { status, statusText } = response;
logger.error('Error fetching topics:', `${status}: ${statusText}`);
});
};
/**
* Fetches data for a specific topic.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#getting-all-topics|here}
* @param {String} id - topic id.
* @returns {Object} serialized specified topic.
*/
export const fetchTopic = (id) => {
logger.info(`Fetch topic: ${id}`);
return WRIAPI.get(`topic/${id}?env=${process.env.API_ENV}&application=${process.env.APPLICATIONS}`)
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
if (status === 404) {
logger.debug(`Topic '${id}' not found, ${status}: ${statusText}`);
} else {
logger.error(`Error fetching topic: ${id}: ${status}: ${statusText}`);
}
throw new Error(statusText);
}
return WRISerializer(data);
}).catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching topic: ${id}: ${status}: ${statusText}`);
});
};
/**
* Creates a topic with the provided data.
* This fetch needs authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#topic|here}
* @param {Object} body - data provided to create the new topic.
* @param {String} token - user's token.
* @returns {Object} serialized created topic.
*/
export const createTopic = (body, token) => {
logger.info('Create topic');
return WRIAPI.post('topic', {
data: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
attributes: { ...body }
}
}, {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
}
})
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
logger.error('Error creating topic:', statusText);
throw new Error(statusText);
}
return WRISerializer(data);
});
};
/**
* Updates a specified topic with the provided data.
* This fetch needs authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#topic|here}
* @param {String} id - topic ID to be updated.
* @param {Object} body - data provided to update the topic.
* @param {String} token - user's token
* @returns {Object} serialized topic with updated data
*/
export const updateTopic = (id, body, token) => {
logger.info(`Updates topic ${id}`);
return WRIAPI.patch(`/topic/${id}`, {
data: {
env: process.env.API_ENV,
application: process.env.APPLICATIONS,
attributes: { ...body }
}
}, {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
}
})
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
if (status !== 404) logger.error(`Error upadting topic ${id}:`, statusText);
throw new Error(statusText);
}
return WRISerializer(data);
});
};
/**
* Deletes a specified topic.
* This fetch needs authentication.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#topic|here}
* @param {String} id - topic ID to be deleted.
* @param {String} token - user's token.
* @returns {Object} fetch response.
*/
export const deleteTopic = (id, token) => {
logger.info(`Deletes topic ${id}`);
return WRIAPI.delete(`/topic/${id}`, {
headers: {
...WRIAPI.defaults.headers,
Authorization: token
}
})
.then((response) => {
const { status, statusText } = response;
if (status >= 300) {
if (status !== 404) logger.error(`Error deleting topic ${id}:`, statusText);
throw new Error(statusText);
}
return response;
});
};
export default {
deleteTopic,
updateTopic,
createTopic,
fetchTopic,
fetchTopics
};