重新梳理@angular

ANGULAR

  • @angular/animations
  • @angular/bazel
  • @angular/benchpress
  • @angular/common
  • @angular/common/http
  • @angular/compiler
  • @angular/compiler-cli
  • @angular/core
  • @angular/elements
export {
  NgElement,
  NgElementConfig,
  NgElementConstructor,
  WithProperties,
  createCustomElement,
  NgElementStrategy,
  NgElementStrategyEvent,
  NgElementStrategyFactory
} from "@angular/elements";
复制代码
  • @angular/forms
  • @angular/http
  • @angular/language-service
  • @angular/platform-browser
  • @angular/platform-browser-dynamic
  • @angular/platform-server
  • @angular/platform-webworker
  • @angular/platform-webworker-dynamic
  • @angular/router
  • @angular/service-worker
  • @angular/upgrade
  • @angular/cli

拦截器 Token

  • HTTP_INTERCEPTORS

Http 事件类型

export enum HttpEventType {
  Sent,
  UploadProgress,
  ResponseHeader,
  DownloadProgress,
  Response,
  User
}
复制代码

接口

  • HttpParameterCodec

  • HttpDownloadProgressEvent

  • HttpInterceptor

  • HttpProgressEvent

  • HttpSentEvent

  • HttpUserEvent

HttpEvent

export type HttpEvent<T> =
  | HttpSentEvent
  | HttpHeaderResponse
  | HttpResponse<T>
  | HttpProgressEvent
  | HttpUserEvent<T>;
复制代码

模块 NgModule

  • HttpClientXsrfModule
  • HttpClientModule
  • HttpClientJsonpModule

  • HttpClient

  • NoopInterceptor

  • JsonpClientBackend > HttpBackend

  • JsonpInterceptor

  • HttpInterceptingHandler > HttpHandler

  • HttpXsrfInterceptor > HttpInterceptor

  • HttpXsrfCookieExtractor > HttpXsrfTokenExtractor

  • interface HttpInterceptor

  • class HttpInterceptorHandler implements HttpHandler

  • const HTTP_INTERCEPTORS

  • class NoopInterceptor implements HttpInterceptor

  • const JSONP_ERR_NO_CALLBACK

  • const JSONP_ERR_WRONG_METHOD

  • const JSONP_ERR_WRONG_RESPONSE_TYPE

  • abstract class JsonpCallbackContext

  • class JsonpClientBackend implements HttpBackend

  • class JsonpInterceptor

  • class HttpInterceptingHandler implements HttpHandler

  • function interceptingHandler

  • function jsonpCallbackContext(): Object

  • interface HttpParameterCodec

  • class HttpUrlEncodingCodec implements HttpParameterCodec

  • interface HttpParamsOptions

  • class HttpParams

  • class HttpRequest

  • enum HttpEventType

  • interface HttpProgressEvent

  • interface HttpDownloadProgressEvent

  • interface HttpUploadProgressEvent

  • interface HttpSentEvent

  • interface HttpUserEvent

  • interface HttpJsonParseError

  • type HttpEvent

  • abstract class HttpResponseBase

  • class HttpHeaderResponse extends HttpResponseBase

  • class HttpResponse extends HttpResponseBase

  • class HttpErrorResponse extends HttpResponseBase implements Error

  • HttpBackend
  • HttpClient
  • HttpHeaders
  • HttpInterceptorHandler
  • HttpErrorResponse
  • HttpUrlEncodingCodec

HttpClient 构造函数

constructor(private handler: HttpHandler) {}
复制代码

HttpClient 核心 request

request(
    first: string | HttpRequest<any>,
    url?: string,
    options: {
      body?: any;
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    // 创建http请求
    let req: HttpRequest<any>;
    if (first instanceof HttpRequest) {
      req = first as HttpRequest<any>;
    } else {
      let headers: HttpHeaders | undefined = undefined;
      if (options.headers instanceof HttpHeaders) {
        headers = options.headers;
      } else {
        headers = new HttpHeaders(options.headers);
      }
      let params: HttpParams | undefined = undefined;
      if (!!options.params) {
        if (options.params instanceof HttpParams) {
          params = options.params;
        } else {
          params = new HttpParams({
            fromObject: options.params
          } as HttpParamsOptions);
        }
      }
      req = new HttpRequest(
        first,
        url!,
        options.body !== undefined ? options.body : null,
        {
          headers,
          params,
          reportProgress: options.reportProgress,
          responseType: options.responseType || "json",
          withCredentials: options.withCredentials
        }
      );
    }
    const events$: Observable<HttpEvent<any>> = of(req).pipe(
      concatMap((req: HttpRequest<any>) => this.handler.handle(req))
    );
    if (first instanceof HttpRequest || options.observe === "events") {
      return events$;
    }
    const res$: Observable<HttpResponse<any>> = <Observable<
      HttpResponse<any>
    >>events$.pipe(
      filter((event: HttpEvent<any>) => event instanceof HttpResponse)
    );
    switch (options.observe || "body") {
      case "body":
        switch (req.responseType) {
          case "arraybuffer":
            return res$.pipe(
              map((res: HttpResponse<any>) => {
                if (res.body !== null && !(res.body instanceof ArrayBuffer)) {
                  throw new Error("Response is not an ArrayBuffer.");
                }
                return res.body;
              })
            );
          case "blob":
            return res$.pipe(
              map((res: HttpResponse<any>) => {
                if (res.body !== null && !(res.body instanceof Blob)) {
                  throw new Error("Response is not a Blob.");
                }
                return res.body;
              })
            );
          case "text":
            return res$.pipe(
              map((res: HttpResponse<any>) => {
                if (res.body !== null && typeof res.body !== "string") {
                  throw new Error("Response is not a string.");
                }
                return res.body;
              })
            );
          case "json":
          default:
            return res$.pipe(map((res: HttpResponse<any>) => res.body));
        }
      case "response":
        return res$;
      default:
        throw new Error(
          `Unreachable: unhandled observe type ${options.observe}}`
        );
    }
  }
复制代码

HttpClient.delete

delete(
    url: string,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("DELETE", url, options as any);
  }
复制代码

HttpClient.get

get(
    url: string,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("GET", url, options as any);
  }
复制代码

HttpClient.head

head(
    url: string,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("HEAD", url, options as any);
  }
复制代码

HttpClient.jsonp

jsonp<T>(url: string, callbackParam: string): Observable<T> {
    return this.request<any>("JSONP", url, {
      params: new HttpParams().append(callbackParam, "JSONP_CALLBACK"),
      observe: "body",
      responseType: "json"
    });
  }
复制代码

HttpClient.patch

patch(
    url: string,
    body: any | null,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("PATCH", url, addBody(options, body));
  }
复制代码

HttpClient.post

post(
    url: string,
    body: any | null,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("POST", url, addBody(options, body));
  }
复制代码

HttpClient.put

put(
    url: string,
    body: any | null,
    options: {
      headers?: HttpHeaders | { [header: string]: string | string[] };
      observe?: HttpObserve;
      params?: HttpParams | { [param: string]: string | string[] };
      reportProgress?: boolean;
      responseType?: "arraybuffer" | "blob" | "json" | "text";
      withCredentials?: boolean;
    } = {}
  ): Observable<any> {
    return this.request<any>("PUT", url, addBody(options, body));
  }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值