box-node-sdk
Version:
Official SDK for Box Plaform APIs
36 lines • 1.24 kB
JavaScript
;
/**
* @fileoverview URL Path Builder
*/
// ------------------------------------------------------------------------------
// Private
// ------------------------------------------------------------------------------
// Pattern to check for relative paths
const PATTERN = /\/\.+/;
/**
* remove leading & trailing slashes from some string. This is useful for
* removing slashes from the path segments that are actually a part of the
* path itself. Without this step, these slashes would be uri-encoded.
*
* @param {string} segment The path segment (ex: '/users')
* @returns {string} The path segment with slashes trimmed (ex: 'users')
* @private
*/
function trimSlashes(segment) {
return segment.replace(/^\/|\/$/g, '');
}
module.exports = function urlPath(...args) {
const path = args
.map((x) => String(x))
.map((x) => {
var trimmedX = trimSlashes(x);
if (PATTERN.test(trimmedX)) {
throw new Error(`An invalid path parameter exists in ${trimmedX}. Relative path parameters cannot be passed.`);
}
return trimmedX;
})
.map((x) => encodeURIComponent(x))
.join('/');
return `/${path}`;
};
//# sourceMappingURL=url-path.js.map