File size: 762 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { HonoRequest } from "hono";

export function getGeoContext(req: HonoRequest) {
  const headers = req.header();

  // Client-sent headers take priority, fall back to Cloudflare geo headers
  const country =
    headers["x-user-country"]?.toUpperCase() ?? headers["cf-ipcountry"] ?? null;
  const locale = headers["x-user-locale"] ?? null;
  const timezone = headers["x-user-timezone"] ?? headers["cf-timezone"] ?? null;
  const city = headers["cf-ipcity"] ?? null;
  const region = headers["cf-region"] ?? null;
  const continent = headers["cf-ipcontinent"] ?? null;
  const ip = headers["cf-connecting-ip"] ?? headers["x-forwarded-for"] ?? null;

  return {
    country,
    city,
    region,
    continent,
    locale,
    timezone,
    ip,
  };
}