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)

155 lines (125 loc) 5.2 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _util = require('../../util'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Convenience wrapper for HTTP service (not meant to be instantiated directly, * use `.service('http', '<service-name>')` on a {@link StitchClient} instance). * * @class * @return {HTTPService} a HTTPService instance. */ var HTTPService = function () { function HTTPService(client, serviceName) { _classCallCheck(this, HTTPService); this.client = client; this.serviceName = serviceName; } /** * Send a GET request to a resource (result must be application/json) * * @param {String|Object} urlOrOptions the url to request, or an object of GET args * @param {Object} [options] optional settings for the GET operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ _createClass(HTTPService, [{ key: 'get', value: function get(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('get', this, buildArgs(urlOrOptions, options)); } /** * Send a POST request to a resource with payload * * @param {String|Object} urlOrOptions the url to request, or an object of POST args * @param {Object} [options] optional settings for the POST operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ }, { key: 'post', value: function post(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('post', this, buildArgs(urlOrOptions, options)); } /** * Send a PUT request to a resource with payload * * @param {String|Object} urlOrOptions the url to request, or an object of PUT args * @param {Object} [options] optional settings for the PUT operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ }, { key: 'put', value: function put(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('put', this, buildArgs(urlOrOptions, options)); } /** * Send a PATCH request to a resource with payload * * @param {String|Object} urlOrOptions the url to request, or an object of PATCH args * @param {Object} [options] optional settings for the PATCH operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ }, { key: 'patch', value: function patch(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('patch', this, buildArgs(urlOrOptions, options)); } /** * Send a DELETE request to a resource * * @param {String|Object} urlOrOptions the url to request, or an object of DELETE args * @param {Object} [options] optional settings for the DELETE operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ }, { key: 'delete', value: function _delete(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('delete', this, buildArgs(urlOrOptions, options)); } /** * Send a HEAD request to a resource * * @param {String|Object} urlOrOptions the url to request, or an object of HEAD args * @param {Object} [options] optional settings for the HEAD operation * @param {String} [options.authUrl] url that grants a cookie * @return {Promise} */ }, { key: 'head', value: function head(urlOrOptions) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return buildResponse('head', this, buildArgs(urlOrOptions, options)); } }]); return HTTPService; }(); function buildArgs(urlOrOptions, options) { var args = void 0; if (typeof urlOrOptions !== 'string') { args = urlOrOptions; } else { args = { url: urlOrOptions }; if (!!options.authUrl) args.authUrl = options.authUrl; } return args; } function buildResponse(action, service, args) { return (0, _util.serviceResponse)(service, { action: action, args: args }); } exports.default = HTTPService; module.exports = exports['default'];