diff --git a/src/lib/whois.ts b/src/lib/whois.ts index f04956f..503d675 100644 --- a/src/lib/whois.ts +++ b/src/lib/whois.ts @@ -1,15 +1,38 @@ +import type { + WhoIsResponse, + WhoIsData, + WhoIsEvent, + WhoIsIpAddresses, +} from "@/types"; + +function checkEvents(events: WhoIsEvent[]): boolean { + for (const event of events) { + if (event.eventAction === "registration" && event.eventDate === "") { + return true; + } + } + return false; +} + +function checkIpAddresses(ipAddresses: WhoIsIpAddresses): boolean { + return ipAddresses.v4.length === 0 && ipAddresses.v6.length === 0; +} + +function checkWhoIsData(data: WhoIsData): boolean { + return ( + checkEvents(data.events) && + checkIpAddresses(data.ipAddresses) && + data.nameservers.length === 0 + ); +} + export async function isDomainAvailable(domain: string): Promise { try { const response = await fetch( `https://whois.klemek.fr/api/lookup/${domain}`, ); - const content = await response.json(); - for (const event of content?.data?.events ?? []) { - if (event?.eventAction === "registration" && event?.eventDate === "") { - return true; - } - } - return false; + const content: WhoIsResponse = await response.json(); + return checkWhoIsData(content.data); } catch { return false; } diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..e7136e3 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,33 @@ +export interface WhoIsResponse { + type: string; + data: WhoIsData; +} + +export interface WhoIsData { + ldhName: string; + handle: string; + status: string[]; + ipAddresses: WhoIsIpAddresses; + events: WhoIsEvent[]; + nameservers: WhoIsNameserver[]; + entities: WhoIsEntity[]; +} + +export interface WhoIsIpAddresses { + v4: string[]; + v6: string[]; +} + +export interface WhoIsEvent { + eventAction: string; + eventDate: string; +} + +export interface WhoIsNameserver { + ldhName: string; +} + +export interface WhoIsEntity { + roles: string[]; + vcardArray: [string, [string, object, string, string][]]; +}