internal: add true-myth for result/maybe types, test maybe on getType() handler, disable /old page

This commit is contained in:
2024-05-09 01:21:37 -05:00
parent f67cdadde0
commit 3b9153fb97
5 changed files with 24 additions and 15 deletions

View File

@@ -20,6 +20,7 @@
"react-hook-form": "^7.42.1", "react-hook-form": "^7.42.1",
"react-ogp": "^0.0.3", "react-ogp": "^0.0.3",
"sass": "^1.57.1", "sass": "^1.57.1",
"true-myth": "^7.1.0",
"usehooks-ts": "^2.9.1", "usehooks-ts": "^2.9.1",
"zod": "^3.20.2" "zod": "^3.20.2"
}, },

View File

@@ -35,7 +35,7 @@ const useLookup = (warningHandler?: WarningHandler) => {
const uriType = useMemo<TargetType | "unknown">( const uriType = useMemo<TargetType | "unknown">(
function () { function () {
return getType(target) ?? "unknown"; return getType(target).unwrapOr("unknown");
}, },
[target] [target]
); );
@@ -143,21 +143,29 @@ const useLookup = (warningHandler?: WarningHandler) => {
const targetType = getType(target); const targetType = getType(target);
switch (targetType) { if (targetType.isNothing) {
throw new Error(
`The type could not be detected given the target (${JSON.stringify(
target
)}).`
);
}
switch (targetType.value) {
// Block scoped case to allow url const reuse // Block scoped case to allow url const reuse
case "ip4": { case "ip4": {
await loadBootstrap("ip4"); await loadBootstrap("ip4");
const url = getRegistryURL(targetType, target); const url = getRegistryURL(targetType.value, target);
return await getAndParse<IpNetwork>(url, IpNetworkSchema); return await getAndParse<IpNetwork>(url, IpNetworkSchema);
} }
case "ip6": { case "ip6": {
await loadBootstrap("ip6"); await loadBootstrap("ip6");
const url = getRegistryURL(targetType, target); const url = getRegistryURL(targetType.value, target);
return await getAndParse<IpNetwork>(url, IpNetworkSchema); return await getAndParse<IpNetwork>(url, IpNetworkSchema);
} }
case "domain": { case "domain": {
await loadBootstrap("domain"); await loadBootstrap("domain");
const url = getRegistryURL(targetType, target); const url = getRegistryURL(targetType.value, target);
if (url.startsWith("http://") && url != repeatableRef.current) { if (url.startsWith("http://") && url != repeatableRef.current) {
repeatableRef.current = url; repeatableRef.current = url;
@@ -171,15 +179,9 @@ const useLookup = (warningHandler?: WarningHandler) => {
} }
case "autnum": { case "autnum": {
await loadBootstrap("autnum"); await loadBootstrap("autnum");
const url = getRegistryURL(targetType, target); const url = getRegistryURL(targetType.value, target);
return await getAndParse<AutonomousNumber>(url, AutonomousNumberSchema); return await getAndParse<AutonomousNumber>(url, AutonomousNumberSchema);
} }
case null:
throw new Error(
`The type could not be detected given the target (${JSON.stringify(
target
)}).`
);
case "url": case "url":
case "tld": case "tld":
case "registrar": case "registrar":

View File

@@ -1,4 +1,5 @@
import type { TargetType } from "@/types"; import type { TargetType } from "@/types";
import { Maybe } from "true-myth";
// keeps track of the elements we've created so we can assign a unique ID // keeps track of the elements we've created so we can assign a unique ID
// let elementCounter = 123456; // let elementCounter = 123456;
@@ -773,9 +774,9 @@ const URIPatterns: [RegExp, TargetType][] = [
], ],
]; ];
export function getType(value: string): TargetType | null { export function getType(value: string): Maybe<TargetType> {
for (const [pattern, type] of URIPatterns) { for (const [pattern, type] of URIPatterns) {
if (pattern.test(value)) return type; if (pattern.test(value)) return Maybe.of(type);
} }
return null; return Maybe.nothing();
} }

View File

@@ -2312,6 +2312,11 @@ to-regex-range@^5.0.1:
dependencies: dependencies:
is-number "^7.0.0" is-number "^7.0.0"
"true-myth@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/true-myth/-/true-myth-7.1.0.tgz#79077b5f1848af1ea0c241234aec2c4d0c11b91a"
integrity sha512-DcdyFRHfNyG31HgNtxqrVAHmBf7y1w4YZCpU7IR2Ohytm4LITv+u075Vc3dLyK9cdEPq6K9398iao6YpkU8hHA==
tsconfig-paths@^3.14.1: tsconfig-paths@^3.14.1:
version "3.14.1" version "3.14.1"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a" resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"