Skip to content

Commit

Permalink
Loose types of app routes return value (#55849)
Browse files Browse the repository at this point in the history
### 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
huozhi authored Sep 25, 2023
1 parent adcf6e6 commit a63b89b
Show file tree
Hide file tree
Showing 28 changed files with 106 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ if ('${method}' in entry) {
'${method}'
>
>()
${
''
// Adding void to support never return type without explicit return:
// e.g. notFound() will interrupt the execution but the handler return type is inferred as void.
// x-ref: https://github.com/microsoft/TypeScript/issues/16608#issuecomment-309327984
}
checkFields<
Diff<
{
__tag__: '${method}',
__return_type__: Response | Promise<Response>
__return_type__: Response | void | never | Promise<Response | void | never>
},
{
__tag__: '${method}',
Expand Down Expand Up @@ -428,7 +434,7 @@ declare module 'next/link' {
import type { LinkProps as OriginalLinkProps } from 'next/dist/client/link.js'
import type { AnchorHTMLAttributes, DetailedHTMLProps } from 'react'
import type { UrlObject } from 'url'
type LinkRestProps = Omit<
Omit<
DetailedHTMLProps<
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/trace/trace-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import findUp from 'next/dist/compiled/find-up'
import fsPromise from 'fs/promises'
import child_process from 'child_process'
import assert from 'assert'
// @ts-ignore
import fetch from 'next/dist/compiled/node-fetch'
import os from 'os'
import { createInterface } from 'readline'
Expand Down
6 changes: 6 additions & 0 deletions packages/next/types/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ declare module 'next/dist/compiled/@next/react-refresh-utils/dist/ReactRefreshWe
export = m
}

declare module 'next/dist/compiled/node-fetch' {
import fetch from 'node-fetch'
export * from 'node-fetch'
export default fetch
}

declare module 'next/dist/compiled/node-html-parser' {
export * from 'node-html-parser'
}
Expand Down
3 changes: 0 additions & 3 deletions test/development/api-route-errors/app/pages/api/error.js

This file was deleted.

This file was deleted.

This file was deleted.

67 changes: 0 additions & 67 deletions test/development/api-route-errors/index.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DELETE as delete } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { GET as get } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HEAD as head } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { OPTIONS as options } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PATCH as patch } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { POST as post } from '../../../hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PUT as put } from '../../../hello'
15 changes: 15 additions & 0 deletions test/development/app-dir/app-routes-error/hello.js
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
40 changes: 40 additions & 0 deletions test/development/app-dir/app-routes-error/index.test.ts
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')
}
)
})
}
)
52 changes: 16 additions & 36 deletions test/e2e/app-dir/app-routes/app-custom-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,45 +589,25 @@ createNextDescribe(
})

if (isNextDev) {
describe('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(basePath + '/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\.ts'\. Export the uppercase '.+' method name to fix this error\./
)
return 'yes'
}, 'yes')
}
)
})

describe('invalid exports', () => {
beforeAll(async () => {
await next.patchFile(
'app/default/route.ts',
`\
export { GET as default } from '../../handlers/hello'
`
)
})
afterAll(async () => {
await next.deleteFile('app/default/route.ts')
})
it('should print an error when exporting a default handler in dev', async () => {
const res = await next.fetch(basePath + '/default')

// Ensure we get a 405 (Method Not Allowed) response when there is no
// exported handler for the GET method.
expect(res.status).toEqual(405)
await check(async () => {
const res = await next.fetch(basePath + '/default')

await check(() => {
// Ensure we get a 405 (Method Not Allowed) response when there is no
// exported handler for the GET method.
expect(res.status).toEqual(405)
expect(next.cliOutput).toMatch(
/Detected default export in '.+\/route\.ts'\. Export a named export for each HTTP method instead\./
)
Expand Down
1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/default/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/delete/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/get/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/head/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/options/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/patch/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/post/route.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/e2e/app-dir/app-routes/app/lowercase/put/route.ts

This file was deleted.

12 changes: 12 additions & 0 deletions test/e2e/app-dir/app-routes/app/mixed-response/route.ts
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')
}
}
2 changes: 1 addition & 1 deletion test/e2e/app-dir/app-routes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ type Cookies = {
export function getRequestMeta(
headersOrCookies:
| Headers
| import('node-fetch').Headers
| Cookies
| ReadonlyHeaders
| ReadonlyRequestCookies
| import('next/dist/compiled/node-fetch').Headers
): Record<string, any> {
const headerOrCookie = headersOrCookies.get(KEY)
if (!headerOrCookie) return {}
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/app-dir/app-routes/next.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* @type {import('next').NextConfig}
*/
const config = {
typescript: {
ignoreBuildErrors: true,
},
}
const config = {}

if (process.env.BASE_PATH) {
config.basePath = process.env.BASE_PATH
Expand Down

0 comments on commit a63b89b

Please sign in to comment.