# apim-tools

> APIM Tools

Latest version **0.5.3** (published 2018-09-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install apim-tools
pnpm add apim-tools
yarn add apim-tools
bun add apim-tools
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.3 |
| Published | 2018-09-28 |
| First published | 2017-07-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 18 |
| Unpacked size | 7.7 MB |
| Known vulnerabilities | 0 (+4 in 4 direct dependencies) |
| Install scripts | no |
| Author | wuhuiyao |
| Maintainers | wuhuiyao |
| Keywords | APIM, schema validator, mock |

## Links

- npm: https://www.npmjs.com/package/apim-tools
- npm.io page: https://npm.io/package/apim-tools

## Dependencies (18)

- [etpl](https://npm.io/package/etpl.md) ^3.2.0
- [glob](https://npm.io/package/glob.md) ^7.1.2
- [mime](https://npm.io/package/mime.md) ^1.4.0
- [send](https://npm.io/package/send.md) ^0.16.1
- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [chalk](https://npm.io/package/chalk.md) ^2.1.0
- [json5](https://npm.io/package/json5.md) ^0.5.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [rimraf](https://npm.io/package/rimraf.md) ^2.6.2
- [co-body](https://npm.io/package/co-body.md) ^5.1.1
- [request](https://npm.io/package/request.md) ^2.82.0
- [type-is](https://npm.io/package/type-is.md) ^1.6.15
- [apim-mock](https://npm.io/package/apim-mock.md) ^0.1.13
- [formidable](https://npm.io/package/formidable.md) ^1.1.1
- [apim-validator](https://npm.io/package/apim-validator.md) ^0.1.3
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^1.7.0
- [stream-buffers](https://npm.io/package/stream-buffers.md) ^3.0.1
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.5.3 (latest) — 2018-09-28
- 0.5.2 — 2018-09-03
- 0.5.1 — 2018-03-20
- 0.5.0 — 2018-03-06
- 0.4.0 — 2018-01-24
- 0.3.1 — 2017-12-05
- 0.3.0 — 2017-11-02
- 0.2.3 — 2017-10-12
- 0.2.2 — 2017-09-21
- 0.2.1 — 2017-09-19
- 0.2.0 — 2017-08-31
- 0.1.10 — 2017-08-28
- 0.1.9 — 2017-08-24
- 0.1.8 — 2017-08-16
- 0.1.7 — 2017-08-15
- … 7 more at https://npm.io/package/apim-tools/versions

## README

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>

---
_Source: https://npm.io/package/apim-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
