RequestProvider for Request, modify headers api calls

I’m not sure that this is best performance, but works for me. Task: how to add custom headers to every http request. My solution working on cookies. I think the similar solution we can get by creating custom HttpProvider.

The problem was that, Provider is starting once, then you have access only for static methods, but this is not good solution to create method services as static. So one of solution is like this.

Just call

this.requestHeaderService.add(k,v)
@NgModule({
    declarations: [
        StatesComponent,
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        routing,

    ],
    providers: [
        appRoutingProviders,
        {provide: RequestOptions, useClass: RequestHeaderService},
        RequestHeaderService,
        LanguageService,  
    ],
    bootstrap: [StatesComponent]
})
export class StatesModule {
}

 

 

@Injectable()
export class RequestHeaderService extends BaseRequestOptions {

    private static COOKIE_NAME = "RequestExtraHeader";


    merge(options?: RequestOptionsArgs): RequestOptions {

        let headers = options.headers || new Headers;
        let data: any = Cookie.get(RequestHeaderService.COOKIE_NAME);
        if (data != 'null' && data) {
            data = JSON.parse(data);
            if (typeof data == 'object') {
            }
            for (let key in data) {
                headers.append(key, data[key]);
            }
        }
        options.headers = headers;

        return super.merge(options);
    }

    add(k, v) {
        let _data = Cookie.get(RequestHeaderService.COOKIE_NAME);
        let data = {};
        if (_data && _data != 'null') {
            data = JSON.parse(_data);
        }
        data[k] = v;
        Cookie.set(RequestHeaderService.COOKIE_NAME, JSON.stringify(data), 10);
    }

    delete(k) {
        let _data = Cookie.get(RequestHeaderService.COOKIE_NAME);
        let data = {};
        if (_data && _data != 'null') {
            data = JSON.parse(_data);
        }
        if (k in data){
            delete data[k];
        }
        Cookie.set(RequestHeaderService.COOKIE_NAME, JSON.stringify(data), 10);
    }
}