import { Assert } from '@rsdk/common'; import type { PropertyParser } from '../../types'; import { InvalidProtocol, InvalidUrl } from './exception'; export class UrlParser implements PropertyParser { private readonly protocols: string[]; constructor(...protocols: string[]) { this.protocols = protocols; } type(): string { return 'URL'; } description(): string { return 'Parses string into instance of URL'; } parse(value: unknown): URL { Assert.isString(value); try { const url = new URL(value); this.assertProtocol(url); return url; } catch (error) { throw new InvalidUrl(value, error); } } private assertProtocol(url: URL): void { if (this.protocols.length === 0) { return; } if (!this.protocols.includes(url.protocol)) { throw new InvalidProtocol(this.protocols, url.protocol, url); } } }