Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
client.postJSON to isomorphic-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
indeyets committed May 24, 2016
1 parent d889e81 commit 6605304
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,31 @@ export default class ApiClient
}

async postJSON(relativeUrl, data = null) {
let req = request.post(this.apiUrl(relativeUrl));
let headers = {
'Content-Type': 'application/json'
},
body;

if (this.serverReq !== null && 'cookie' in this.serverReq.headers) {
req = req.set('Cookie', this.serverReq.headers['cookie']);
headers = {
Cookie: this.serverReq.headers['cookie'],
'Content-Type': 'application/json'
};
}

if (data !== null) {
req = req.send(data);
body = JSON.stringify(data);
}

const req = fetch(
this.apiUrl(relativeUrl),
{
method: 'POST',
headers,
body
}
);

return Promise.resolve(req);
}

Expand Down Expand Up @@ -327,23 +342,23 @@ export default class ApiClient

async updateUser(user) {
const response = await this.postJSON(`/api/v1/user`, user);
return response.body;
return await response.json();
}

async changePassword(old_password, new_password) {
const response = await this.postJSON(`/api/v1/user/password`, { old_password, new_password });
return response.body;
return await response.json();
}

async resetPassword(email) {
const response = await this.postJSON(`/api/v1/resetpassword`, { email });

return response.body;
return await response.json();
}

async newPassword(hash, password, password_repeat) {
const response = await this.postJSON(`/api/v1/newpassword/${hash}`, { password, password_repeat });
return response.body;
return await response.json();
}

async unfollow(userName) {
Expand Down Expand Up @@ -394,12 +409,12 @@ export default class ApiClient
async createPost(type, data) {
data.type = type;
const response = await this.postJSON(`/api/v1/posts`, data);
return response.body;
return await response.json();
}

async updatePost(uuid, data) {
const response = await this.postJSON(`/api/v1/post/${uuid}`, data);
return response.body;
return await response.json();
}

async deletePost(uuid) {
Expand All @@ -409,17 +424,17 @@ export default class ApiClient

async updateGeotag(uuid, data) {
const response = await this.postJSON(`/api/v1/geotag/${uuid}`, data);
return response.body;
return await response.json();
}

async updateHashtag(uuid, data) {
const response = await this.postJSON(`/api/v1/tag/${uuid}`, data);
return response.body;
return await response.json();
}

async updateSchool(uuid, data) {
const response = await this.postJSON(`/api/v1/school/${uuid}`, data);
return response.body;
return await response.json();
}

async pickpoint(options) {
Expand Down Expand Up @@ -518,7 +533,7 @@ export default class ApiClient
transforms: JSON.stringify(transforms),
derived_id
});
return response.body;
return await response.json();
}

async createComment(postId, text) {
Expand Down
6 changes: 6 additions & 0 deletions test/integration/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('Client test', () => {

expect(await response.text(), 'to be', 'test message in post response');
});

it('#POSTJSON', async () => {
let response = await client.postJSON('/api/v1/test', {foo: 'bar'});

expect(await response.text(), 'to be', 'test message in post response');
});
});

it('#checkGeotagExists return false for non existing geotag', async () => {
Expand Down

0 comments on commit 6605304

Please sign in to comment.