API reference › @evolu/common › Type › HttpUrlFromString
const HttpUrlFromString: transform(
"HttpUrlFromString",
String,
HttpUrl,
{
from: (value: string): Result<string, HttpUrlFromStringError> => {
const canonical = canonicalizeUrl(value);
return canonical !== undefined && isHttpUrl(canonical)
? ok(canonical)
: err({ type: "HttpUrlFromString", value });
},
to: (value: HttpUrl) => value,
},
formatHttpUrlFromStringError,
);
Defined in: packages/common/src/Type.ts:9867
Transforms an HTTP or HTTPS URL into its canonical HttpUrl.
Accepts what UrlFromString accepts with the http: or https:
scheme. Encoding returns the canonical spelling, not the original one.
Example
import {
assertEqual,
assertErr,
assertOk,
HttpUrlFromString,
} from "@evolu/common";
assertOk(
HttpUrlFromString.fromUnknown("HTTPS://Example.com"),
"https://example.com/",
);
const invalid = HttpUrlFromString.fromUnknown("example.com");
assertErr(invalid, { type: "HttpUrlFromString", value: "example.com" });
assertEqual(
HttpUrlFromString.formatError(invalid.error),
'The value "example.com" is not a valid HTTP or HTTPS URL.',
);