# ts-mock-cli

> mock json data by typescript

Latest version **0.0.3** (published 2022-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-mock-cli
pnpm add ts-mock-cli
yarn add ts-mock-cli
bun add ts-mock-cli
```

Provides the command `tsMock`.

## 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.0.3 |
| Published | 2022-11-04 |
| First published | 2022-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | yjh15167143670@163.com |
| Maintainers | yangjunhua |
| Keywords | mock, json |

## Links

- npm: https://www.npmjs.com/package/ts-mock-cli
- Homepage: https://github.com/yjh30/ts-mock-cli
- npm.io page: https://npm.io/package/ts-mock-cli

## Dependencies (3)

- [commander](https://npm.io/package/commander.md) ^9.4.1
- [json-schema-faker](https://npm.io/package/json-schema-faker.md) 0.5.0-rcv.46
- [typescript-json-schema](https://npm.io/package/typescript-json-schema.md) ^0.54.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.3 (latest) — 2022-11-04
- 0.0.2 — 2022-11-04
- 0.0.1 — 2022-11-03

## README

# ts-mock-cli
> 前端经常在后端接口联调之前需要进行mock数据，对于一个复杂的api接口数据，我们一般会写ts类型数据定义，`ts-mock-cli`可以通过我们写的ts文件，同时配置类型名称生成我们想要的精准mock数据

## 1. 安装 & 使用

```shell
pnpm add -D ts-mock-cli
```

```muse
Usage: tsMock [options]

Options:
  -v, --version                   当前版本
  -c, --config <configFilePath>   配置文件 { dist, list: Array<{file, typeNameList}> }>
  -h, --help                      显示命令帮助
```

`tsMock --config example/confifg.js`


## 2. 配置文件参考
```js
const path = require('path')

const resolve = (dir) => path.resolve(__dirname, dir)

module.exports = {
  // 文件输出目录（绝对路径）
  dist: resolve('../example/schema-json'),

  // 配置列表
  list: [
    {
      // 文件路径（绝对路径）
      file: resolve('../example/definations/index.ts'),

      // file文件中定义的类型名称列表
      typeNameList: ['IUser', 'IUserList', 'IPagingResponseData']
    }
  ]
}
```

## 3. 生成的文件目录
```
├── example
  ├── schema-json
    ├── IUser.json
    ├── IUserList.json
    ├── IPagingResponseData.json
```

## 4、ts类型定义
> [更多用法](https://github.com/YousefED/typescript-json-schema/blob/master/api.md)
```ts
export interface IUser {
  /**
   * 字符串：长度限制
   * @minLength 2
   * @maxLength 10
   */
  name: string

  /**
   * 中文字符串：长度限制；[去我而他与哦怕]{2,10}这种正则实现不起作用
   * @pattern (?:去|我|而|他|与|哦|怕|是|对|方|过|后|就|哭|了){2,10}
   */
  chineseName: string

  /**
   * 字符串：正则匹配手机号码
   * @pattern ^1\d{10}$
   */
  mobile: string

  /**
   * 字符串：其他内置格式
   * 内置格式还有 日期date，时间time，日期时间date-time，主机名hostname，IP地址ipv4，资源标识符uri，正则regex
   * @format email
   */
  email: string

  /** 数字：设置值的范围
   * @minimum 10
   * @maximum 100
   */
  height: number

  /**
   * 数字：设置值的范围，10的倍数，模拟时间戳Date.now()
   * @minimum 1667394203287
   * @maximum 1668431201403
   * @multipleOf 10
   */
  timestamp: number

  /**
   * 简单数组
   */
  numArray: Array<number>

  /**
   * 元组
   */
  tupleArray: [
    string,
    number,
    boolean
  ]

  /**
   * otherInfo 对象成员必须有一个是固定，否则生成的成员名及值可能都是随机
   */
  otherInfo: {
    /**
     * 字符串：正则匹配身份证号码
     * @pattern ^\d{15}$|^\d{17}(\d|x|X)$
     */
    idCard: string
    hobby?: '音乐' | '电影' | '篮球' | '足球' | '羽毛球'
    /**
     * @minimum 2
     * @maximum 100
     */
    familyMembers?: number
  },
}

/**
 * 每页请求数
 * @minItems 20
 * @maxItems 20
 */
export type IUserList = IUser[]

/**
 * 分页响应数据
 */
export interface IPagingResponseData {
  records: IUserList,
  /**
   * 总条数
   * @minimum 100
   * @maximum 100
   */
  total: number
}
```

PS：如果你的ts文件中使用到了全局声明文件中的类型，可以使用[三斜线指令](https://www.tslang.cn/docs/handbook/triple-slash-directives.html)，这里有一个好处如果tsMock失败，说明ts全局声明文件存在问题，可以帮助我们发现问题

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