@0y0/reaxios 中文文档教程

发布于 3年前 浏览 39 项目主页 更新于 3年前

@0y0/reaxios · GitHub licensenpmPackage Status Test Status

@0y0/reaxios 是一个易于使用的 HTTP 客户端,基于 axios 用于浏览器和 NodeJS .

Installation

npm install @0y0/reaxios --save
import Reaxios from '@0y0/reaxios'

作为使用 npm 的替代方法,您可以使用 @0y0/reaxios 作为

<script src="https://unpkg.com/@0y0/reaxios/dist/index.min.js"></script>
<script>Reaxios.get('http://...')</script>

Usage

Send a request

Reaxios.get('/api')
Reaxios.post('/api')
Reaxios.put('/api')
Reaxios.delete('/api')

Set HTTP header

// headers: { foo: 'bar' }
Reaxios.get('/api').header('foo', 'bar')

// headers: { Authorization: 'bar' }
Reaxios.get('/api').auth('bar')

// headers: { Authorization: 'Bearer token' }
Reaxios.get('/api').bearer('token')

// headers: { foo1: 'bar1', Authorization: 'token' }
Reaxios.get('/api')
  .header('foo1', 'bar1')
  .header('foo2', undefined)
  .auth('token')

Set URL parameters

// url: /api/?foo=bar
Reaxios.get('/api').param('foo', 'bar')

// url: /api/?foo2=bar2
Reaxios.get('/api').param('foo1', undefined).param('foo2', 'bar2')

// url: /api/?foo1=bar1&foo2=bar2
Reaxios.get('/api').params({ foo1: 'bar1', foo2: 'bar2' })

Set request body

Reaxios.post('/api').body('content')

// Content-Type: application/json
Reaxios.post('/api').body({ foo: 'bar' })

// Content-Type: multipart/form-data
Reaxios.post('/api')
  .header('Content-Type', 'multipart/form-data')
  .body(new FormData())

// Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Reaxios.post('/api')
  .header('Content-Type', 'application/x-www-form-urlencoded')
  .body(new URLSearchParams())

Transform request and response

Reaxios.post('/api')
  .body('data')
  // send `{ data: 'data' }` to server
  .transformRequest(data => ({ data }))
  // receive `{ data: 'data' }` from server and resolve promise with `'data'`
  .transformResponse(({ data }) => data)

Cancel the request

const promsie = async () => await Reaxios.post('/api').body('content')
promise.cancel()

License

麻省理工学院

@0y0/reaxios · GitHub licensenpmPackage Status Test Status

@0y0/reaxios is a easy-to-use HTTP client based on axios for the browser and NodeJS.

Installation

npm install @0y0/reaxios --save
import Reaxios from '@0y0/reaxios'

As an alternative to using npm, you can use @0y0/reaxios as a <script> tag from a CDN.

<script src="https://unpkg.com/@0y0/reaxios/dist/index.min.js"></script>
<script>Reaxios.get('http://...')</script>

Usage

Send a request

Reaxios.get('/api')
Reaxios.post('/api')
Reaxios.put('/api')
Reaxios.delete('/api')

Set HTTP header

// headers: { foo: 'bar' }
Reaxios.get('/api').header('foo', 'bar')

// headers: { Authorization: 'bar' }
Reaxios.get('/api').auth('bar')

// headers: { Authorization: 'Bearer token' }
Reaxios.get('/api').bearer('token')

// headers: { foo1: 'bar1', Authorization: 'token' }
Reaxios.get('/api')
  .header('foo1', 'bar1')
  .header('foo2', undefined)
  .auth('token')

Set URL parameters

// url: /api/?foo=bar
Reaxios.get('/api').param('foo', 'bar')

// url: /api/?foo2=bar2
Reaxios.get('/api').param('foo1', undefined).param('foo2', 'bar2')

// url: /api/?foo1=bar1&foo2=bar2
Reaxios.get('/api').params({ foo1: 'bar1', foo2: 'bar2' })

Set request body

Reaxios.post('/api').body('content')

// Content-Type: application/json
Reaxios.post('/api').body({ foo: 'bar' })

// Content-Type: multipart/form-data
Reaxios.post('/api')
  .header('Content-Type', 'multipart/form-data')
  .body(new FormData())

// Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Reaxios.post('/api')
  .header('Content-Type', 'application/x-www-form-urlencoded')
  .body(new URLSearchParams())

Transform request and response

Reaxios.post('/api')
  .body('data')
  // send `{ data: 'data' }` to server
  .transformRequest(data => ({ data }))
  // receive `{ data: 'data' }` from server and resolve promise with `'data'`
  .transformResponse(({ data }) => data)

Cancel the request

const promsie = async () => await Reaxios.post('/api').body('content')
promise.cancel()

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文