0

I created a custom $http service because I wanted something more similar to the AngularJS one.

$http(config: CustomHttpConfig = this._generateHttpConfig()): Observable<any> {
    if (!config.method) {
        config.method = this.httpMethods.get;
    }
    if (!config.url) {
        console.error("Error: [$http:badreq]", config);
        return new Observable(observer => {
            observer.error(this._ubiConstants.messages.genericErrorKey);
        });
    }
    switch (config.method) {
        case this.httpMethods.get:
            let params = new HttpParams();
            //Accepting 0, null, void 0 as empty params
            if (!!config.data) {
                if (this._ubiConstants.isStrictlyObject(config.data)) {
                    for (let key in config.data) {
                        params = params.append(key, config.data[key]);
                    }
                }
                else {
                    console.warn("Error: [CustomRest:badparams]");
                }
            }
            return this._http.get(config.url, {params: params});
        case this.httpMethods.post:
            return this._http.post(config.url, config.data);
        case this.httpMethods.del:
            return this._http.delete(config.url);
        case this.httpMethods.put:
            return this._http.put(config.url, config.data);

        default:
            console.error("Error: [CustomRest:badmethod]", this.$http.arguments);
            return new Observable(observer => {
                observer.error("Error: [CustomRest:badmethod]");
            });
    }
};

I recently discovered that you can override the type of the Observable returned by the http by doing

this._http.get<MyType>(url)

By doing my custom service I'm loosing this feature.

Is there a way (for example using generics) to override the "any" of my $http and pass its value to the _http ?

Thanks

1
  • For the ones who may read this in the future: I don't know why I was returning observables in case of error. I obviously removed them and returned void 0 instead Commented Apr 9, 2018 at 7:45

2 Answers 2

2

You can make your function generic as well, and pass on the generic parameter to the _http methods:

$http<T>(config: CustomHttpConfig = this._generateHttpConfig()): Observable<T> {
  if (!config.method) {
    config.method = this.httpMethods.get;
  }
  if (!config.url) {
    console.error("Error: [$http:badreq]", config);
    return new Observable<T>(observer => {
      observer.error(this._ubiConstants.messages.genericErrorKey);
    });
  }
  switch (config.method) {
    case this.httpMethods.get:
      let params = new HttpParams();
      //Accepting 0, null, void 0 as empty params
      if (!!config.data) {
        if (this._ubiConstants.isStrictlyObject(config.data)) {
          for (let key in config.data) {
            params = params.append(key, config.data[key]);
          }
        }
        else {
          console.warn("Error: [CustomRest:badparams]");
        }
      }
      return this._http.get<T>(config.url, { params: params });
    case this.httpMethods.post:
      return this._http.post<T>(config.url, config.data);
    case this.httpMethods.del:
      return this._http.delete<T>(config.url);
    case this.httpMethods.put:
      return this._http.put<T>(config.url, config.data);

    default:
      console.error("Error: [CustomRest:badmethod]", this.$http.arguments);
      return new Observable<T>(observer => {
        observer.error("Error: [CustomRest:badmethod]");
      });
  }
};
Sign up to request clarification or add additional context in comments.

4 Comments

ahahah you are late by 12 seconds. It's unbelievable ahahaha
anyway yours is more precise, since you casted also the errors. Thanks.
@TonySamperi Sorry, didn't see your answer :)
Don't worry. Thanks for your time: I'll select your answer anyway. I will be able in about 1 minute...
2

I think this does the job:

$http<T>(config: CustomHttpConfig = this._generateHttpConfig()): Observable<T> {
    if (!config.method) {
        config.method = this.httpMethods.get;
    }
    if (!config.url) {
        console.error("Error: [$http:badreq]", config);
        return new Observable(observer => {
            observer.error(this._ubiConstants.messages.genericErrorKey);
        });
    }
    switch (config.method) {
        case this.httpMethods.get:
            let params = new HttpParams();
            //Accepting 0, null, void 0 as empty params
            if (!!config.data) {
                if (this._ubiConstants.isStrictlyObject(config.data)) {
                    for (let key in config.data) {
                        params = params.append(key, config.data[key]);
                    }
                }
                else {
                    console.warn("Error: [CustomRest:badparams]");
                }
            }
            return this._http.get<T>(config.url, {params: params});
        case this.httpMethods.post:
            return this._http.post<T>(config.url, config.data);
        case this.httpMethods.del:
            return this._http.delete<T>(config.url);
        case this.httpMethods.put:
            return this._http.put<T>(config.url, config.data);

        default:
            console.error("Error: [CustomRest:badmethod]", this.$http.arguments);
            return new Observable(observer => {
                observer.error("Error: [CustomRest:badmethod]");
            });
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.