Giter Site home page Giter Site logo

url-api-chain's Introduction

url-api-chain

URL API 链式调用, 基于es6。根据URL实现完全动态的调用。

现在很多网站都搞REST API,调用API的URL类似:

http://serverhost/user/userinfo/

http://serverhost/user/fans/list/

给每个URL对应的API都写一个方法是件很掏粪的事情.

基于es6 中的proxy 可以实现如下链式调用:

示例:GET

GET /user/info?id=5

对应JS请求:

api.user.info.get(
    {
        'id': 5
    },
    (res)=>{
        log(res.data)
    },
    (err)=>{

    }
)

示例:POST, 且路径含有参数

POST /user/5/login
发送数据

data = {
    'password': '123',
    'username': 'username'    
}

对应JS请求:

api.user.pathArg(5).login.post(
    {
        'password': '123',
        'username': 'username'
    },
    (res)=>{
        log(res)
    },
    (err)=>{
        log(err)
    }
)

vue demo运行

npm install --save axios
npm run dev

在vue 中使用示例


import apiChain from './utils/util'

var config = {
    header: '',
    baseUrl: 'http://120.79.78.236:3000/mock/39'
}

var api = new apiChain(config)

var log = console.log.bind(console)

export default {
    name: 'App',
    data(){
        return{

            getResult: 'sds',
            postResult: ''

        }
    },

    methods:{

        getUserInfo(){
            var that = this
            api.user.info.get(
                {
                    'id': 5
                },
                (res)=>{
                    log(res.data)
                    this.getResult = res
                },
                (err)=>{

                }
            )
        },

        login(){
            var that = this
            var user_id = 5
            api.user.pathArg(user_id).login.post(
                {
                    'password': '123',
                    'username': 'username'
                },
                (res)=>{
                    log(res)
                    that.postResult = res
                    log("***", that.postResult)
                },
                (err)=>{
                    log(err)
                }
            )
        }
    }
}

url-api-chain's People

Contributors

vincijy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.