Unverified Commit cc9e1ea3 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix middleware types with skipLibCheck: false (#32025)

This ensures type checking passes correctly for middleware types when `skipLibCheck: false` is set in `tsconfig.json`. This also moves the `middleware-types` to be an isolated test to ensure it isn't relying on any monorepo dependencies. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/31992#issuecomment-984174872
parent 25371dd1
import type { I18NConfig } from '../../config-shared'
import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import { NextURL } from '../next-url'
import { toNodeHeaders } from '../utils'
import cookie from 'next/dist/compiled/cookie'
import { CookieSerializeOptions } from '../types'
const INTERNALS = Symbol('internal response')
const REDIRECTS = new Set([301, 302, 303, 307, 308])
......
......@@ -7,6 +7,17 @@ export interface NodeHeaders {
[header: string]: string | string[] | undefined
}
export interface CookieSerializeOptions {
domain?: string
encode?(val: string): string
expires?: Date
httpOnly?: boolean
maxAge?: number
path?: string
sameSite?: boolean | 'lax' | 'strict' | 'none'
secure?: boolean
}
export interface RequestData {
geo?: {
city?: string
......
/* eslint-env jest */
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '..')
describe('TypeScript Middleware', () => {
describe('next build', () => {
it('should not fail to build middleware', async () => {
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).not.toMatch(/Failed to compile/)
expect(stderr).not.toMatch(/is not assignable to type/)
expect(code).toBe(0)
})
})
})
......@@ -5,7 +5,7 @@ export const middleware: NextMiddleware = function (request) {
console.log(request.ua?.isBot)
console.log(request.ua?.ua)
return new Response(null, {
return new Response('hello from middleware', {
headers: {
'req-url-basepath': request.nextUrl.basePath,
'req-url-pathname': request.nextUrl.pathname,
......
......@@ -6,7 +6,7 @@
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"skipLibCheck": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
......
/* eslint-env jest */
import { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
const appDir = join(__dirname, '../app')
describe('should set-up next', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(appDir, 'pages')),
'tsconfig.json': new FileRef(join(appDir, 'tsconfig.json')),
'next.config.js': new FileRef(join(appDir, 'next.config.js')),
},
dependencies: {
typescript: 'latest',
'@types/react': 'latest',
'@types/react-dom': 'latest',
},
})
})
afterAll(() => next.destroy())
it('should have built and started', async () => {
const html = await renderViaHTTP(next.url, '/interface/static')
expect(html).toContain('hello from middleware')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment