UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

53 lines (52 loc) 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var PathParameter = /** @class */ (function () { function PathParameter() { } PathParameter.fillParam = function (path, key, value) { var map = {}; map[key] = value; return PathParameter.fillParams(path, map); }; /** * Same as fillParams but will throw an error if some parameters has not been replaced */ PathParameter.fillAllParams = function (path, pathParameters) { path = PathParameter.fillParams(path, pathParameters); var remainingArgs = PathParameter.extractParams(path); if (remainingArgs.length > 0) { throw new Error("Bad request. Some parameter have not been substitute: " + remainingArgs); } return path; }; PathParameter.fillParams = function (path, mapping) { for (var key in mapping) { var match = '{' + key + '}'; if (path.indexOf(match) >= 0) { path = path.replace(match, mapping[key].toString()); } else { throw new Error("Parameter " + key + " does not exist in path: " + path); } } return path; }; /** * * @param path * @returns names of path parameters */ PathParameter.extractParams = function (path) { var found = [], rxp = PathParameter.PARAMETER_REGEX, curMatch; while (curMatch = rxp.exec(path)) { found.push(curMatch[1]); } return found; }; PathParameter.hasParams = function (path) { return PathParameter.extractParams(path).length > 0; }; PathParameter.PARAMETER_REGEX = /{([^}]+)}/g; return PathParameter; }()); exports.PathParameter = PathParameter;