All files / src/utils url.ts

73.33% Statements 11/15
20% Branches 1/5
100% Functions 3/3
73.33% Lines 11/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 4733x               33x 36x 36x       36x     36x                       33x 36x                 33x 36x       36x    
import { BeeArgumentError } from './error'
 
/**
 * Validates that passed string is valid URL of Bee.
 * We support only HTTP and HTTPS protocols.
 *
 * @param url
 */
export function isValidBeeUrl(url: unknown): url is URL {
  try {
    Iif (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
  }
}
 
/**
 * 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
 */
export function assertBeeUrl(url: unknown): asserts url is URL {
  Iif (!isValidBeeUrl(url)) {
    throw new BeeArgumentError('URL is not valid!', url)
  }
}
 
/**
 * Removes trailing slash out of the given string.
 * @param url
 */
export function stripLastSlash(url: string): string {
  Iif (url.endsWith('/')) {
    return url.slice(0, -1)
  }
 
  return url
}