vite-plugin-ai-mock-generator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Your Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # vite-plugin-ai-mock-generator
2
+
3
+ AI-powered Mock data generator for Vite. Generate realistic test data with AI or basic algorithms.
4
+
5
+ ## ✨ Features
6
+
7
+ - 🤖 **AI Generation** - Use OpenAI to generate realistic business data
8
+ - ⚡ **Fast Mode** - Basic algorithm generation without AI
9
+ - 💾 **Data Persistence** - Save generated data to files
10
+ - 🔄 **Auto Generation** - Generate data on server start
11
+ - 🎯 **Type Safe** - Full TypeScript support
12
+ - 🔍 **Query Support** - Filter, sort, and pagination
13
+ - 🎭 **Mock Server** - Built-in mock server with request interception
14
+
15
+ ## 📦 Installation
16
+
17
+ ::: code-group
18
+
19
+ ```bash [npm]
20
+ npm install -D vite-plugin-ai-mock-generator
21
+ ```
22
+
23
+ ```bash [yarn]
24
+ yarn add -D vite-plugin-ai-mock-generator
25
+ ```
26
+
27
+ ```bash [pnpm]
28
+ pnpm add -D vite-plugin-ai-mock-generator
29
+ ```
30
+
31
+ :::
32
+
33
+ ## 🚀 Quick Start
34
+
35
+ ```typescript
36
+ // vite.config.ts
37
+ import { defineConfig } from "vite";
38
+ import { vitePluginAIMockGenerator } from "vite-plugin-ai-mock-generator";
39
+
40
+ export default defineConfig({
41
+ plugins: [
42
+ vitePluginAIMockGenerator({
43
+ apiKey: process.env.OPENAI_API_KEY,
44
+ enabled: true,
45
+ autoGenerate: true,
46
+ endpoints: [
47
+ {
48
+ path: "/api/users",
49
+ method: "GET",
50
+ response: {
51
+ name: "User",
52
+ properties: [
53
+ { name: "id", type: "number" },
54
+ { name: "name", type: "string", comment: "用户名" },
55
+ { name: "email", type: "string", comment: "邮箱" },
56
+ ],
57
+ isArray: true,
58
+ },
59
+ count: 20,
60
+ },
61
+ ],
62
+ generation: {
63
+ locale: "zh-CN",
64
+ quality: "fast", // 'fast' | 'balanced' | 'high'
65
+ },
66
+ storage: {
67
+ dir: "mock-data",
68
+ persist: true,
69
+ },
70
+ }),
71
+ ],
72
+ });
73
+ ```
74
+
75
+ ## 📚 Documentation
76
+
77
+ Full documentation: https://mo520.github.io/vite-plugin-ai/plugins/ai-mock-generator
78
+
79
+ ## 📄 License
80
+
81
+ MIT
@@ -0,0 +1,272 @@
1
+ import { ViteDevServer, Plugin } from 'vite';
2
+
3
+ /**
4
+ * AI Mock Generator - 类型定义
5
+ */
6
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
7
+ type Locale = 'zh-CN' | 'en-US';
8
+ type Quality = 'fast' | 'balanced' | 'high';
9
+ /**
10
+ * 插件配置选项
11
+ */
12
+ interface MockGeneratorOptions {
13
+ apiKey?: string;
14
+ apiUrl?: string;
15
+ model?: string;
16
+ enabled?: boolean;
17
+ autoGenerate?: boolean;
18
+ sources?: {
19
+ types?: string[];
20
+ openapi?: string;
21
+ jsdoc?: boolean;
22
+ };
23
+ endpoints?: EndpointConfig[];
24
+ generation?: {
25
+ locale?: Locale;
26
+ count?: number;
27
+ seed?: number;
28
+ quality?: Quality;
29
+ };
30
+ storage?: {
31
+ dir?: string;
32
+ format?: 'json' | 'js' | 'ts';
33
+ persist?: boolean;
34
+ cache?: boolean;
35
+ };
36
+ server?: {
37
+ port?: number;
38
+ prefix?: string;
39
+ delay?: number | [number, number];
40
+ cors?: boolean;
41
+ };
42
+ output?: {
43
+ console?: boolean;
44
+ ui?: boolean;
45
+ logs?: boolean;
46
+ };
47
+ }
48
+ /**
49
+ * 端点配置
50
+ */
51
+ interface EndpointConfig {
52
+ path: string;
53
+ method: HttpMethod;
54
+ response: string | TypeDefinition;
55
+ request?: string | TypeDefinition;
56
+ count?: number;
57
+ template?: string;
58
+ enabled?: boolean;
59
+ custom?: CustomHandler;
60
+ pagination?: PaginationConfig;
61
+ }
62
+ /**
63
+ * 类型定义
64
+ */
65
+ interface TypeDefinition {
66
+ name: string;
67
+ properties: PropertyDefinition[];
68
+ isArray?: boolean;
69
+ }
70
+ /**
71
+ * 属性定义
72
+ */
73
+ interface PropertyDefinition {
74
+ name: string;
75
+ type: string;
76
+ comment?: string;
77
+ required?: boolean;
78
+ constraints?: PropertyConstraints;
79
+ }
80
+ /**
81
+ * 属性约束
82
+ */
83
+ interface PropertyConstraints {
84
+ min?: number;
85
+ max?: number;
86
+ pattern?: string;
87
+ unique?: boolean;
88
+ enum?: string[];
89
+ }
90
+ /**
91
+ * 分页配置
92
+ */
93
+ interface PaginationConfig {
94
+ pageSize: number;
95
+ total: number;
96
+ }
97
+ /**
98
+ * 自定义处理函数
99
+ */
100
+ type CustomHandler = (data: any, params?: any) => any | Promise<any>;
101
+ /**
102
+ * 生成上下文
103
+ */
104
+ interface GenerationContext {
105
+ locale: Locale;
106
+ count: number;
107
+ seed?: number;
108
+ quality: Quality;
109
+ type: TypeDefinition;
110
+ }
111
+ /**
112
+ * Mock 数据存储
113
+ */
114
+ interface MockDataStore {
115
+ endpoint: string;
116
+ method: HttpMethod;
117
+ data: any;
118
+ metadata: {
119
+ count: number;
120
+ generatedAt: string;
121
+ version: string;
122
+ type: string;
123
+ };
124
+ }
125
+
126
+ /**
127
+ * AI Mock Generator - 数据存储
128
+ */
129
+
130
+ declare class MockStorage {
131
+ private storageDir;
132
+ private cache;
133
+ private persist;
134
+ constructor(options?: {
135
+ dir?: string;
136
+ persist?: boolean;
137
+ });
138
+ /**
139
+ * 生成存储 key
140
+ */
141
+ private getKey;
142
+ /**
143
+ * 生成文件名
144
+ */
145
+ private getFileName;
146
+ /**
147
+ * 获取数据
148
+ */
149
+ get(endpoint: string, method?: HttpMethod): any;
150
+ /**
151
+ * 设置数据
152
+ */
153
+ set(endpoint: string, method: HttpMethod, data: any, metadata?: Partial<MockDataStore['metadata']>): void;
154
+ /**
155
+ * 删除数据
156
+ */
157
+ delete(endpoint: string, method?: HttpMethod): void;
158
+ /**
159
+ * 清空所有数据
160
+ */
161
+ clear(): void;
162
+ /**
163
+ * 获取所有端点
164
+ */
165
+ getAll(): MockDataStore[];
166
+ /**
167
+ * 保存到文件
168
+ */
169
+ private save;
170
+ /**
171
+ * 从文件加载
172
+ */
173
+ private load;
174
+ /**
175
+ * 导出所有数据
176
+ */
177
+ export(outputPath: string): void;
178
+ /**
179
+ * 导入数据
180
+ */
181
+ import(inputPath: string): void;
182
+ }
183
+
184
+ /**
185
+ * AI Mock Generator - 数据生成器
186
+ */
187
+
188
+ declare class MockDataGenerator {
189
+ private apiKey;
190
+ private apiUrl;
191
+ private model;
192
+ constructor(options: {
193
+ apiKey: string;
194
+ apiUrl: string;
195
+ model: string;
196
+ });
197
+ /**
198
+ * 生成 Mock 数据
199
+ */
200
+ generate(context: GenerationContext): Promise<any>;
201
+ /**
202
+ * 构建 AI Prompt
203
+ */
204
+ private buildPrompt;
205
+ /**
206
+ * 格式化属性
207
+ */
208
+ private formatProperty;
209
+ /**
210
+ * 调用 AI API
211
+ */
212
+ private callAI;
213
+ /**
214
+ * 解析 AI 响应
215
+ */
216
+ private parseResponse;
217
+ /**
218
+ * 生成基础数据(不使用 AI)
219
+ */
220
+ generateBasic(type: TypeDefinition, count: number): any[];
221
+ /**
222
+ * 生成基础值
223
+ */
224
+ private generateBasicValue;
225
+ }
226
+
227
+ /**
228
+ * AI Mock Generator - Mock 服务器
229
+ */
230
+
231
+ declare class MockServer {
232
+ private storage;
233
+ private endpoints;
234
+ private options;
235
+ constructor(storage: MockStorage, options: MockGeneratorOptions);
236
+ /**
237
+ * 配置服务器中间件
238
+ */
239
+ configureServer(server: ViteDevServer): void;
240
+ /**
241
+ * 匹配端点
242
+ */
243
+ private matchEndpoint;
244
+ /**
245
+ * 解析请求
246
+ */
247
+ private parseRequest;
248
+ /**
249
+ * 获取 Mock 数据
250
+ */
251
+ private getMockData;
252
+ /**
253
+ * 添加端点
254
+ */
255
+ addEndpoint(endpoint: EndpointConfig): void;
256
+ /**
257
+ * 移除端点
258
+ */
259
+ removeEndpoint(path: string, method: HttpMethod): void;
260
+ /**
261
+ * 获取所有端点
262
+ */
263
+ getEndpoints(): EndpointConfig[];
264
+ }
265
+
266
+ /**
267
+ * AI Mock Generator - Vite 插件入口
268
+ */
269
+
270
+ declare function vitePluginAIMockGenerator(options?: MockGeneratorOptions): Plugin;
271
+
272
+ export { type EndpointConfig, MockDataGenerator, type MockGeneratorOptions, MockServer, MockStorage, vitePluginAIMockGenerator };
@@ -0,0 +1,272 @@
1
+ import { ViteDevServer, Plugin } from 'vite';
2
+
3
+ /**
4
+ * AI Mock Generator - 类型定义
5
+ */
6
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
7
+ type Locale = 'zh-CN' | 'en-US';
8
+ type Quality = 'fast' | 'balanced' | 'high';
9
+ /**
10
+ * 插件配置选项
11
+ */
12
+ interface MockGeneratorOptions {
13
+ apiKey?: string;
14
+ apiUrl?: string;
15
+ model?: string;
16
+ enabled?: boolean;
17
+ autoGenerate?: boolean;
18
+ sources?: {
19
+ types?: string[];
20
+ openapi?: string;
21
+ jsdoc?: boolean;
22
+ };
23
+ endpoints?: EndpointConfig[];
24
+ generation?: {
25
+ locale?: Locale;
26
+ count?: number;
27
+ seed?: number;
28
+ quality?: Quality;
29
+ };
30
+ storage?: {
31
+ dir?: string;
32
+ format?: 'json' | 'js' | 'ts';
33
+ persist?: boolean;
34
+ cache?: boolean;
35
+ };
36
+ server?: {
37
+ port?: number;
38
+ prefix?: string;
39
+ delay?: number | [number, number];
40
+ cors?: boolean;
41
+ };
42
+ output?: {
43
+ console?: boolean;
44
+ ui?: boolean;
45
+ logs?: boolean;
46
+ };
47
+ }
48
+ /**
49
+ * 端点配置
50
+ */
51
+ interface EndpointConfig {
52
+ path: string;
53
+ method: HttpMethod;
54
+ response: string | TypeDefinition;
55
+ request?: string | TypeDefinition;
56
+ count?: number;
57
+ template?: string;
58
+ enabled?: boolean;
59
+ custom?: CustomHandler;
60
+ pagination?: PaginationConfig;
61
+ }
62
+ /**
63
+ * 类型定义
64
+ */
65
+ interface TypeDefinition {
66
+ name: string;
67
+ properties: PropertyDefinition[];
68
+ isArray?: boolean;
69
+ }
70
+ /**
71
+ * 属性定义
72
+ */
73
+ interface PropertyDefinition {
74
+ name: string;
75
+ type: string;
76
+ comment?: string;
77
+ required?: boolean;
78
+ constraints?: PropertyConstraints;
79
+ }
80
+ /**
81
+ * 属性约束
82
+ */
83
+ interface PropertyConstraints {
84
+ min?: number;
85
+ max?: number;
86
+ pattern?: string;
87
+ unique?: boolean;
88
+ enum?: string[];
89
+ }
90
+ /**
91
+ * 分页配置
92
+ */
93
+ interface PaginationConfig {
94
+ pageSize: number;
95
+ total: number;
96
+ }
97
+ /**
98
+ * 自定义处理函数
99
+ */
100
+ type CustomHandler = (data: any, params?: any) => any | Promise<any>;
101
+ /**
102
+ * 生成上下文
103
+ */
104
+ interface GenerationContext {
105
+ locale: Locale;
106
+ count: number;
107
+ seed?: number;
108
+ quality: Quality;
109
+ type: TypeDefinition;
110
+ }
111
+ /**
112
+ * Mock 数据存储
113
+ */
114
+ interface MockDataStore {
115
+ endpoint: string;
116
+ method: HttpMethod;
117
+ data: any;
118
+ metadata: {
119
+ count: number;
120
+ generatedAt: string;
121
+ version: string;
122
+ type: string;
123
+ };
124
+ }
125
+
126
+ /**
127
+ * AI Mock Generator - 数据存储
128
+ */
129
+
130
+ declare class MockStorage {
131
+ private storageDir;
132
+ private cache;
133
+ private persist;
134
+ constructor(options?: {
135
+ dir?: string;
136
+ persist?: boolean;
137
+ });
138
+ /**
139
+ * 生成存储 key
140
+ */
141
+ private getKey;
142
+ /**
143
+ * 生成文件名
144
+ */
145
+ private getFileName;
146
+ /**
147
+ * 获取数据
148
+ */
149
+ get(endpoint: string, method?: HttpMethod): any;
150
+ /**
151
+ * 设置数据
152
+ */
153
+ set(endpoint: string, method: HttpMethod, data: any, metadata?: Partial<MockDataStore['metadata']>): void;
154
+ /**
155
+ * 删除数据
156
+ */
157
+ delete(endpoint: string, method?: HttpMethod): void;
158
+ /**
159
+ * 清空所有数据
160
+ */
161
+ clear(): void;
162
+ /**
163
+ * 获取所有端点
164
+ */
165
+ getAll(): MockDataStore[];
166
+ /**
167
+ * 保存到文件
168
+ */
169
+ private save;
170
+ /**
171
+ * 从文件加载
172
+ */
173
+ private load;
174
+ /**
175
+ * 导出所有数据
176
+ */
177
+ export(outputPath: string): void;
178
+ /**
179
+ * 导入数据
180
+ */
181
+ import(inputPath: string): void;
182
+ }
183
+
184
+ /**
185
+ * AI Mock Generator - 数据生成器
186
+ */
187
+
188
+ declare class MockDataGenerator {
189
+ private apiKey;
190
+ private apiUrl;
191
+ private model;
192
+ constructor(options: {
193
+ apiKey: string;
194
+ apiUrl: string;
195
+ model: string;
196
+ });
197
+ /**
198
+ * 生成 Mock 数据
199
+ */
200
+ generate(context: GenerationContext): Promise<any>;
201
+ /**
202
+ * 构建 AI Prompt
203
+ */
204
+ private buildPrompt;
205
+ /**
206
+ * 格式化属性
207
+ */
208
+ private formatProperty;
209
+ /**
210
+ * 调用 AI API
211
+ */
212
+ private callAI;
213
+ /**
214
+ * 解析 AI 响应
215
+ */
216
+ private parseResponse;
217
+ /**
218
+ * 生成基础数据(不使用 AI)
219
+ */
220
+ generateBasic(type: TypeDefinition, count: number): any[];
221
+ /**
222
+ * 生成基础值
223
+ */
224
+ private generateBasicValue;
225
+ }
226
+
227
+ /**
228
+ * AI Mock Generator - Mock 服务器
229
+ */
230
+
231
+ declare class MockServer {
232
+ private storage;
233
+ private endpoints;
234
+ private options;
235
+ constructor(storage: MockStorage, options: MockGeneratorOptions);
236
+ /**
237
+ * 配置服务器中间件
238
+ */
239
+ configureServer(server: ViteDevServer): void;
240
+ /**
241
+ * 匹配端点
242
+ */
243
+ private matchEndpoint;
244
+ /**
245
+ * 解析请求
246
+ */
247
+ private parseRequest;
248
+ /**
249
+ * 获取 Mock 数据
250
+ */
251
+ private getMockData;
252
+ /**
253
+ * 添加端点
254
+ */
255
+ addEndpoint(endpoint: EndpointConfig): void;
256
+ /**
257
+ * 移除端点
258
+ */
259
+ removeEndpoint(path: string, method: HttpMethod): void;
260
+ /**
261
+ * 获取所有端点
262
+ */
263
+ getEndpoints(): EndpointConfig[];
264
+ }
265
+
266
+ /**
267
+ * AI Mock Generator - Vite 插件入口
268
+ */
269
+
270
+ declare function vitePluginAIMockGenerator(options?: MockGeneratorOptions): Plugin;
271
+
272
+ export { type EndpointConfig, MockDataGenerator, type MockGeneratorOptions, MockServer, MockStorage, vitePluginAIMockGenerator };