axios-client

0.0.9 • Public • Published

AXIOS API CLIENT

Installing this package

Run below command to install the axios-client package to your current project

$ yarn add axios-client
Creating New file in your project apiConfig.js

After install the axios-client, you need to create a apiConfig file in your project under service folder All your API used in your project will be set in apiConfig

  1. import Endpoint function and Request method from 'axios-client'
  2. set your API's calling method and uri
How to set apiConfig
  1. allCountry, portList, getFormFields are your API name, you can name it whatever you want
  2. inside EndPoint() function, first parameter is the API's request method. And there are 4 type of method (Request.GET, Request.POST, Request.PUT, Request.DELETE). Second parameter is the API's endpoints(URIs), URI parameter will be in curly bracket {uriParam}
    import { Endpoint, Request } from 'axios-client';

        const apiConfig = {
        getList : Endpoint(Request.GET, "/api/all"),
        getWithParam : Endpoint(Request.GET, "/api/{param1}/{param2}"),
        postMethod: Endpoint(Request.POST, "/api/postapi"),
    }

export default apiConfig;
Sample calling API using axios-client in your store
  1. import your apiConfig file
  2. import the RestClient from 'axios-client'

RestClient.call() accept 3 parameters

  • first (request.method and API's URI) which you already set in apiConfig file.
  • second parameter are URI's params values(object format), request header
  • third parameter is your request body
    import apiConfig from './apiConfig';
    import { RestClient } from 'axios-client';
    
    let firstname = 'Daniel'
    let lastname = 'Cai'
    
    const FormFields = () =>{
        RestClient.call(apiConfig.getFormFields, {param1: firstname, param2: lastname}, null)
        .then(res => {
            console.log(res.data)
        })
    }
Using PROXY for development

add proxy in package.json

    "proxy": "http://www.your-api-url.com/",

Package Sidebar

Install

npm i axios-client

Weekly Downloads

6

Version

0.0.9

License

MIT

Unpacked Size

8.23 kB

Total Files

3

Last publish

Collaborators

  • danielcai