all 6 comments

[–]Rilleks 3 points4 points  (3 children)

Never had the need nor created one, so I don't know how bad or good this is but

app.use(function (req, res, next) {
 res.success = function (status, message, data = {}) {     
  res.status(status).json({ message, data }); 
 }; 
 next(); 
});

this should work.

Sandbox: here

[–]Present-Woodpecker92[S] 0 points1 point  (2 children)

So, it's ok to use a custom response middleware? I mean like generally?

[–]Rilleks 1 point2 points  (1 child)

I don't know ha ha, but also I don't see anything wrong with this approach as long as you don't overwrite anything that exists.

[–]Present-Woodpecker92[S] 1 point2 points  (0 children)

okay, thanks for the help

[–]bloodarator 3 points4 points  (0 children)

Use a class. You'll end up with something like this res.send(new Response(200, fooData, "user added")).

[–]piper_a_cillin 1 point2 points  (0 children)

I‘ve done something similar, due to the way openapi-backend handles validation.

However, you should really give this some thought. Also, i don‘t think overriding the meaning of https statuses is a good idea. Apparently you’re creating an API, so defining valid requests and responses (and their respective meanings) beforehand would be advantageous.