Skip to content

Commit 005a9f0

Browse files
committed
增加对异步操作的支持
1 parent ea9a0d5 commit 005a9f0

File tree

7 files changed

+147
-17
lines changed

7 files changed

+147
-17
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## ks-mock
2-
一个模拟服务端api的工具, 支持 https、触发鉴权、模拟接口、模拟请求错误、转发请求 等功能
2+
一个模拟服务端api的工具, 支持 https、触发鉴权、模拟接口、模拟请求错误、转发请求, 异步操作 等功能
33

44
## npm命令
55
``` npm
@@ -59,8 +59,15 @@ new KsMock({
5959
module.exports = {
6060
login: {
6161
format: (method, params, result, { body }) => {
62-
Object.assign(result.data, params);
63-
return result
62+
Object.assign(result.data, params);
63+
// 同步
64+
return result
65+
// 异步
66+
return new Promise((resolve, reject) => {
67+
setTimeout(() => {
68+
resolve(result)
69+
}, 1e4)
70+
})
6471
},
6572
post: {
6673
message: "登录成功!"

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ var Auth = /** @class */ (function () {
138138
* 退出登录
139139
*/
140140
Auth.prototype.logout = function () {
141-
console.log(server);
142141
user.clear();
143142
server.set('login', false);
144143
};
@@ -367,7 +366,22 @@ var Server$1 = function (option, callback) {
367366
next();
368367
return;
369368
}
370-
http.writeHead(200, headConfig);
369+
if (result instanceof Promise) {
370+
try {
371+
result.then(function (data) {
372+
http.writeHead(200, headConfig);
373+
http.end(data);
374+
}).catch(function (e) {
375+
http.writeHead(500, headConfig);
376+
http.end(e);
377+
});
378+
}
379+
catch (e) { }
380+
return;
381+
}
382+
else {
383+
http.writeHead(200, headConfig);
384+
}
371385
}
372386
else {
373387
http.writeHead(405, headConfig);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "ks-mock",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "mock server 模拟后端API接口",
55
"main": "index.js",
66
"scripts": {
77
"mock": "node ./public/server.js",
8-
"prepublishOnly": "npm run build",
8+
"prepublishOnly": "npm run mockData && npm run build",
99
"build": "rollup -c -i ./src/index.ts -o index.js",
1010
"typedoc": "typedoc --options typedoc.ts",
11+
"mockData": "tsc src/datas.ts --outdir public",
1112
"test": "jest"
1213
},
1314
"repository": {

public/config.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
extendStatics(d, b);
11+
function __() { this.constructor = d; }
12+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13+
};
14+
})();
15+
exports.__esModule = true;
16+
var Base = /** @class */ (function () {
17+
function Base() {
18+
this.data = {};
19+
}
20+
Base.prototype.get = function (key) {
21+
return key ? this.data[key] : this.data;
22+
};
23+
Base.prototype.set = function (key, val) {
24+
this.data[key] = val;
25+
return this;
26+
};
27+
Base.prototype.reset = function (obj) {
28+
this.clear();
29+
Object.assign(this.data, obj);
30+
return this;
31+
};
32+
Base.prototype.clear = function () {
33+
for (var key in this.data) {
34+
delete this.data[key];
35+
}
36+
return this;
37+
};
38+
return Base;
39+
}());
40+
var Server = /** @class */ (function (_super) {
41+
__extends(Server, _super);
42+
function Server() {
43+
var _this = _super !== null && _super.apply(this, arguments) || this;
44+
_this.data = {
45+
login: false
46+
};
47+
return _this;
48+
}
49+
return Server;
50+
}(Base));
51+
var User = /** @class */ (function (_super) {
52+
__extends(User, _super);
53+
function User() {
54+
var _this = _super !== null && _super.apply(this, arguments) || this;
55+
_this.data = {};
56+
return _this;
57+
}
58+
return User;
59+
}(Base));
60+
var Config = /** @class */ (function (_super) {
61+
__extends(Config, _super);
62+
function Config() {
63+
var _this = _super !== null && _super.apply(this, arguments) || this;
64+
_this.data = {
65+
username: 'admin',
66+
password: '123456',
67+
retryUrl: 'http://localhost:3030/info'
68+
};
69+
return _this;
70+
}
71+
return Config;
72+
}(Base));
73+
exports.server = new Server;
74+
exports.user = new User;
75+
exports.config = new Config;
76+
exports.crossDomain = {
77+
'Access-Control-Allow-Origin': '*',
78+
'Access-Control-Allow-Methods': '*',
79+
'Access-Control-Allow-Headers': '*',
80+
'Access-Control-Allow-Credentials': true
81+
};
82+
exports["default"] = {
83+
user: exports.user,
84+
server: exports.server,
85+
config: exports.config,
86+
crossDomain: exports.crossDomain
87+
};

public/datas.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"use strict";
22
exports.__esModule = true;
3-
var config = {
4-
username: 'admin',
5-
password: '123456',
6-
retryUrl: '/info'
7-
};
8-
var defaultConfig = Object.assign({}, config);
3+
/// <reference path="../types/datas.d.ts" />
4+
var config_1 = require("./config");
5+
var defaultConfig = Object.assign({}, config_1.config.data);
6+
var config = defaultConfig;
97
exports.login = {
108
// 转发
119
relay: '',
@@ -59,8 +57,13 @@ exports.info = {
5957
},
6058
format: function (method, params, result) {
6159
result.message = 'hello ' + (params.username || 'world');
60+
return new Promise(function (resolve, reject) {
61+
setTimeout(function () {
62+
reject(result);
63+
}, 1e4);
64+
});
6265
// 不返回, 那么修改无效
63-
return result;
66+
// return result
6467
},
6568
get: {
6669
code: 200,

src/datas.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
/// <reference path="../types/datas.d.ts" />
23
import { config as _config } from './config';
34
const defaultConfig = Object.assign({}, _config.data);
45
const config = defaultConfig;
@@ -53,8 +54,13 @@ export const info: api = {
5354
},
5455
format(method, params, result) {
5556
result.message = 'hello ' + (params.username || 'world')
57+
return new Promise((resolve, reject) => {
58+
setTimeout(() => {
59+
reject(result);
60+
}, 1e4);
61+
})
5662
// 不返回, 那么修改无效
57-
return result
63+
// return result
5864
},
5965
get: {
6066
code: 200,

src/server.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,20 @@ const Server = (option: anyObject, callback?: Function) => {
190190
next()
191191
return
192192
}
193-
http.writeHead(200, headConfig)
193+
if (result instanceof Promise) {
194+
try {
195+
result.then((data) => {
196+
http.writeHead(200, headConfig)
197+
http.end(data)
198+
}).catch((e) => {
199+
http.writeHead(500, headConfig)
200+
http.end(e)
201+
})
202+
} catch(e) {}
203+
return
204+
} else {
205+
http.writeHead(200, headConfig)
206+
}
194207
} else {
195208
http.writeHead(405, headConfig)
196209
result = {
@@ -216,7 +229,6 @@ const Server = (option: anyObject, callback?: Function) => {
216229
urlKey,
217230
params,
218231
} = getInfo(req, option, config.crossDomain)
219-
220232
mockData = mockData || {}
221233
let body = {
222234
code: 201,

0 commit comments

Comments
 (0)