14.0.0 • Published 3 years ago

rxxfetch v14.0.0

Weekly downloads
40
License
MIT
Repository
github
Last release
3 years ago

RxxFetch

Observable HTTP Fetch() wrapped by RxJS6, support browser and Node.js

GitHub tag License npm.io ci codecov Conventional Commits lerna

Features

  • Reactive Ajax programming
  • Request Cancelable via AbortController
  • Runs in browsers and Node.js (node-fetch polyfill though)
  • Restful API GET POST PUT DELETE via get() post() put() remove()
  • Retrieve and append cookies during 30x redirect on Node.js (via keepRedirectCookies:true)
  • Apis support Generics, eg. get<string>(url).subscribe(txt => console.info(txt.slice(1)))
  • Send file via FormData or Stream on Node.js

Browser support (v2.x)

Build Status

  • Should work fine without polyfills in every modern browser
  • IE11 needs polyfills whatwg-fetch, es6-shim, es7-shim
  • Edge14 has 1 failure on remove() with form data
  • Safari 11 (Mac OS X/iOS) may get failure on abortController.abort() with TypeError: Origin http://localhost:9876 is not allowed by Access-Control-Allow-Origin
  • Mobile Safari 10.0.0 (iOS 10.3.0) may get failure on abortController.abort() with TypeError: Type error
  • Mobile Safari 9.0.0 (iOS 9.3.0) may get failure on redirect with AssertionError: TypeError: Network request failed
  • Android 4.4 will get failure on parseResponseType() with TypeError: Object function ArrayBuffer() { [native code] } has no method 'isView'

Installing

npm install rxxfetch

Usage

GET JSON

import { get } from 'rxxfetch'

const url = 'https://httpbin.org/get'

get<HttpbinGetResponse>(url, args).subscribe(
  json => {
    console.log(json.url)
  },
  console.error,
)

/** GET Response Interface of httpbin.org */
export interface HttpbinGetResponse {
  args: any
  headers: {
    Accept: string
    Connection: string
    Host: string
    'User-Agent': string,
  }
  origin: string  // ip
  url: string
}

GET HTML

import { get, RxRequestInit } from 'rxxfetch'

const url = 'https://httpbin.org/get'
const args: RxRequestInit = {
  dataType: 'text'
}

get<string>(url, args).subscribe(
  txt => {
    console.log(txt.slice(0, 10))
  },
  console.error,
)

POST

JSON

import { post, RxRequestInit } from 'rxxfetch'

const url = 'https://httpbin.org/post'
const pdata = {
  cn: '测试',
  p1: Math.random(),
  p2: Math.random().toString(),
  p3: {
    foo: Math.random() + '',
  },
}
const args: RxRequestInit = {}
args.data = { ...pdata }

const res = await post<HttpbinPostResponse>(url, args).toPromise()
assert(res && res.url === url)

const json: typeof pdata = res.json
assert(json.cn === pdata.cn, `Should got "${pdata.cn}"`)
assert(json.p1 === pdata.p1, `Should got "${pdata.p1}"`)
assert(json.p2 === pdata.p2, `Should got "${pdata.p2}"`)
assert(json.p3.foo === pdata.p3.foo, `Should got "${pdata.p3.foo}"`)

FORM

import { post, RxRequestInit, ContentTypeList } from 'rxxfetch'

const url = 'https://httpbin.org/post'
const pdata = {
  p1: Math.random(),
  p2: Math.random().toString(),
}
const args: RxRequestInit = {
  // default value is ContentTypeList.json
  contentType: ContentTypeList.formUrlencoded,
  data: pdata
}

post<HttpbinPostResponse>(url, args).subscribe(
  res => {
    const form = res.form
    assert(form && form.p1 === pdata.p1.toString(), `Should got "${pdata.p1}"`)
    assert(form && form.p2 === pdata.p2, `Should got "${pdata.p2}"`)
  },
)

/** POST Response Interface of httpbin.org */
export interface HttpbinPostResponse extends HttpbinGetResponse {
  data: string
  files: any
  form: any
  json: any
}

PUT REMOVE goto TEST

On Node.js

  • Handle cookies when 302/303/307 redirect on Node.js, CODE

    const args = <RxRequestInit> {
      keepRedirectCookies: true,  // <---- intercept redirect -->
    }
  • Cancel an request via AbortController, details in CODE

  • POST FILE
    • via FormData, goto CODE
    • via Stream, goto CODE

Demos

Packages

PackageVersionDependenciesDevDependencies
rxxfetchrxxfetch-svgrxxfetch-d-svgrxxfetch-dd-svg
egg-fetchegg-svgegg-d-svgegg-dd-svg

License

MIT

Languages

14.0.0

3 years ago

13.0.0

3 years ago

12.6.4

3 years ago

12.6.2

3 years ago

12.6.3

3 years ago

12.6.1

3 years ago

12.6.0

3 years ago

12.5.1

3 years ago

12.2.1

3 years ago

12.4.0

3 years ago

12.4.1

3 years ago

12.2.0

3 years ago

12.3.0

3 years ago

12.5.0

3 years ago

12.0.0

3 years ago

11.4.2

3 years ago

11.4.3

3 years ago

11.4.0

3 years ago

11.4.1

3 years ago

12.1.0

3 years ago

11.3.1

3 years ago

11.5.0

3 years ago

11.3.0

3 years ago

10.0.0

3 years ago

10.0.1

3 years ago

8.4.0

3 years ago

8.6.0

3 years ago

8.3.0

3 years ago

8.1.2

3 years ago

8.1.1

3 years ago

11.2.2

3 years ago

11.2.3

3 years ago

9.1.1

3 years ago

9.1.0

3 years ago

11.2.0

3 years ago

11.2.1

3 years ago

11.0.0

3 years ago

8.5.0

3 years ago

8.7.0

3 years ago

8.5.1

3 years ago

8.2.1

3 years ago

8.2.0

3 years ago

9.2.0

3 years ago

9.0.0

3 years ago

11.1.0

3 years ago

5.5.0

3 years ago

5.3.0

3 years ago

5.1.0

3 years ago

7.1.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

8.1.0

3 years ago

4.0.0

3 years ago

5.4.0

3 years ago

5.2.0

3 years ago

5.0.0

3 years ago

6.0.0

3 years ago

7.0.0

3 years ago

7.2.0

3 years ago

3.1.0

3 years ago

8.0.0

3 years ago

4.1.0

3 years ago

2.2.0

4 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.1

5 years ago

1.12.0

5 years ago

1.11.4

5 years ago

1.11.3

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago