npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@liangskyli/http-mock-gen

v4.0.4

Published

http mock gen for web mockServer

Downloads

41

Readme

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, --configFile | mock数据生成配置文件 配置参数见下面 | ./mock.config.ts |

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

  • 类型:IGenMockDataOpts | IGenMockDataOpts[]

IGenMockDataOpts 参数属性

| 属性 | 说明 | 类型 | 默认值 | |------------------------|----------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------| | mockDir | mock文件夹所在目录 | string | ./ | | mockPathPrefix | mock接口URL地址前缀 | string | undefined | | jsonSchemaFakerOptions | 生成mock 数据 faker配置参数 | 详情配置见 json-schema-faker options文档 | { alwaysFakeOptionals: true, fillProperties: false } | | mockDataReplace | 生成mock 数据处理函数,可以覆盖faker数据 | (this: any, key: string, value: any) => any | undefined | | 其它属性参数 | 其它属性参数依赖仓库@liangskyli/openapi-gen-ts | 详情配置见 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);
  },
};