Я хочу создать пользовательский оператор (caching
) для добавления новых параметров в `HttpRequest.
const caching = (opt: {maxCacheAge: number} = {maxCacheAge: 30}) => (source: Observable<any>) =>
new Observable(observer => {
/*
* I found the HttpRequest at this level!!!
*/
const httpRequest = source.source.source.source.value as HttpRequest<any>;
const req = httpRequest.clone({
setParams: {
maxCacheAge: opt.maxCacheAge.toString()
}
});
source.source.source.source.value = req;
/*
* Here the HttpRequest is changed correctly,
* but into HttpInterceptor there is the original request.
*
* Moreover, `source` is an internal implementation so is deprecated
*/
console.log('obs:', source);
return source.subscribe({
next(res) { observer.next(res); },
error(err) { observer.error(err); },
complete() { observer.complete(); }
});
});
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private rest: Rest) {
this.getCapital('rome').subscribe(res => {
console.log('res', res);
});
}
getCapital(name: string): Observable<any> {
return this.rest.get(`https://restcountries.eu/rest/v2/capital/${name}`).pipe(
caching()
);
}
}
Возможно ли изменить запрос с помощью rxjs?
Я использую Angular 7.2 и rxjs 6.3