voliva / angular2-interceptors

79 stars 20 forks source link

How do I get to know about request data in InterceptedResponse? #32

Open abhishekdgeek opened 6 years ago

abhishekdgeek commented 6 years ago

I just have a application using this plugin and now have an application that needs me to work on InterceptedResponse with API request methods i.e. GET, PUT, POST, DELETE. Also I need to access request data.

Thanks.

smallg commented 6 years ago
import {Interceptor, InterceptedRequest, InterceptedResponse} from 'ng2-interceptors';
import {Events} from "ionic-angular";
import {Injectable} from "@angular/core";

@Injectable()
export class ServerURLInterceptor implements Interceptor {

    constructor(public events: Events) {
    }

    public interceptBefore(request: InterceptedRequest): InterceptedRequest {
        return request;
    }

    public interceptAfter(response: InterceptedResponse): InterceptedResponse {
        try {
            let res = response.response['_body'];
            let body = JSON.parse(res);
            if (body.exception === 'NoOperatorException') {
                this.events.publish('need-login');
            }
        } finally {
        }

        return response;

    }

}