UNPKG

@chatopera/store

Version:

Chatopera Store 是证书商店,快速接入代码实现软件的计费、发票、证书管理。https://store.chatopera.com

95 lines (82 loc) 2.96 kB
/** * Chatopera Store */ const debug = require("debug")("store:index"); const axios = require("axios"); const utils = require("./lib/utils"); const { StoreValueError } = require("./lib/exceptions"); const _ = require("lodash"); // set SDK name const SDK_NAME = "nodejs"; class Store { constructor(serverinstId, serviceName, provider = "https://store.chatopera.com") { this.serverinstId = _.trim(serverinstId); this.provider = provider.endsWith("/") ? utils.replaceLastOccurrence(provider, "/", "") : provider; debug("[constructor] provider %s, serverinstId %s", this.provider, this.serverinstId); this.serviceName = serviceName; if (!this.serverinstId) throw new StoreValueError("Invalid serverinstId value"); } /** * 获得证书配额余量信息 * @param {String} licenseId * @returns Promise */ getLicenseQuotaInfo(licenseId) { return new Promise((resolve, reject) => { axios.get(`${this.provider}/api/quotawd/license/${licenseId}`).then((response) => { if (response.status === 200) { resolve(response.data); } else { reject({ error: "invalid response status" }) } }, (err) => { console.error(err); reject(err); }); }); } /** * 发送配额操作记录 * @param {String} licenseId * @returns Promise */ writeQuotaWd(licenseId, consumes) { let consumesVal = null; if (_.isArray(consumes)) { consumesVal = []; for (let x of consumes) { consumesVal.push(_.toInteger(x)); } } else if (_.isNumber(consumes)) { consumesVal = [consumes]; } else if (_.isString(consumes)) { consumesVal = [_.toInteger(consumes)]; } else { throw new StoreValueError("Invalid consumes, set it as Integer or Integer array."); } return new Promise((resolve, reject) => { axios.post(`${this.provider}/api/quotawd/write`, { "licenseId": licenseId, "serverinstId": this.serverinstId, "servicename": this.serviceName, "consumes": consumesVal, "requestlangorsdk": SDK_NAME }).then((response) => { if (response.status === 200) { resolve(response.data); } else { reject({ error: "invalid response status" }) } }, (err) => { console.error(err); reject(err); }); }) } } module.exports = exports = Store;