Giter VIP home page Giter VIP logo

api-proxy's Introduction

api-proxy

node 前后端分离接口代理模块

Travis Coverage Status David npm (scoped) node (scoped) GitHub license

为 node 前后端分离项目设计的现代化接口代理模块,以 koa 中间件方式处理接口请求,可以自由的在各个阶段对数据加工或日志记录。

安装

$ yarn add @toomee/api-proxy # 推荐
# 或者
$ npm i -S @toomee/api-proxy

使用

准备一份接口配置:

// ./interfaces.js
module.exports = {
  title: 'simple',
  version: '1.0.0',
  hosts: {
    prod: 'http://localhost:3000/prod',
    dev: 'http://localhost:3000/dev',
  },
  host: 'dev',
  decompress: false,
  json: true,
  interfaces: [
    {
      name: '用户-列表',
      id: 'user.list',
      path: '/user/list',
    },
    {
      name: '用户-信息',
      id: 'user.info',
      path: '/user/info',
      host: 'prod',
    },
    {
      name: '用户-创建',
      id: 'user.create',
      method: 'post',
      path: '/user/create',
    },

    {
      name: 'Restful-用户信息',
      id: 'rest.user.info',
      path: '/user/:id',
    },
    {
      name: 'Restful-新增用户',
      id: 'rest.user.create',
      method: 'post',
      path: '/user',
    },
    {
      name: 'Restful-修改信息',
      id: 'rest.user.modify',
      method: 'put',
      path: '/user',
    },
    {
      name: 'Restful-删除用户',
      id: 'rest.user.del',
      method: 'delete',
      path: '/user',
    },
  ],
};

调用接口

const ModelProxy = require('@toomee/api-proxy');
const interfaces = require('./interfaces');

const model = new ModelProxy(interfaces);

const { body } = await model('user.list');
// 或
const { body } = await model('user.create', { body: { name: 'steve' } });
// 或 restful
const { body } = await model('rest.user.info', { params: { id: 1 } });

License

MIT

api-proxy's People

Contributors

52cik avatar

Stargazers

猫掌柜 avatar  avatar 许子禾 avatar

Watchers

James Cloos avatar yueluo avatar  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.