From 794c4269277f3d680554a30a60d57771db8dcff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Fri, 2 Feb 2024 15:40:40 +0100 Subject: [PATCH 1/3] feat(ts): expose `MiddlewareConfig` interface --- packages/next/server.d.ts | 1 + packages/next/src/build/analysis/get-page-static-info.ts | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/next/server.d.ts b/packages/next/server.d.ts index a47014498321d..113628a8a1a39 100644 --- a/packages/next/server.d.ts +++ b/packages/next/server.d.ts @@ -8,6 +8,7 @@ export { NextFetchEvent } from 'next/dist/server/web/spec-extension/fetch-event' export { NextRequest } from 'next/dist/server/web/spec-extension/request' export { NextResponse } from 'next/dist/server/web/spec-extension/response' export { NextMiddleware } from 'next/dist/server/web/types' +export type { MiddlewareConfig } from 'next/dist/build/analysis/get-page-static-info' export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-agent' export { userAgent } from 'next/dist/server/web/spec-extension/user-agent' export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url' diff --git a/packages/next/src/build/analysis/get-page-static-info.ts b/packages/next/src/build/analysis/get-page-static-info.ts index 57a008dbe9408..ab68fb4365e93 100644 --- a/packages/next/src/build/analysis/get-page-static-info.ts +++ b/packages/next/src/build/analysis/get-page-static-info.ts @@ -24,7 +24,12 @@ import { PAGE_TYPES } from '../../lib/page-types' // Don't forget to update the next-types-plugin file as well const AUTHORIZED_EXTRA_ROUTER_PROPS = ['maxDuration'] +/** @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional */ export interface MiddlewareConfig { + /** + * @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#matcher + * @see https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths + */ matchers?: MiddlewareMatcher[] unstable_allowDynamicGlobs?: string[] regions?: string[] | string From 4cfbb11f8e52a909a9d46f6cec15e5927eca9a0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Fri, 2 Feb 2024 15:45:02 +0100 Subject: [PATCH 2/3] re-export --- packages/next/server.d.ts | 3 +-- packages/next/src/server/web/types.ts | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/next/server.d.ts b/packages/next/server.d.ts index 113628a8a1a39..d5eb0a58dd801 100644 --- a/packages/next/server.d.ts +++ b/packages/next/server.d.ts @@ -7,8 +7,7 @@ declare global { export { NextFetchEvent } from 'next/dist/server/web/spec-extension/fetch-event' export { NextRequest } from 'next/dist/server/web/spec-extension/request' export { NextResponse } from 'next/dist/server/web/spec-extension/response' -export { NextMiddleware } from 'next/dist/server/web/types' -export type { MiddlewareConfig } from 'next/dist/build/analysis/get-page-static-info' +export { NextMiddleware, MiddlewareConfig } from 'next/dist/server/web/types' export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-agent' export { userAgent } from 'next/dist/server/web/spec-extension/user-agent' export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url' diff --git a/packages/next/src/server/web/types.ts b/packages/next/src/server/web/types.ts index 672acf3e502e1..6722641e7c78d 100644 --- a/packages/next/src/server/web/types.ts +++ b/packages/next/src/server/web/types.ts @@ -6,6 +6,8 @@ import type { CloneableBody } from '../body-streams' import type { OutgoingHttpHeaders } from 'http' import type { FetchMetrics } from '../base-http' +export type { MiddlewareConfig } from '../../build/analysis/get-page-static-info' + export interface RequestData { geo?: { city?: string From f1a8b159e128c76ed24da2dd8f8f3e0103304b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Mon, 5 Feb 2024 04:31:11 +0100 Subject: [PATCH 3/3] add TS test --- .../middleware-typescript/app/middleware.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/production/middleware-typescript/app/middleware.ts b/test/production/middleware-typescript/app/middleware.ts index 5010b26952794..cdf0780acd41a 100644 --- a/test/production/middleware-typescript/app/middleware.ts +++ b/test/production/middleware-typescript/app/middleware.ts @@ -1,4 +1,9 @@ -import { NextMiddleware, NextResponse, URLPattern } from 'next/server' +import { + NextMiddleware, + NextResponse, + URLPattern, + MiddlewareConfig, +} from 'next/server' export const middleware: NextMiddleware = function (request) { const pattern = new URLPattern({ @@ -18,3 +23,9 @@ export const middleware: NextMiddleware = function (request) { }) } } + +export const config: MiddlewareConfig = { + matchers: [], + regions: [], + unstable_allowDynamicGlobs: undefined, +}