@cmss/http-client-rxjs

httpModule是由Angular的httpClient模块打包的网络请求库。Angular的httpClient模块采用了typescript和Rx.js。使用httpModule要用函数式的方式,如果用上Rx.js丰富的操作符,可以写出十分简洁明了的代码。

Usage no npm install needed!

<script type="module">
  import cmssHttpClientRxjs from 'https://cdn.skypack.dev/@cmss/http-client-rxjs';
</script>

README

httpModule

httpModule是由Angular的httpClient模块打包的网络请求库。Angular的httpClient模块采用了typescript和Rx.js。使用httpModule要用函数式的方式,如果用上Rx.js丰富的操作符,可以写出十分简洁明了的代码。

基本用法

// http配置
const httpOptions = {
  headers: {
    'Content-Type': 'application/json'
  }
};

let http = new RestClient(httpOptions);

// 发起请求
http.get(url).subscribe(value => {
  ...
})

错误处理

import { throwError } from 'rxjs';
import { retry, catchError } from 'rxjs/operators';

const handleError = (error) => {
  ...
  // 返回一个 observable 用于 rxjs catch 处理,方便用户自定义 catch 处理方式。
  return throwError('Something bad happened; please try again later.');
}

http.get(url).pipe(
  retry(3),
  catchError(handleError)
).subscribe(value => {
  ...
})