Skip to content

Commit bea8c03

Browse files
committed
修改类型描述文件
1 parent 3ed28fe commit bea8c03

File tree

10 files changed

+79
-92
lines changed

10 files changed

+79
-92
lines changed

index.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export namespace mock {
2+
interface anyObject {
3+
[prop: string]: any
4+
}
5+
type apiFunc = (
6+
method: string,
7+
params: anyObject,
8+
result: anyObject,
9+
rest?: {
10+
url: string;
11+
body: object;
12+
[prop: string]: any;
13+
}
14+
) => void | object;
15+
16+
type apiErrorFunc = (
17+
method: string,
18+
params: anyObject,
19+
result: anyObject,
20+
rest?: {
21+
url: string;
22+
body: object;
23+
[prop: string]: any;
24+
}
25+
) => undefined | object | string | Function;
26+
27+
interface api {
28+
public?: boolean;
29+
relay?: string | apiFunc;
30+
format?: apiFunc;
31+
error?: apiErrorFunc;
32+
post?: anyObject;
33+
get?: anyObject;
34+
[prop: string]: any;
35+
}
36+
}

index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -487,21 +487,21 @@ var Server$1 = function (option, callback) {
487487
createServer(option, callback);
488488
};
489489

490-
/**
491-
* mock实例
492-
* @param option 配置
493-
*/
494-
function KsMock(option) {
495-
this.option = option;
496-
}
497-
KsMock.prototype.apply = function (compiler) {
498-
Server$1(this.option);
499-
compiler.plugin("emit", function (compilation, callback) {
500-
callback();
501-
});
502-
};
503-
KsMock.prototype.server = function (option) {
504-
Server$1(option || this.option);
505-
};
490+
var KsMock = /** @class */ (function () {
491+
function KsMock(option) {
492+
this.option = {};
493+
this.option = option;
494+
}
495+
KsMock.prototype.apply = function (compiler) {
496+
Server$1(this.option);
497+
compiler.plugin("emit", function (compilation, callback) {
498+
callback();
499+
});
500+
};
501+
KsMock.prototype.server = function (option) {
502+
Server$1(option || this.option);
503+
};
504+
return KsMock;
505+
}());
506506

507507
module.exports = KsMock;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ks-mock",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "mock server 模拟后端API接口",
55
"main": "index.js",
66
"scripts": {

public/datas.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22
exports.__esModule = true;
3-
/// <reference path="../types/datas.d.ts" />
43
var config_1 = require("./config");
54
var defaultConfig = Object.assign({}, config_1.config.data);
65
var config = defaultConfig;

src/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import {mock} from '../'
12
class Base {
2-
data: anyObject = {}
3+
data: mock.anyObject = {}
34
get(key?: string): any {
45
return key ? this.data[key] : this.data
56
}
67
set(key: string, val: any): Base {
78
this.data[key] = val
89
return this
910
}
10-
reset(obj: anyObject): Base {
11+
reset(obj: mock.anyObject): Base {
1112
this.clear()
1213
Object.assign(this.data, obj)
1314
return this

src/datas.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
2-
/// <reference path="../types/datas.d.ts" />
1+
import { mock } from '..';
32
import { config as _config } from './config';
43
const defaultConfig = Object.assign({}, _config.data);
54
const config = defaultConfig;
65

7-
export const login: api = {
6+
export const login: mock.api = {
87
// 转发
98
relay: '',
109
// 格式化请求结果
@@ -28,15 +27,15 @@ export const login: api = {
2827
message: '登录成功!'
2928
}
3029
}
31-
export const logout: api = {
30+
export const logout: mock.api = {
3231
format: (method, params, result, { body }) => {
3332
return Object.assign(body || {}, result)
3433
},
3534
post: {
3635
message: '退出成功!'
3736
}
3837
}
39-
export const relay: api = {
38+
export const relay: mock.api = {
4039
relay (method, params, result) {
4140
let key = /get/i.test(method) ? 'qs' : 'form'
4241
return {
@@ -48,7 +47,7 @@ export const relay: api = {
4847
}
4948
}
5049

51-
export const info: api = {
50+
export const info: mock.api = {
5251
error(method, params, result) {
5352
if (!params.username) {
5453
return '参数不足'
@@ -70,7 +69,7 @@ export const info: api = {
7069
data: {}
7170
}
7271
}
73-
export const settings: api = {
72+
export const settings: mock.api = {
7473
// 公开当前接口
7574
public: true,
7675
format(method, params, result) {

src/index.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import server from './server.js'
2-
/**
3-
* mock实例
4-
* @param option 配置
5-
*/
6-
function KsMock(option) {
7-
this.option = option
2+
class KsMock {
3+
option = {}
4+
constructor(option) {
5+
this.option = option
6+
}
7+
apply(compiler) {
8+
server(this.option)
9+
compiler.plugin("emit", function (compilation, callback) {
10+
callback()
11+
})
12+
}
13+
server(option) {
14+
server(option || this.option)
15+
}
816
}
9-
10-
KsMock.prototype.apply = function (compiler) {
11-
server(this.option)
12-
compiler.plugin("emit", function (compilation, callback) {
13-
callback()
14-
})
15-
}
16-
KsMock.prototype.server = function (option) {
17-
server(option || this.option)
18-
}
19-
2017
export default KsMock

src/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import rules from './rules';
66
import * as https from 'https';
77
import { Http, getInfo, formatResult, mockResult, authHandler, errorHandler, relayHandler, methodHandler } from './utils'
88
import * as fs from 'fs';
9+
import { mock } from '../';
910
const server = jsonServer.create()
1011
// 路径从根目录开始?
1112
const router = jsonServer.router(path.resolve(process.cwd(), 'db.json'))
@@ -14,7 +15,7 @@ const middlewares = jsonServer.defaults({
1415
})
1516

1617
server.use(middlewares)
17-
const createServer = (option: anyObject, callback?: Function) => {
18+
const createServer = (option: mock.anyObject, callback?: Function) => {
1819
let config = option.https
1920
config = /^(boolean|number)$/.test(typeof config) ? config && {} : config
2021
let currentServer
@@ -53,7 +54,7 @@ const createServer = (option: anyObject, callback?: Function) => {
5354
* @param {boolean=} option.crossDomain - 是否跨域 (便于在不设置请求头时, 快速配置跨域)
5455
* @param {number=} port - 服务器端口
5556
*/
56-
const Server = (option: anyObject, callback?: Function) => {
57+
const Server = (option: mock.anyObject, callback?: Function) => {
5758
option = Object.assign({
5859
port: 3030,
5960
crossDomain: true,

types/datas.d.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

types/index.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)