diff --git a/src/app/api/addresses/route.ts b/src/app/api/addresses/route.ts deleted file mode 100644 index a0864d7..0000000 --- a/src/app/api/addresses/route.ts +++ /dev/null @@ -1,19 +0,0 @@ -export async function getEPCData(postcode: string) { - const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`; - - const headers = { - Accept: "application/json", - Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`, - }; - - try { - const response = await fetch(url, { headers }); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - return await response.json(); - } catch (error) { - console.error("Failed to fetch EPC data:", error); - throw error; - } -} diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts new file mode 100644 index 0000000..43f2fe7 --- /dev/null +++ b/src/app/api/search/route.ts @@ -0,0 +1,58 @@ +// export async function getEPCData(postcode: string) { +// const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`; + +// const headers = { +// Accept: "application/json", +// Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`, +// }; + +// console.log("Fetching EPC data from:", headers); + +// try { +// const response = await fetch(url, { headers }); +// if (!response.ok) { +// throw new Error(`HTTP error! status: ${response.status}`); +// } +// return await response.json(); +// } catch (error) { +// console.error("Failed to fetch EPC data:", error); +// throw error; +// } +// } + +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(request: NextRequest) { + const postcode = request.nextUrl.searchParams.get("postcode"); + + // Check if the postcode is provided + if (!postcode) { + return new NextResponse("Postcode parameter is missing", { status: 400 }); + } + + const headers = { + Accept: "application/json", + Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`, + }; + + try { + const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`; + + const headers = { + Accept: "application/json", + Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`, + }; + + const response = await fetch(url, { + headers: headers, + next: { revalidate: 60 }, + }); + const data = await response.json(); + + // Handle the response from the external API and return it + return NextResponse.json(data); + } catch (error) { + console.error("Error fetching data:", error); + return NextResponse.json({ message: "Internal server error", status: 500 }); + } +} diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 0a59d71..820da2a 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -34,18 +34,17 @@ export default function Search() { return; }; - const fetchData = async () => { - const response = await fetch(`/api/addresses/${postcode}`); + console.log(data); - if (!response.ok) { - // handle error - console.error("An error occurred while fetching data"); - return; + async function fetchData() { + try { + const response = await fetch(`/api/search?postcode=${postcode}`); + const data = await response.json(); + setData(data.message); + } catch (error) { + console.error("Error fetching data:", error); } - - const data = await response.json(); - setData(data); - }; + } const submitProps = { buttonDisabled: buttonDisabled,