# icinfo-tool-api

> 解析后端的微服务api文档，自动生成所需要的接口函数（TS格式的）

Latest version **1.0.2** (published 2021-10-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install icinfo-tool-api
pnpm add icinfo-tool-api
yarn add icinfo-tool-api
bun add icinfo-tool-api
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-10-20 |
| First published | 2021-10-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 72.4 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| Author | majinhui |
| Maintainers | damon04 |

## Links

- npm: https://www.npmjs.com/package/icinfo-tool-api
- Repository: git@gitlab.icinfo.co:fed/icinfo-cli
- npm.io page: https://npm.io/package/icinfo-tool-api

## Dependencies (3)

- [axios](https://npm.io/package/axios.md) ^0.23.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0

## Recent versions

- 1.0.2 (latest) — 2021-10-20

## README

# api自动化生成代码工具

解析后端的微服务api文档，自动生成所需要的接口函数（TS格式的）

## 引入的命令
```sh
npm i -D git+ssh://git@gitlab.icinfo.co:fed/tool.api.git#v0.2.7
```

## 基本用法案例
在需要的项目中创建一个文件，比如 api.ts
```ts
import axios from 'axios';

import { cleanDir } from 'tutils/fileSystemSync';
import { generate } from 'api-server';

async function main() {
  try {
    cleanDir('src/api');
  } catch (e) {
    // eslint-disable-next-line no-console
  console.log(e);
  }

    [
        'http://api.fed.icinfo.co/xyxf/xyxf_zzgl/develop/api.json',
        'http://api.fed.icinfo.co/xyxf/origin-framework-cloud/member/api.json',
        'http://api.fed.icinfo.co/business-component/check-form/develop/api.json',
    ].forEach(async (url) => {
    const result = await axios(url);

    generate(result.data, {
      tsDir: 'src/api',
      merge: true,
      request: 'request',
      prepend: 'import {request, RequestOptions} from "@/untils/http"',
      optionsType: 'RequestOptions',
      dataDescription: '要提交给服务器的数据',
      optionsDescription: '附加选项',
    });

    // eslint-disable-next-line no-console
    console.log('已成功生成：', url.replace(/\/api\.json$/, ''));
  });
}

main();
```

同时在`package.json`配置scripts
```sh
"api": "ts-node ./scripts/api.ts"
```

执行方法为
```sh
npm run api
```

### 可配置的参数
| 名称               | 类型              | 是否必填 | 说明                                                  | 默认值   | 备注 |
|--------------------|-------------------|---------|-----------------------------------------------------|----------|------|
| getMockData        | function          | 否       | 自定义个别字段的模拟数据                              | -        | -    |
| mockCount          | function          | 否       | 自定义个别字段的模拟个数                              | -        | -    |
| mockDir            | string            | 否       | 模拟数据文件夹地址                                    | -        | -    |
| merge              | boolean           | 否       | 是否合并已有的模拟数据                                | -        | -    |
| mockDepth          | number            | 否       | 嵌套对象的最多模拟次数                                | -        | -    |
| mockPrefix         | string            | 否       | 模拟数据的前缀                                        | -        | -    |
| tsDir              | string            | 否       | 接口文件夹地址                                        | -        | -    |
| docFile            | string            | 否       | 生成的文档文件名                                      | -        | -    |
| prepend            | string            | 否       | 模块前追加的内容                                      | -        | -    |
| optionsType        | string            | 否       | 选项的类型                                            | -        | -    |
| dataDescription    | string            | 否       | 数据的描述                                            | -        | -    |
| optionsDescription | string            | 否       | 附加选项的描述                                        | -        | -    |
| request            | string            | 否       | 发起请求接口名                                        | -        | -    |
| newLine            | string            | 否       | 使用的换行符                                          | -        | -    |
| apiType            | 'jquery'\|'axios' | 否       | 参数配置的类型， 默认为jquery.ajax的方案，可选axios方案 | 'jquery' | -    |

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