@0y0/reaxios

Easy-to-use HTTP client based on axios for the browser and NodeJS.

Usage no npm install needed!

<script type="module">
  import 0y0Reaxios from 'https://cdn.skypack.dev/@0y0/reaxios';
</script>

README

@0y0/reaxios ยท GitHub license npm Package 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