UNPKG

4.83 kBMarkdownView Raw
1# Manta Style ![CircleCI](https://img.shields.io/circleci/project/github/Cryrivers/manta-style.svg?style=flat-square) ![Codecov](https://img.shields.io/codecov/c/github/Cryrivers/manta-style.svg?style=flat-square) ![GitHub](https://img.shields.io/github/license/Cryrivers/manta-style.svg?style=flat-square)
2
3> 🚀 Futuristic API Mock Server for Frontend
4
5[Manta Style](https://github.com/Cryrivers/manta-style/issues/1) generates API mock endpoints from TypeScript type definitions automatically.
6
7Contents
8- [Installation](#installation)
9- [Quick Start](#quick-start)
10- [Usage](#usage)
11- [Contributing](#contributing)
12- [Acknowledgments](#acknowledgments)
13- [License](#license)
14
15## Installation
16
17### Yarn
18
19```sh
20yarn add -D @manta-style/cli
21```
22
23### npm
24
25```sh
26npm install --save-dev @manta-style/cli
27```
28
29You could also install it globally, which adds a command line tool `ms` to your system.
30
31## Quick Start
32
33### Create mock API configuration
34
35You could use following configuration for test purpose. For more information about syntax, please check out [Syntax](./documentation/syntax.md).
36
37```ts
38interface User {
39 /**
40 * @example {{internet.userName}}
41 */
42 userName: string;
43 gender: 0 | 1 | 2;
44 /**
45 * @timestamp past
46 */
47 birthday: number;
48 /**
49 * @example {{address.country}}
50 */
51 country: string;
52 /**
53 * @example {{address.state}}
54 */
55 state: string;
56 /**
57 * @example {{address.city}}
58 */
59 city: string;
60}
61
62type WithResponseSuccess<T> = {
63 status: 'ok';
64 data: T;
65};
66
67type WithResponseFailure = {
68 status: 'error';
69 /**
70 * @example Bad Request
71 */
72 message: string;
73};
74
75type WithResponse<T> = WithResponseSuccess<T> | WithResponseFailure;
76
77export type GET = {
78 '/user': WithResponse<User>;
79};
80```
81
82### Launch Manta Style
83
84```sh
85ms -c ./config.ts
86```
87
88Manta Style launches a mock server at port 3000 by default. The above-stated example would generate following output in the terminal:
89
90```
91Manta Style launched at http://localhost:3000
92┌────────┬────────────────────────────┬────────┬───────┐
93│ Method │ Endpoint │ Mocked │ Proxy │
94├────────┼────────────────────────────┼────────┼───────┤
95│ GET │ http://localhost:3000/user │ Y │ │
96└────────┴────────────────────────────┴────────┴───────┘
97Press O to configure selective mocking
98[FAKER MODE] Press S to take an instant snapshot
99```
100
101### Access endpoints in your browser
102
103To 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.
104
105As `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:
106
1071. `WithResponseSuccess<User>`:
108
109```json
110{
111 "status": "ok",
112 "data": {
113 "userName": "Zachariah.VonRueden20",
114 "gender": 2,
115 "birthday": 646869600,
116 "country": "Holy See (Vatican City State)",
117 "state": "Massachusetts",
118 "city": "South Evietown"
119 }
120}
121```
122
1232. `WithResponseFailure`:
124
125```json
126{ "status": "error", "message": "Bad Request" }
127```
128
129Press <kbd>S</kbd> to enable snapshot mode for a constant output.
130
131Press <kbd>O</kbd> to interactively disable or proxy a mocked endpoint.
132
133## Usage
134```
135$ ms --help
136
137 Usage: ms [options]
138
139 Options:
140
141 -V, --version output the version number
142 -c --configFile <file> the TypeScript config file to generate entry points
143 -p --port <i> [3000] To use a port different than 3000
144 --proxyUrl <url> To enable proxy for disabled endpoints
145 --generateSnapshot <file> To generate a API mock data snapshot (Not yet implemented.)
146 --useSnapshot <file> To launch a server with data snapshot
147 -v --verbose show debug information
148 -h, --help output usage information
149```
150
151## Contributing
152
153### Getting Started
154
155```sh
156yarn install
157yarn run bootstrap
158yarn run build
159```
160
161## Acknowledgments
162
163- [Zhongliang Wang](https://github.com/Cryrivers) for original idea, architecture design, initial implementation of runtime and transformers.
164- [Tan Li Hau](https://github.com/tanhauhau) for the design and implementation of selective mocking, watch mode and experiments of future crazy idea.
165
166## License
167
168Manta Style is [MIT licensed](https://github.com/Cryrivers/manta-style/blob/master/LICENSE)