Skip to content

Commit 27df67c

Browse files
committed
Refactor GET function in route handlers to improve request handling and add URL construction
1 parent 4eced1e commit 27df67c

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

app/api/verify/alabama/route.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
// import { NextResponse, type NextRequest } from "next/server";
2-
import { NextResponse } from "next/server";
1+
import { NextResponse, type NextRequest } from "next/server";
32
import BlobSync from "@/data/controls/blobs/blobSync";
43
import BlobCreate from "@/data/controls/blobs/blobCreate";
54
import BlobUpdate from "@/data/controls/blobs/blobUpdate";
65
import BlobFetch from "@/data/controls/blobs/blobFetch";
76
import BlobConvert from "@/data/controls/blobs/BlobConvert";
87
import { verify } from "./logic"
98

10-
export async function GET() {
11-
// export async function GET(request: NextRequest) {
9+
export async function GET(request: NextRequest) {
1210
// const { searchParams } = new URL(request.url);
1311
// const firstName = searchParams.get("firstname") || "";
1412
// const lastName = searchParams.get("lastname") || "";
1513
// const licenseNumber = searchParams.get("license") || "";
16-
1714
const key = "alabama";
15+
const { search } = new URL(request.url);
16+
const backupURL =
17+
"https://raw.githubusercontent.com/BorDevTech/ClearView/refs/heads/main/app/api/verify/alabama/VET.json" +
18+
(search || "");
19+
1820

1921
try {
2022
const data = await BlobFetch(key);
@@ -34,6 +36,7 @@ export async function GET() {
3436
// If blob does not exist, fetch and parse, then create/update blob
3537
try {
3638
// 🌐 Fetch Alabama portal HTML
39+
console.log(backupURL);
3740
const url = `https://licensesearch.alabama.gov/ASBVME`;
3841
const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0" } });
3942
const html = await response.text();

app/api/verify/alaska/route.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import BlobConvert from "@/data/controls/blobs/BlobConvert";
88

99
export async function GET(request: NextRequest) {
1010
const key = "alaska";
11+
const { search } = new URL(request.url);
12+
const url =
13+
"https://raw.githubusercontent.com/BorDevTech/ClearView/refs/heads/main/app/api/verify/missouri/VET.json" +
14+
(search || "");
15+
1116
try {
1217
const data = await BlobFetch(key);
1318
// ✅ Convert and write blob immediately after fetch
@@ -18,15 +23,24 @@ export async function GET(request: NextRequest) {
1823
count: Array.isArray(data) ? data.length : 0,
1924
});
2025
} catch (error: unknown) {
26+
// // // 🌐 Fallback: fetch HTML and parse
27+
// const { verify } = await import(`./../../../app/api/verify/${key}/logic`);
28+
// // Forward all query string parameters from the incoming request
29+
// const { search } = new URL(request.url);
30+
// const results = await verify(search); // 👈 verify now parses HTML directly
2131
console.warn(`⚠️ BlobFetch failed for ${key}, falling back to live parse: ${error}`);
2232
// If blob does not exist, fetch and parse, then create/update blob
33+
////
2334
try {
24-
25-
// // 🌐 Fallback: fetch HTML and parse
26-
const { verify } = await import(`./../../../app/api/verify/${key}/logic`);
27-
// Forward all query string parameters from the incoming request
28-
const { search } = new URL(request.url);
29-
const results = await verify(search); // 👈 verify now parses HTML directly
35+
const response = await fetch(url, {
36+
method: "GET",
37+
headers: {
38+
"User-Agent": "Mozilla/5.0",
39+
Accept: "application/json",
40+
},
41+
});
42+
const data = await response.text();
43+
const results = JSON.parse(data);
3044
await BlobCreate(key);
3145
await BlobUpdate(key, {
3246
timestamp: new Date().toISOString(),
@@ -35,7 +49,6 @@ export async function GET(request: NextRequest) {
3549
});
3650
// Optionally, sync the blob after update
3751
const blob = await BlobSync(key, results);
38-
3952
return NextResponse.json({ count: results.length, blob, results });
4053
} catch (fallbackError) {
4154
return NextResponse.json({

app/components/Project/Header.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,37 @@ import { ListedStates } from "@/app/components/StateSelector";
1414

1515
const totalStates = ListedStates.items.length;
1616

17-
const onlineStates = ListedStates.items.filter(
18-
(state) => state.active === true
19-
).length;
20-
const offlineStates = ListedStates.items.filter(
21-
(state) => state.active === false
22-
).length;
23-
const checkingStates = ListedStates.items.filter(
24-
(state) => state.active === null
25-
).length;
2617

18+
interface StateDefinition {
19+
active: boolean | null;
20+
value: string;
21+
label: string;
22+
}
23+
interface StateCounts {
24+
online: number;
25+
offline: number;
26+
checking: number;
27+
}
28+
// const onlineStates = ListedStates.items.filter(
29+
// (state) => state.active === true
30+
// ).length;
31+
// const offlineStates = ListedStates.items.filter(
32+
// (state) => state.active === false
33+
// ).length;
34+
// const checkingStates = ListedStates.items.filter(
35+
// (state) => state.active === null
36+
// ).length;
2737

38+
const { offline: offlineStates, checking: checkingStates, online: onlineStates } =
39+
ListedStates.items.reduce(
40+
(counts: StateCounts, { active }: StateDefinition) => {
41+
if (active === true) counts.online += 1;
42+
else if (active === false) counts.offline += 1;
43+
else counts.checking += 1;
44+
return counts;
45+
},
46+
{ offline: 0, checking: 0, online: 0 }
47+
);
2848

2949
export function ProjectHeader({
3050
icon,

0 commit comments

Comments
 (0)