angularionic2angular2-services

extending http class for custom usage ionic2/Angular2 causing error


I'm building a app which uses jwt tokens. There are still a few routes that don't need them but most of the calls need a token. So I wanted to extend the http class and add my custom headers. I still want to use the original http class for the normal calls. I read online (and on stackoverflow) about this. But for some reason I get the following error:

EXCEPTION: Error in :0:0 caused by: No provider for ConnectionBackend!

my app module looks like this:

    @NgModule({
    declarations: [
        MyApp,
        DashboardPage,
    ],
    imports: [
        IonicModule.forRoot(MyApp)
    ],
    bootstrap: [IonicApp],
    entryComponents: [
        MyApp,
        DashboardPage,
    ],
    providers: [
        {
            provide: [Http,SecureHttpService],
            deps: [XHRBackend, RequestOptions],
            useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => {
                return new SecureHttpService(backend, defaultOptions);
            },
            useClass: SecureHttpService
        },
        {
            provide: ErrorHandler,
            useClass: IonicErrorHandler,
        },
        SecureHttpService
    ]
})
export class AppModule {}

the service extend looks like this:

import { Injectable } from '@angular/core';
import { Http, ConnectionBackend, Headers, RequestOptions, Response, RequestOptionsArgs} from '@angular/http';

@Injectable()
export class SecureHttpService extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
        super(backend, defaultOptions);
    }
}

and I want to use it in another service like this:

    constructor (private http: Http, private secureHttp: SecureHttpService) {}

I have also tried to use the provide like this (without http):

provide: SecureHttpService,

but everything I try results in the same error. I'm not understanding this error and why it's happening.


Solution

  • after some debugging a saw that i was adding the SecureHttpService 2 times as a provider. so the error came because the second time i provide "SecureHttpService" the dependencies etc are not there.... my mistake. so just remove it, and it will work