To install this library, run:
$ npm install ng2-awesome-http --save
import { NgModule } from '@angular/core';
import { AwesomeHttpModule, AwesomeHttpService } from 'ng2-awesome-http';
@NgModule({
imports: [
AwesomeHttpModule
]
})
export class AppModule {}
Use as Angular http module
public getUser(userCode): Observable<any> {
return this.awesomeHttpService.get('http://foo/api/users/' + userCode)
.map(res => res.json());
}
You have the possibility to configure each request
public getUser(userCode): Observable<any> {
return this.awesomeHttpService.get('users/' + userCode, null, {baseUrl: 'http://foo/api', useCache: true, forceUpdate: true})
.map(res => res.json());
}
You can set global options for all request
import { NgModule } from '@angular/core';
import { AwesomeHttpModule, AwesomeHttpService } from 'ng2-awesome-http';
@NgModule({
imports: [
AwesomeHttpModule
]
})
export class AppModule {
constructor(private _awesomeHttpService: AwesomeHttpService) {
this._awesomeHttpService.setConfig({baseUrl: 'http://foo/api', useCache: true});
}
}
public getUser(userCode): Observable<any> {
return this.awesomeHttpService.get('users/' + userCode)
.map(res => res.json());
}
- baseUrl : Base url to use on each request. default : ''
- useCache : enable/disable cache for GET request. default : false
- ttl: ttl for cache. default : undefined
- forceUpdate: when cache is enable, force to update it.
You can add an interceptor on request
this._awesomeHttpService.addRequestInterceptor({
beforeRequest(): void {
//Put here your code
...
}
});
You can add an interceptor on response error
this._awesomeHttpService.addResponseErrorInterceptor({
afterResponse(response: Response): void {
//Put here your code
...
}
});
You can add an interceptor on response success
this._awesomeHttpService.addResponseSuccessInterceptor({
afterResponse(response: Response): void {
//Put here your code
...
}
});
You can set an header to be added on each request
this._awesomeHttpService.addGlobalHeader('Authorization', 'token jkfsdf82fsdhfsdf8');
To generate all *.js
, *.js.map
and *.d.ts
files:
$ npm run build
To lint all *.ts
files:
$ npm run lint
MIT © Christophe Sailly