Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as expressInterceptor from 'express-interceptor';
- export const responseStatusInterceptor = expressInterceptor((req, res) => {
- const hasErrorCode = (status) => status >= 400;
- const intercept = (body, send) => {
- const error = hasErrorCode(res.statusCode);
- try {
- body = JSON.parse(body);
- } catch (e) {
- const text = body;
- body = {
- message: text,
- };
- }
- send(JSON.stringify({
- ...body,
- error,
- }));
- };
- const isInterceptable = () => {
- return !(res.get('Content-Type') === 'application/pdf' || res.get('Content-Type') === 'application/octet-stream');
- };
- return {
- isInterceptable,
- intercept,
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement