Skip to content

Commit 2bdbd20

Browse files
authored
Merge pull request #57 from maxmind/fix-ip-country
Handle empty ip_address country and location responses
2 parents a5e5acc + 533914e commit 2bdbd20

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cloneDeep = require('lodash.clonedeep');
2+
import * as insights from '../../../fixtures/insights.json';
3+
import Insights from './insights';
4+
5+
describe('Insights()', () => {
6+
it('handles empty country responses', () => {
7+
let input = cloneDeep(insights) as any;
8+
input = input.response.full;
9+
delete input.ip_address.country;
10+
11+
const model = new Insights(input);
12+
13+
expect(model.ipAddress.country.isHighRisk).toBeUndefined();
14+
});
15+
16+
it('handles empty location responses', () => {
17+
let input = cloneDeep(insights) as any;
18+
input = input.response.full;
19+
delete input.ip_address.location;
20+
21+
const model = new Insights(input);
22+
23+
expect(model.ipAddress.location.localTime).toBeUndefined();
24+
});
25+
});

src/response/models/insights.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,15 @@ export default class Insights extends Score {
4545
response: webRecords.InsightsResponse
4646
): records.IpAddress {
4747
const insights = new GeoInsights(response.ip_address) as records.IpAddress;
48-
insights.country.isHighRisk = response.ip_address.country.is_high_risk;
49-
insights.location.localTime = response.ip_address.location.local_time;
48+
49+
insights.country.isHighRisk = response.ip_address.country
50+
? response.ip_address.country.is_high_risk
51+
: undefined;
52+
53+
insights.location.localTime = response.ip_address.location
54+
? response.ip_address.location.local_time
55+
: undefined;
56+
5057
insights.risk = response.ip_address.risk;
5158

5259
delete insights.maxmind;

src/response/records.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export interface GeoIPCountry extends CountryRecord {
1616
* @category Deprecated
1717
* @deprecated
1818
*/
19-
isHighRisk: boolean;
19+
isHighRisk?: boolean;
2020
}
2121

2222
export interface GeoIPLocation extends LocationRecord {
23-
localTime: string;
23+
localTime?: string;
2424
}
2525

2626
export interface IpAddress extends Insights {

src/response/web-records.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface GeoIPLocation extends LocationRecord {
1414

1515
export interface IpAddress extends CityResponse {
1616
readonly risk: number;
17-
readonly country: GeoIPCountry;
18-
readonly location: GeoIPLocation;
17+
readonly country?: GeoIPCountry;
18+
readonly location?: GeoIPLocation;
1919
}
2020

2121
export interface CreditCardIssuer {

0 commit comments

Comments
 (0)