Skip to content

toomeefed/api-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published