x-resources
The plugin for axios provides services for making web requests via resources and handle responses.
Browser Support
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
Installation
You can install it via yarn or NPM.
$ yarn add x-resources
$ npm install x-resources
x-resources goal
In short, we want clean your code from a mess like this:
axios
To something way more readable and easy to maintain like this:
userResource
What do you gain by using x-resources?
- easy to maintain resources;
- creating resources through actions;
- more code expressivity;
- write less code;
- let your resource take care of HTTP methods;
- let your resource take care of url data injections and query params;
- Your development team won't have any headaches anymore replacing URLs in the system because the backend dude/chick decided to change it;
- Your development team won't have the issue with sending the wrong URL to production;
- Your development team won't have to type
axios[http_methos_goes_here]
every single time you want to call an API; - Your development team now you have resources! All you care about any HTTP request you made is in a file (read "How can I use it?"), meaning is so easy to maintain you might forget how your life was before using
x-resources
;
OK! How can I use it?
Let us show you everything x-resources
can do.
Start by creating a resources file. For our example, we created a folder called resources
and created a user.js
file in it. In the resources/user.js
we create our resource like:
const baseURL = 'https://api.github.com' const actions = find: method: 'GET' url: '/users' findOne: method: 'GET' url: '/users/:id' filter: method: 'GET' url: '/users/filterBy=:filters' create: method: 'POST' url: '/users' append: method: 'POST' url: '/users/:id' update: method: 'PUT' url: '/users/:id' remove: method: 'DELETE' url: '/users/:id' baseURL actions
OK! How can you use the resources/user.js
we've just created? Simple:
// HTTP method: GET// url: https://api.github.com/usersuserResources // HTTP method: GET// url: https://api.github.com/users/1userResources // HTTP method: GET// url: https://api.github.com/users/filterBy=nameuserResources // HTTP method: POST// url: https://api.github.com/users// payload: { name: 'john', age: 20 }userResources // HTTP method: POST// url: https://api.github.com/users/1// payload: { name: 'john', age: 20 }userResources // HTTP method: PUT// url: https://api.github.com/users/filterBy=nameuserResources // HTTP method: DELETE// url: https://api.github.com/users/1userResources // HTTP method: DELETE// HTTP header: Content-Type:application/pdf// url: https://api.github.com/users/1userResources
You're done! Any of those resources will always return a Promise from the Promise API.
Recomendations
- We strongly recommend that you use
x-resources
on current or future projects :D; - We recommend that you always have a
baseURL
in your resource file (like the one we createdresources/user.js
), because you could change this URL based on your production, homolog, test or development environment. Doing so you could easily have a function returning abaseURL
for you with the exact URL you need to run your resources;
Milestones
- create a
baseURL
to handle any url based on your develop/test/homolog/production environments; - make
x-resources
agnostic from any HTTP client but supporting anyaxios
client user;