UNPKG

mongodb-stitch

Version:

[![Join the chat at https://gitter.im/mongodb/stitch](https://badges.gitter.im/mongodb/stitch.svg)](https://gitter.im/mongodb/stitch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

57 lines (46 loc) 1.82 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.uriEncodeObject = exports.serviceResponse = undefined; var _Base = require('Base64'); var base64 = _interopRequireWildcard(_Base); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } /** * @namespace util * @private */ /** * Utility method for executing a service action as a function call. * * @memberof util * @param {Object} service the service to execute the action on * @param {String} action the service action to execute * @param {Array} args the arguments to supply to the service action invocation * @returns {Promise} the API response from the executed service action */ function serviceResponse(service, _ref) { var _ref$serviceName = _ref.serviceName, serviceName = _ref$serviceName === undefined ? service.serviceName : _ref$serviceName, action = _ref.action, args = _ref.args; var client = service.client; if (!client) { throw new Error('Service has no client'); } return client.executeServiceFunction(serviceName, action, args); } /** * Utility function to encode a JSON object into a valid string that can be * inserted in a URI. The object is first stringified, then encoded in base64, * and finally encoded via the builtin encodeURIComponent function. * * @memberof util * @param {Object} obj The object to encode * @returns {String} The encoded object */ function uriEncodeObject(obj) { return encodeURIComponent(base64.btoa(JSON.stringify(obj))); } exports.serviceResponse = serviceResponse; exports.uriEncodeObject = uriEncodeObject;