Skip to content

Instantly share code, notes, and snippets.

@JCPurger
Last active November 24, 2020 14:23
Show Gist options
  • Save JCPurger/6ef06f9440ea577f62f953b94ad8266c to your computer and use it in GitHub Desktop.
Save JCPurger/6ef06f9440ea577f62f953b94ad8266c to your computer and use it in GitHub Desktop.
Normalização para resposta de api #nodejs #javascript
const apiResponse = (payload = {}) => {
const DataSymbol = Symbol('data');
const StatusSymbol = Symbol('status');
const ErrorsSymbol = Symbol('errors');
const MessageSymbol = Symbol('message');
class ApiResponse {
constructor({ data = {}, status = 1, errors = [], message = '' }) {
this.data = data;
this.status = status;
this.errors = errors;
this.message = message;
}
get data() {
return this[DataSymbol];
}
set data(data) {
if (typeof data === 'undefined')
throw new Error('Data must be defined');
this[DataSymbol] = data;
}
get status() {
return this[StatusSymbol];
}
set status(status) {
if (isNaN(status) || (status !== 0 && status !== 1))
throw new Error('Status must be a number, 1 is OK, 0 is BAD');
this[StatusSymbol] = status;
}
get errors() {
return this[ErrorsSymbol];
}
set errors(errors) {
if (!Array.isArray(errors))
throw new Error('Errors must be an array');
this[ErrorsSymbol] = errors;
}
get message() {
return this[MessageSymbol];
}
set message(message) {
if (typeof message !== 'string')
throw new Error('Message must be a string');
this[MessageSymbol] = message;
}
toJSON() {
return {
data: this.data,
status: this.status,
errors: this.errors.map(e => e.stack ? e.stack : e),
message: this.message,
}
}
}
return new ApiResponse(payload);
}
//Usage
const todos = [{ ... }, { ... }]; // an array of todos
router.get('/todos', function(req, res, next){
res.status(200);
res.json(apiResponse({
data: todos,
message: 'You have a lot todo!',
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment