Skip to content

Commit e42c7cf

Browse files
authored
Add codemod for middleware to proxy (#84127)
Stacked to #84119 This PR adds a codemod for renaming `middleware` to `proxy`. Below are the behaviors: - Rename `middleware.*`, `src/middleware.*` to `proxy.*`, `src/proxy.*` - Rename function/variable exports from `middleware` to `proxy`. Default exports are skipped.
1 parent dd5e3ad commit e42c7cf

21 files changed

+460
-0
lines changed

packages/next-codemod/lib/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ export const TRANSFORMER_INQUIRER_CHOICES = [
121121
value: 'next-lint-to-eslint-cli',
122122
version: '16.0.0',
123123
},
124+
{
125+
title: 'Migrate from deprecated `middleware` convention to `proxy`',
126+
value: 'middleware-to-proxy',
127+
version: '16.0.0',
128+
},
124129
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
export async function middleware(request: NextRequest) {
4+
const response = await fetch('/api/auth')
5+
return NextResponse.redirect(new URL('/home', request.url))
6+
}
7+
8+
export const config = {
9+
matcher: '/about/:path*',
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
export async function proxy(request: NextRequest) {
4+
const response = await fetch('/api/auth')
5+
return NextResponse.redirect(new URL('/home', request.url))
6+
}
7+
8+
export const config = {
9+
matcher: '/about/:path*',
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
const middleware = (request: NextRequest) => {
4+
return NextResponse.redirect(new URL('/home', request.url))
5+
}
6+
7+
export { middleware }
8+
9+
export const config = {
10+
matcher: '/about/:path*',
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
const proxy = (request: NextRequest) => {
4+
return NextResponse.redirect(new URL('/home', request.url))
5+
}
6+
7+
export { proxy }
8+
9+
export const config = {
10+
matcher: '/about/:path*',
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
export default function middleware(request: NextRequest) {
4+
return NextResponse.redirect(new URL('/home', request.url))
5+
}
6+
7+
export const config = {
8+
matcher: '/about/:path*',
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
export default function proxy(request: NextRequest) {
4+
return NextResponse.redirect(new URL('/home', request.url))
5+
}
6+
7+
export const config = {
8+
matcher: '/about/:path*',
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
3+
const proxy = 'existing proxy variable'
4+
5+
function middleware() {
6+
return NextResponse.next()
7+
}
8+
9+
export { middleware as randomName }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
3+
const proxy = 'existing proxy variable'
4+
5+
function _proxy1() {
6+
return NextResponse.next()
7+
}
8+
9+
export { _proxy1 as randomName }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse, NextRequest } from 'next/server'
2+
3+
function middleware(request: NextRequest) {
4+
return NextResponse.redirect(new URL('/home', request.url))
5+
}
6+
7+
const config = {
8+
matcher: '/about/:path*',
9+
}
10+
11+
export { middleware, config }

0 commit comments

Comments
 (0)