Skip to content

Commit

Permalink
feat(ts): expose MiddlewareConfig interface (#61576)
Browse files Browse the repository at this point in the history
### What?

Expose the `MiddlewareConfig` interface.

### Why?

You can now `import type { MiddlewareConfig } from "next/server"` to
type the `config` object in your `middleware.ts` file.

Now you an type the entire file for example like so:
```ts
// middleware.ts
import type { NextMiddleware, MiddlewareConfig } from "next/server"

export const middleware: NextMiddleware = async (req) => {
  //...
}

export const config: MiddlewareConfig = {
  //...
}
```

### How?

Re-exported the interface from its current location via
`server/web/types`, to colocate it with `NextMidldeware`.

I wonder if we could somehow type this file automatically, but it might
be dependent on microsoft/TypeScript#38511

Closes NEXT-2308

[Slack
thread](https://vercel.slack.com/archives/C03S9JCH2Q5/p1706287433026409?thread_ts=1706058855.423019&cid=C03S9JCH2Q5),
[Slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1706659724141899)
  • Loading branch information
balazsorban44 authored Feb 5, 2024
1 parent d1fa287 commit 492b415
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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 { 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'
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/web/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion test/production/middleware-typescript/app/middleware.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -18,3 +23,9 @@ export const middleware: NextMiddleware = function (request) {
})
}
}

export const config: MiddlewareConfig = {
matchers: [],
regions: [],
unstable_allowDynamicGlobs: undefined,
}

0 comments on commit 492b415

Please sign in to comment.