Skip to content

Commit 5215688

Browse files
committed
chore: rename again
1 parent 8deaba4 commit 5215688

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

demos/middleware/middleware.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextResponse } from 'next/server'
22
import type { NextRequest } from 'next/server'
33

4-
import { EnhancedMiddleware } from '@netlify/plugin-nextjs/middleware'
4+
import { MiddlewareRequest } from '@netlify/plugin-nextjs/middleware'
55

66
export async function middleware(request: NextRequest) {
77
let response
@@ -11,7 +11,7 @@ export async function middleware(request: NextRequest) {
1111

1212
if (pathname.startsWith('/static')) {
1313
// Unlike NextResponse.next(), this actually sends the request to the origin
14-
const res = await new EnhancedMiddleware(request).next()
14+
const res = await new MiddlewareRequest(request).next()
1515
const message = `This was static but has been transformed in ${request.geo.city}`
1616

1717
// Transform the response page data
@@ -38,13 +38,13 @@ export async function middleware(request: NextRequest) {
3838
if (pathname.startsWith('/api/hello')) {
3939
// Add a header to the request
4040
request.headers.set('x-hello', 'world')
41-
return new EnhancedMiddleware(request).next()
41+
return new MiddlewareRequest(request).next()
4242
}
4343

4444
if (pathname.startsWith('/headers')) {
4545
// Add a header to the rewritten request
4646
request.headers.set('x-hello', 'world')
47-
return new EnhancedMiddleware(request).rewrite('/api/hello')
47+
return new MiddlewareRequest(request).rewrite('/api/hello')
4848
}
4949

5050
if (pathname.startsWith('/cookies')) {

plugin/src/middleware/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './enhanced-next-response'
2-
export * from './enhanced-middleware'
1+
export * from './next-origin-response'
2+
export * from './middleware-request'
33
export * from './html-rewriter'

plugin/src/middleware/enhanced-middleware.ts renamed to plugin/src/middleware/middleware-request.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextURL } from 'next/dist/server/web/next-url'
33
import { NextResponse } from 'next/server'
44
import type { NextRequest } from 'next/server'
55

6-
import { EnhancedNextResponse } from './enhanced-next-response'
6+
import { NextOriginResponse } from './next-origin-response'
77

88
// TODO: add Context type
99
type Context = {
@@ -21,7 +21,7 @@ type AugmentedGeo = NextRequest['geo'] & {
2121
/**
2222
* Supercharge your Next middleware with Netlify Edge Functions
2323
*/
24-
export class EnhancedMiddleware {
24+
export class MiddlewareRequest {
2525
context: Context
2626
originalRequest: Request
2727

@@ -44,10 +44,10 @@ export class EnhancedMiddleware {
4444
})
4545
}
4646

47-
async next(): Promise<EnhancedNextResponse> {
47+
async next(): Promise<NextOriginResponse> {
4848
this.applyHeaders()
4949
const response = await this.context.next()
50-
return new EnhancedNextResponse(response)
50+
return new NextOriginResponse(response)
5151
}
5252

5353
rewrite(destination: string | URL | NextURL, init?: ResponseInit): NextResponse {
@@ -57,5 +57,12 @@ export class EnhancedMiddleware {
5757
this.applyHeaders()
5858
return NextResponse.rewrite(destination, init)
5959
}
60+
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)
66+
}
6067
}
6168
/* eslint-enable no-underscore-dangle */

plugin/src/middleware/enhanced-next-response.ts renamed to plugin/src/middleware/next-origin-response.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ export type NextDataTransform = <T extends Record<string, any>>(props: T) => T
77

88
// A NextReponse that wraps the Netlify origin response
99
// We can't pass it through directly, because Next disallows returning a response body
10-
export class EnhancedNextResponse extends NextResponse {
10+
export class NextOriginResponse extends NextResponse {
1111
private readonly dataTransforms: NextDataTransform[]
1212
private readonly elementHandlers: Array<[selector: string, handlers: ElementHandlers]>
1313
constructor(public originResponse: Response) {
1414
super()
15-
this.originResponse = originResponse
1615

1716
// These are private in Node when compiling, but we access them in Deno at runtime
1817
Object.defineProperty(this, 'dataTransforms', {

plugin/src/templates/edge/utils.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ export const addMiddlewareHeaders = async (
3737
return response
3838
}
3939

40-
interface EnhancedNextResponse extends Response {
40+
interface NextOriginResponse extends Response {
4141
originResponse: Response
4242
dataTransforms: NextDataTransform[]
4343
elementHandlers: Array<[selector: string, handlers: ElementHandlers]>
4444
}
4545

46-
interface EnhancedMiddleware {
46+
interface MiddlewareRequest {
4747
request: Request
4848
context: Context
4949
originalRequest: Request
50-
next(): Promise<EnhancedNextResponse>
50+
next(): Promise<NextOriginResponse>
5151
rewrite(destination: string | URL, init?: ResponseInit): Response
5252
}
5353

54-
function isEnhancedMiddleware(response: Response | EnhancedMiddleware): response is EnhancedMiddleware {
54+
function isMiddlewareRequest(response: Response | MiddlewareRequest): response is MiddlewareRequest {
5555
return 'originalRequest' in response
5656
}
5757

58-
function isEnhancedNextResponse(response: Response | EnhancedNextResponse): response is EnhancedNextResponse {
58+
function isNextOriginResponse(response: Response | NextOriginResponse): response is NextOriginResponse {
5959
return 'dataTransforms' in response
6060
}
6161

@@ -68,11 +68,11 @@ export const buildResponse = async ({
6868
request: Request
6969
context: Context
7070
}) => {
71-
// They've returned the EnhancedMiddleware directly, so we'll call `next()` for them.
72-
if (isEnhancedMiddleware(result.response)) {
71+
// They've returned the MiddlewareRequest directly, so we'll call `next()` for them.
72+
if (isMiddlewareRequest(result.response)) {
7373
result.response = await result.response.next()
7474
}
75-
if (isEnhancedNextResponse(result.response)) {
75+
if (isNextOriginResponse(result.response)) {
7676
const { response } = result
7777
// If it's JSON we don't need to use the rewriter, we can just parse it
7878
if (response.originResponse.headers.get('content-type')?.includes('application/json')) {

0 commit comments

Comments
 (0)