vite-plugin-lessmock
Version:
A vite plugin that auto generate mock data with fake data for TypeScript interfaces.
107 lines (88 loc) • 1.78 kB
Markdown
A vite plugin that auto generate mock data with fake data for TypeScript interfaces.
```bash
npm install vite-plugin-lessmock
```
_vite.config.ts_:
```ts
import { defineConfig } from 'vite'
import lessMock from 'vite-plugin-lessmock'
import { resolve } from 'path'
export default defineConfig({
plugins: [
lessMock({
apiPrefix: '/api/mock/',
mockDir: resolve('mock')
})
]
})
```
## Options
```ts
lessMock(options)
type TOptions = {
/**
* The directory to search for mock files.
*/
mockDir: string;
/**
* Intercept api requests based on this prefix
*/
apiPrefix: string;
/**
* @default TLessMock
*/
useType?: string;
}
```
Project structure:
```text
- my_project
- mock
-
-
- a
-
```
- URL `/a/b` with `GET` method will match _my_project/mock/
- URL `/a/c` with `POST` method will match _my_project/mock/
- URL `/a/d/e` with `GET` method will match _my_project/mock/a/
> Warn: make sure `.get.ts` `.post.ts` is lower case, and file and directory name is case sensitive.
_
```ts
type ReturnType<T> = {
success: 200 | 201 | 204;
data: T;
msg: string;
}
export type TLessMock = ReturnType<{
id: number;
/**
* @format name.firstName
*/
name: string;
/**
* @maximum 100
* @minimum 1
*/
age: number;
status: boolean;
}>
```
The response content that you request `/a/b` will be like blew:
```json
{
"success": 204,
"data": {
"id": 42365,
"name": "Cecil",
"age": 23,
"status": false
},
"msg": "Illo deleniti fuga inventore asperiores tempora."
}
```