Ensure region code includes country.

This commit is contained in:
Mike Cao 2023-08-27 22:36:37 -07:00
parent 5b3a598593
commit 4b0118dce1

View File

@ -65,19 +65,27 @@ export async function getLocation(ip, req) {
// Cloudflare headers // Cloudflare headers
if (req.headers['cf-ipcountry']) { if (req.headers['cf-ipcountry']) {
const country = safeDecodeURIComponent(req.headers['cf-ipcountry']);
const subdivision1 = safeDecodeURIComponent(req.headers['cf-region-code']);
const city = safeDecodeURIComponent(req.headers['cf-ipcity']);
return { return {
country: safeDecodeURIComponent(req.headers['cf-ipcountry']), country,
subdivision1: safeDecodeURIComponent(req.headers['cf-region-code']), subdivision1: subdivision1.includes('-') ? subdivision1 : `${country}-${subdivision1}`,
city: safeDecodeURIComponent(req.headers['cf-ipcity']), city,
}; };
} }
// Vercel headers // Vercel headers
if (req.headers['x-vercel-ip-country']) { if (req.headers['x-vercel-ip-country']) {
const country = safeDecodeURIComponent(req.headers['x-vercel-ip-country']);
const subdivision1 = safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']);
const city = safeDecodeURIComponent(req.headers['x-vercel-ip-city']);
return { return {
country: safeDecodeURIComponent(req.headers['x-vercel-ip-country']), country,
subdivision1: safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']), subdivision1: subdivision1.includes('-') ? subdivision1 : `${country}-${subdivision1}`,
city: safeDecodeURIComponent(req.headers['x-vercel-ip-city']), city,
}; };
} }