Skip to content

feat: add enhanced middleware support #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jul 30, 2022
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5d67f5f
fix: update patch syntax
ascorbic Jul 22, 2022
efdb03d
feat: add support for rewriting middleware responses
ascorbic Jul 22, 2022
e1c41e9
chore: format
ascorbic Jul 22, 2022
d9011d0
chore: add extra content
ascorbic Jul 23, 2022
a2c88b7
chore: use city in demo
ascorbic Jul 23, 2022
ba284d9
feat: add html rewriting
ascorbic Jul 23, 2022
d66e08e
feat: add request header support
ascorbic Jul 23, 2022
55b5282
feat: add rewriting
ascorbic Jul 23, 2022
426a116
chore: move NetlifyReponse into a subpackage
ascorbic Jul 24, 2022
733e352
feat: allow returning `NetlifyReponse` directly
ascorbic Jul 24, 2022
138c225
chore: remove inlined types from middleware demo
ascorbic Jul 24, 2022
0256f25
chore: remove modified toml
ascorbic Jul 24, 2022
f35a285
chore: add demo links
ascorbic Jul 24, 2022
e5df385
chore: add comments to example
ascorbic Jul 25, 2022
2e1a4c6
chore: don't lint generated types
ascorbic Jul 25, 2022
5f5e17d
Merge branch 'main' into mk/rewrite-props-middleware
ascorbic Jul 25, 2022
d2d16df
chore: rename class
ascorbic Jul 25, 2022
e0861cb
chore: add comment about source of htmlrewriter types
ascorbic Jul 26, 2022
9d0f004
refactor: use type guards
ascorbic Jul 26, 2022
8deaba4
chore: rename classes
ascorbic Jul 26, 2022
5215688
chore: rename again
ascorbic Jul 26, 2022
3854370
chore: rename again
ascorbic Jul 26, 2022
e54fec6
chore: update example
ascorbic Jul 26, 2022
468a0d3
chore: make req a subclass of Request
ascorbic Jul 26, 2022
d4c0dc0
chore: switch from hidden fields to global map
ascorbic Jul 27, 2022
d5f2b95
Merge branch 'main' into mk/rewrite-props-middleware
ascorbic Jul 27, 2022
71fa122
Merge branch 'main' into mk/rewrite-props-middleware
ascorbic Jul 29, 2022
0fd6c33
ci: add cypress middleware tests
ascorbic Jul 29, 2022
328a584
ci: add tests for middleware headers
ascorbic Jul 29, 2022
4f6a2aa
ci: add tests for enhanced middleware
ascorbic Jul 29, 2022
1cf3e1f
chore: fix test
ascorbic Jul 29, 2022
33ade90
fix: handle other HTTP verbs
ascorbic Jul 29, 2022
9f4044c
feat: add helper methods
ascorbic Jul 29, 2022
78c8225
fix: less flaky test
ascorbic Jul 29, 2022
6c5664e
Merge branch 'main' into mk/rewrite-props-middleware
nickytonline Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions plugin/src/middleware/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ type AugmentedGeo = NextRequest['geo'] & {
/**
* Supercharge your Next middleware with Netlify Edge Functions
*/
export class MiddlewareRequest {
export class MiddlewareRequest extends Request {
context: Context
originalRequest: Request

constructor(public request: NextRequest) {
constructor(private nextRequest: NextRequest) {
super(nextRequest)
if (!('Deno' in globalThis)) {
throw new Error('NetlifyMiddleware only works in a Netlify Edge Function environment')
}
const geo = request.geo as AugmentedGeo
const geo = nextRequest.geo as AugmentedGeo
if (!geo) {
throw new Error('NetlifyMiddleware must be instantiated with a NextRequest object')
}
Expand All @@ -39,7 +40,7 @@ export class MiddlewareRequest {

// Add the headers to the original request, which will be passed to the origin
private applyHeaders() {
this.request.headers.forEach((value, name) => {
this.headers.forEach((value, name) => {
this.originalRequest.headers.set(name, value)
})
}
Expand All @@ -52,17 +53,35 @@ export class MiddlewareRequest {

rewrite(destination: string | URL | NextURL, init?: ResponseInit): NextResponse {
if (typeof destination === 'string' && destination.startsWith('/')) {
destination = new URL(destination, this.request.url)
destination = new URL(destination, this.url)
}
this.applyHeaders()
return NextResponse.rewrite(destination, init)
}

redirect(destination: string | URL | NextURL, init?: number | ResponseInit) {
if (typeof destination === 'string' && destination.startsWith('/')) {
destination = new URL(destination, this.request.url)
}
return NextResponse.redirect(destination, init)
get headers() {
return this.nextRequest.headers
}

get cookies() {
return this.nextRequest.cookies
}

get geo() {
return this.nextRequest.geo
}

get ip() {
return this.nextRequest.ip
}

get nextUrl() {
return this.nextRequest.url
}

get url() {
return this.nextRequest.url.toString()
}
}

/* eslint-enable no-underscore-dangle */