troyanskiy / ngx-resource

Resource (REST) Client for Angular 2
http://troyanskiy.github.io/ngx-resource/
200 stars 46 forks source link

Howto retry request #179

Open bluewalk opened 3 years ago

bluewalk commented 3 years ago

First of all, great work, saves a bunch of work on my side 👍

What I'm unable to figure out is how to retry a request. For example I have the following, a class that extends Resource which overrides

After refreshing the bearer token (which returns a boolean) I would like to re-execute the original request so the user doesn't have to click the button twice.

How would I go and achieve that? Is there a way to send the original request back to this.requestHandler? Or is there some other way?

Below the handleErrorResponse method

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }

    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }
troyanskiy commented 3 years ago

Hi @bluewalk

Thank you for warm words :) It's a log time I did not develop with the library since I switched to Flutter.

But I think you can try to call return this.$restAction(options) in that case (not sure).

  $handleErrorResponse(options: IResourceActionInner, resp: IResourceResponse): any {
    this.loader.complete();

    switch (resp.status) {
      case 401:
        if (this._authservice.refresh()) {
          console.log('gotta retry request, but how?');
          return this.$restAction(options); // hope it will help you
        }
        break;
      case 403:
        this._authservice.logout();
        break;
    }

    if (options.returnData && this.$_canSetInternalData(options)) {
      options.returnData.$resolved = true;
    }
    if (options.actionAttributes.onError) {
      options.actionAttributes.onError(resp);
    }
  }

Let me know if that will help you.

bluewalk commented 3 years ago

I was thinking of something like that as well, unfortunately it doesn't work: Error: Uncaught (in promise): [object Undefined]

The methods are IResourceMethodPromise's