My angular4 useful tools
export const environment = {
production: false,
api: {
prefix: 'api',
version: 'v1',
url: 'example.com',
config: {
ssl: true
}
}
};An injectable config service for getting e.g API URL to HTTP requests. For variables from above the service returns URL like https://api.example.com/v1/
@Injectable()
export class ConfigService {
public getApiUrl( path: string ): string {
if ( path.substr(0, 1) == '/' )
path = path.replace('/', '');
return this.getApiEndPoint() + '/' + path;
}
}An injectable service which extends Http and adds API URL to each request.
// app.module.ts
export function eHttp(backend: XHRBackend, options: RequestOptions, configService: ConfigService) {
return new ExtHttp(backend, options, configService);
}
@NgModule({
...
providers: [
ConfigService,
{
provide: ExtHttp,
useFactory: eHttp,
deps: [XHRBackend, RequestOptions, ConfigService]
}
],
...
})// our component class
constructor(private extHttp: ExtHttp) {
let q = new URLSearchParams();
q.set('q', 'query');
this.extHttp.get('search', { search: search }).subscribe((response: Response) => {
// logic
});
}