Skip to content

Commit 468a0d3

Browse files
committed
chore: make req a subclass of Request
1 parent e54fec6 commit 468a0d3

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

plugin/src/middleware/request.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ type AugmentedGeo = NextRequest['geo'] & {
2121
/**
2222
* Supercharge your Next middleware with Netlify Edge Functions
2323
*/
24-
export class MiddlewareRequest {
24+
export class MiddlewareRequest extends Request {
2525
context: Context
2626
originalRequest: Request
2727

28-
constructor(public request: NextRequest) {
28+
constructor(private nextRequest: NextRequest) {
29+
super(nextRequest)
2930
if (!('Deno' in globalThis)) {
3031
throw new Error('NetlifyMiddleware only works in a Netlify Edge Function environment')
3132
}
32-
const geo = request.geo as AugmentedGeo
33+
const geo = nextRequest.geo as AugmentedGeo
3334
if (!geo) {
3435
throw new Error('NetlifyMiddleware must be instantiated with a NextRequest object')
3536
}
@@ -39,7 +40,7 @@ export class MiddlewareRequest {
3940

4041
// Add the headers to the original request, which will be passed to the origin
4142
private applyHeaders() {
42-
this.request.headers.forEach((value, name) => {
43+
this.headers.forEach((value, name) => {
4344
this.originalRequest.headers.set(name, value)
4445
})
4546
}
@@ -52,17 +53,35 @@ export class MiddlewareRequest {
5253

5354
rewrite(destination: string | URL | NextURL, init?: ResponseInit): NextResponse {
5455
if (typeof destination === 'string' && destination.startsWith('/')) {
55-
destination = new URL(destination, this.request.url)
56+
destination = new URL(destination, this.url)
5657
}
5758
this.applyHeaders()
5859
return NextResponse.rewrite(destination, init)
5960
}
6061

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()
6684
}
6785
}
86+
6887
/* eslint-enable no-underscore-dangle */

0 commit comments

Comments
 (0)