Skip to content

Instantly share code, notes, and snippets.

@paritosh64ce
Created November 15, 2017 06:30
Show Gist options
  • Save paritosh64ce/9e4fbd8de38853534b36227be4d003c6 to your computer and use it in GitHub Desktop.
Save paritosh64ce/9e4fbd8de38853534b36227be4d003c6 to your computer and use it in GitHub Desktop.
See how Observable.flatMap makes the world a better place to code! compare this with https://gist.github.com/paritosh64ce/917e70f43e7d160a27b00f36491b87c0
import { Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class myHTTPSvc {
constructor(private http: Http, private upSvc: MyUserProfileService) {}
get(url) : Observable<any> {
this.upSvc.getLoggedInUserDetails()
.flatMap(details => {
let headerConfig = this.getHeaderConfig(details);
return this.http.get(url, headerConfig)
.map(data => data.json());
});
}
private getHeaderConfig(loggedInUserDetails: any): RequestOptions {
let options = new RequestOptions();
options.headers = new Headers();
options.headers.append('license', loggedInUserDetails.licenseInfo);
// or any details you want to push in message header
return options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment