Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
coverage
.idea
.DS_Store
node_modules/
.idea/
coverage/
DS_Store
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ function getUsers(req, res) {
module.exports = getUsers;

```

## Changelog

##### 0.1.1
- set node version to 6 in travis.yml for es6 support

##### 0.1.2
- add support for missing req.flash

##### 0.1.3
- fix circular json in response
5 changes: 4 additions & 1 deletion lib/methods/jsonResponseError.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const makeJsonResponseBody = require('./_makeJsonResponseBody'),

const HTTP_STATUS_CODE_SERVER_ERROR = 500;

const stringify = require('json-stringify-safe');

/**
* Respond to the client with an unsuccessful JSON payload.
* @param {Object} req
Expand All @@ -25,7 +27,8 @@ const jsonResponseError = R.curry((req, res, err) => {
setFlashData(res, response.flash);
}

return res.status(statusCode).json(response);
res.set('Content-Type', 'application/json');
return res.status(statusCode).send(stringify(response));
});

module.exports = jsonResponseError;
4 changes: 3 additions & 1 deletion lib/methods/jsonResponseSuccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const makeJsonResponseBody = require('./_makeJsonResponseBody'),

const HTTP_STATUS_CODE_SUCCESS = 200;

const stringify = require('json-stringify-safe');
/**
* Respond to the client with a successful JSON payload.
* @param {Object} req
Expand All @@ -25,7 +26,8 @@ const jsonResponseSuccess = R.curry((req, res, data) => {
setFlashData(res, response.flash);
}

return res.json(response);
res.set('Content-Type', 'application/json');
return res.send(stringify(response));
});

module.exports = jsonResponseSuccess;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "alien-node-api-utils",
"version" : "0.1.2",
"version" : "0.1.3",
"description" : "Helper functions for API handling on NodeJS",
"main" : "lib/API.js",
"dependencies" : {
Expand Down
40 changes: 22 additions & 18 deletions spec/jsonResponseErrorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mockReqWithSession = {
session : {
flash : {}
},
flash : R.identity
flash : R.identity
};

const mockReqNoFlash = {
Expand All @@ -23,47 +23,51 @@ const mockRes = {
status : () => {
return mockRes;
},
locals : {}
locals : {},
set : () => {
},
send : R.identity

};

const FAKE_HTTP_STATUS_CODE_ERROR = 1337;

const FAKE_ERROR_RESPONSE = {
const FAKE_ERROR_RESPONSE = {
statusCode : FAKE_HTTP_STATUS_CODE_ERROR,
foo : 'bar'
},
FAKE_ERROR_RESPONSE_TRIMMED = {
foo : 'bar'
};

const EXPECTED_RESPONSE_DATA_NO_SESSION = {
const EXPECTED_RESPONSE_DATA_NO_SESSION = JSON.stringify({
statusCode : FAKE_HTTP_STATUS_CODE_ERROR,
flash : {
notice : [],
error : []
},
data : FAKE_ERROR_RESPONSE_TRIMMED
},
EXPECTED_RESPONSE_DATA_WITH_SESSION = {
}),
EXPECTED_RESPONSE_DATA_WITH_SESSION = JSON.stringify({
statusCode : FAKE_HTTP_STATUS_CODE_ERROR,
flash : {
notice : mockReqWithSession.flash('notice'),
error : mockReqWithSession.flash('error')
},
data : FAKE_ERROR_RESPONSE_TRIMMED
};
});

describe('makeJsonResponseError without session', () => {

let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseError(mockReqNoSession, mockRes, FAKE_ERROR_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_NO_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_NO_SESSION);
});

});
Expand All @@ -73,12 +77,12 @@ describe('makeJsonResponseError without flash', () => {
let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseError(mockReqNoFlash, mockRes, FAKE_ERROR_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_NO_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_NO_SESSION);
});

});
Expand All @@ -88,19 +92,19 @@ describe('makeJsonResponseError with session', () => {
let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseError(mockReqWithSession, mockRes, FAKE_ERROR_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_WITH_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(EXPECTED_RESPONSE_DATA_WITH_SESSION);
});

});

describe('mockRes.json', () => {
describe('mockRes.send', () => {
it('returns the value given', () => {
expect(mockRes.json('foo')).toBe('foo');
expect(mockRes.send('foo')).toBe('foo');
});
});

Expand Down
37 changes: 20 additions & 17 deletions spec/jsonResponseSuccessSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mockReqWithSession = {
session : {
flash : {}
},
flash : R.identity
flash : R.identity
};

const mockReqNoSession = {
Expand All @@ -20,7 +20,10 @@ const mockReqNoFlash = {

const mockRes = {
json : R.identity,
locals : {}
locals : {},
send : R.identity,
set : () => {
}
};

const FAKE_DATABASE_RESPONSE = {
Expand All @@ -29,34 +32,34 @@ const FAKE_DATABASE_RESPONSE = {

const HTTP_STATUS_CODE_SUCCESS = 200;

const FAKE_API_RESPONSE_NO_SESSION = {
const FAKE_API_RESPONSE_NO_SESSION = JSON.stringify({
statusCode : HTTP_STATUS_CODE_SUCCESS,
flash : {
notice : [],
error : []
},
data : FAKE_DATABASE_RESPONSE
},
FAKE_API_RESPONSE_WITH_SESSION = {
}),
FAKE_API_RESPONSE_WITH_SESSION = JSON.stringify({
statusCode : HTTP_STATUS_CODE_SUCCESS,
flash : {
notice : mockReqWithSession.flash('notice'),
error : mockReqWithSession.flash('error')
},
data : FAKE_DATABASE_RESPONSE
};
});

describe('makeJsonResponseSuccess without session', () => {

let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseSuccess(mockReqNoSession, mockRes, FAKE_DATABASE_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(FAKE_API_RESPONSE_NO_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(FAKE_API_RESPONSE_NO_SESSION);
});

});
Expand All @@ -66,12 +69,12 @@ describe('makeJsonResponseSuccess without flash', () => {
let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseSuccess(mockReqNoFlash, mockRes, FAKE_DATABASE_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(FAKE_API_RESPONSE_NO_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(FAKE_API_RESPONSE_NO_SESSION);
});

});
Expand All @@ -81,19 +84,19 @@ describe('makeJsonResponseSuccess with session', () => {
let response = {};

beforeEach(() => {
spyOn(mockRes, 'json');
spyOn(mockRes, 'send');
response = jsonResponseSuccess(mockReqWithSession, mockRes, FAKE_DATABASE_RESPONSE);
});

it('executes the mock res.json function', () => {
expect(mockRes.json).toHaveBeenCalledWith(FAKE_API_RESPONSE_WITH_SESSION);
it('executes the mock res.send function', () => {
expect(mockRes.send).toHaveBeenCalledWith(FAKE_API_RESPONSE_WITH_SESSION);
});

});

describe('mockRes.json', () => {
describe('mockRes.send', () => {
it('returns the value given', () => {
expect(mockRes.json('foo')).toBe('foo');
expect(mockRes.send('foo')).toBe('foo');
});
});

Expand Down
Loading