0.0.1 • Published 6 years ago

babel-plugin-s2s-axios-api v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

babel-plugin-s2s-axios-api

generate axios api

Install

$ yarn add --dev babel-plugin-s2s-axios-api

Create axios-api template

You should create babel-plugin-s2s-axios-api template. \ In your node project, you create a folder named templates in the same direcotry at the package.json

mkdir templates

And create a axios-api.js

touch templates/axios-api.js

Write this code.

import axios from "./config"

Create a axios-api config file

You prepare shared configure file named config.js in src/api/. \ For example, write these codes.

import axios from 'axios'

const API_ROOT = process.env.API_ROOT || 'http://localhost:8080'

axios.defaults.timeout = 5000
axios.defaults.baseURL = API_ROOT
axios.defaults.headers.post['Content-Type'] = 'application/json'

export default axios

s2s.config.js

s2s-axios-api plugin watch the src/api/*.js files.

module.exports = {
  watch: './**/*.js',
  plugins: [
    {
      test: /src\/apis\/(?!.*index).*\.js/,
      plugin: ['s2s-redux-axios-api']
    },
  ],
  templates: [
    {
      test: /src\/api\/.*\.js/, input: 'axios-api.js'
    }
  ]
}

Start s2s

Start the s2s with yarn command

yarn run s2s

Usage

When create a axios api file

When you create a src/api/*.js, the below code is inserted automatically.

import axios from "./config"

In:

In the axios api file, type action name with camelcase such as getPokemonRequest and save it.

import axios from "./config"
getPokemonRequest

It will be expanded like this.

Out:

import axios from "./config"
export const getPokemonRequest = config => axios.get(``, config);

Comment

Well, this expand pattern can not handle various http request methods.

Test

This plugin has two test files. \ First is babel plugin main test file named test.js on root directory. \ Next is a test/index.js that will be transformed by the plugin.

Run this command.

npm run test

Test will run and you can see what happen.

If you modify the target javascript source code, please change the test/index.js.