Angular: Transform a request result in a observable

BrunoNoriller

I started doing my project in a module by module basis, so each one had to access the user.

Now that I'm polishing it, I'm having multiple API requests for the same thing (user info).

Much I found on the internet is deprecated and doesn't work anymore (mostly RxJs stuff).

What I have: the callback from the API with the user info. What I want: something shareable globally in the project (I only need one request for the API for the user info)

I'm trying right now to turn an observable into another:

@Injectable({
  providedIn: 'root',
})
export class LoginInfoService {
  constructor(private http: HttpClient) {}

  private userURL = `${environment.API}/loginUser`;

  private user$: Observable<LoggedUser>;
  private request = false;

  public retrieveUser(): Observable<LoggedUser> {

    if (!this.request&& !this.user$) {
      console.log("call request");
      this.user$ = this.getUserJson();
    }

    return this.user$;
  }

  private getUserJson() {
    console.log('actual request');
    this.request = true;
    return this.http.get<LoggedUser>(`${this.userURL}?key='123'`);
  }
}

I'm using for testing "json-server" and while I get only one 'actual request' printed, the server shows multiple requests there. I'm using the latest stable version of Angular.

Yasser Nascimento

You can use shareReplay, the http request will be executed only once then the result will be cached for any new subscription. Also, you can remove the request variable.

@Injectable({
  providedIn: 'root',
})
export class LoginInfoService {
  constructor(private http: HttpClient) {}

  private userURL = `${environment.API}/loginUser`;

  private user$: Observable<LoggedUser>;

  public retrieveUser(): Observable<LoggedUser> {
    return this.user$.pipe(shareReplay(1));
  }

  private getUserJson() {
    return this.http.get<LoggedUser>(`${this.userURL}?key='123'`);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to transform Observable result in piping process

Angular return result of Subscription as Observable

HTTP Transform request in Angular 2

delay observable to return value until i get result from my HTTP request in angular 5

What does "Result" means in a .map operator of an Observable obtained from an http.get request in nativescript/angular

Transform pipe in Angular 6 with Observable or Promise

Angular 2 second request to Observable

Transform data from one observable using another observable in angular rxjs

Angular - rxjs - Transform Observable array to another Observable array

Angular 2 cache observable http result data

Angular 2: Displaying result of Observable in model

Angular5 HttpInterceptor depending on the result of an Observable

Angular: How to use result of `.subscribe()` in consequent Observable

Angular add Observable result inside Map

Angular - How to use the result of an observable, inside another observable?

Angular: Observable executes request multiple times

Observable request inside Angular pipe for HTML filtering

Ionic 3 Angular 5 How fetch data and transform it to Observable in ngOnInit

Make Http Request Observation and return anm Observable with the result

RxJava get result of previous observable in chained network request

Angular9/rxjs6.5: How to Transform an Array of Observable Object Array to an Observable Object Property Array?

Transform a hot observable to a cold observable

Transform Observable<String[]> to Observable<DataType[]>

Transform to Observable only if not already Observable

Angular 7 adding data to result of http request

How to get result data in Angular promise request?

Angular how to display the result of a post request in a component

In Angular 7, how do I extract the result from an Observable?

Angular 5+ cannot save result form observable response