@@ -6,12 +6,20 @@ import type {
6
6
7
7
import type { MiddlewareOutputEvent } from "../../core/routingHandler" ;
8
8
9
+ const cfPropNameToHeaderName = {
10
+ city : "x-open-next-city" ,
11
+ country : "x-open-next-country" ,
12
+ region : "x-open-next-region" ,
13
+ latitude : "x-open-next-latitude" ,
14
+ longitude : "x-open-next-longitude" ,
15
+ } ;
16
+
9
17
const handler : WrapperHandler <
10
18
InternalEvent ,
11
19
InternalResult | ( { type : "middleware" } & MiddlewareOutputEvent )
12
20
> =
13
21
async ( handler , converter ) =>
14
- async ( event : Request , env : Record < string , string > ) : Promise < Response > => {
22
+ async ( request : Request , env : Record < string , string > ) : Promise < Response > => {
15
23
globalThis . process = process ;
16
24
17
25
// Set the environment variables
@@ -22,7 +30,20 @@ const handler: WrapperHandler<
22
30
}
23
31
}
24
32
25
- const internalEvent = await converter . convertFrom ( event ) ;
33
+ const internalEvent = await converter . convertFrom ( request ) ;
34
+
35
+ // Retrieve geo information from the cloudflare request
36
+ // See https://developers.cloudflare.com/workers/runtime-apis/request
37
+ // Note: This code could be moved to a cloudflare specific converter when one is created.
38
+ const cfProperties = ( request as any ) . cf as Record < string , string | null > ;
39
+ for ( const [ propName , headerName ] of Object . entries (
40
+ cfPropNameToHeaderName ,
41
+ ) ) {
42
+ const propValue = cfProperties [ propName ] ;
43
+ if ( propValue !== null ) {
44
+ internalEvent . headers [ headerName ] = propValue ;
45
+ }
46
+ }
26
47
27
48
const response = await handler ( internalEvent ) ;
28
49
0 commit comments