UNPKG

@ethersphere/bee-js

Version:
48 lines (47 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stripLastSlash = exports.assertBeeUrl = exports.isValidBeeUrl = void 0; const error_1 = require("./error"); /** * Validates that passed string is valid URL of Bee. * We support only HTTP and HTTPS protocols. * * @param url */ function isValidBeeUrl(url) { try { if (typeof url !== 'string') { return false; } const urlObject = new URL(url); // There can be wide range of protocols passed. return urlObject.protocol === 'http:' || urlObject.protocol === 'https:'; } catch (e) { return false; } } exports.isValidBeeUrl = isValidBeeUrl; /** * Validates that passed string is valid URL of Bee, if not it throws BeeArgumentError. * We support only HTTP and HTTPS protocols. * @param url * @throws BeeArgumentError if non valid URL */ function assertBeeUrl(url) { if (!isValidBeeUrl(url)) { throw new error_1.BeeArgumentError('URL is not valid!', url); } } exports.assertBeeUrl = assertBeeUrl; /** * Removes trailing slash out of the given string. * @param url */ function stripLastSlash(url) { if (url.endsWith('/')) { return url.slice(0, -1); } return url; } exports.stripLastSlash = stripLastSlash;