Advertisement
Guest User

Untitled

a guest
May 28th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import * as expressInterceptor from 'express-interceptor';
  2.  
  3. export const responseStatusInterceptor = expressInterceptor((req, res) => {
  4. const hasErrorCode = (status) => status >= 400;
  5. const intercept = (body, send) => {
  6. const error = hasErrorCode(res.statusCode);
  7. try {
  8. body = JSON.parse(body);
  9. } catch (e) {
  10. const text = body;
  11. body = {
  12. message: text,
  13. };
  14. }
  15. send(JSON.stringify({
  16. ...body,
  17. error,
  18. }));
  19. };
  20.  
  21. const isInterceptable = () => {
  22. return !(res.get('Content-Type') === 'application/pdf' || res.get('Content-Type') === 'application/octet-stream');
  23. };
  24.  
  25. return {
  26. isInterceptable,
  27. intercept,
  28. };
  29. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement