Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49083

Angular 10 HTTP

$
0
0

How to convert this code to @angular/common/http

angular 10

import {Injectable} from '@angular/core';
import {BrowserXhr, Request, ResponseOptions, XHRBackend, XHRConnection, XSRFStrategy, Response} from '@angular/http';
import 'rxjs/add/operator/map';
import "rxjs/add/operator/catch";
import "rxjs/add/observable/throw";
import {Observable} from "rxjs";
import {appContainer} from "../../app/app.container";
import {JwtClientProvider} from "../jwt-client/jwt-client";
import {RedirectorProvider} from "../redirector/redirector";
import {NetworkCheckProvider} from "../network-check/network-check";


@Injectable()
export class DefaultXHRBackendProvider extends XHRBackend {

    constructor(browserXHR: BrowserXhr,
                baseResponseOptions: ResponseOptions,
                xsrfStrategy: XSRFStrategy) {
        super(browserXHR, baseResponseOptions, xsrfStrategy);
    }


    createConnection(request: Request): XHRConnection {

        let xhrConnection = super.createConnection(request);
        xhrConnection.response = xhrConnection
            .response
            .map((response) => {
                this.tokenSetter(response);
                return response;
            })
            .catch(responseError => {
                let networkCheck: NetworkCheckProvider = appContainer().get(NetworkCheckProvider);
                networkCheck.showAlertInternetDisconneted();
                this.unauthenticated(responseError);
                return Observable.throw(responseError);
            });

        return xhrConnection;
    }

    tokenSetter(response: Response) {
        let jwtClient = appContainer().get(JwtClientProvider);
        if (response.headers.has('Authorization')) {
            let authorization = response.headers.get('Authorization');
            let token = authorization.replace('Bearer ', '');
            jwtClient.setToken(token);
        }
    }

    unauthenticated(responseError: Response) {
        let redirector = appContainer().get(RedirectorProvider);
        if (responseError.status === 401) {
            redirector.redirector();
        }
    }

}

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 49083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>