Skip to content

Commit 8d22226

Browse files
Copilotserhalp
andcommitted
Address code review feedback: use LocalState class, remove CLI references, hook up to edge functions
Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
1 parent 0ad614b commit 8d22226

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

packages/dev-utils/src/lib/geo-location.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Context } from '@netlify/types'
22

3+
import { LocalState } from './local-state.js'
4+
35
export type Geolocation = Context['geo']
46

57
export const mockLocation: Geolocation = {
@@ -19,11 +21,6 @@ const CACHE_TTL = 8.64e7
1921
// 10 seconds
2022
const REQUEST_TIMEOUT = 1e4
2123

22-
interface State {
23-
get(key: string): unknown
24-
set(key: string, value: unknown): void
25-
}
26-
2724
/**
2825
* Returns geolocation data from a remote API, the local cache, or a mock location, depending on the
2926
* specified mode.
@@ -37,7 +34,7 @@ export const getGeoLocation = async ({
3734
mode: 'cache' | 'update' | 'mock'
3835
geoCountry?: string | undefined
3936
offline?: boolean | undefined
40-
state: State
37+
state: LocalState
4138
}): Promise<Geolocation> => {
4239
// Early return for pure mock mode (no geoCountry, no offline)
4340
if (mode === 'mock' && !geoCountry && !offline) {
@@ -63,14 +60,15 @@ export const getGeoLocation = async ({
6360
}
6461
}
6562

66-
// If `--country` was used, we also set `--mode=mock`.
63+
// If a country code was provided, we use mock mode to generate
64+
// location data for that country.
6765
if (geoCountry) {
6866
mode = 'mock'
6967
}
7068

71-
// If the `--geo` option is set to `mock`, we use the default mock location.
72-
// If the `--offline` option was used, we can't talk to the API, so let's
73-
// also use the mock location. Otherwise, use the country code passed in by
69+
// If the mode is set to `mock`, we use the default mock location.
70+
// If the `offline` option was used, we can't talk to the API, so let's
71+
// also use the mock location. Otherwise, use the country code passed in by
7472
// the user.
7573
if (mode === 'mock' || offline || geoCountry) {
7674
if (geoCountry) {

packages/dev/src/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path'
44
import process from 'node:process'
55

66
import { resolveConfig } from '@netlify/config'
7-
import { ensureNetlifyIgnore, getAPIToken, mockLocation, LocalState, type Logger, HTTPServer } from '@netlify/dev-utils'
7+
import { ensureNetlifyIgnore, getAPIToken, getGeoLocation, mockLocation, LocalState, type Logger, HTTPServer } from '@netlify/dev-utils'
88
import { EdgeFunctionsHandler } from '@netlify/edge-functions/dev'
99
import { FunctionsHandler } from '@netlify/functions/dev'
1010
import { HeadersHandler, type HeadersCollector } from '@netlify/headers'
@@ -475,11 +475,16 @@ export class NetlifyDev {
475475
),
476476
}
477477

478+
const geolocation = await getGeoLocation({
479+
mode: 'cache',
480+
state,
481+
})
482+
478483
const edgeFunctionsHandler = new EdgeFunctionsHandler({
479484
configDeclarations: this.#config?.config.edge_functions ?? [],
480485
directories: [this.#config?.config.build.edge_functions].filter(Boolean) as string[],
481486
env: edgeFunctionsEnv,
482-
geolocation: mockLocation,
487+
geolocation,
483488
logger: this.#logger,
484489
siteID,
485490
siteName: config?.siteInfo.name,

0 commit comments

Comments
 (0)