From 3b9153fb975defb9da551b8fb00a75c5213e0cbe Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 9 May 2024 01:21:37 -0500 Subject: [PATCH] internal: add true-myth for result/maybe types, test maybe on getType() handler, disable /old page --- package.json | 1 + src/hooks/useLookup.tsx | 26 +++++++++++++------------ src/pages/{old.tsx => old.tsx.disabled} | 0 src/rdap.ts | 7 ++++--- yarn.lock | 5 +++++ 5 files changed, 24 insertions(+), 15 deletions(-) rename src/pages/{old.tsx => old.tsx.disabled} (100%) diff --git a/package.json b/package.json index e59b7ee..cadce1c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "react-hook-form": "^7.42.1", "react-ogp": "^0.0.3", "sass": "^1.57.1", + "true-myth": "^7.1.0", "usehooks-ts": "^2.9.1", "zod": "^3.20.2" }, diff --git a/src/hooks/useLookup.tsx b/src/hooks/useLookup.tsx index ba3c32e..965e99f 100644 --- a/src/hooks/useLookup.tsx +++ b/src/hooks/useLookup.tsx @@ -35,7 +35,7 @@ const useLookup = (warningHandler?: WarningHandler) => { const uriType = useMemo( function () { - return getType(target) ?? "unknown"; + return getType(target).unwrapOr("unknown"); }, [target] ); @@ -143,21 +143,29 @@ const useLookup = (warningHandler?: WarningHandler) => { 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 case "ip4": { await loadBootstrap("ip4"); - const url = getRegistryURL(targetType, target); + const url = getRegistryURL(targetType.value, target); return await getAndParse(url, IpNetworkSchema); } case "ip6": { await loadBootstrap("ip6"); - const url = getRegistryURL(targetType, target); + const url = getRegistryURL(targetType.value, target); return await getAndParse(url, IpNetworkSchema); } case "domain": { await loadBootstrap("domain"); - const url = getRegistryURL(targetType, target); + const url = getRegistryURL(targetType.value, target); if (url.startsWith("http://") && url != repeatableRef.current) { repeatableRef.current = url; @@ -171,15 +179,9 @@ const useLookup = (warningHandler?: WarningHandler) => { } case "autnum": { await loadBootstrap("autnum"); - const url = getRegistryURL(targetType, target); + const url = getRegistryURL(targetType.value, target); return await getAndParse(url, AutonomousNumberSchema); } - case null: - throw new Error( - `The type could not be detected given the target (${JSON.stringify( - target - )}).` - ); case "url": case "tld": case "registrar": diff --git a/src/pages/old.tsx b/src/pages/old.tsx.disabled similarity index 100% rename from src/pages/old.tsx rename to src/pages/old.tsx.disabled diff --git a/src/rdap.ts b/src/rdap.ts index 6cbad4a..acc43ab 100644 --- a/src/rdap.ts +++ b/src/rdap.ts @@ -1,4 +1,5 @@ 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 // let elementCounter = 123456; @@ -773,9 +774,9 @@ const URIPatterns: [RegExp, TargetType][] = [ ], ]; -export function getType(value: string): TargetType | null { +export function getType(value: string): Maybe { 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(); } diff --git a/yarn.lock b/yarn.lock index c046b7f..9a03b92 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2312,6 +2312,11 @@ to-regex-range@^5.0.1: dependencies: 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: version "3.14.1" resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz#ba0734599e8ea36c862798e920bcf163277b137a"