Skip to content

Commit

Permalink
feat: Location#city is optional (#13)
Browse files Browse the repository at this point in the history
We don't always get city information when we do a `geoip-lite` lookup. Therefore annotated the `city` field as optional.
  • Loading branch information
teogeb authored Mar 11, 2024
1 parent 8490270 commit baab264
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/entities/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class Location {
latitude!: number
@Field(() => Float)
longitude!: number
@Field()
city!: string
@Field(() => String, { nullable: true })
city!: string | null
@Field()
country!: string
}
Expand Down
2 changes: 1 addition & 1 deletion src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getLocationFromIpAddress = (ipAddress: string): Location | undefine
return {
latitude: data.ll[0],
longitude: data.ll[1],
city: data.city,
city: (data.city) !== '' ? (data.city) : null,
country: data.country
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ describe('getLocationFromIpAddress', () => {
it('data unavailable', () => {
expect(getLocationFromIpAddress('127.0.0.1')).toBeUndefined()
})

it('no city', () => {
expect(getLocationFromIpAddress('1.2.3.4')).toEqual({
city: null,
country: 'AU',
latitude: -33.494,
longitude: 143.2104,
})
})
})

0 comments on commit baab264

Please sign in to comment.