forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.js
196 lines (187 loc) · 7.1 KB
/
collections.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Retrieve all collections from a user
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#get-collections|here}
* @param {String} token User's token
* @param {Object} params Request optional parameters
*/
export const fetchAllCollections = (
token,
params = {
env: process.env.API_ENV,
application: process.env.APPLICATIONS
}
) => {
logger.info('Fetch all collections');
return WRIAPI.get('collection', {
headers: {
Authorization: token,
'Upgrade-Insecure-Requests': 1
},
params
})
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching all collections: ${status}: ${statusText}`);
throw new Error(`Error fetching all collections: ${status}: ${statusText}`);
});
};
/**
* Retrieve a specific collection
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#get-collection-by-id|here}
* @param {String} token User's token
* @param {String} collectionId Id of the collection we are asking for.
* @param {Object} params Request parameters
*/
export const fetchCollection = (
token,
collectionId,
params = {
env: process.env.API_ENV,
application: process.env.APPLICATIONS
}
) => {
logger.info(`Fetch collection ${collectionId}`);
return WRIAPI.get(`collection/${collectionId}`, {
headers: {
Authorization: token,
'Upgrade-Insecure-Requests': 1
},
params
})
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching collection ${collectionId}: ${status}: ${statusText}`);
throw new Error(`Error fetching collection ${collectionId}: ${status}: ${statusText}`);
});
};
/**
* Create a new collection associated to the authenticated user
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#create-collection|here}
* @param {String} token User's token
* @param {Object} data collection data
*/
export const createCollection = (token, data = {}) => {
logger.info('Create collection');
return WRIAPI.post(`${process.env.WRI_API_URL}/collection`, data, {
headers: {
'Content-Type': 'application/json',
Authorization: token
}
})
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error creating collection: ${status}: ${statusText}`);
// we shouldn't assume 400 is duplicated collection,
// but there's no another way to find it out at this moment
if (status === 400) {
throw new Error(`Collection duplicated. The collection "${data.name}" already exists.`);
} else {
throw new Error(`Error creating collection: ${status}: ${statusText}`);
}
});
};
/**
* Delete an existing collection associated to the authenticated user
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#delete-collection|here}
* @param {String} token User's token
* @param {String} collectionId Id of the collection to be removed
*/
export const deleteCollection = (token, collectionId) => {
logger.info(`Delete collection ${collectionId}`);
return WRIAPI.delete(`collection/${collectionId}`, { headers: { Authorization: token } })
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error deleting collection ${collectionId}: ${status}: ${statusText}`);
throw new Error(`Error deleting collection ${collectionId}: ${status}: ${statusText}`);
});
};
/**
* Update an existing collection associataed to the authenticated user
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#update-collection|here}
* @param {String} token User's token
* @param {String} collectionId Id of the collection to be edited
* @param {Object} data Data to be updated
*/
export const updateCollection = (token, collectionId, data) => {
logger.info(`Update collection ${collectionId}`);
return WRIAPI.patch(`collection/${collectionId}`, data, {
headers: {
'content-type': 'application/json',
Authorization: token
}
})
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error updating collection ${collectionId}: ${status}: ${statusText}`);
throw new Error(`Error updating collection ${collectionId}: ${status}: ${statusText}`);
});
};
/* Resources management */
/**
* Add a resource to the collection
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#add-resource-to-collection|here}
* @param {String} token User's token
* @param {String} collectionId Id of the collection to be edited
* @param {Object} resource Resource to be addded to the collection
*/
export const addResourceToCollection = (token, collectionId, resource = {}) => {
logger.info(`Add resource to collection ${collectionId}`);
return WRIAPI.post(
`collection/${collectionId}/resource`,
{ ...resource },
{
headers: {
'Content-Type': 'application/json',
Authorization: token
}
}
)
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error adding resource to collection ${collectionId}: ${status}: ${statusText}`);
throw new Error(
`Error adding resource to collection ${collectionId}: ${status}: ${statusText}`
);
});
};
/**
* Remove resource from collection
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#delete-collection-resource|here}
* @param {String} token User's token
* @param {String} collectionId Id of the collection to be edited
* @param {Object} resource Resource to be removed from the collection
*/
export const removeResourceFromCollection = (token, collectionId, resource = {}) => {
logger.info(`Remove resource from collection ${collectionId}`);
const { type, id } = resource;
return WRIAPI.delete(`collection/${collectionId}/resource/${type}/${id}`, { headers: { Authorization: token } })
.then(response => WRISerializer(response.data))
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(
`Error removing resource from collection ${collectionId}: ${status}: ${statusText}`
);
throw new Error(
`Error removing resource from collection ${collectionId}: ${status}: ${statusText}`
);
});
};
export default {
fetchAllCollections,
fetchCollection,
createCollection,
deleteCollection,
updateCollection,
addResourceToCollection,
removeResourceFromCollection
};