added lpi as additional dataset for ordnance survey to retrieve full results

This commit is contained in:
Khalim Conn-Kowlessar 2025-11-07 14:03:35 +00:00
parent 86d919d7df
commit eeb036712f
2 changed files with 16 additions and 6 deletions

View file

@ -24,9 +24,9 @@ async function fetchOsPlacesPage(
retries: number,
delay: number
): Promise<{ status: number; data?: OSPlacesResponse; error?: string }> {
const url = `${OS_API_URL}?postcode=${encodeURIComponent(
const url = `https://api.os.uk/search/places/v1/postcode?postcode=${encodeURIComponent(
postcode
)}&key=${OS_API_KEY}&maxresults=${maxResults}&offset=${offset}`;
)}&dataset=DPA,LPI&maxresults=100&offset=${offset}&key=${process.env.OS_API_KEY}`;
try {
const res = await fetch(url, { method: "GET" });

View file

@ -53,10 +53,20 @@ export async function GET(
const addresses = mapOsPlacesToFlat(result.results);
const total = result.data.header?.totalresults ?? addresses.length;
await db.insert(postcodeSearch).values({
postcode: postcode,
resultData: result.data,
});
// We shouldn't really run into conflicts here, but just in case, we allow for an update
await db
.insert(postcodeSearch)
.values({
postcode,
resultData: result.data,
})
.onConflictDoUpdate({
target: postcodeSearch.postcode,
set: {
resultData: result.data,
createdAt: new Date(), // update timestamp
},
});
// Step 4: return results
return NextResponse.json({