npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guillaumejasmin/axios-rest

v1.0.0

Published

Axios Rest - Tool for build REST resources with axios

Downloads

4

Readme

Axios REST

Build Status Coverage Status npm version jest

Build resources and actions for axios

Install

npm install axios-rest --save

What is a resource ?

A resource is a REST endpoint, with a list of actions.

Globals resource actions

Globals resource actions are actions available for all resources.

The most common resource actions are CRUD methods: fetch, create, update and delete.

Example of posts resource:

| method | URL | description | Axios Rest | | :---------- | :--------- | :------------- | ------------------------------------------------------- | | GET | /posts | fetch all post | api.posts().fetch() | | GET | /posts/1 | fetch one post | api.posts(1).fetch() | | POST | /posts | create post | api.posts().create({ data: { title: 'foo' } }) | | PATCH / PUT | /posts/1 | update post | api.posts().update({ { data: id: 1, title: 'bar' } }) | | DELETE | /posts/1 | delete post | api.posts(1).delete() |

Custom actions

A resource can also have custom actions only available for this resource:

| method | URL | description | | | :----- | :------------------- | :----------------------------- | -------------------------- | | POST | /comments/1/like | add a like to the comment 1 | api.comments(1).like() | | POST | /comments/1/unlike | remove a like to the comment 1 | api.comments(1).unlike() |

A resource can have sub resources

| method | URL | description | Axios Rest | | :----- | :------------------ | :-------------------------- | ----------------------------------------------------------- | | GET | /posts/1/comments | fetch all comment of post 1 | api.posts(1).comments().fetch() | | POST | /posts/1/comments | create a comment of post 1 | api.posts(1).comments().create({ data: { text: '...' } }) |

What is an action ?

An action is a single endpoint. The most common action is login

| method | URL | description | Axios Rest | | :----- | :-------- | :---------------------- | ----------------------------------------------------------- | | POST | /login | login to admin panel | api.login({ data: { username: '...', password: '...' } }) | | POST | /logout | logout from admin panel | api.logout() |

createAxiosRest(axiosInst, config)

createAxiosRest(axiosInst, config)

  • axiosInst - Axios Instance - required - create with axios.create() . See Axios documentation

  • config - object - required

Config

{
  idKey: 'id',
  resources: undefined,
  actions: undefined,
  globalResourceActions: undefined // Actions available for all resources
}

Config resources

{
  [resourceName]: {
    url: '',
    resources: undefined // sub resources
    actions: undefined
  }
}

Config actions

{
  [actionName]: {
    // axios request config
  }
}

// or if you need to get resource id
{
  [actionName]: (id, data) => ({
    // axios request config
  })
}

Examples

Basic config

import axios from 'axios'
import { createAxiosRest, CRUDActions } from 'axios-rest'

const axiosInst = axios.create({
  baseURL: 'http://api.website.com',
})

const config = {
  globalResourceActions: CRUDActions, // use can use predefined CRUD action or build yours
  resources: {
    posts: {
      url: '/posts',
      resources: {
        comments: {
          url: '/comments',
        },
      },
    },
    comments: {
      url: '/comments',
      actions: {
        like: id => ({
          url: `/${id}/like`,
          method: 'POST',
        }),
        unlike: id => ({
          url: `/${id}/unlike`,
          method: 'POST',
        }),
      },
    },
  },
  actions: {
    login: {
      url: '/login',
      method: 'POST',
    },
    logout: {
      url: '/logout',
      method: 'POST',
    },
  },
}

const api = createAxiosRest(axiosInst, config)

Basic usage

Resources

// GET /posts
api
  .posts()
  .fetch()
  .then(res => console.log(res.data))

// GET /posts/1
api.posts(1).fetch()

// POST /posts
api.posts().create({ data: { title: '...' } })

// PATCH /posts/1
api.posts().update({ data: { id: 1, title: '...' } })
// or
api.posts(1).update({ data: { title: '...' } })

// PUT /posts/1
api.posts().update({ data: { id: 1, title: '...' }, method: 'put' })

// DELETE /posts/1
api.posts(1).delete()

Actions

api.login({ data: { email: '...', password: '...' } }).then(res => {
  // success login
})

Sub resources and actions

// GET /posts/1/comments
api
  .posts(1)
  .comments()
  .fetch()

// POST /posts/1/comments
api
  .posts(1)
  .comments()
  .create({ data: { author: '...', text: 'Amazing article !' } })

// POST /comments/1/like
api.comments(1).like()

URL params

const config = {
  actions: {
    myAction: (id, data) => ({
      url: `custom-action/${data.postId}/${data.commentId}`,
    }),
  },
}

// ...

api.myAction({ data: { postId: '', commentId: '' } })
  • Note: id is unused here, because it's an action. Id can only be used with resource: api.posts(id).anotherAction()

Axios request config

You have 2 ways to set axios config for an action:

  • globally
  • during the action call (override global config with shallow merge)
const config = {
  actions: {
    myAction: {
      url: 'custom-action',
      method: 'GET',
      headers: {
        X_CUSTOM_HEADER: 'foo',
      },
      params: {
        page: 1,
      },
    },
  },
}

// ...

// GET /custom-action&page=1
// with header X_CUSTOM_HEADER
api.myAction()

// GET /custom-action&page=2&lang=en
// with header X_CUSTOM_HEADER
api.myAction({ params: { page: 2, lang: 'en' } })