Skip to content

Commit 9b9346a

Browse files
committed
feat: fallback for country
1 parent 7a6b493 commit 9b9346a

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

apps/basket/src/utils/ip-geo.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { createHash } from 'node:crypto';
22
import { isIP } from 'node:net';
3-
import type { City } from '@maxmind/geoip2-node';
3+
import type { City, Country } from '@maxmind/geoip2-node';
44
import { AddressNotFoundError, Reader } from '@maxmind/geoip2-node';
55

66
// import { logger } from '../lib/logger';
77
const logger = console;
88

99
interface GeoIPReader extends Reader {
1010
city(ip: string): City;
11+
country(ip: string): Country;
1112
}
1213

1314
const IPV4_URL = process.env.IPV4_URL;
@@ -126,24 +127,51 @@ export async function getGeoLocation(ip: string) {
126127
}
127128

128129
try {
129-
const response = reader.city(ip);
130-
const region = response.subdivisions?.[0]?.names?.en;
131-
const city = response.city?.names?.en;
132-
const country = response.country?.names?.en;
133-
134-
logger.debug('IP lookup successful', {
135-
ip,
136-
type: ipType,
137-
country,
138-
region,
139-
city,
140-
});
141-
142-
return {
143-
country,
144-
region,
145-
city,
146-
};
130+
// Try city method first (for full city databases)
131+
try {
132+
const response = reader.city(ip);
133+
const region = response.subdivisions?.[0]?.names?.en;
134+
const city = response.city?.names?.en;
135+
const country = response.country?.names?.en;
136+
137+
logger.debug('IP lookup successful (city)', {
138+
ip,
139+
type: ipType,
140+
country,
141+
region,
142+
city,
143+
});
144+
145+
return {
146+
country,
147+
region,
148+
city,
149+
};
150+
} catch (cityError) {
151+
// If city method fails, try country method (for country-only databases)
152+
if (
153+
cityError instanceof Error &&
154+
cityError.message.includes('cannot be used with')
155+
) {
156+
logger.debug('Falling back to country lookup', { ip, type: ipType });
157+
158+
const response = reader.country(ip);
159+
const country = response.country?.names?.en;
160+
161+
logger.debug('IP lookup successful (country)', {
162+
ip,
163+
type: ipType,
164+
country,
165+
});
166+
167+
return {
168+
country,
169+
region: undefined,
170+
city: undefined,
171+
};
172+
}
173+
throw cityError;
174+
}
147175
} catch (error) {
148176
if (error instanceof AddressNotFoundError) {
149177
logger.debug('IP not found in database', { ip, type: ipType });

0 commit comments

Comments
 (0)