Skip to content

Commit c408327

Browse files
authored
Merge pull request #52 from ClickPop/feature/api-details-returned
👽 Add api details to root response
2 parents 0cc7fed + d020512 commit c408327

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

helpers/apiDetails.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const pjson = require('../package.json');
2+
const apiDetails = {
3+
name: pjson.name,
4+
version: pjson.version,
5+
};
6+
const rootResponse = {
7+
data: 'Welcome to the Backtalk API',
8+
};
9+
10+
if (pjson.author) {
11+
apiDetails.author = pjson.author;
12+
}
13+
if (pjson.contributors) {
14+
apiDetails.contributors = pjson.contributors;
15+
}
16+
if (pjson.homepage) {
17+
apiDetails.homepage = pjson.homepage;
18+
}
19+
20+
rootResponse.api = apiDetails;
21+
22+
module.exports = {
23+
apiDetails: apiDetails,
24+
rootResponse: rootResponse,
25+
default: apiDetails,
26+
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const morgan = require('morgan');
66
const cookieParser = require('cookie-parser');
77
const restrictAccess = require('../middleware/restrictAccess');
88
const cors = require('cors');
9+
const { rootResponse } = require('../helpers/apiDetails');
910

1011
app.use(cookieParser(process.env.COOKIE_SECRET));
1112
app.use(express.json());
@@ -27,7 +28,7 @@ if (process.env.NODE_ENV !== 'production') {
2728

2829
// Default Route
2930
app.get('/', (req, res) => {
30-
return res.json({ data: 'Welcome to this survey app!' });
31+
return res.json(rootResponse);
3132
});
3233

3334
// API (v1) Routes Loader

tests/01-init.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Initial Endpoint', () => {
1313
it('should respond at api root', async (done) => {
1414
const res = await req.get('/').set('User-Agent', uaString);
1515
expect(res.status).toBe(200);
16-
expect(res.body.data).toBe('Welcome to this survey app!');
16+
expect(res.body.data).toBe('Welcome to the Backtalk API');
1717
done();
1818
});
1919
});

0 commit comments

Comments
 (0)