4.0.0 • Published 2 months ago

@liangskyli/http-mock-gen v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

http mock 代码生成工具

  • 基于openapi v3 生成 ts数据类型和http mock 数据代码。
  • mock数据填充默认值,自定义mock数据支持按需配置,未配置使用默认值。
  • ts接口类型文件生成
  • 通用请求库接口调用文件生成

安装:

yarn add @liangskyli/http-mock-gen --dev

如果项目没有安装prettier,需要安装prettier(^2.0.0 || ^3.0.0)

yarn add prettier --dev

生成方式:

1、CLI 命令方式(推荐)

  • 默认配置文件在运行目录下mock.config.ts文件
yarn http-mock-gen
  • 配置文件别名mock.config2.ts
yarn http-mock-gen -c ./mock.config2.ts

命令参数

参数说明默认值
-c, --configFilemock数据生成配置文件 配置参数见下面./mock.config.ts

命令参数 configFile mock数据生成配置文件参数属性

  • 类型:IGenMockDataOpts | IGenMockDataOpts[]

IGenMockDataOpts 参数属性

属性说明类型默认值
mockDirmock文件夹所在目录string./
mockPathPrefixmock接口URL地址前缀stringundefined
jsonSchemaFakerOptions生成mock 数据 faker配置参数详情配置见 json-schema-faker options文档{ alwaysFakeOptionals: true, fillProperties: false }
mockDataReplace生成mock 数据处理函数,可以覆盖faker数据(this: any, key: string, value: any) => anyundefined
其它属性参数其它属性参数依赖仓库@liangskyli/openapi-gen-ts V3版本详情配置见 openapi-ts文档IGenTsDataOpts 参数属性
  • configFile mock数据生成配置文件示例
    • 配置文件支持使用defineConfig定义ts类型
import { defineConfig } from '@liangskyli/http-mock-gen';

export default defineConfig([
    {
        mockDir: './',
        openapiPath: './openapi/openapiv3-example.json',
        jsonSchemaFakerOptions: {
            minItems: 1,
            maxItems: 1,
        },
        mockDataReplace: (key, value) => {
            if (typeof value === 'string') {
                return key;
            }
            if (typeof value === 'number') {
                return 0;
            }
            if (typeof value === 'boolean') {
                return false;
            }
            return value;
        },
    }
]);
  • openapi v3 YAML or JSON 格式的文件示例openapi 需要自己根据业务逻辑生成。
  • openapi v3 method 支持任意接口类型,生成responses 200或default的第一个响应数据(优先200)
  • 如果你的http接口使用routing-controllers,可以使用@liangskyli/routing-controllers-openapi 工具生成openapi文件
  • openpai 生成数据类型和接口使用说明,详见使用说明
  • 生成mock 数据结构,最终使用interface-mock-data.ts文件
  • Mock 数据修改指引 文档
  • 接口API使用指引 文档

生成的interface-mock-data.ts 文件(不要手动修改这个文件),用于http mock 功能。

// This file is auto generated by @liangskyli/http-mock-gen, do not edit!
import type {
  ICustomData,
  PartialAll,
  Request,
  Response,
} from '@liangskyli/http-mock-gen';
import { getMockData } from '@liangskyli/http-mock-gen';
import CustomData from './custom-data';
import type { IApi } from './schema-api/interface-api';

export default {
  'GET /v1/building/get-list': (req: Request, res: Response) => {
    type IData = IApi['/v1/building/get-list']['get']['Response'];
    const data = (
            CustomData as ICustomData<
                    PartialAll<IData>,
                    '/v1/building/get-list',
                    'get'
            >
    )['/v1/building/get-list']?.get;
    const json = getMockData<IData>(
            {
              retCode: -80948936.74257948,
              data: {
                isFuLi: false,
                blockList: [
                  { buildingName: { description: '11楼盘名称' }, isBindErp: true },
                  { buildingName: { description: '11楼盘名称' }, isBindErp: false },
                ],
              },
              retMsg: 'officia voluptate in nulla',
            },
            req,
            data,
    );
    res.json(json);
  },
  'POST /v1/card/delete': (req: Request, res: Response) => {
    type IData = IApi['/v1/card/delete']['post']['Response'];
    const data = (
            CustomData as ICustomData<PartialAll<IData>, '/v1/card/delete', 'post'>
    )['/v1/card/delete']?.post;
    const json = getMockData<IData>(
            { retCode: -41217908.89226498, data: {}, retMsg: 'Duis nulla' },
            req,
            data,
    );
    res.json(json);
  },
};
4.0.0

2 months ago

4.0.0-beta.0

2 months ago

3.2.0-beta.0

5 months ago

3.0.0-beta.1

10 months ago

3.0.0-beta.0

10 months ago

3.1.0

5 months ago

3.0.1

7 months ago

3.0.0

10 months ago

2.0.0-beta.2

1 year ago

2.0.0-beta.5

12 months ago

2.1.0

11 months ago

2.0.0-beta.4

1 year ago

2.0.0

12 months ago

2.0.0-beta.3

1 year ago

1.6.0

1 year ago

2.0.0-beta.1

1 year ago

2.0.0-beta.0

1 year ago

1.5.0

1 year ago

1.4.0

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-beta.0

2 years ago

0.10.0

2 years ago

0.11.0-beta.0

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.10.0-beta.0

2 years ago

0.10.0-beta.1

2 years ago

0.9.1

2 years ago

0.9.1-beta.8

2 years ago

0.9.1-beta.5

2 years ago

0.9.1-beta.3

2 years ago

0.9.1-beta.2

2 years ago

0.9.1-beta.1

2 years ago

0.9.1-beta.0

2 years ago

0.9.0

2 years ago

0.9.0-beta.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.8.0-beta.0

2 years ago