-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
41 lines (41 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import method from './src/lib/method.js';
import parameter from './src/lib/parameter.js';
import mixin from './src/lib/mixin.js';
import intercept from './src/lib/intercept.js';
import CopyProxy from './src/CopyProxy'
import getFinally from './utils/finally'
getFinally();
/**
* options:Object,默认Object,设置默认的request参数
* proxt:Boolean,默认为true,是否将wx的所有api都封装成Promise
*/
export default function Api(options = {}, proxy = true) {
//默认为get
function api(url, data) {
return this.method({ url, data, method: api.defaults.method })
}
//导入method方法
method(api)
//设置默认参数
parameter(api)
//混入默认配置
mixin(api)
//导入拦截方法
intercept(api)
/**
*options 设置defaults
*/
api.build = function (options, proxy) {
let cp = new CopyProxy(this);
let obj = cp.clone()
if (proxy)
cp.make(obj, 'wx', key => (url, data) => this.method({ url, data, api: key }));
obj.options(options)
obj.create = Api;
return obj
}
api.options = function (options) {
Object.keys(options).forEach(val => this.defaults[val] = options[val])
}
return api.build(options, proxy)
}