-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
54 lines (46 loc) · 1.5 KB
/
route.ts
File metadata and controls
54 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// /src/app/api/geocode/route.ts
import { NextRequest } from 'next/server';
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const postcode = searchParams.get('postcode');
if (!postcode) {
return new Response(JSON.stringify({ error: 'Postcode is required' }), {
status: 400,
});
}
const apiKey = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY;
console.log('Loaded server API key:', apiKey);
if (!apiKey) {
console.error('Missing GOOGLE_MAPS_API_KEY in environment');
return new Response(
JSON.stringify({ error: 'Missing server API key' }),
{ status: 500 }
);
}
try {
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(
postcode
)}&key=${apiKey}`;
const response = await fetch(url);
const data = await response.json();
if (data.status === 'OK') {
const location = data.results[0].geometry.location;
return new Response(JSON.stringify({ location }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} else {
console.error('Geocoding failed:', data.status, data.error_message);
return new Response(
JSON.stringify({ error: 'Geocoding failed', details: data }),
{ status: 400 }
);
}
} catch (error) {
console.error('Error fetching geocode:', error);
return new Response(
JSON.stringify({ error: 'Internal Server Error' }),
{ status: 500 }
);
}
}