@@ -21,15 +21,16 @@ type AugmentedGeo = NextRequest['geo'] & {
21
21
/**
22
22
* Supercharge your Next middleware with Netlify Edge Functions
23
23
*/
24
- export class MiddlewareRequest {
24
+ export class MiddlewareRequest extends Request {
25
25
context : Context
26
26
originalRequest : Request
27
27
28
- constructor ( public request : NextRequest ) {
28
+ constructor ( private nextRequest : NextRequest ) {
29
+ super ( nextRequest )
29
30
if ( ! ( 'Deno' in globalThis ) ) {
30
31
throw new Error ( 'NetlifyMiddleware only works in a Netlify Edge Function environment' )
31
32
}
32
- const geo = request . geo as AugmentedGeo
33
+ const geo = nextRequest . geo as AugmentedGeo
33
34
if ( ! geo ) {
34
35
throw new Error ( 'NetlifyMiddleware must be instantiated with a NextRequest object' )
35
36
}
@@ -39,7 +40,7 @@ export class MiddlewareRequest {
39
40
40
41
// Add the headers to the original request, which will be passed to the origin
41
42
private applyHeaders ( ) {
42
- this . request . headers . forEach ( ( value , name ) => {
43
+ this . headers . forEach ( ( value , name ) => {
43
44
this . originalRequest . headers . set ( name , value )
44
45
} )
45
46
}
@@ -52,17 +53,35 @@ export class MiddlewareRequest {
52
53
53
54
rewrite ( destination : string | URL | NextURL , init ?: ResponseInit ) : NextResponse {
54
55
if ( typeof destination === 'string' && destination . startsWith ( '/' ) ) {
55
- destination = new URL ( destination , this . request . url )
56
+ destination = new URL ( destination , this . url )
56
57
}
57
58
this . applyHeaders ( )
58
59
return NextResponse . rewrite ( destination , init )
59
60
}
60
61
61
- redirect ( destination : string | URL | NextURL , init ?: number | ResponseInit ) {
62
- if ( typeof destination === 'string' && destination . startsWith ( '/' ) ) {
63
- destination = new URL ( destination , this . request . url )
64
- }
65
- return NextResponse . redirect ( destination , init )
62
+ get headers ( ) {
63
+ return this . nextRequest . headers
64
+ }
65
+
66
+ get cookies ( ) {
67
+ return this . nextRequest . cookies
68
+ }
69
+
70
+ get geo ( ) {
71
+ return this . nextRequest . geo
72
+ }
73
+
74
+ get ip ( ) {
75
+ return this . nextRequest . ip
76
+ }
77
+
78
+ get nextUrl ( ) {
79
+ return this . nextRequest . url
80
+ }
81
+
82
+ get url ( ) {
83
+ return this . nextRequest . url . toString ( )
66
84
}
67
85
}
86
+
68
87
/* eslint-enable no-underscore-dangle */
0 commit comments