Code Monkey home page Code Monkey logo

api-class's Introduction

API Class

faster way to generate API calls with axios

Installation

You can install the package using npm

npm install api-class

https://www.npmjs.com/package/api-class

Usage

Create an API instance by import the API class from api-class. As parameter pass the basic url of your api.

const myAPI = new API({ url: '/api' })

Endpoints

To add an endpoint, use the .createEntity method.

const myAPI = new API({ url: '/api' })
myAPI.createEntity({ name : 'posts' })
myApi.endpoints.posts.getAll()

Methods

All API's endpoints have basic CRUD operations

getAll(params, config)
getOne({ id }, config)
create(toCreate, config)
update(toUpdate, config)
delete({ id }, config)

Q&A

How to pass headers to axios?

const myAPI = new API({ url: '/api' })
myAPI.createEntity({ name : 'posts' })
myAPI.endpoints.posts({ id: 0 },  { headers: {....} } )

You can check it out my medium article for further informations

https://medium.com/@FrancescoZ/how-to-call-api-in-a-smart-way-2ca572c6fe86

api-class's People

Contributors

dependabot[bot] avatar francescosaveriozuppichini avatar mciszczon avatar omair115 avatar vrajeshkanna avatar yarkovaleksei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

api-class's Issues

Support PATCH verb

Would be nice if this nice little package allowed to use PATCH method on resources. Should be easy to do, I might make a PR later today.

Unable to implement this in tsx

Actually tried to implement API class in tsx file taken from that medium blog. it is giving type error. can you write the same for tsx. it would be a great help.

actually i edited it to make it compatible with tsx, but at time of running, it says, query is not defined when try to call getAll()

getAll method is not handling headers properly

Current code

endpoints.getAll = ({ query={}}, config={} ) => axios.get(resourceURL, Object.assign({ params: { query }, config }))

endpoints.getAll = ({ query={}}, config={} ) => axios.get(resourceURL, Object.assign({ params: { query }, config }))

here the merge is not happening properly, because of which headers are not sent.

The below code will fix the problem

Object.assign({ params: { query } }, config)

BTW all other methods are working fine

Uncaught TypeError: Cannot read property 'query' of undefined

Thanks for this amazing utility! I can't seem to make it work though. I followed the instructions. This is my code:

import API from 'api-class';

const myAPI = new API({ url: 'http://baerenmatte.test/api' });

myAPI.createEntity({ name: 'events' });
myAPI.endpoints.events.getAll()
  .then(({data}) => console.log(data));

And this is the error I get in the console:

API.js:68 Uncaught TypeError: Cannot read property 'query' of undefined
    at Object.endpoints.getAll (API.js:68)
    at Module.<anonymous> (app.js:102)
    at Module../site/resources/js/app.js (app.js:30155)
    at __webpack_require__ (bootstrap:19)
    at Object.0 (app.js:31559)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83

What am I doing wrong?

Hyphens

What if the resourceUrl path has a hyphen?

API.createEntity({ name: 'data-systems' });

Add a local cache?

I was thinking that it may be useful to add a local cache in order to store some requests. Maybe having a parameter to specify the cache size.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.