Skip to content

Full Toolkit for Next-Level Domain Analysis

Migrate API routes to app dir 🏗️

Felix Wotschofsky9fbdffa6b06d
diff --git a/pages/api/lookupCerts.ts b/app/api/lookupCerts/route.ts
similarity index 88%
rename from pages/api/lookupCerts.ts
rename to app/api/lookupCerts/route.ts
index 25abe13..a87ef0c 100644
--- a/pages/api/lookupCerts.ts
+++ b/app/api/lookupCerts/route.ts
@@ -1,5 +1,4 @@
import isValidDomain from 'is-valid-domain';
-import type { NextRequest } from 'next/server';
export type CertLookupResponse = {
certificates: {
@@ -13,12 +12,10 @@ export type CertLookupResponse = {
}[];
};
-export const config = {
- runtime: 'edge',
-};
+export const runtime = 'edge';
-export default async function handler(req: NextRequest) {
- const { searchParams } = new URL(req.url);
+export async function GET(request: Request) {
+ const { searchParams } = new URL(request.url);
const domain = searchParams.get('domain');
if (!domain || !isValidDomain(domain || '')) {
diff --git a/app/api/lookupIp/route.ts b/app/api/lookupIp/route.ts
new file mode 100644
index 0000000..c8f2320
--- /dev/null
+++ b/app/api/lookupIp/route.ts
@@ -0,0 +1,68 @@
+import dns from 'dns';
+import { promisify } from 'util';
+import isIP from 'validator/lib/isIP';
+
+export type IpLookupResponse = {
+ city: string;
+ country: string;
+ isp: string;
+ lat: number;
+ lon: number;
+ org: string;
+ region: string;
+ reverse: string[];
+ timezone: string;
+};
+
+export type IpLookupErrorResponse = { error: true; message: string };
+
+export async function GET(request: Request) {
+ const { searchParams } = new URL(request.url);
+ const ip = searchParams.get('ip');
+
+ if (!ip || !isIP(ip)) {
+ return new Response(
+ JSON.stringify({
+ error: true,
+ message: '"ip" param missing or invalid',
+ }),
+ {
+ status: 400,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ }
+ );
+ }
+
+ const url = `http://ip-api.com/json/${ip}`;
+ const response = await fetch(url);
+ const data = (await response.json()) as Record<string, any>;
+
+ let reverse: string[] = [];
+ try {
+ reverse = await promisify(dns.reverse)(ip);
+ } catch (error) {
+ console.error(error);
+ }
+
+ return new Response(
+ JSON.stringify({
+ reverse: reverse,
+ isp: data.isp,
+ org: data.org,
+ country: data.country,
+ region: data.regionName,
+ city: data.city,
+ timezone: data.timezone,
+ lat: data.lat,
+ lon: data.lon,
+ }),
+ {
+ status: 200,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ }
+ );
+}
diff --git a/app/api/lookupWhois/route.ts b/app/api/lookupWhois/route.ts
new file mode 100644
index 0000000..4e8b2fe
--- /dev/null
+++ b/app/api/lookupWhois/route.ts
@@ -0,0 +1,45 @@
+import isValidDomain from 'is-valid-domain';
+import whoiser, { type WhoisSearchResult } from 'whoiser';
+
+export type WhoisLookupResponse = {
+ [domainName: string]: string;
+};
+
+export type WhoisLookupErrorResponse = { error: true; message: string };
+
+export async function GET(request: Request) {
+ const { searchParams } = new URL(request.url);
+ const domain = searchParams.get('domain');
+
+ if (!domain || !isValidDomain(domain)) {
+ return new Response(
+ JSON.stringify({
+ error: true,
+ message: '"domain" param missing or invalid',
+ }),
+ {
+ status: 400,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ }
+ );
+ }
+
+ const result = await whoiser(domain, {
+ raw: true,
+ timeout: 3000,
+ });
+
+ const mappedResults: Record<string, string> = {};
+ for (const key in result) {
+ mappedResults[key] = (result[key] as WhoisSearchResult).__raw as string;
+ }
+
+ return new Response(JSON.stringify(mappedResults), {
+ status: 200,
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ });
+}
diff --git a/components/CertInfo.tsx b/components/CertInfo.tsx
index b364877..800a3c2 100644
--- a/components/CertInfo.tsx
+++ b/components/CertInfo.tsx
@@ -20,7 +20,7 @@ import {
TooltipTrigger,
} from '@/components/ui/tooltip';
-import type { CertLookupResponse } from '@/api/lookupCerts';
+import type { CertLookupResponse } from '@/app/api/lookupCerts/route';
type CertInfoProps = {
domain: string;
diff --git a/components/IpDetailsModal.tsx b/components/IpDetailsModal.tsx
index cb974a1..ba59ea4 100644
--- a/components/IpDetailsModal.tsx
+++ b/components/IpDetailsModal.tsx
@@ -21,7 +21,7 @@ import {
TooltipTrigger,
} from '@/components/ui/tooltip';
-import type { IpLookupResponse } from '@/api/lookupIp';
+import type { IpLookupResponse } from '@/app/api/lookupIp/route';
const LocationMap = dynamic(() => import('@/components/LocationMap'), {
ssr: false,
diff --git a/components/WhoisInfo.tsx b/components/WhoisInfo.tsx
index e532fb0..326ceb9 100644
--- a/components/WhoisInfo.tsx
+++ b/components/WhoisInfo.tsx
@@ -5,7 +5,7 @@ import useSWR from 'swr';
import { Spinner } from '@/components/ui/spinner';
-import type { WhoisLookupResponse } from '@/api/lookupWhois';
+import type { WhoisLookupResponse } from '@/app/api/lookupWhois/route';
type IpDetailsModalProps = {
domain: string;
diff --git a/pages/api/lookupIp.ts b/pages/api/lookupIp.ts
deleted file mode 100644
index aacc146..0000000
--- a/pages/api/lookupIp.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-import dns from 'dns';
-import type { NextApiRequest, NextApiResponse } from 'next';
-import { promisify } from 'util';
-import isIP from 'validator/lib/isIP';
-
-export type IpLookupResponse = {
- city: string;
- country: string;
- isp: string;
- lat: number;
- lon: number;
- org: string;
- region: string;
- reverse: string[];
- timezone: string;
-};
-
-export type IpLookupErrorResponse = { error: true; message: string };
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse<IpLookupResponse | IpLookupErrorResponse>
-) {
- if (
- !req.query.ip ||
- typeof req.query.ip === 'object' ||
- !isIP(req.query.ip)
- ) {
- res.status(400).json({
- error: true,
- message: 'Bad Request',
- });
- return;
- }
-
- const url = `http://ip-api.com/json/${req.query.ip}`;
- const response = await fetch(url);
- const data = (await response.json()) as Record<string, any>;
-
- let reverse: string[] = [];
- try {
- reverse = await promisify(dns.reverse)(req.query.ip);
- } catch (error) {
- console.error(error);
- }
-
- res.json({
- reverse: reverse,
- isp: data.isp,
- org: data.org,
- country: data.country,
- region: data.regionName,
- city: data.city,
- timezone: data.timezone,
- lat: data.lat,
- lon: data.lon,
- });
-}
diff --git a/pages/api/lookupWhois.ts b/pages/api/lookupWhois.ts
deleted file mode 100644
index 6bcdf46..0000000
--- a/pages/api/lookupWhois.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import isValidDomain from 'is-valid-domain';
-import type { NextApiRequest, NextApiResponse } from 'next';
-import whoiser, { type WhoisSearchResult } from 'whoiser';
-
-export type WhoisLookupResponse = {
- [domainName: string]: string;
-};
-
-export type WhoisLookupErrorResponse = { error: true; message: string };
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse<WhoisLookupResponse | WhoisLookupErrorResponse>
-) {
- if (
- !req.query.domain ||
- typeof req.query.domain === 'object' ||
- !isValidDomain(req.query.domain)
- ) {
- res.status(400).json({
- error: true,
- message: 'Bad Request',
- });
- return;
- }
-
- const result = await whoiser(req.query.domain, { raw: true, timeout: 3000 });
-
- const mappedResults: Record<string, string> = {};
- for (const key in result) {
- mappedResults[key] = (result[key] as WhoisSearchResult).__raw as string;
- }
-
- res.json(mappedResults);
-}