Skip to content

Commit

Permalink
finish main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Maples7 committed Jul 27, 2018
1 parent 5dbad81 commit 6fc73bf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib');
40 changes: 40 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Boom = require('boom');

module.exports = (
env = 'production',
errStatusCodePropertie = ['status', 'statusCode', 'code']
) => async (ctx, next) => {
try {
await next();
if (ctx.status === 404) {
throw Boom.notFound('API Not Found');
}
const response = ctx.body;
ctx.body = {
success: true,
data: response
};
} catch (err) {
if (!Boom.isBoom(err)) {
let statusCode = 500;
for (let property of errStatusCodePropertie) {
if (Number.isInteger(err[property])) {
statusCode = ~~err[property];
}
}
err = Boom.boomify(err, {
statusCode: statusCode
});
}

const payload = err.output.payload;
ctx.status = payload.statusCode;
ctx.body = {
success: false,
error: payload.error,
message:
(env !== 'production' ? err.message : payload.message) ||
payload.message
};
}
};
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
"bugs": {
"url": "https://github.com/Maples7/koa-final-response/issues"
},
"homepage": "https://github.com/Maples7/koa-final-response#readme"
"homepage": "https://github.com/Maples7/koa-final-response#readme",
"engines": {
"node": ">=7.6.0"
},
"dependencies": {
"boom": "^7.2.0"
}
}

0 comments on commit 6fc73bf

Please sign in to comment.