├── .gitignore ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "axios-helpers", 3 | "version": "1.3.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/MauricioHernanCabrera/axios-helpers.git" 12 | }, 13 | "keywords": [ 14 | "axios", 15 | "helpers", 16 | "for", 17 | "data" 18 | ], 19 | "author": "Mauricio Hernan Cabrera", 20 | "license": "ISC", 21 | "bugs": { 22 | "url": "https://github.com/MauricioHernanCabrera/axios-helpers/issues" 23 | }, 24 | "homepage": "https://github.com/MauricioHernanCabrera/axios-helpers#readme" 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Helpers 2 | 3 | ### API Data 4 | helpers for get, post, delete, patch and put http methods 5 | 6 | ```js 7 | // bad 8 | const exampleCreated = await axios.post('/examples', data) 9 | console.log(exampleCreated.data) 10 | 11 | // good 12 | const exampleCreated = await axios.$post('/examples', data) 13 | console.log(exampleCreated) 14 | ``` 15 | 16 | 17 | ### Set Token 18 | 19 | ```js 20 | // bad 21 | axios.defaults.headers.common['Authorization'] = token 22 | 23 | // good 24 | axios.setToken(token) 25 | ``` 26 | 27 | ### Set Bearer token 28 | ```js 29 | // bad 30 | axios.defaults.headers.common['Authorization'] = `Bearer ${token}` 31 | 32 | // good 33 | axios.setToken(token, 'Bearer') 34 | ``` 35 | 36 | ### Set Base URL 37 | ```js 38 | // bad 39 | axios.defaults.baseURL = 'https://example.com' 40 | 41 | // good 42 | axios.setBaseURL('https://example.com'); 43 | ``` 44 | 45 | 46 | 47 | ### Set header 48 | ```js 49 | // bad 50 | axios.defaults.headers.common['Content-Type'] = 'application/json' 51 | 52 | // good 53 | axios.setHeader('Content-Type', 'application/json') 54 | ``` 55 | 56 | 57 | 58 | ### Basic example 59 | ```js 60 | const axiosHelpers = require('axios-helpers'); 61 | const axios = axiosHelpers(require('axios')); 62 | axios.setBaseURL('https://example.com'); 63 | 64 | const createExample = async (data) => { 65 | const exampleCreated = await axios.$post('/examples', data) 66 | console.log(exampleCreated) 67 | } 68 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const axiosData = 2 | (axios, httpMethod) => 3 | async (...params) => { 4 | try { 5 | const response = await axios[httpMethod](...params); 6 | return Promise.resolve(response.data); 7 | } catch (error) { 8 | return Promise.reject(error.response.data); 9 | } 10 | }; 11 | 12 | const setHeader = (axios) => (property, data) => { 13 | axios.defaults.headers.common[property] = data; 14 | }; 15 | 16 | const setBaseURL = (axios) => (baseURL) => { 17 | axios.defaults.baseURL = baseURL; 18 | }; 19 | 20 | const setToken = 21 | (axios) => 22 | (token, type = null) => { 23 | switch (type) { 24 | case "Paymentez": { 25 | return (axios.defaults.headers.common["Auth-Token"] = token); 26 | } 27 | 28 | case "Bearer": { 29 | return (axios.defaults.headers.common["Authorization"] = `Bearer ${token}`); 30 | } 31 | 32 | case "Basic": { 33 | const { user, pass } = token; 34 | return (axios.defaults.headers.common["Authorization"] = `Basic ${Buffer.from( 35 | `${user}:${pass}` 36 | ).toString("base64")}`); 37 | } 38 | 39 | default: { 40 | return (axios.defaults.headers.common["Authorization"] = token); 41 | } 42 | } 43 | }; 44 | 45 | const axiosHelpers = (axios) => { 46 | const httpMethods = ["post", "get", "delete", "patch", "put"]; 47 | 48 | httpMethods.forEach((httpMethod) => { 49 | axios[`$${httpMethod}`] = axiosData(axios, httpMethod); 50 | }); 51 | 52 | axios.setHeader = setHeader(axios); 53 | axios.setBaseURL = setBaseURL(axios); 54 | axios.setToken = setToken(axios); 55 | 56 | return axios; 57 | }; 58 | 59 | module.exports = axiosHelpers; 60 | --------------------------------------------------------------------------------