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

axios-crud

v1.0.1

Published

Simple set of functions made for CRUD requests on REST API

Downloads

7

Readme

axios-crud provides a simple set of functions to do some CRUD requests to an API using axios.

Install

yarn add axios-crud or npm i -S axios-crud

Get started

  1. Supposing you have
class UserAPI {
  doARequestToTheAPI() {
    // do some stuff
  }
}

and you need to have some CRUD functions for a model called "users" on the backend side, this is how the code would look like:

class UserAPI extends CrudModel {
  constructor() {
    super('users');
    // Now you can access the CRUD methods in your class
  }

  doARequestToTheAPI() {
    // do some stuff
  }
}

The CrudModel constructor takes the model name as first paramater, and a config object as second parameter.

  1. Calling the createCrudModel which simply returns the set of CRUD functions. Takes the name of the model as first parameter and a config object as second parameter.

Config

You may want to set a global config to use in all of your CrudModel's.

This can be done using the config method, which takes an object as parameter and merges the default config with the one passed as parameter.

You can set the following properties in the config object:

  • adapter : defaults to axios. It is usefull if you want to use an abstraction of your networking library. This adapter must contain the same methods than axios has.
  • modelNameAsAttribute : add an attribute which name is the model name as a param for create and edit requests.
  • singularize : used if modelNameAsAttribute is true. Taking back our UserAPI example, our model is called "users". You may want to remove the last "s" in case of creation and edition (that's how it works with RoR). Setting this to true will then remove the last character from the model name when passing it as a key of the params object for the create / edit requests. Defaults to true.

API

  • getAll(...args)
  • get(id, ...args)
  • edit(id, modelData, additionalData, ...args)
  • create(modelData, additionalData, ...args)
  • delete(id, ...args)

For edit and create, modelData will be associated with the key [modelName] key, and additionalData will simply be merged with the params object

args are optional.

Simple example

import { createCrudModel, config } from 'axios-crud';

const Model = createCrudModel('users');

// Get all users
Model.getAll().then((result) => console.log(result));
// Get a user data with id 10
Model.get(10).then((result) => console.log(result));

Run the example folder:

yarn or npm i then yarn start or npm start