New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@guillaumejasmin/axios-rest

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@guillaumejasmin/axios-rest

Axios Rest - Tool for build REST resources with axios

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

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:

methodURLdescriptionAxios Rest
GET/postsfetch all postapi.posts().fetch()
GET/posts/1fetch one postapi.posts(1).fetch()
POST/postscreate postapi.posts().create({ data: { title: 'foo' } })
PATCH / PUT/posts/1update postapi.posts().update({ { data: id: 1, title: 'bar' } })
DELETE/posts/1delete postapi.posts(1).delete()

Custom actions

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

methodURLdescription
POST/comments/1/likeadd a like to the comment 1api.comments(1).like()
POST/comments/1/unlikeremove a like to the comment 1api.comments(1).unlike()

A resource can have sub resources

methodURLdescriptionAxios Rest
GET/posts/1/commentsfetch all comment of post 1api.posts(1).comments().fetch()
POST/posts/1/commentscreate a comment of post 1api.posts(1).comments().create({ data: { text: '...' } })

What is an action ?

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

methodURLdescriptionAxios Rest
POST/loginlogin to admin panelapi.login({ data: { username: '...', password: '...' } })
POST/logoutlogout from admin panelapi.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' } })

FAQs

Package last updated on 07 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc