Skip to content

Commit bcbcdcc

Browse files
authored
refactor: use geolocation from @netlify/dev-utils package (#7414)
* Imported Geolocation from @netlify/dev-utils package * Small convention and format fixes * Fixed Geolocation imports/exports
1 parent ea66dd2 commit bcbcdcc

File tree

4 files changed

+8
-25
lines changed

4 files changed

+8
-25
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@netlify/build": "34.2.3",
6464
"@netlify/build-info": "10.0.7",
6565
"@netlify/config": "23.1.0",
66+
"@netlify/dev-utils": "^3.2.2",
6667
"@netlify/edge-bundler": "14.2.1",
6768
"@netlify/edge-functions": "2.15.6",
6869
"@netlify/headers-parser": "9.0.1",

src/lib/geo-location.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fetch from 'node-fetch'
2+
import { type Geolocation, mockLocation } from '@netlify/dev-utils'
23

34
const API_URL = 'https://netlifind.netlify.app'
45
const STATE_GEO_PROPERTY = 'geolocation'
@@ -8,35 +9,13 @@ const CACHE_TTL = 8.64e7
89
// 10 seconds
910
const REQUEST_TIMEOUT = 1e4
1011

11-
export type Geolocation = {
12-
city: string
13-
country: {
14-
code: string
15-
name: string
16-
}
17-
subdivision: {
18-
code: string
19-
name: string
20-
}
21-
longitude: number
22-
latitude: number
23-
timezone: string
24-
}
12+
export { Geolocation }
2513

2614
interface State {
2715
get(key: string): unknown
2816
set(key: string, value: unknown): void
2917
}
3018

31-
export const mockLocation: Geolocation = {
32-
city: 'San Francisco',
33-
country: { code: 'US', name: 'United States' },
34-
subdivision: { code: 'CA', name: 'California' },
35-
longitude: 0,
36-
latitude: 0,
37-
timezone: 'UTC',
38-
}
39-
4019
/**
4120
* Returns geolocation data from a remote API, the local cache, or a mock location, depending on the
4221
* specified mode.
@@ -63,7 +42,7 @@ export const getGeoLocation = async ({
6342
// `cache`, let's try to use it.
6443
// Or, if the country we're trying to mock is the same one as we have in the
6544
// cache, let's use the cache instead of the mock.
66-
if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country.code === geoCountry)) {
45+
if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country?.code === geoCountry)) {
6746
const age = Date.now() - cacheObject.timestamp
6847

6948
// Let's use the cached data if it's not older than the TTL. Also, if the

tests/unit/lib/geo-location.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import nock from 'nock'
22
import { describe, expect, test } from 'vitest'
33

4-
import { type Geolocation, getGeoLocation, mockLocation } from '../../../src/lib/geo-location.js'
4+
import { mockLocation } from '@netlify/dev-utils'
5+
import { getGeoLocation, type Geolocation } from '../../../src/lib/geo-location.js'
56

67
describe('getGeoLocation', () => {
78
test('returns geolocation data from the API if `mode: "cache"`', async () => {

0 commit comments

Comments
 (0)