Skip to content
Axios endpoints helps you to create a more concise endpoint mapping with axios.
TypeScript JavaScript
Branch: master
Clone or download
dependabot-preview and renancaraujo Bump rollup-plugin-typescript2 from 0.24.3 to 0.25.1 (#45)
Bumps [rollup-plugin-typescript2](https://github.com/ezolenko/rollup-plugin-typescript2) from 0.24.3 to 0.25.1.
- [Release notes](https://github.com/ezolenko/rollup-plugin-typescript2/releases)
- [Commits](ezolenko/rollup-plugin-typescript2@0.24.3...0.25.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Latest commit 79cb5a9 Nov 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist 0.2.3 Sep 23, 2019
src remove json type Jun 19, 2019
.eslintignore
.eslintrc ts types May 28, 2019
.gitignore add ts May 28, 2019
.travis.yml attempting to setup travis Jun 15, 2018
CHANGELOG.md infra improvements Jun 16, 2018
LICENSE
README.md Update README.md Sep 5, 2018
index.js index Oct 30, 2018
jest.config.js gcam fix tests May 28, 2019
package-lock.json Bump rollup-plugin-typescript2 from 0.24.3 to 0.25.1 (#45) Nov 5, 2019
package.json Bump rollup-plugin-typescript2 from 0.24.3 to 0.25.1 (#45) Nov 5, 2019
rollup.config.js ts types May 28, 2019
tsconfig.json ts types May 28, 2019

README.md

Axios endpoints

Build Status npm

Axios endpoints helps you to create a more concise endpoint mapping with a simple but flexible api. It is as wrapper over axios.

Getting started

Before anything, install axios-endpoints

npm install --save axios #axios is a peer dependency
npm install --save axios-endpoints

Usage with Factory (recommended)

For a more complete usage of axios settings, you may use the Factory.

import axios from "axios";
import EndpointFactory from "axios-endpoints";

const axiosInstance = axios.create({
    baseURL: "https://localhost:8080/api"
});

const Endpoint = EndpointFactory(axiosInstance);

const cityEndpoint = new Endpoint("/city"); // GET https://localhost:8080/api/city
const postEndpoint = new Endpoint(({id = ""}) => "/post/" + id);

// Header changing example
function setAuthorization(MYTOKEN){
  axiosInstance.defaults.headers.common["Authorization"] = MYTOKEN;
}



function getCityList(callback) {
   cityEndpoint.get().then(response=>{
      callback(response.data);
   }).catch(e=>{
      console.log("That didnt go well");
   });
}

// do you like async stuff? so take this async example
async function getCityList(){
   try{
      const { data } = await cityEndpoint.get();
      return data;
   } catch (e) {
      console.log("That didnt go well");
   }
}

Usage without factory

The fastest way to use axios-endpoints. ideal when you dont want to set any axios configs:

// endpoints.js
import { Endpoint } from "axios-endpoints";

export const userEndpoint = new Endpoint("https://localhost:8080/api/user");


// anotherfile.js
import { userEndpoint } from "./endpoints";

async function getUserList(){
   try{
      const { data } = await userEndpoint.get();
      return data;
   } catch (e) {
      console.log("That didnt go well");
   }
}

HTTP methods

You can user either .get .post .put and .delete as well:

const cityEndpoint = new Endpoint("/city");

const { data } = await cityEndpoint.get(); // GET https://localhost:8080/api/city
const { data } = await cityEndpoint.get({
    params: {country:"brazil"}
}); // GET https://localhost:8080/api/city?country=brazil


const { data } = await cityEndpoint.post({
   name: "Hortolandia", 
   country: "Brazil", 
   hazardLevel: 10000
}, {
  params: { someParam: "icecream" }
}); 
/*
curl --request POST \  
  --url https://localhost:8080/api/city?someParam=icecream \
  --header 'Content-Type: application/json' \
  --data '{
   "name": "Hortolandia", 
   "country": "Brazil", 
   "hazardLevel": 10000
  }'
*/

uriParams

Not always your endpoint will be represented by a fixed string. For that, uri params exists.

const postEndpoint = new Endpoint(({postId = ""}) => "/post/" + postId)

const { data } = await postEndpoint.get({
   uriParams: {
      postId: 1
   }
}); // GET https://localhost:8080/api/post/1

For more information about parameters and returned values, check the API section.

API

EndpointFactory

import axios from "axios";
import EndpointFactory from "axios-endpoints"

const axiosInstance = axios.create(config);
const Enpoint = EndpointFactory(axiosInstance);

Type: function

Parameters
axiosInstance Axios instance

axiosInstance is generated via axios.create function. For more information, check axios documentation.

Return: Endpoint

Endpoint

import axios from "axios";
import EndpointFactory from "axios-endpoints"

const axiosInstance = axios.create(config);
const Enpoint = EndpointFactory(axiosInstance);

Type: class

Constructor
Constructor Parameters Type
endpointIdentifier String or Function any => String
Instance methods
endpoint#get(options)
endpoint#post(payload, options)
endpoint#put(payload, options)
endpoint#patch(payload, options)
endpoint#delete(options)
Parameters Type
options The same object as the Request Config with the extra property uriParams.
You may use params and uriParams more often.
payload The object that will be carried as json payload of the request

Contributing

If you got so far reading this README, you are maybe thinking about contributing. Pull requests are welcome.

You can’t perform that action at this time.