打字稿:编写可转换函数

时间:2018-04-09 07:21:02

标签: typescript types casting angular5 typescript2.4

我创建了一个自定义的$ http服务,因为我想要一些与AngularJS更相似的东西。

&{integer}

我最近发现你可以通过执行

来覆盖http返回的Observable的类型
$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]");
            });
    }
};

通过我的自定义服务,我将失去此功能。

是否有办法(例如使用泛型)覆盖&#34;任何&#34;我的$ http并将其值传递给_http?

谢谢

2 个答案:

答案 0 :(得分:2)

我认为这可以胜任:

$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]");
            });
    }
};

答案 1 :(得分:2)

您也可以使您的函数通用,并将泛型参数传递给_http方法:

$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]");
      });
  }
};