1.0.11 • Published 3 years ago

ts-mock-generator v1.0.11

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

English | 简体中文

✨ Features

  • 📦 Mock generation tool out of the box
  • 😛 Define the return value through the interface Interface automatically generates mock data
  • 📄 Support generating mock files for secondary editing
  • 🔥 Support file change monitoring (generated mock files, interface and request files)

📦 Installation

yarn add -D ts-mock-generator
npm install ts-mock-generator --save-dev

🔨 Example

import { MockDataResolver } from 'ts-mock-generator';

const resolver = new MockDataResolver({
  basePath: path.join(process.cwd(), 'src', 'apis'),
  configPath: path.join(process.cwd(), 'tsconfig.json'),
  mockDir: path.join(process.cwd(), 'src', 'mock'),
  includes: [/^.*Service/],
});

// Obtain mock data
resolver.getOrGenerateData();

resolver.watchMockFile(() => {
  // When mock.json changes, callback will be triggered
});

resolver.watchRequestFile(() => {
  // When interface and service change, callback will be triggered
});

Note: If you need to use a plug-in to generate mock data, please meet the following two requirements:

  • GET or POST accepts a generic return value, the first parameter of the method is url
  • GET or POST calls a general request method, the second parameter of the general request method is the general backend return body type

Such as:

class Request {
  get<T>(url: string, opts?: Record<string, any>) {
    return this.fetch<T>({
      method: 'get',
      url: `${url}?_t=${Date.now()}`,
      ...opts,
    });
  }

  post<T>(url: string, opts?: Record<string, any>) {
    return this.fetch<T>({
      method: 'post',
      url,
      ...opts,
    });
  }

  fetch<T, R = ResponseData<T>>(opts: Record<string, any>) {
    return new Promise<T>((resolve) => {
      resolve({} as T);
    });
  }
}

export default new Request();

⚙️ Configuration items

-basePath: The root directory to be parsed, please provide an absolute path

-configPath: The directory where the tsconfig.json file is located, used for parsing by the compiler

-mockDir: The location of mock file generation. When set, mock.json and structure.json will be automatically generated

-includes: The class name rules to be parsed, you can set a regular expression or an array of regular expressions

-excludes and includes are mutually exclusive, meaning type rules not to be parsed, generally not used

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago