mirror of
https://github.com/wassname/talk.git
synced 2026-08-14 12:50:17 +08:00
Moving init and responsehandler to shared functions, updating all routes to return valid JSON.
This commit is contained in:
@@ -8,6 +8,23 @@ export const ADD_ITEM = 'ADD_ITEM';
|
||||
export const UPDATE_ITEM = 'UPDATE_ITEM';
|
||||
export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY';
|
||||
|
||||
const getInit = (method, body) => {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
};
|
||||
|
||||
const init = {method, headers};
|
||||
if (method.toLowerCase() !== 'get') {
|
||||
init.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
return init;
|
||||
};
|
||||
|
||||
const responseHandler = response => {
|
||||
return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`);
|
||||
};
|
||||
/**
|
||||
* Action creators
|
||||
*/
|
||||
@@ -79,12 +96,8 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) =>
|
||||
*/
|
||||
export function getStream (assetId) {
|
||||
return (dispatch) => {
|
||||
return fetch(`/api/v1/stream?asset_id=${assetId}`)
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
)
|
||||
return fetch(`/api/v1/stream?asset_id=${assetId}`, getInit('GET'))
|
||||
.then(responseHandler)
|
||||
.then((json) => {
|
||||
|
||||
/* Add items to the store */
|
||||
@@ -148,13 +161,8 @@ export function getStream (assetId) {
|
||||
|
||||
export function getItemsArray (ids) {
|
||||
return (dispatch) => {
|
||||
return fetch(`/v1/item/${ids}`)
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.json()
|
||||
: Promise.reject(`${response.status } ${ response.statusText}`);
|
||||
}
|
||||
)
|
||||
return fetch(`/v1/item/${ids}`, getInit('GET'))
|
||||
.then(responseHandler)
|
||||
.then((json) => {
|
||||
for (let i = 0; i < json.items.length; i++) {
|
||||
dispatch(addItem(json.items[i]));
|
||||
@@ -183,20 +191,8 @@ export function postItem (item, type, id) {
|
||||
if (id) {
|
||||
item.id = id;
|
||||
}
|
||||
let options = {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(item),
|
||||
headers: {
|
||||
'Content-Type':'application/json'
|
||||
}
|
||||
};
|
||||
return fetch(`/api/v1/${type}`, options)
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.json()
|
||||
: Promise.reject(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
)
|
||||
return fetch(`/api/v1/${type}`, getInit('POST', item))
|
||||
.then(responseHandler)
|
||||
.then((json) => {
|
||||
dispatch(addItem({...item, id:json.id}, type));
|
||||
return json.id;
|
||||
@@ -227,21 +223,9 @@ export function postAction (item_id, action_type, user_id, item_type) {
|
||||
action_type,
|
||||
user_id
|
||||
};
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type':'application/json'
|
||||
},
|
||||
body: JSON.stringify(action)
|
||||
};
|
||||
|
||||
return fetch(`/api/v1/${item_type}/${item_id}/actions`, options)
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.json()
|
||||
: Promise.reject(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
);
|
||||
return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('POST', action))
|
||||
.then(responseHandler);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -266,20 +250,8 @@ export function deleteAction (item_id, action_type, user_id, item_type) {
|
||||
action_type,
|
||||
user_id
|
||||
};
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type':'application/json'
|
||||
},
|
||||
body: JSON.stringify(action)
|
||||
};
|
||||
|
||||
return fetch(`/api/v1/${item_type}/${item_id}/actions`, options)
|
||||
.then(
|
||||
response => {
|
||||
return response.ok ? response.text()
|
||||
: Promise.reject(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
);
|
||||
return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('DELETE', action))
|
||||
.then(responseHandler);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ router.delete('/:comment_id', (req, res, next) => {
|
||||
Comment
|
||||
.removeById(req.params.comment_id)
|
||||
.then(() => {
|
||||
res.status(201).send('OK. Removed');
|
||||
res.status(201).send({});
|
||||
})
|
||||
.catch(error => {
|
||||
next(error);
|
||||
@@ -137,7 +137,7 @@ router.delete('/:comment_id/actions', (req, res, next) => {
|
||||
Comment
|
||||
.removeAction(req.params.comment_id, req.body.user_id, req.body.action_type)
|
||||
.then(() => {
|
||||
res.status(201).send('OK. Removed');
|
||||
res.status(201).sent({});
|
||||
})
|
||||
.catch(error => {
|
||||
next(error);
|
||||
|
||||
@@ -119,6 +119,7 @@ describe('itemActions', () => {
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type':'application/json'
|
||||
},
|
||||
body: JSON.stringify(item.data)
|
||||
@@ -166,11 +167,11 @@ describe('itemActions', () => {
|
||||
|
||||
describe('deleteAction', () => {
|
||||
it ('should remove an action', () => {
|
||||
fetchMock.delete('*', 'Action removed.');
|
||||
fetchMock.delete('*', {});
|
||||
return actions.deleteAction('abc', 'flag', '123', 'comments')(store.dispatch)
|
||||
.then(response => {
|
||||
expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/comments/abc/actions');
|
||||
expect(response).to.equal('Action removed.');
|
||||
expect(response).to.deep.equal({});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user