-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load WHOIS preview through API route 👀
- Loading branch information
1 parent
cf1a7c5
commit 5d6b721
Showing
4 changed files
with
118 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import whoiser, { WhoisSearchResult } from 'whoiser'; | ||
|
||
import isValidDomain from '@/utils/isValidDomain'; | ||
|
||
export type WhoisSummaryResponse = { | ||
registrar: string | null; | ||
createdAt: string | null; | ||
dnssec: string | null; | ||
}; | ||
|
||
export type WhoisSummaryErrorResponse = { error: true; message: string }; | ||
|
||
const getSummary = async (domain: string): Promise<WhoisSummaryResponse> => { | ||
// TODO Allow resolving for TLDs | ||
if (!isValidDomain(domain)) { | ||
return { | ||
registrar: null, | ||
createdAt: null, | ||
dnssec: null, | ||
}; | ||
} | ||
|
||
try { | ||
const results = await whoiser(domain, { | ||
timeout: 5000, | ||
}); | ||
|
||
const resultsKey = Object.keys(results).find( | ||
// @ts-expect-error | ||
(key) => !('error' in results[key]) | ||
); | ||
if (!resultsKey) { | ||
throw new Error('No valid results found for domain ' + domain); | ||
} | ||
const firstResult = results[resultsKey] as WhoisSearchResult; | ||
|
||
return { | ||
registrar: firstResult['Registrar']?.toString(), | ||
createdAt: | ||
firstResult && 'Created Date' in firstResult | ||
? new Date(firstResult['Created Date'].toString()).toLocaleDateString( | ||
'en-US' | ||
) | ||
: null, | ||
dnssec: firstResult['DNSSEC']?.toString(), | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
return { | ||
registrar: null, | ||
createdAt: null, | ||
dnssec: null, | ||
}; | ||
} | ||
}; | ||
|
||
export async function GET( | ||
_request: Request, | ||
{ params }: { params: { domain: string } } | ||
) { | ||
if (!params.domain) { | ||
return Response.json( | ||
{ error: true, message: 'No domain provided' }, | ||
{ | ||
status: 400, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
); | ||
} | ||
|
||
try { | ||
const summary = await getSummary(params.domain); | ||
return Response.json(summary); | ||
} catch (error) { | ||
return Response.json( | ||
{ error: true, message: 'Error fetching whois summary' }, | ||
{ | ||
status: 500, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,49 @@ | ||
import { FC } from 'react'; | ||
import whoiser, { type WhoisSearchResult } from 'whoiser'; | ||
'use client'; | ||
|
||
import isValidDomain from '@/utils/isValidDomain'; | ||
import type { FC } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import { WhoisSummaryResponse } from '@/app/lookup/[domain]/whois-summary/route'; | ||
|
||
import { Skeleton } from './ui/skeleton'; | ||
|
||
type WhoisQuickInfoProps = { | ||
domain: string; | ||
}; | ||
|
||
const getSummary = async ( | ||
domain: string | ||
): Promise<{ registrar: string; createdAt: string; dnssec: string }> => { | ||
// TODO Allow resolving for TLDs | ||
if (!isValidDomain(domain)) { | ||
return { | ||
registrar: 'Unavailable', | ||
createdAt: 'Unavailable', | ||
dnssec: 'Unavailable', | ||
}; | ||
} | ||
|
||
try { | ||
const results = await whoiser(domain, { | ||
timeout: 5000, | ||
}); | ||
|
||
const resultsKey = Object.keys(results).find( | ||
// @ts-expect-error | ||
(key) => !('error' in results[key]) | ||
); | ||
if (!resultsKey) { | ||
throw new Error('No valid results found for domain ' + domain); | ||
} | ||
const firstResult = results[resultsKey] as WhoisSearchResult; | ||
|
||
return { | ||
registrar: firstResult['Registrar']?.toString() || 'Unavailable', | ||
createdAt: | ||
firstResult && 'Created Date' in firstResult | ||
? new Date(firstResult['Created Date'].toString()).toLocaleDateString( | ||
'en-US' | ||
) | ||
: 'Unavailable', | ||
dnssec: firstResult['DNSSEC']?.toString() || 'Unavailable', | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
return { | ||
registrar: 'Unavailable', | ||
createdAt: 'Unavailable', | ||
dnssec: 'Unavailable', | ||
}; | ||
} | ||
}; | ||
|
||
const WhoisQuickInfo: FC<WhoisQuickInfoProps> = async ({ domain }) => { | ||
const results = await getSummary(domain); | ||
const WhoisQuickInfo: FC<WhoisQuickInfoProps> = ({ domain }) => { | ||
const { data, isLoading } = useSWR<WhoisSummaryResponse>( | ||
`/lookup/${domain}/whois-summary` | ||
); | ||
|
||
return ( | ||
<div className="my-8 flex gap-8"> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">Registrar</h3> | ||
<p className="text-sm">{results.registrar}</p> | ||
{isLoading ? ( | ||
<Skeleton className="mt-1 h-4 w-24 rounded-sm" /> | ||
) : ( | ||
<p className="text-sm">{data?.registrar || 'Unavailable'}</p> | ||
)} | ||
</div> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">Creation Date</h3> | ||
<p className="text-sm">{results.createdAt}</p> | ||
{isLoading ? ( | ||
<Skeleton className="w-18 mt-1 h-4 rounded-sm" /> | ||
) : ( | ||
<p className="text-sm">{data?.createdAt || 'Unavailable'}</p> | ||
)} | ||
</div> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">DNSSEC</h3> | ||
<p className="text-sm">{results.dnssec}</p> | ||
{isLoading ? ( | ||
<Skeleton className="mt-1 h-4 w-16 rounded-sm" /> | ||
) : ( | ||
<p className="text-sm">{data?.dnssec || 'Unavailable'}</p> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default WhoisQuickInfo; | ||
|
||
export const WhoisQuickInfoPlaceholder: FC = () => ( | ||
<div className="my-8 flex gap-8"> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">Registrar</h3> | ||
<Skeleton className="mt-1 h-4 w-24 rounded-sm" /> | ||
</div> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">Creation Date</h3> | ||
<Skeleton className="w-18 mt-1 h-4 rounded-sm" /> | ||
</div> | ||
<div> | ||
<h3 className="text-xs text-muted-foreground">DNSSEC</h3> | ||
<Skeleton className="mt-1 h-4 w-16 rounded-sm" /> | ||
</div> | ||
</div> | ||
); |
5d6b721
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
domain-digger – ./
domain-digger-git-main-wotschofsky.vercel.app
digger.tools
domain-digger.vercel.app
domain-digger-wotschofsky.vercel.app
www.digger.tools