From e9bdbcf96bb5e679d21a40d14976bd6e1390ecf0 Mon Sep 17 00:00:00 2001 From: silvio-cardoso <79902733+silvio-cardoso@users.noreply.github.com> Date: Fri, 21 May 2021 09:38:42 +0200 Subject: [PATCH] Flexibility for the user remove/include meta info When user informs his meta object we don't include additional information on it. If meta isn't informed, we include requestID In case it isn't production env, timer and path are included --- src/index.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 6132de4..1d2763a 100644 --- a/src/index.js +++ b/src/index.js @@ -43,12 +43,16 @@ function getCode(body) { module.exports = { 'application/vnd.api+json': function jsonapiFormatter(req, res, body) { - const response = { - meta: { - id: req.id(), - ...res.meta || {}, - }, - }; + const response = {} + if (res.meta !== undefined) { + response.meta = res.meta + } else { + response.meta = { id: req.id() } + if (!isProd) { + response.meta.timers = req.timers; + response.meta.path = req.path(); + } + } if (res.links !== undefined) { response.links = { ...res.links }; @@ -58,11 +62,6 @@ module.exports = { response.included = [...res.included]; } - if (!isProd) { - response.meta.timers = req.timers; - response.meta.path = req.path(); - } - if (body instanceof Error) { // check for error message res.statusCode = getCode(body);