0.0.28 • Published 2 years ago

@freenit-framework/axios v0.0.28

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
2 years ago

Freenit Axios

Install package

yarn add @freenit-framework/axios

Initialize API object

For example in api.js

import React from 'react'
import { API } from '@freenit-framework/axios'

export const api = new API()

Use it in a component

import React from 'react'
import { api } from 'api'

class App extends React.Component {
  state = {
    users: [],
  }

  constructor(props) {
    super(props)
    this.fetch()
  }

  fetch = async () => {
    const response = await api.user.getList()
    if (response.ok) {
      this.setState({ users: response.data })
    } else {
      console.log('error in App', api.errors(response).message)
    }
  }

  render() {
    return (
      <div>
        <h1>Freenit User List</h1>
        {this.state.users.map(user => (
          <div key={user.id}>{user.email}</div>
        ))}
      </div>
    )
  }
}

Config

API accepts two parameters: prefix and config. Prefix is what axios calls baseURL and by default is /api/v0. If you want to further customize axios you can pass config object like so const api = new API('/api/v1', { withCredentials: false }). All axios config params are supported.

API also has 3 objects, one for auth, me and user. That means you can call

await api.auth.login('admin@example.com', 'Sekrit')
await api.me.patch({ email: 'something@example.com' })

to login and change your email.

API Reference

This is the full list of supported calls:

auth

  • login
  • logout

me

  • get
  • patch

user

  • get
  • getList
  • patch
  • post
  • delete

If you need to implement call to new endpoint

const doit = async () => {
  return await api.get('/some/endpoint')
}

What it returns is either data that came with the response or the error object. If you need access to underlaying axios object use api.api instead of api.

0.0.28

2 years ago

0.0.27

3 years ago

0.0.26

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago