The `hono-geo-middleware` package provides a specialized Hono middleware for extracting geographical location data from incoming HTTP requests. It is designed to work across various serverless environments, including Cloudflare Workers, Vercel, Netlify Edge Functions, and AWS CloudFront/Lambda@Edge, by intelligently parsing platform-specific headers and context objects. The middleware normalizes this data into a consistent `Geo` interface, offering details like IP address, city, country, country code, and timezone. Currently at version 0.1.2, the package is in its early stages of development, suggesting a potentially frequent release cadence for enhancements and bug fixes. Its primary differentiator is simplifying geo-IP data access within Hono applications, abstracting the complexities of diverse serverless runtime environments and supporting custom extraction logic.
npm install hono-geo-middlewareVerified import paths — ran on the pinned version, not inferred.
Initializes a Hono application, applies the GeoMiddleware to all routes, and provides an endpoint to retrieve the extracted geolocation data.
Always review the changelog when updating minor versions and pin exact versions in production environments to prevent unexpected breakage.
Implement robust error handling and fallback logic when consuming geo data, checking for `undefined` or null values for specific properties. For example: `c.json(getGeo(c)?.countryCode ?? 'unknown')`.
Developers are solely responsible for ensuring their use of geolocation data complies with all applicable privacy laws and regulations, including obtaining user consent where required.
Carefully consider the order of extractors, placing general or less specific ones before more precise or custom ones if overriding is desired, or vice-versa if specific extractors should take precedence.
Install the package using your package manager: `npm install hono-geo-middleware` or `yarn add hono-geo-middleware`. For Deno, use the `esm.sh` import URL: `import { GeoMiddleware } from 'https://esm.sh/hono-geo-middleware';`Ensure `app.use('/*', GeoMiddleware())` or a more specific path is correctly applied before the route attempting to read geo data. Also, handle cases where geo data might not be available, e.g., `const ip = getGeo(c)?.ip;`Add `import { Context } from 'hono';` to the top of your TypeScript file where the custom extractor is defined.