API reference › @evolu/common › Type › UrlFromString
const UrlFromString: transform(
"UrlFromString",
String,
Url,
{
from: (value: string): Result<string, UrlFromStringError> => {
const canonical = canonicalizeUrl(value);
return canonical === undefined
? err({ type: "UrlFromString", value })
: ok(canonical);
},
to: (value: Url) => value,
},
formatUrlFromStringError,
);
Defined in: packages/common/src/Type.ts:9764
Transforms an absolute URL into its canonical Url.
Accepts what the platform's URL parses without a base URL, as Url
describes, and returns its href. Encoding returns the canonical spelling,
not the original one.
Example
import { assertEqual, assertErr, assertOk, UrlFromString } from "@evolu/common";
// Lowercases the scheme and host, removes 443, the default port of https:,
// and resolves /a/../b to /b.
assertOk(
UrlFromString.fromUnknown("HTTPS://Example.com:443/a/../b"),
"https://example.com/b",
);
const invalid = UrlFromString.fromUnknown("/b");
assertErr(invalid, { type: "UrlFromString", value: "/b" });
assertEqual(
UrlFromString.formatError(invalid.error),
'The value "/b" is not a valid URL.',
);