forked from TencentCloudBase/tcb-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (81 loc) · 1.9 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const storage = require("./src/storage");
const database = require("./src/db").Db;
const functions = require("./src/functions");
function Tcb() {
this.config = {
get secretId() {
return this._secretId
? this._secretId
: process.env.TENCENTCLOUD_SECRETID;
},
set secretId(id) {
this._secretId = id;
},
get secretKey() {
return this._secretKey
? this._secretKey
: process.env.TENCENTCLOUD_SECRETKEY;
},
set secretKey(key) {
this._secretKey = key;
},
get sessionToken() {
if (this._sessionToken === undefined) {
//默认临时密钥
return process.env.TENCENTCLOUD_SESSIONTOKEN;
} else if (this._sessionToken === false) {
//固定秘钥
return undefined;
} else {
//传入的临时密钥
return this._sessionToken;
}
},
set sessionToken(token) {
this._sessionToken = token;
},
envName: undefined,
proxy: undefined
};
}
Tcb.prototype.init = function ({
secretId,
secretKey,
sessionToken,
env,
proxy
}) {
if ((secretId && !secretKey) || (!secretId && secretKey)) {
throw Error("secretId and secretKey must be a pair");
}
if (secretId) {
this.config.secretId = secretId;
}
if (secretKey) {
this.config.secretKey = secretKey;
}
if (secretId && secretKey) {
this.config.sessionToken = sessionToken ? sessionToken : false;
}
env && (this.config.envName = env);
proxy && (this.config.proxy = proxy);
};
Tcb.prototype.database = function () {
return new database(this);
};
function each(obj, fn) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
fn(obj[i], i);
}
}
}
function extend(target, source) {
each(source, function (val, key) {
target[key] = source[key];
});
return target;
}
extend(Tcb.prototype, functions);
extend(Tcb.prototype, storage);
module.exports = new Tcb();