lengoo-api-response-formatter
Advanced tools
Comparing version
@@ -67,2 +67,3 @@ const _ = require('lodash'); | ||
this._validations = []; | ||
this._pagination; | ||
this._mongoError = {}; | ||
@@ -84,6 +85,2 @@ } | ||
set status(input) { | ||
if (Number.isInteger(input)) { | ||
throw new TypeError('"status" must to be an integer, ' + (typeof input) + ' given.'); | ||
} | ||
this._status = input | ||
@@ -268,2 +265,26 @@ } | ||
set pagination(input) { | ||
let defaults = { | ||
records: { | ||
limit: 0, | ||
total: 0 | ||
}, | ||
pages: { | ||
current: 0, | ||
total: 0, | ||
}, | ||
links: { | ||
previous: false, | ||
current: false, | ||
next: false, | ||
} | ||
}; | ||
this._pagination = _.pickBy(input, function (value, key) { | ||
if (_.has(defaults, key)) { | ||
return true; | ||
} | ||
}); | ||
} | ||
/** | ||
@@ -275,7 +296,13 @@ * Get the formatted response | ||
get() { | ||
return { | ||
let response = { | ||
data: this._data, | ||
error: this._error, | ||
validations: this._validations | ||
}; | ||
if (this._pagination) { | ||
response.pagination = this._pagination; | ||
} | ||
return response; | ||
} | ||
@@ -282,0 +309,0 @@ } |
{ | ||
"name": "lengoo-api-response-formatter", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "A lib to keep your responses formatted and consistent through your project", | ||
@@ -19,3 +19,3 @@ "main": "./lib/Response.js", | ||
], | ||
"author": "Rodrigo Duarte <rodrigo.duarte@lengoo.de> (https://www.lengoo.de)", | ||
"author": "Rodrigo Duarte <rodrigo.terminus@gmail.com> (https://www.lengoo.de)", | ||
"license": "MIT", | ||
@@ -22,0 +22,0 @@ "bugs": { |
17469
3.26%271
9.72%