Skip to content

Commit f1d11bc

Browse files
committed
修改url消息
1 parent 87227de commit f1d11bc

File tree

7 files changed

+2152
-36
lines changed

7 files changed

+2152
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## ks-mock
2-
一个mock服务端api的工具, 除模拟请求api接口数据外, 还提供了 https、触发鉴权、模拟请求错误、转发请求 的功能
2+
一个模拟服务端api的工具, 支持 https、触发鉴权、模拟接口、模拟请求错误、转发请求 等功能
33

44
## npm命令
55
``` npm

localhost.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var rules = {
136136
};
137137

138138
var getInfo = function (req, option, headers) {
139-
var url = req._parsedUrl.pathname.replace(/^\//, '');
139+
var url = req._parsedUrl.href.replace(/^\//, '');
140140
return {
141141
url: url,
142142
data: option.mockData[url],
@@ -176,7 +176,7 @@ var middlewares = jsonServer.defaults({
176176
static: path.resolve(__dirname, './public')
177177
});
178178
server$1.use(middlewares);
179-
var createServer = function (option) {
179+
var createServer = function (option, callback) {
180180
var config = option.https;
181181
if (config instanceof Object) {
182182
if (typeof config.key !== 'string' || typeof config.cert !== 'string' || config.key.length + config.cert.length === 0) {
@@ -188,13 +188,15 @@ var createServer = function (option) {
188188
console.log();
189189
console.log("\u5DF2\u542F\u52A8json-server\u670D\u52A1\u5668 https://localhost:" + option.port);
190190
console.log();
191+
typeof callback == 'function' && callback();
191192
});
192193
}
193194
else {
194195
server$1.listen(option.port, function () {
195196
console.log();
196197
console.log("\u5DF2\u542F\u52A8json-server\u670D\u52A1\u5668 http://localhost:" + option.port);
197198
console.log();
199+
typeof callback == 'function' && callback();
198200
});
199201
}
200202
};
@@ -207,7 +209,7 @@ var createServer = function (option) {
207209
* @param {boolean=} option.crossDomain - 是否跨域 (便于在不设置请求头时, 快速配置跨域)
208210
* @param {number=} port - 服务器端口
209211
*/
210-
var Server$1 = function (option) {
212+
var Server$1 = function (option, callback) {
211213
option = Object.assign({
212214
port: 3030,
213215
crossDomain: true,
@@ -364,7 +366,7 @@ var Server$1 = function (option) {
364366
res.status(200).jsonp(body);
365367
};
366368
server$1.use(router);
367-
createServer(option);
369+
createServer(option, callback);
368370
};
369371

370372
var config$1 = {

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "ks-mock",
3-
"version": "1.1.15",
3+
"version": "1.1.16",
44
"description": "mock server 模拟后端API接口",
55
"main": "index.js",
66
"scripts": {
77
"mock": "npm run bundle && node localhost",
88
"prepublishOnly": "npm run bundle && npm run build",
99
"bundle": "rollup -c -i ./src/localhost.ts -o localhost.js",
1010
"build": "rollup -c -i ./src/index.ts -o index.js",
11-
"typedoc": "typedoc --options typedoc.ts"
11+
"typedoc": "typedoc --options typedoc.ts",
12+
"test": "jest"
1213
},
1314
"repository": {
1415
"type": "git",
@@ -28,6 +29,8 @@
2829
},
2930
"homepage": "https://github.com/kscript/mock#readme",
3031
"dependencies": {
32+
"fs": "0.0.1-security",
33+
"https": "^1.0.0",
3134
"json-server": "^0.14.0",
3235
"mockjs": "^1.0.1-beta3",
3336
"path": "^0.12.7",
@@ -36,8 +39,7 @@
3639
"devDependencies": {
3740
"@types/mocha": "^5.2.6",
3841
"@types/node": "^12.0.0",
39-
"fs": "0.0.1-security",
40-
"https": "^1.0.0",
42+
"jest": "^24.9.0",
4143
"rollup-plugin-typescript": "^1.0.1",
4244
"tslib": "*",
4345
"typedoc": "^0.14.2",

src/server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const middlewares = jsonServer.defaults({
1515
})
1616

1717
server.use(middlewares)
18-
const createServer = (option: anyObject) => {
18+
const createServer = (option: anyObject, callback?: Function) => {
1919
let config = option.https
2020
if(config instanceof Object) {
2121
if (typeof config.key !== 'string' || typeof config.cert !== 'string' || config.key.length + config.cert.length === 0){
@@ -27,12 +27,14 @@ const createServer = (option: anyObject) => {
2727
console.log()
2828
console.log(`已启动json-server服务器 https://localhost:${option.port}`)
2929
console.log()
30+
typeof callback == 'function' && callback();
3031
})
3132
} else {
3233
server.listen(option.port, () => {
3334
console.log()
3435
console.log(`已启动json-server服务器 http://localhost:${option.port}`)
3536
console.log()
37+
typeof callback == 'function' && callback();
3638
})
3739
}
3840
}
@@ -45,7 +47,7 @@ const createServer = (option: anyObject) => {
4547
* @param {boolean=} option.crossDomain - 是否跨域 (便于在不设置请求头时, 快速配置跨域)
4648
* @param {number=} port - 服务器端口
4749
*/
48-
const Server = option => {
50+
const Server = (option: anyObject, callback?: Function) => {
4951
option = Object.assign({
5052
port: 3030,
5153
crossDomain: true,
@@ -217,6 +219,6 @@ const Server = option => {
217219
res.status(200).jsonp(body)
218220
}
219221
server.use(router)
220-
createServer(option);
222+
createServer(option, callback);
221223
}
222224
export default Server

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Mock from 'mockjs';
22

33
export const getInfo = (req, option, headers) => {
4-
let url = req._parsedUrl.pathname.replace(/^\//, '')
4+
let url = req._parsedUrl.href.replace(/^\//, '')
55
return {
66
url,
77
data: option.mockData[url],

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"exclude": [
1313
"node_modules",
14-
".vscode"
14+
".vscode",
15+
"**/*.test.ts"
1516
]
1617
}

0 commit comments

Comments
 (0)