UNPKG

rest-methods

Version:

Declaratively publish functions for remote invocation.

143 lines (116 loc) 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } var _lodash = require("lodash"); var _lodash2 = _interopRequireDefault(_lodash); var _pageJs = require("./page-js"); var _pageJs2 = _interopRequireDefault(_pageJs); var _jsUtil = require("js-util"); /** * Constructs a URL to a method. * @param {string} name: The unique name of the method. * @param {string} host: The host-name of the remote server. * @param {string} pattern: The URL route pattern. * @param {array} args: An array of arguments. */ var _jsUtil2 = _interopRequireDefault(_jsUtil); var getMethodUrl = function getMethodUrl(methodName, host, route, args) { args = _lodash2["default"].flatten(args); var url = route.path; // Convert arguments into URL. var urlParams = route.keys.map(function (item) { return item.name; }); if (urlParams.length > 0) { (function () { // Ensure enough arguments were passed. if (args.length < urlParams.length) { throw new Error("Not enough arguments for method '" + methodName + "'. The URL (" + url + ") requires " + urlParams.length + ". Given args: [" + args + "]."); } // Construct the string. var i = 0; urlParams.forEach(function (key) { key = ":" + key; var index = url.indexOf(key); url = "" + url.substr(0, index) + args[i] + url.substring(index + key.length, url.length); i += 1; }); })(); } // Prepend the host (if one exists). // NOTE: This is only required when doing server-to-server communications. if (host) { url = host + url; } // Finish up. return url; }; /** * Extracts the parameters from a URL. * * For a URL pattern of "/foo/:id/edit?skip=:skip" * the URL of "/foo/abc/edit?skip=10" * would produce an: * - an [id] of "abc" * - a [skip] or 10. * * @param {object} route: The route definition to match with. * @param {string} url: The URL path to examine. * @return {array} of string values. */ exports.getMethodUrl = getMethodUrl; var getUrlParams = function getUrlParams(route, url) { var result = []; if (_lodash2["default"].isString(url)) { (function () { var _url$split = url.split("?"); var _url$split2 = _slicedToArray(_url$split, 2); var path = _url$split2[0]; var queryString = _url$split2[1]; var context = new _pageJs2["default"].Context(path); // Perform reg-ex matching. // NB: Only the path (not the query-string) is used to match. // This prevents an error where page-js does not spot the path variables // in the presence of a query-string. route = new _pageJs2["default"].Route(route.path.split("?")[0]); route.match(context.path, context.params); var params = context.params; // Extract params from the query-string. if (!_lodash2["default"].isEmpty(queryString)) { queryString.split("&").forEach(function (item) { var _item$split = item.split("="); var _item$split2 = _slicedToArray(_item$split, 2); var key = _item$split2[0]; var value = _item$split2[1]; if (!_lodash2["default"].isUndefined(value)) { params[key] = value; } }); } // Process the params into the return array. _lodash2["default"].keys(params).forEach(function (key) { var value = params[key]; if (_jsUtil2["default"].isNumeric(value)) { // Convert to number. value = parseFloat(value); } else { // Convert to boolean. switch (value) { case "true": value = true;break; case "false": value = false;break; } } result[key] = value; result.push(value); }); })(); } return result; }; exports.getUrlParams = getUrlParams; // Split the URL into the [path] and [query-string].