mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-09 13:51:57 -04:00

enforce trailing commas as this is the norm in the frontend and makes diffs better readable Signed-off-by: Philip Molares <philip.molares@udo.edu>
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2025 The HedgeDoc developers (see AUTHORS file)
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
import { NoSubdirectoryAllowedError, WrongProtocolError } from './errors.js'
|
|
import { Optional } from '@mrdrogdrog/optional'
|
|
|
|
/**
|
|
* Parses the given string as URL
|
|
*
|
|
* @param {String | undefined} url the raw url
|
|
* @return An {@link Optional} that contains the parsed URL or is empty if the raw value isn't a valid URL
|
|
* @throws WrongProtocolError if the protocol of the URL isn't either http nor https
|
|
* @throws NoSubdirectoryAllowedError if the URL has a path that doesn't end with a trailing slash
|
|
*/
|
|
export function parseUrl(url: string | undefined): Optional<URL> {
|
|
return createOptionalUrl(url)
|
|
.guard(
|
|
(value) => value.protocol === 'https:' || value.protocol === 'http:',
|
|
() => new WrongProtocolError(),
|
|
)
|
|
.guard(
|
|
(value) => value.pathname === '/',
|
|
() => new NoSubdirectoryAllowedError(),
|
|
)
|
|
}
|
|
|
|
function createOptionalUrl(url: string | undefined): Optional<URL> {
|
|
try {
|
|
return Optional.ofNullable(url).map((value) => new URL(value))
|
|
} catch {
|
|
return Optional.empty()
|
|
}
|
|
}
|