From cf7a925a829e78681dba2173dd6ebf5bfc3a8844 Mon Sep 17 00:00:00 2001 From: Keanu Aloua Date: Thu, 10 Jul 2025 14:19:33 -0700 Subject: [PATCH 1/3] Imported Geolocation from @netlify/dev-utils package --- package-lock.json | 2 ++ package.json | 1 + src/lib/geo-location.ts | 27 ++------------------------- tests/unit/lib/geo-location.test.ts | 3 ++- 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2bbb64edec6..ca18b8d25f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@netlify/build": "34.1.0", "@netlify/build-info": "10.0.7", "@netlify/config": "23.1.0", + "@netlify/dev-utils": "^3.2.2", "@netlify/edge-bundler": "14.0.7", "@netlify/edge-functions": "2.15.6", "@netlify/headers-parser": "9.0.1", @@ -2843,6 +2844,7 @@ "version": "3.2.2", "resolved": "https://registry.npmjs.org/@netlify/dev-utils/-/dev-utils-3.2.2.tgz", "integrity": "sha512-ECz/xEaqhAPUoFkeC2Ofpky1HBEKwPCsAL66iK/dLFHUFs39SC3y6Bn5QY76DzONmt+RjWmoYkSIEhJ1xAWHfA==", + "license": "MIT", "dependencies": { "@whatwg-node/server": "^0.10.0", "ansis": "^4.1.0", diff --git a/package.json b/package.json index a2b12d00795..e994ebb9cc2 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "@netlify/build": "34.1.0", "@netlify/build-info": "10.0.7", "@netlify/config": "23.1.0", + "@netlify/dev-utils": "^3.2.2", "@netlify/edge-bundler": "14.0.7", "@netlify/edge-functions": "2.15.6", "@netlify/headers-parser": "9.0.1", diff --git a/src/lib/geo-location.ts b/src/lib/geo-location.ts index 4ad9e1a806f..a2330370bb5 100644 --- a/src/lib/geo-location.ts +++ b/src/lib/geo-location.ts @@ -1,4 +1,5 @@ import fetch from 'node-fetch' +import { Geolocation, mockLocation } from '@netlify/dev-utils'; const API_URL = 'https://netlifind.netlify.app' const STATE_GEO_PROPERTY = 'geolocation' @@ -8,35 +9,11 @@ const CACHE_TTL = 8.64e7 // 10 seconds const REQUEST_TIMEOUT = 1e4 -export type Geolocation = { - city: string - country: { - code: string - name: string - } - subdivision: { - code: string - name: string - } - longitude: number - latitude: number - timezone: string -} - interface State { get(key: string): unknown set(key: string, value: unknown): void } -export const mockLocation: Geolocation = { - city: 'San Francisco', - country: { code: 'US', name: 'United States' }, - subdivision: { code: 'CA', name: 'California' }, - longitude: 0, - latitude: 0, - timezone: 'UTC', -} - /** * Returns geolocation data from a remote API, the local cache, or a mock location, depending on the * specified mode. @@ -63,7 +40,7 @@ export const getGeoLocation = async ({ // `cache`, let's try to use it. // Or, if the country we're trying to mock is the same one as we have in the // cache, let's use the cache instead of the mock. - if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country.code === geoCountry)) { + if (cacheObject !== undefined && (mode === 'cache' || cacheObject?.data?.country?.code === geoCountry)) { const age = Date.now() - cacheObject.timestamp // Let's use the cached data if it's not older than the TTL. Also, if the diff --git a/tests/unit/lib/geo-location.test.ts b/tests/unit/lib/geo-location.test.ts index 66a2a7505dc..ef096bd296d 100644 --- a/tests/unit/lib/geo-location.test.ts +++ b/tests/unit/lib/geo-location.test.ts @@ -1,7 +1,8 @@ import nock from 'nock' import { describe, expect, test } from 'vitest' -import { type Geolocation, getGeoLocation, mockLocation } from '../../../src/lib/geo-location.js' +import { Geolocation, mockLocation } from '@netlify/dev-utils' +import { getGeoLocation } from '../../../src/lib/geo-location.js' describe('getGeoLocation', () => { test('returns geolocation data from the API if `mode: "cache"`', async () => { From dc0856fbe76c11b5e824d26322ff680ef754fe98 Mon Sep 17 00:00:00 2001 From: Keanu Aloua Date: Fri, 11 Jul 2025 09:09:13 -0700 Subject: [PATCH 2/3] Small convention and format fixes --- src/lib/geo-location.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/geo-location.ts b/src/lib/geo-location.ts index a2330370bb5..462e0f525c1 100644 --- a/src/lib/geo-location.ts +++ b/src/lib/geo-location.ts @@ -1,5 +1,5 @@ import fetch from 'node-fetch' -import { Geolocation, mockLocation } from '@netlify/dev-utils'; +import { Geolocation, mockLocation } from '@netlify/dev-utils' const API_URL = 'https://netlifind.netlify.app' const STATE_GEO_PROPERTY = 'geolocation' @@ -40,7 +40,7 @@ export const getGeoLocation = async ({ // `cache`, let's try to use it. // Or, if the country we're trying to mock is the same one as we have in the // cache, let's use the cache instead of the mock. - if (cacheObject !== undefined && (mode === 'cache' || cacheObject?.data?.country?.code === geoCountry)) { + if (cacheObject !== undefined && (mode === 'cache' || cacheObject.data.country?.code === geoCountry)) { const age = Date.now() - cacheObject.timestamp // Let's use the cached data if it's not older than the TTL. Also, if the From 2060c09e2ddc4e7c20053c055a4f72aa9aa0aba1 Mon Sep 17 00:00:00 2001 From: Keanu Aloua Date: Fri, 11 Jul 2025 10:46:21 -0700 Subject: [PATCH 3/3] Fixed Geolocation imports/exports --- src/lib/geo-location.ts | 4 +++- tests/unit/lib/geo-location.test.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/geo-location.ts b/src/lib/geo-location.ts index 462e0f525c1..49ff1878cb4 100644 --- a/src/lib/geo-location.ts +++ b/src/lib/geo-location.ts @@ -1,5 +1,5 @@ import fetch from 'node-fetch' -import { Geolocation, mockLocation } from '@netlify/dev-utils' +import { type Geolocation, mockLocation } from '@netlify/dev-utils' const API_URL = 'https://netlifind.netlify.app' const STATE_GEO_PROPERTY = 'geolocation' @@ -9,6 +9,8 @@ const CACHE_TTL = 8.64e7 // 10 seconds const REQUEST_TIMEOUT = 1e4 +export { Geolocation } + interface State { get(key: string): unknown set(key: string, value: unknown): void diff --git a/tests/unit/lib/geo-location.test.ts b/tests/unit/lib/geo-location.test.ts index ef096bd296d..e7e74756873 100644 --- a/tests/unit/lib/geo-location.test.ts +++ b/tests/unit/lib/geo-location.test.ts @@ -1,8 +1,8 @@ import nock from 'nock' import { describe, expect, test } from 'vitest' -import { Geolocation, mockLocation } from '@netlify/dev-utils' -import { getGeoLocation } from '../../../src/lib/geo-location.js' +import { mockLocation } from '@netlify/dev-utils' +import { getGeoLocation, type Geolocation } from '../../../src/lib/geo-location.js' describe('getGeoLocation', () => { test('returns geolocation data from the API if `mode: "cache"`', async () => {