@fairys/mocker-cli 0.0.1
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/README.md +36 -0
- package/bin/fairys-mocker +22 -0
- package/esm/base.d.ts +28 -0
- package/esm/base.js +80 -0
- package/esm/ci.d.ts +2 -0
- package/esm/ci.js +49 -0
- package/esm/connect.d.ts +15 -0
- package/esm/connect.js +38 -0
- package/esm/controller/base.d.ts +2 -0
- package/esm/controller/base.js +3 -0
- package/esm/controller/index.d.ts +3 -0
- package/esm/controller/index.js +3 -0
- package/esm/controller/mock.router.d.ts +21 -0
- package/esm/controller/mock.router.js +497 -0
- package/esm/controller/proxy.router.d.ts +18 -0
- package/esm/controller/proxy.router.js +503 -0
- package/esm/index.d.ts +5 -0
- package/esm/index.js +5 -0
- package/esm/main.d.ts +15 -0
- package/esm/main.js +38 -0
- package/esm/plugins/rsbuild.d.ts +2 -0
- package/esm/plugins/rsbuild.js +12 -0
- package/esm/router/base.d.ts +12 -0
- package/esm/router/base.js +23 -0
- package/esm/router/index.d.ts +3 -0
- package/esm/router/index.js +3 -0
- package/esm/router/mock.d.ts +7 -0
- package/esm/router/mock.js +32 -0
- package/esm/router/proxy.d.ts +14 -0
- package/esm/router/proxy.js +67 -0
- package/esm/utils/decorator.d.ts +18 -0
- package/esm/utils/decorator.js +55 -0
- package/esm/utils/index.d.ts +2 -0
- package/esm/utils/index.js +2 -0
- package/esm/utils/mcok.proxy.d.ts +36 -0
- package/esm/utils/mcok.proxy.js +141 -0
- package/esm/utils/utils.d.ts +20 -0
- package/esm/utils/utils.js +31 -0
- package/package.json +52 -0
- package/public/_fairys_mocker/favicon.png +0 -0
- package/public/_fairys_mocker/index.html +1 -0
- package/public/_fairys_mocker/static/css/index.2ba69ff5.css +1 -0
- package/public/_fairys_mocker/static/js/514.950758f1.js +2 -0
- package/public/_fairys_mocker/static/js/514.950758f1.js.LICENSE.txt +5 -0
- package/public/_fairys_mocker/static/js/index.f70ed1dc.js +1 -0
- package/public/_fairys_mocker/static/js/lib-react.2748fa4b.js +2 -0
- package/public/_fairys_mocker/static/js/lib-react.2748fa4b.js.LICENSE.txt +49 -0
- package/src/base.ts +125 -0
- package/src/ci.ts +61 -0
- package/src/connect.ts +62 -0
- package/src/controller/base.ts +4 -0
- package/src/controller/index.ts +3 -0
- package/src/controller/mock.router.ts +175 -0
- package/src/controller/proxy.router.ts +182 -0
- package/src/index.ts +5 -0
- package/src/main.ts +59 -0
- package/src/plugins/rsbuild.ts +14 -0
- package/src/router/base.ts +36 -0
- package/src/router/index.ts +3 -0
- package/src/router/mock.ts +38 -0
- package/src/router/proxy.ts +89 -0
- package/src/utils/decorator.ts +90 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/mcok.proxy.ts +162 -0
- package/src/utils/utils.ts +46 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import node_fs from "node:fs";
|
|
2
|
+
import { Controller, Get, Post } from "../utils/decorator.js";
|
|
3
|
+
import { utils } from "../utils/index.js";
|
|
4
|
+
import { MockRouter } from "../router/mock.js";
|
|
5
|
+
import { BaseController } from "./base.js";
|
|
6
|
+
import { createMockFile, getMcokFile } from "../utils/mcok.proxy.js";
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
function applyDecs2203RFactory() {
|
|
9
|
+
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
10
|
+
return function(initializer) {
|
|
11
|
+
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
12
|
+
assertCallable(initializer, "An initializer");
|
|
13
|
+
initializers.push(initializer);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
|
|
17
|
+
var kindStr;
|
|
18
|
+
switch(kind){
|
|
19
|
+
case 1:
|
|
20
|
+
kindStr = "accessor";
|
|
21
|
+
break;
|
|
22
|
+
case 2:
|
|
23
|
+
kindStr = "method";
|
|
24
|
+
break;
|
|
25
|
+
case 3:
|
|
26
|
+
kindStr = "getter";
|
|
27
|
+
break;
|
|
28
|
+
case 4:
|
|
29
|
+
kindStr = "setter";
|
|
30
|
+
break;
|
|
31
|
+
default:
|
|
32
|
+
kindStr = "field";
|
|
33
|
+
}
|
|
34
|
+
var ctx = {
|
|
35
|
+
kind: kindStr,
|
|
36
|
+
name: isPrivate ? "#" + name : name,
|
|
37
|
+
static: isStatic,
|
|
38
|
+
private: isPrivate,
|
|
39
|
+
metadata: metadata
|
|
40
|
+
};
|
|
41
|
+
var decoratorFinishedRef = {
|
|
42
|
+
v: false
|
|
43
|
+
};
|
|
44
|
+
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
|
|
45
|
+
var get, set;
|
|
46
|
+
if (0 === kind) if (isPrivate) {
|
|
47
|
+
get = desc.get;
|
|
48
|
+
set = desc.set;
|
|
49
|
+
} else {
|
|
50
|
+
get = function() {
|
|
51
|
+
return this[name];
|
|
52
|
+
};
|
|
53
|
+
set = function(v) {
|
|
54
|
+
this[name] = v;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
else if (2 === kind) get = function() {
|
|
58
|
+
return desc.value;
|
|
59
|
+
};
|
|
60
|
+
else {
|
|
61
|
+
if (1 === kind || 3 === kind) get = function() {
|
|
62
|
+
return desc.get.call(this);
|
|
63
|
+
};
|
|
64
|
+
if (1 === kind || 4 === kind) set = function(v) {
|
|
65
|
+
desc.set.call(this, v);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (get) {
|
|
69
|
+
var originalGet = get;
|
|
70
|
+
get = function(target) {
|
|
71
|
+
if (0 === arguments.length) target = this;
|
|
72
|
+
return originalGet.call(target);
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (set) {
|
|
76
|
+
var originalSet = set;
|
|
77
|
+
set = function(target, value) {
|
|
78
|
+
if (1 === arguments.length) {
|
|
79
|
+
value = target;
|
|
80
|
+
target = this;
|
|
81
|
+
}
|
|
82
|
+
return originalSet.call(target, value);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (isPrivate) ctx.access = get && set ? {
|
|
86
|
+
get: get,
|
|
87
|
+
set: set
|
|
88
|
+
} : get ? {
|
|
89
|
+
get: get
|
|
90
|
+
} : {
|
|
91
|
+
set: set
|
|
92
|
+
};
|
|
93
|
+
else {
|
|
94
|
+
var has = function(target) {
|
|
95
|
+
return name in target;
|
|
96
|
+
};
|
|
97
|
+
ctx.access = get && set ? {
|
|
98
|
+
has: has,
|
|
99
|
+
get: get,
|
|
100
|
+
set: set
|
|
101
|
+
} : get ? {
|
|
102
|
+
has: has,
|
|
103
|
+
get: get
|
|
104
|
+
} : {
|
|
105
|
+
has: has,
|
|
106
|
+
set: set
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
var newValue = dec(value, ctx);
|
|
110
|
+
decoratorFinishedRef.v = true;
|
|
111
|
+
return newValue;
|
|
112
|
+
}
|
|
113
|
+
function assertNotFinished(decoratorFinishedRef, fnName) {
|
|
114
|
+
if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
|
|
115
|
+
}
|
|
116
|
+
function assertCallable(fn, hint) {
|
|
117
|
+
if ("function" != typeof fn) throw new TypeError(hint + " must be a function");
|
|
118
|
+
}
|
|
119
|
+
function assertValidReturnValue(kind, value) {
|
|
120
|
+
var type = typeof value;
|
|
121
|
+
if (1 === kind) {
|
|
122
|
+
if ("object" !== type || null === value) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
|
|
123
|
+
if (void 0 !== value.get) assertCallable(value.get, "accessor.get");
|
|
124
|
+
if (void 0 !== value.set) assertCallable(value.set, "accessor.set");
|
|
125
|
+
if (void 0 !== value.init) assertCallable(value.init, "accessor.init");
|
|
126
|
+
} else if ("function" !== type) {
|
|
127
|
+
var hint;
|
|
128
|
+
hint = 0 === kind ? "field" : 10 === kind ? "class" : "method";
|
|
129
|
+
throw new TypeError(hint + " decorators must return a function or void 0");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
|
|
133
|
+
var decs = decInfo[0];
|
|
134
|
+
var desc, init, value;
|
|
135
|
+
if (isPrivate) desc = 0 === kind || 1 === kind ? {
|
|
136
|
+
get: decInfo[3],
|
|
137
|
+
set: decInfo[4]
|
|
138
|
+
} : 3 === kind ? {
|
|
139
|
+
get: decInfo[3]
|
|
140
|
+
} : 4 === kind ? {
|
|
141
|
+
set: decInfo[3]
|
|
142
|
+
} : {
|
|
143
|
+
value: decInfo[3]
|
|
144
|
+
};
|
|
145
|
+
else if (0 !== kind) desc = Object.getOwnPropertyDescriptor(base, name);
|
|
146
|
+
if (1 === kind) value = {
|
|
147
|
+
get: desc.get,
|
|
148
|
+
set: desc.set
|
|
149
|
+
};
|
|
150
|
+
else if (2 === kind) value = desc.value;
|
|
151
|
+
else if (3 === kind) value = desc.get;
|
|
152
|
+
else if (4 === kind) value = desc.set;
|
|
153
|
+
var newValue, get, set;
|
|
154
|
+
if ("function" == typeof decs) {
|
|
155
|
+
newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
156
|
+
if (void 0 !== newValue) {
|
|
157
|
+
assertValidReturnValue(kind, newValue);
|
|
158
|
+
if (0 === kind) init = newValue;
|
|
159
|
+
else if (1 === kind) {
|
|
160
|
+
init = newValue.init;
|
|
161
|
+
get = newValue.get || value.get;
|
|
162
|
+
set = newValue.set || value.set;
|
|
163
|
+
value = {
|
|
164
|
+
get: get,
|
|
165
|
+
set: set
|
|
166
|
+
};
|
|
167
|
+
} else value = newValue;
|
|
168
|
+
}
|
|
169
|
+
} else for(var i = decs.length - 1; i >= 0; i--){
|
|
170
|
+
var dec = decs[i];
|
|
171
|
+
newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
172
|
+
if (void 0 !== newValue) {
|
|
173
|
+
assertValidReturnValue(kind, newValue);
|
|
174
|
+
var newInit;
|
|
175
|
+
if (0 === kind) newInit = newValue;
|
|
176
|
+
else if (1 === kind) {
|
|
177
|
+
newInit = newValue.init;
|
|
178
|
+
get = newValue.get || value.get;
|
|
179
|
+
set = newValue.set || value.set;
|
|
180
|
+
value = {
|
|
181
|
+
get: get,
|
|
182
|
+
set: set
|
|
183
|
+
};
|
|
184
|
+
} else value = newValue;
|
|
185
|
+
if (void 0 !== newInit) if (void 0 === init) init = newInit;
|
|
186
|
+
else if ("function" == typeof init) init = [
|
|
187
|
+
init,
|
|
188
|
+
newInit
|
|
189
|
+
];
|
|
190
|
+
else init.push(newInit);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (0 === kind || 1 === kind) {
|
|
194
|
+
if (void 0 === init) init = function(instance, init) {
|
|
195
|
+
return init;
|
|
196
|
+
};
|
|
197
|
+
else if ("function" != typeof init) {
|
|
198
|
+
var ownInitializers = init;
|
|
199
|
+
init = function(instance, init) {
|
|
200
|
+
var value = init;
|
|
201
|
+
for(var i = 0; i < ownInitializers.length; i++)value = ownInitializers[i].call(instance, value);
|
|
202
|
+
return value;
|
|
203
|
+
};
|
|
204
|
+
} else {
|
|
205
|
+
var originalInitializer = init;
|
|
206
|
+
init = function(instance, init) {
|
|
207
|
+
return originalInitializer.call(instance, init);
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
ret.push(init);
|
|
211
|
+
}
|
|
212
|
+
if (0 !== kind) {
|
|
213
|
+
if (1 === kind) {
|
|
214
|
+
desc.get = value.get;
|
|
215
|
+
desc.set = value.set;
|
|
216
|
+
} else if (2 === kind) desc.value = value;
|
|
217
|
+
else if (3 === kind) desc.get = value;
|
|
218
|
+
else if (4 === kind) desc.set = value;
|
|
219
|
+
if (isPrivate) if (1 === kind) {
|
|
220
|
+
ret.push(function(instance, args) {
|
|
221
|
+
return value.get.call(instance, args);
|
|
222
|
+
});
|
|
223
|
+
ret.push(function(instance, args) {
|
|
224
|
+
return value.set.call(instance, args);
|
|
225
|
+
});
|
|
226
|
+
} else if (2 === kind) ret.push(value);
|
|
227
|
+
else ret.push(function(instance, args) {
|
|
228
|
+
return value.call(instance, args);
|
|
229
|
+
});
|
|
230
|
+
else Object.defineProperty(base, name, desc);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function applyMemberDecs(Class, decInfos, metadata) {
|
|
234
|
+
var ret = [];
|
|
235
|
+
var protoInitializers;
|
|
236
|
+
var staticInitializers;
|
|
237
|
+
var existingProtoNonFields = new Map();
|
|
238
|
+
var existingStaticNonFields = new Map();
|
|
239
|
+
for(var i = 0; i < decInfos.length; i++){
|
|
240
|
+
var decInfo = decInfos[i];
|
|
241
|
+
if (Array.isArray(decInfo)) {
|
|
242
|
+
var kind = decInfo[1];
|
|
243
|
+
var name = decInfo[2];
|
|
244
|
+
var isPrivate = decInfo.length > 3;
|
|
245
|
+
var isStatic = kind >= 5;
|
|
246
|
+
var base;
|
|
247
|
+
var initializers;
|
|
248
|
+
if (isStatic) {
|
|
249
|
+
base = Class;
|
|
250
|
+
kind -= 5;
|
|
251
|
+
staticInitializers = staticInitializers || [];
|
|
252
|
+
initializers = staticInitializers;
|
|
253
|
+
} else {
|
|
254
|
+
base = Class.prototype;
|
|
255
|
+
protoInitializers = protoInitializers || [];
|
|
256
|
+
initializers = protoInitializers;
|
|
257
|
+
}
|
|
258
|
+
if (0 !== kind && !isPrivate) {
|
|
259
|
+
var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
|
|
260
|
+
var existingKind = existingNonFields.get(name) || 0;
|
|
261
|
+
if (true === existingKind || 3 === existingKind && 4 !== kind || 4 === existingKind && 3 !== kind) throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
|
|
262
|
+
if (!existingKind && kind > 2) existingNonFields.set(name, kind);
|
|
263
|
+
else existingNonFields.set(name, true);
|
|
264
|
+
}
|
|
265
|
+
applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
pushInitializers(ret, protoInitializers);
|
|
269
|
+
pushInitializers(ret, staticInitializers);
|
|
270
|
+
return ret;
|
|
271
|
+
}
|
|
272
|
+
function pushInitializers(ret, initializers) {
|
|
273
|
+
if (initializers) ret.push(function(instance) {
|
|
274
|
+
for(var i = 0; i < initializers.length; i++)initializers[i].call(instance);
|
|
275
|
+
return instance;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
function applyClassDecs(targetClass, classDecs, metadata) {
|
|
279
|
+
if (classDecs.length > 0) {
|
|
280
|
+
var initializers = [];
|
|
281
|
+
var newClass = targetClass;
|
|
282
|
+
var name = targetClass.name;
|
|
283
|
+
for(var i = classDecs.length - 1; i >= 0; i--){
|
|
284
|
+
var decoratorFinishedRef = {
|
|
285
|
+
v: false
|
|
286
|
+
};
|
|
287
|
+
var nextNewClass = classDecs[i](newClass, {
|
|
288
|
+
kind: "class",
|
|
289
|
+
name: name,
|
|
290
|
+
addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
|
|
291
|
+
metadata
|
|
292
|
+
});
|
|
293
|
+
decoratorFinishedRef.v = true;
|
|
294
|
+
if (void 0 !== nextNewClass) {
|
|
295
|
+
assertValidReturnValue(10, nextNewClass);
|
|
296
|
+
newClass = nextNewClass;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return [
|
|
300
|
+
defineMetadata(newClass, metadata),
|
|
301
|
+
function() {
|
|
302
|
+
for(var i = 0; i < initializers.length; i++)initializers[i].call(newClass);
|
|
303
|
+
}
|
|
304
|
+
];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function defineMetadata(Class, metadata) {
|
|
308
|
+
return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
|
|
309
|
+
configurable: true,
|
|
310
|
+
enumerable: true,
|
|
311
|
+
value: metadata
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return function(targetClass, memberDecs, classDecs, parentClass) {
|
|
315
|
+
if (void 0 !== parentClass) var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
|
|
316
|
+
var metadata = Object.create(void 0 === parentMetadata ? null : parentMetadata);
|
|
317
|
+
var e = applyMemberDecs(targetClass, memberDecs, metadata);
|
|
318
|
+
if (!classDecs.length) defineMetadata(targetClass, metadata);
|
|
319
|
+
return {
|
|
320
|
+
e: e,
|
|
321
|
+
get c () {
|
|
322
|
+
return applyClassDecs(targetClass, classDecs, metadata);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
328
|
+
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
329
|
+
}
|
|
330
|
+
var _dec, _initClass, _BaseController, _dec1, _dec2, _dec3, _dec4, _dec5, _initProto;
|
|
331
|
+
_dec = Controller('/_fairys'), _dec1 = Post('/_mock'), _dec2 = Get('/_mock'), _dec3 = Get('/_mock/_is_enabled'), _dec4 = Get('/_mock/_start'), _dec5 = Get('/_mock/_destroy');
|
|
332
|
+
let _MockRouterController;
|
|
333
|
+
class MockRouterController extends (_BaseController = BaseController) {
|
|
334
|
+
static{
|
|
335
|
+
({ e: [_initProto], c: [_MockRouterController, _initClass] } = _apply_decs_2203_r(this, [
|
|
336
|
+
[
|
|
337
|
+
_dec1,
|
|
338
|
+
2,
|
|
339
|
+
"post_mock"
|
|
340
|
+
],
|
|
341
|
+
[
|
|
342
|
+
_dec2,
|
|
343
|
+
2,
|
|
344
|
+
"get_mock"
|
|
345
|
+
],
|
|
346
|
+
[
|
|
347
|
+
_dec3,
|
|
348
|
+
2,
|
|
349
|
+
"get_mock_enabled"
|
|
350
|
+
],
|
|
351
|
+
[
|
|
352
|
+
_dec4,
|
|
353
|
+
2,
|
|
354
|
+
"get_mock_start"
|
|
355
|
+
],
|
|
356
|
+
[
|
|
357
|
+
_dec5,
|
|
358
|
+
2,
|
|
359
|
+
"get_mock_destroy"
|
|
360
|
+
]
|
|
361
|
+
], [
|
|
362
|
+
_dec
|
|
363
|
+
], _BaseController));
|
|
364
|
+
}
|
|
365
|
+
constructor(){
|
|
366
|
+
super();
|
|
367
|
+
this.router = new MockRouter();
|
|
368
|
+
}
|
|
369
|
+
router = (_initProto(this), null);
|
|
370
|
+
post_mock(req, res) {
|
|
371
|
+
try {
|
|
372
|
+
const { mockList, dir, fileName = 'index.mock', rootDir } = req.body;
|
|
373
|
+
let _rootDir = rootDir || utils.rootDir;
|
|
374
|
+
if (rootDir && !node_fs.existsSync(rootDir)) _rootDir = utils.rootDir;
|
|
375
|
+
const mockData = createMockFile(mockList, _rootDir, dir, fileName);
|
|
376
|
+
if (mockData?.mockConfig) {
|
|
377
|
+
if (this.router?.isEnabled) this.router?.load(mockData.mockConfig);
|
|
378
|
+
res.json({
|
|
379
|
+
code: 200,
|
|
380
|
+
message: 'Mock 配置保存成功',
|
|
381
|
+
data: mockData.mockConfig,
|
|
382
|
+
mockList: mockList,
|
|
383
|
+
rootDir: mockData.rootDir,
|
|
384
|
+
dir: mockData.dir,
|
|
385
|
+
cache: mockData.cache,
|
|
386
|
+
fileName: mockData.fileName
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
} catch (error) {
|
|
390
|
+
res.status(500).json({
|
|
391
|
+
code: 500,
|
|
392
|
+
message: '保存 Mock 配置失败',
|
|
393
|
+
error: error.message
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
get_mock(req, res) {
|
|
398
|
+
try {
|
|
399
|
+
const savePath = req.query?.dir?.trim();
|
|
400
|
+
const saveFileName = req.query?.fileName?.trim();
|
|
401
|
+
const rootDir = req.query?.rootDir?.trim();
|
|
402
|
+
const mockData = getMcokFile(rootDir, savePath, saveFileName);
|
|
403
|
+
if (mockData?.mockList) res.json({
|
|
404
|
+
code: 200,
|
|
405
|
+
message: '读取 Mock 配置成功',
|
|
406
|
+
data: mockData.mockList,
|
|
407
|
+
rootDir: mockData.rootDir,
|
|
408
|
+
dir: mockData.dir,
|
|
409
|
+
cache: mockData.cache,
|
|
410
|
+
fileName: mockData.fileName
|
|
411
|
+
});
|
|
412
|
+
else res.json({
|
|
413
|
+
code: 404,
|
|
414
|
+
message: 'Mock 配置文件不存在'
|
|
415
|
+
});
|
|
416
|
+
} catch (error) {
|
|
417
|
+
res.status(500).json({
|
|
418
|
+
code: 500,
|
|
419
|
+
message: '读取 Mock 配置失败',
|
|
420
|
+
error: error.message
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
get_mock_enabled(req, res) {
|
|
425
|
+
res.json({
|
|
426
|
+
code: 200,
|
|
427
|
+
message: 'Mock 配置服务是否启用',
|
|
428
|
+
data: this.router?.isEnabled
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
get_mock_start(req, res) {
|
|
432
|
+
try {
|
|
433
|
+
const mockData = getMcokFile(req.query.rootDir, req.query.dir, req.query.fileName);
|
|
434
|
+
if (mockData?.mockList) {
|
|
435
|
+
this.router?.load(mockData.mockList);
|
|
436
|
+
res.json({
|
|
437
|
+
code: 200,
|
|
438
|
+
message: '启动 Mock 配置服务成功',
|
|
439
|
+
data: mockData.mockList,
|
|
440
|
+
rootDir: mockData.rootDir,
|
|
441
|
+
dir: mockData.dir,
|
|
442
|
+
cache: mockData.cache,
|
|
443
|
+
fileName: mockData.fileName,
|
|
444
|
+
isEnabled: true
|
|
445
|
+
});
|
|
446
|
+
} else res.json({
|
|
447
|
+
code: 404,
|
|
448
|
+
message: 'Mock 配置文件不存在'
|
|
449
|
+
});
|
|
450
|
+
} catch (error) {
|
|
451
|
+
res.status(500).json({
|
|
452
|
+
code: 500,
|
|
453
|
+
message: '启动 Mock 配置失败',
|
|
454
|
+
error: error.message
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
get_mock_destroy(req, res) {
|
|
459
|
+
try {
|
|
460
|
+
if (this.router?.router) this.router?.destroy("销毁mock路由器实例");
|
|
461
|
+
else res.json({
|
|
462
|
+
code: 404,
|
|
463
|
+
message: 'Mock 配置服务不存在'
|
|
464
|
+
});
|
|
465
|
+
res.json({
|
|
466
|
+
code: 200,
|
|
467
|
+
message: '销毁 Mock 配置服务成功'
|
|
468
|
+
});
|
|
469
|
+
} catch (error) {
|
|
470
|
+
res.status(500).json({
|
|
471
|
+
code: 500,
|
|
472
|
+
message: '销毁 Mock 配置服务',
|
|
473
|
+
error: error.message
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
start = (rootDir, dir, fileName)=>{
|
|
478
|
+
try {
|
|
479
|
+
const mockData = getMcokFile(rootDir, dir, fileName);
|
|
480
|
+
if (mockData?.mockList) try {
|
|
481
|
+
this.router?.load(mockData.mockList);
|
|
482
|
+
console.log(chalk.green(` 启动 Mock 配置服务成功,配置文件:${mockData.rootDir}/${mockData.dir}/${mockData.fileName}.ts`));
|
|
483
|
+
console.log('');
|
|
484
|
+
} catch (error) {
|
|
485
|
+
console.log(chalk.red(` 启动 Mock 配置服务失败,配置文件:${mockData.rootDir}/${mockData.dir}/${mockData.fileName}.ts`));
|
|
486
|
+
console.log('');
|
|
487
|
+
}
|
|
488
|
+
} catch (error) {
|
|
489
|
+
console.log(chalk.red(" 启动 Mock 配置服务失败"));
|
|
490
|
+
console.log('');
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
static{
|
|
494
|
+
_initClass();
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
export { _MockRouterController as MockRouterController };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { ProxyRouter } from '../router/proxy.js';
|
|
3
|
+
import { BaseController } from './base.js';
|
|
4
|
+
/**路由*/
|
|
5
|
+
export declare class ProxyRouterController extends BaseController {
|
|
6
|
+
/**代理 路由器实例*/
|
|
7
|
+
router: ProxyRouter | null;
|
|
8
|
+
constructor();
|
|
9
|
+
post_proxy(req: express.Request, res: express.Response): void;
|
|
10
|
+
get_proxy(req: express.Request, res: express.Response): void;
|
|
11
|
+
/**检查代理服务是否启用*/
|
|
12
|
+
get_proxy_enabled(req: express.Request, res: express.Response): void;
|
|
13
|
+
/**启动代理服务*/
|
|
14
|
+
get_proxy_start(req: express.Request, res: express.Response): void;
|
|
15
|
+
/**销毁代理服务*/
|
|
16
|
+
get_proxy_destroy(req: express.Request, res: express.Response): void;
|
|
17
|
+
start: (rootDir?: string, dir?: string, fileName?: string) => void;
|
|
18
|
+
}
|