@0y0/reaxios 中文文档教程
@0y0/reaxios ·
@0y0/reaxios
是一个易于使用的 HTTP 客户端,基于 axios 用于浏览器和 NodeJS .
Installation
npm install @0y0/reaxios --save
import Reaxios from '@0y0/reaxios'
作为使用 npm 的替代方法,您可以使用 @0y0/reaxios
作为 标签,来自 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
@0y0/reaxios ·
@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()