/manta-style

Futuristic API mock server for frontend

Primary LanguageTypeScriptMIT LicenseMIT

Manta Style CircleCI Codecov GitHub Greenkeeper badge

🚀 Futuristic API Mock Server for Frontend

Manta Style generates API mock endpoints from TypeScript type definitions automatically.

Contents

Installation

CLI

npm install --save-dev @manta-style/cli

You could also install it globally, which adds a command line tool ms to your system.

Plugins

Manta Style needs plugins to support different file types and generate mock data.

The example of Quick Start below is using TypeScript. So first you might want to install TypeScript support to Manta Style.

npm install --save-dev @manta-style/plugin-builder-typescript

If you are new to Manta Style, please install the plugins below. We are going to use them in Quick Start.

npm install --save-dev @manta-style/plugin-mock-example @manta-style/plugin-mock-faker

You could check Plugins for the usages of official plugins. You could make our own plugins as well.

Quick Start

Create mock API configuration

You could use following configuration for test purpose. For more information about syntax, please check out Syntax.

interface User {
  /**
   * @faker {{internet.userName}}
   */
  userName: string;
  gender: 0 | 1 | 2;
  /**
   * @faker date.past
   */
  birthday: number;
  /**
   * @faker {{address.country}}
   */
  country: string;
  /**
   * @faker {{address.state}}
   */
  state: string;
  /**
   * @faker {{address.city}}
   */
  city: string;
}

type WithResponseSuccess<T> = {
  status: 'ok';
  data: T;
};

type WithResponseFailure = {
  status: 'error';
  /**
   * @example Bad Request
   */
  message: string;
};

type WithResponse<T> = WithResponseSuccess<T> | WithResponseFailure;

export type GET = {
  '/user': WithResponse<User>;
};

Launch Manta Style

ms -c ./config.ts

Manta Style launches a mock server at port 3000 by default. The above-stated example would generate following output in the terminal:

Manta Style launched at http://localhost:3000
┌────────┬────────────────────────────┬────────┬───────┐
│ Method │ Endpoint                   │ Mocked │ Proxy │
├────────┼────────────────────────────┼────────┼───────┤
│ GET    │ http://localhost:3000/user │ Y      │       │
└────────┴────────────────────────────┴────────┴───────┘
Press O to configure selective mocking
[FAKER MODE] Press S to take an instant snapshot

Access endpoints in your browser

To view the mock data of the example above-stated, just launch a browser (or curl, wget) and access http://localhost:3000/user. Manta Style understands your type definition and generates mock data that respects it.

As WithResponse<User> = WithResponseSuccess<User> | WithResponseFailure, Manta Style would randomly choose one of the types in the union type. Therefore, it could randomly generate mock data for any of following cases:

  1. WithResponseSuccess<User>:
{
  "status": "ok",
  "data": {
    "userName": "Zachariah.VonRueden20",
    "gender": 2,
    "birthday": 646869600,
    "country": "Holy See (Vatican City State)",
    "state": "Massachusetts",
    "city": "South Evietown"
  }
}
  1. WithResponseFailure:
{ "status": "error", "message": "Bad Request" }

Press S to enable snapshot mode for a constant output.

Press O to interactively disable or proxy a mocked endpoint.

Usage

$ ms --help

  Usage: ms [options]

  Options:

    -V, --version              output the version number
    -c --configFile <file>     the TypeScript config file to generate entry points
    -p --port <i> [3000]       To use a port different than 3000
    --proxyUrl <url>           To enable proxy for disabled endpoints
    --generateSnapshot <file>  To generate a API mock data snapshot (Not yet implemented.)
    --useSnapshot <file>       To launch a server with data snapshot
    -v --verbose               show debug information
    -h, --help                 output usage information

Plugins

Mock

Builder

Manta Style supports TypeScript only at the moment via plugin-builder-typescript. More language support is coming soon.

Contributing

Getting Started

yarn install
yarn run bootstrap
yarn run build

Acknowledgments

  • Zhongliang Wang for original idea, architecture design, initial implementation of runtime and transformers.
  • Tan Li Hau for the design and implementation of selective mocking, plugin system, and many official plugins.
  • Jennie Ji for implementation of live-reload feature.

License

Manta Style is MIT licensed