打字稿:编写可转换函数

Typescript: write castable function

我创建了一个自定义 $http 服务,因为我想要更类似于 AngularJS 的服务。

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

我最近发现你可以覆盖 http 返回的 Observable 类型,方法是

this._http.get<MyType>(url)

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

有没有办法(例如使用泛型)覆盖我的 $http 的 "any" 并将其值传递给 _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(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]");
            });
    }
};

您也可以使您的函数通用,并将通用参数传递给 _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]");
      });
  }
};