Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apim-tools

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apim-tools

APIM Tools

  • 0.5.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by400%
Maintainers
1
Weekly downloads
 
Created
Source

apim-tools

APIM 平台客户端工具,提供 mock 中间件 和 mock 管理 web界面

如何使用?

安装

npm i apim-tools --save-dev

作为 express 中间件使用

function artProcessor(customOptions) {
    return function (mockData, options, context) {
        const tplOptions = {
            tplDir: customOptions.tplDir || options.tplDir || options.baseDir
        };

        return new Promise((resolve, reject) => {
            if (!isPlainObject(mockData)) {
                return resolve({_data: mockData});
            }

            let tpl = mockData._tpl;
            if (tpl) {
                let tplFile = pathUtil.join(tplOptions.tplDir, tpl);
                let tplData = context.normalizeMockData(mockData);
                context.logger.debug('render art template using data', tplData);

                let result = art(tplFile, tplData);
                return resolve({_data: result});
            }
            else {
                context.logger.error('to render tpl file missing');
                return reject('to render tpl file missing');
            }
        });
    };
}

const apimMw = require('apim-tools').express({
    // express server 使用的端口号
    port: 9090,
    // 设置存储的 mock 相关数据存储的根目录
    root: __dirname + '/mock',
    // 项目 schema token 具体到 apim 平台查看
    schemaToken: 'd79e5fa60c5ed6111b6a06a2f4fcbb6b',
    // 是否启动时候立刻自动同步
    startAutoSync: true,
    // 是否开启注入选项,默认会在页面注入 mock 管理的入口
    injector: true,
    // 自定义 mock,只有不存在接口定义 mock 才走该自定义 mock 规则
    // mock 规则定义同 https://github.com/wuhy/autoresponse
    mockRules: [
        {
            match: '/users/:id',
            method: ['get', 'patch']
        }
    ],
    // 自定义 mock 数据处理器或者选项
    processors: {
        // 使用 smarty 处理器,接口平台定义的页面模板类型选择 Smarty
        smarty: {
            baseDir: __dirname + '/mock'
        },
        // 使用 etpl 处理器,接口平台定义的页面模板类型选择 ETPL
        etpl: {
            tplDir: __dirname + '/templates',
            tplExtName: 'etpl',
            encoding: 'utf-8',
            engine: {
                commandOpen: '{{',
                commandClose: '}}',
                variableOpen: '{%',
                variableClose: '%}'
            },
            filters: { // 默认提供 json_encode 
                myFilter: function (source) {
                    return source;
                }
            }
        },
        // 自定义 art-template 处理器,接口平台定义的页面模板类型选择其它,
        // 默认的处理器名为模板的文件扩展名
        art: artProcessor({
            tplDir: __dirname + '/templates'
        })
    }
});
app.use(apimMw);

API

  • testAPI
const apim = require('apim-tools')({
    root: __dirname + '/mock'
});
let promise = apim.testAPI({
    path: '/api/getUserInfo?uid=22', // 请求路径
    method: 'get', // 请求方法
    body: {}, // body 数据,可选
    envId: 11, // 可选,默认使用配置的环境
});
let expressMw = apim.express;
// ...

访问 mock 管理界面

http://devServer:devPort/apimclient

Keywords

FAQs

Package last updated on 28 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc