diff --git a/src/app.js b/src/app.js index bf6f8d7..3d43f6a 100644 --- a/src/app.js +++ b/src/app.js @@ -14,6 +14,8 @@ const pino = require('pino-http')({ logger, }); +const { createErrorResponse } = require('./response'); + // Create an express app instance we can use to attach middleware and HTTP routes const app = express(); @@ -38,13 +40,14 @@ app.use('/', require('./routes')); // Add 404 middleware to handle any requests for resources that can't be found app.use((req, res) => { - res.status(404).json({ - status: 'error', - error: { - message: 'not found', - code: 404, - }, - }); + // res.status(404).json({ + // status: 'error', + // error: { + // message: 'not found', + // code: 404, + // }, + // }); + res.status(404).json(createErrorResponse(404, 'not found')); }); // Add error-handling middleware to deal with anything else @@ -60,13 +63,7 @@ app.use((err, req, res, next) => { logger.error({ err }, `Error processing request`); } - res.status(status).json({ - status: 'error', - error: { - message, - code: status, - }, - }); + res.status(status).json(createErrorResponse(status, message)); }); // Export our `app` so we can access it in server.js diff --git a/src/routes/api/get.js b/src/routes/api/get.js index b19a8dc..c81e8fa 100644 --- a/src/routes/api/get.js +++ b/src/routes/api/get.js @@ -1,13 +1,14 @@ // src/routes/api/get.js - +const { createSuccessResponse } = require('../../response'); /** * Get a list of fragments for the current user */ module.exports = (req, res) => { // TODO: this is just a placeholder. To get something working, return an empty array... - res.status(200).json({ - status: 'ok', - // TODO: change me - fragments: [], - }); + + res.status(200).json( + createSuccessResponse({ + fragments: [], + }) + ); }; diff --git a/src/routes/index.js b/src/routes/index.js index 729f385..92668d3 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -10,10 +10,11 @@ const { version, author } = require('../../package.json'); // Create a router that we can use to mount our API const router = express.Router(); +const { createSuccessResponse} = require('../response'); /** * Expose all of our API routes on /v1/* to include an API version. */ -router.use(`/v1`,authenticate(), require('./api')); +router.use(`/v1`, authenticate(), require('./api')); /** * Define a simple health check route. If the server is running @@ -23,13 +24,14 @@ router.get('/', (req, res) => { // Client's shouldn't cache this response (always request it fresh) res.setHeader('Cache-Control', 'no-cache'); // Send a 200 'OK' response - res.status(200).json({ - status: 'ok', - author, - // Use your own GitHub URL for this! - githubUrl: 'https://github.com/brokoli777/fragments', - version, - }); + + res.status(200).json( + createSuccessResponse({ + author, + githubUrl: 'https://github.com/brokoli777/fragments', + version, + }) + ); }); module.exports = router;