1
- import type { Context } from 'https://edge.netlify.com'
2
1
import { NextRequest , NextResponse } from 'https://esm.sh/next/server'
3
2
import { fromFileUrl } from 'https://deno.land/std/path/mod.ts'
4
3
import { buildResponse } from './utils.ts'
5
4
6
- export interface FetchEventResult {
7
- response : Response
8
- waitUntil : Promise < unknown >
9
- }
10
-
11
- interface I18NConfig {
12
- defaultLocale : string
13
- domains ?: DomainLocale [ ]
14
- localeDetection ?: false
15
- locales : string [ ]
16
- }
17
-
18
- interface DomainLocale {
19
- defaultLocale : string
20
- domain : string
21
- http ?: true
22
- locales ?: string [ ]
23
- }
24
- export interface NextRequestInit extends RequestInit {
25
- geo ?: {
26
- city ?: string
27
- country ?: string
28
- region ?: string
29
- }
30
- ip ?: string
31
- nextConfig ?: {
32
- basePath ?: string
33
- i18n ?: I18NConfig | null
34
- trailingSlash ?: boolean
35
- }
36
- }
37
-
38
- export interface RequestData {
39
- geo ?: {
40
- city ?: string
41
- country ?: string
42
- region ?: string
43
- latitude ?: string
44
- longitude ?: string
45
- }
46
- headers : Record < string , string >
47
- ip ?: string
48
- method : string
49
- nextConfig ?: {
50
- basePath ?: string
51
- i18n ?: Record < string , unknown >
52
- trailingSlash ?: boolean
53
- }
54
- page ?: {
55
- name ?: string
56
- params ?: { [ key : string ] : string }
57
- }
58
- url : string
59
- body ?: ReadableStream < Uint8Array >
60
- }
61
-
62
- export interface RequestContext {
63
- request : Request
64
- context : Context
65
- }
66
-
67
- declare global {
68
- // deno-lint-ignore no-var
69
- var NFRequestContextMap : Map < string , RequestContext >
70
- // deno-lint-ignore no-var
71
- var __dirname : string
72
- }
73
-
74
5
globalThis . NFRequestContextMap ||= new Map ( )
75
6
globalThis . __dirname = fromFileUrl ( new URL ( './' , import . meta. url ) ) . slice ( 0 , - 1 )
76
7
77
8
// Check if a file exists, given a relative path
78
- const exists = async ( relativePath : string ) => {
9
+ const exists = async ( relativePath ) => {
79
10
const path = fromFileUrl ( new URL ( relativePath , import . meta. url ) )
80
11
try {
81
12
await Deno . stat ( path )
@@ -88,7 +19,7 @@ const exists = async (relativePath: string) => {
88
19
}
89
20
}
90
21
91
- const handler = async ( req : Request , context : Context ) => {
22
+ const handler = async ( req , context ) => {
92
23
// Uncomment when CLI update lands
93
24
// if (!Deno.env.get('NETLIFY_DEV')) {
94
25
// // Only run in dev
@@ -111,7 +42,7 @@ const handler = async (req: Request, context: Context) => {
111
42
}
112
43
113
44
// This is the format expected by Next.js
114
- const geo : NextRequestInit [ 'geo' ] = {
45
+ const geo = {
115
46
country : context . geo . country ?. code ,
116
47
region : context . geo . subdivision ?. code ,
117
48
city : context . geo . city ,
@@ -125,15 +56,15 @@ const handler = async (req: Request, context: Context) => {
125
56
context,
126
57
} )
127
58
128
- const request : NextRequestInit = {
59
+ const request = {
129
60
headers : Object . fromEntries ( req . headers . entries ( ) ) ,
130
61
geo,
131
62
method : req . method ,
132
63
ip : context . ip ,
133
64
body : req . body || undefined ,
134
65
}
135
66
136
- const nextRequest : NextRequest = new NextRequest ( req , request )
67
+ const nextRequest = new NextRequest ( req , request )
137
68
138
69
try {
139
70
const response = await middleware ( nextRequest )
0 commit comments