UNPKG

node-ciscospark

Version:

Super-Simple Lightweight Javascript wrapper for Cisco Spark API

242 lines (211 loc) 7.2 kB
'use strict'; /** @ignore */ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var _request = require('request'); /** * Cisco Spark API Abstract Class * @abstract */ var CiscoSpark = function () { /** * Constructor for abstract class * @constructor * @param {?string} accessToken - Your Cisco Spark accesstoken * @param {?string} userAgent - User Agent request header * @param {?string} apiUrl - API Url */ function CiscoSpark(accessToken, userAgent, apiUrl) { _classCallCheck(this, CiscoSpark); if (accessToken instanceof CiscoSpark) { /** * @type {string} * @protected */ this.accessToken = accessToken.accessToken || process.env.CISCOSPARK_ACCESS_TOKEN; /** * @type {string} * @protected */ this.userAgent = userAgent || accessToken.userAgent || process.env.CISCOSPARK_USER_AGENT; /** * @type {string} * @protected */ this.apiUrl = apiUrl || accessToken.apiUrl; /** * @type {?requestCallback} * @private */ this._requestCallback = accessToken._requestCallback; } else { this.accessToken = accessToken || process.env.CISCOSPARK_ACCESS_TOKEN; this.userAgent = userAgent || process.env.CISCOSPARK_USER_AGENT; this.apiUrl = apiUrl; this._requestCallback = null; } /** * @type {string} * @protected */ this.idName = 'id'; } /** * @external {HttpResponse} https://nodejs.org/api/http.html#http_class_http_serverresponse */ /** * Request Callback * @typedef {function} requestCallback * @param {Error} error - Error object * @param {Object} body - Response body * @param {HttpResponse} response - Http Server Response */ /** * Make a request * @param {Object} options - Options for Request * @param {requestCallback} callback - Callback * @protected */ _createClass(CiscoSpark, [{ key: 'request', value: function request(options, callback) { if (!this.apiUrl) return callback(new Error('ApiUrl is not set')); if (!options.url && this.apiUrl) options.url = this.apiUrl; if (!options.headers) options.headers = {}; options.headers.Authorization = 'Bearer ' + this.accessToken; if (this.userAgent) options.headers['User-Agent'] = this.userAgent; options.headers['Accept'] = 'application/json'; if (typeof this._requestCallback === 'function') return this._requestCallback(options, callback); return _request(options, function (error, response, body) { callback(error, body, response); }); } /** * Check Id * @abstract * @param {string} id - Id string to be checked * @return {boolean} - Is Id valid? */ }, { key: 'checkId', value: function checkId(id) { return id && (typeof id === 'string' || typeof id === 'number'); } /** * List objects * @param {Object} params - Parameters of request * @param {requestCallback} callback */ }, { key: 'list', value: function list(params, callback) { var args = this.getArgs(params, callback); this.request({ method: 'GET', qs: args.params }, args.callback); } /** * Create an object * @param {Object} params - Parameters of request * @param {requestCallback} callback */ }, { key: 'create', value: function create(params, callback) { var args = this.getArgs(params, callback); if (!args.params) return callback(new Error('Invalid Params.')); this.request({ method: 'POST', form: args.params }, args.callback); } /** * Get an object * @param {string} id - Id of the object * @param {requestCallback} callback */ }, { key: 'get', value: function get(id, callback) { var args = this.getArgs(id, callback); if (!this.checkId(args.id)) return callback(new Error('ID is missing or in the wrong format')); this.request({ method: 'GET', url: this.apiUrl + '/' + args.id }, args.callback); } /** * Update an object * @param {string} id - Id of the object * @param {Object} params - Parameters of request * @param {requestCallback} callback */ }, { key: 'update', value: function update(id, params, callback) { var args = this.getArgs(id, params, callback); if (!this.checkId(args.id)) return callback(new Error('ID is missing or in the wrong format')); this.request({ method: 'PUT', url: this.apiUrl + '/' + args.id, json: args.params }, args.callback); } /** * Delete an Object * @param {string} id - Id of the object * @param {requestCallback} callback */ }, { key: 'delete', value: function _delete(id, callback) { var args = this.getArgs(id, callback); if (!this.checkId(args.id)) return callback(new Error('ID is missing or in the wrong format')); this.request({ method: 'DELETE', url: this.apiUrl + '/' + args.id }, args.callback); } /** * @private */ }, { key: 'getArgs', value: function getArgs() { var result = { id: null, params: null, callback: null }; for (var i = 0; i < arguments.length; ++i) { if (typeof arguments[i] === 'function') { result.callback = arguments[i]; } else if (typeof arguments[i] === 'string' || typeof arguments[i] === 'number') { result.id = arguments[i]; } else if (arguments[i] !== null && _typeof(arguments[i]) === 'object') { result.params = arguments[i]; } } if (result.id === null && result.params) { if (result.params[this.idName]) { result.id = result.params[this.idName]; } else if (result.params.id) { result.id = result.params.id; } } return result; } /** * @type {requestCallback} */ }, { key: 'requestCallback', set: function set(callback) { this._requestCallback = callback; } }]); return CiscoSpark; }(); module.exports = CiscoSpark;