-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loose types of app routes return value (#55849)
### What #51394 introduced a pretty strict type of return value of route type that causing failure with `next build`. There're few ways of writing a app route, it could contain few return values based on the usage: * return a `Response` or promise of it * return `NextResponse` of promise of it, since it's extended from `Response`, same type * use `redirect()` or `notFound(), since it returns `never`, and the below code is not reached, the handler itself could still return void. e.g. using `redirect` in a `GET` route We loosed the type so `redirect()` can be still allowed without specifying the return value there. Related typescript issue: microsoft/TypeScript#16608 (comment) ### How * Re-enable the bail on types / build error in the app-routes tests * Separate the tests, move runtime erroring ones to `test/e2e/app-dir/app-routes-errors` * Add new case to app-routes tests of mixed return value Closes #55623 Related #55604
- Loading branch information
Showing
28 changed files
with
106 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
test/development/api-route-errors/app/pages/api/uncaught-exception.js
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
test/development/api-route-errors/app/pages/api/unhandled-rejection.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/delete/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { DELETE as delete } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/get/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GET as get } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/head/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { HEAD as head } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/options/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { OPTIONS as options } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/patch/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PATCH as patch } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/post/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { POST as post } from '../../../hello' |
1 change: 1 addition & 0 deletions
1
test/development/app-dir/app-routes-error/app/lowercase/put/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PUT as put } from '../../../hello' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const helloHandler = async () => { | ||
if (typeof WebSocket === 'undefined') { | ||
throw new Error('missing WebSocket constructor!!') | ||
} | ||
|
||
return new Response('hello, world') | ||
} | ||
|
||
export const GET = helloHandler | ||
export const HEAD = helloHandler | ||
export const OPTIONS = helloHandler | ||
export const POST = helloHandler | ||
export const PUT = helloHandler | ||
export const DELETE = helloHandler | ||
export const PATCH = helloHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createNextDescribe } from 'e2e-utils' | ||
import { check } from 'next-test-utils' | ||
|
||
createNextDescribe( | ||
'app-dir - app routes errors', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next }) => { | ||
describe('bad lowercase exports', () => { | ||
it.each([ | ||
['get'], | ||
['head'], | ||
['options'], | ||
['post'], | ||
['put'], | ||
['delete'], | ||
['patch'], | ||
])( | ||
'should print an error when using lowercase %p in dev', | ||
async (method: string) => { | ||
await next.fetch('/lowercase/' + method) | ||
|
||
await check(() => { | ||
expect(next.cliOutput).toContain( | ||
`Detected lowercase method '${method}' in` | ||
) | ||
expect(next.cliOutput).toContain( | ||
`Export the uppercase '${method.toUpperCase()}' method name to fix this error.` | ||
) | ||
expect(next.cliOutput).toMatch( | ||
/Detected lowercase method '.+' in '.+\/route\.js'\. Export the uppercase '.+' method name to fix this error\./ | ||
) | ||
return 'yes' | ||
}, 'yes') | ||
} | ||
) | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { redirect } from 'next/navigation' | ||
import { NextResponse } from 'next/server' | ||
|
||
export async function GET() { | ||
if (process.env.COND_1) { | ||
return NextResponse.json({ a: '1' }) | ||
} else if (process.env.COND_2) { | ||
redirect('/no-response') | ||
} else { | ||
return new Response('3') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters