All files / tcb-admin-node index.js

91.43% Statements 32/35
68% Branches 17/25
100% Functions 6/6
91.43% Lines 32/35
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 8914x 14x 14x 14x       30x     14x             16x       16x   148x         16x     148x         16x     74x     74x   74x             16x           16x 16x 16x   16x     14x 9x       42x 84x 84x           42x 84x   42x     14x 14x 14x   14x  
const storage = require("./src/storage");
const database = require("./src/db").Db;
const functions = require("./src/functions");
const wx = require("./src/wx");
 
function Tcb(config) {
  // console.log(config)
  this.config = config ? config : this.config
}
 
Tcb.prototype.init = function ({
  secretId,
  secretKey,
  sessionToken,
  env,
  proxy
} = {}) {
  Iif ((secretId && !secretKey) || (!secretId && secretKey)) {
    throw Error("secretId and secretKey must be a pair");
  }
 
  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() {
      Iif (this._sessionToken === undefined) {
        //默认临时密钥
        return process.env.TENCENTCLOUD_SESSIONTOKEN;
      } else Eif (this._sessionToken === false) {
        //固定秘钥
        return undefined;
      } else {
        //传入的临时密钥
        return this._sessionToken;
      }
    },
    set sessionToken(token) {
      this._sessionToken = token;
    },
    envName: env,
    proxy: proxy
  };
 
  this.config.secretId = secretId;
  this.config.secretKey = secretKey;
  this.config.sessionToken = sessionToken ? sessionToken : (secretId && secretKey ? false : undefined);
 
  return new Tcb(this.config);
};
 
Tcb.prototype.database = function () {
  return new database(this);
};
 
function each(obj, fn) {
  for (var i in obj) {
    Eif (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);
extend(Tcb.prototype, wx)
 
module.exports = new Tcb();