|
1 | 1 | import type { ServerResponse } from 'node:http'
|
2 | 2 | import { isPromise } from 'node:util/types'
|
3 | 3 |
|
| 4 | +import type { NextApiResponse } from 'next' |
| 5 | + |
4 | 6 | import type { RequestContext } from './handlers/request-context.cjs'
|
5 | 7 |
|
| 8 | +type ResRevalidateMethod = NextApiResponse['revalidate'] |
| 9 | + |
| 10 | +function isRevalidateMethod( |
| 11 | + key: string, |
| 12 | + nextResponseField: unknown, |
| 13 | +): nextResponseField is ResRevalidateMethod { |
| 14 | + return key === 'revalidate' && typeof nextResponseField === 'function' |
| 15 | +} |
| 16 | + |
6 | 17 | // Needing to proxy the response object to intercept the revalidate call for on-demand revalidation on page routes
|
7 | 18 | export const nextResponseProxy = (res: ServerResponse, requestContext: RequestContext) => {
|
8 | 19 | return new Proxy(res, {
|
9 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
10 |
| - get(target: any[string], key: string) { |
11 |
| - const originalValue = target[key] |
12 |
| - if (key === 'revalidate') { |
13 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
14 |
| - return async function newRevalidate(...args: any[]) { |
| 20 | + get(target: ServerResponse, key: string) { |
| 21 | + const originalValue = Reflect.get(target, key) |
| 22 | + if (isRevalidateMethod(key, originalValue)) { |
| 23 | + return function newRevalidate(...args: Parameters<ResRevalidateMethod>) { |
15 | 24 | requestContext.didPagesRouterOnDemandRevalidate = true
|
16 | 25 |
|
17 |
| - const result = originalValue?.apply(target, args) |
| 26 | + const result = originalValue.apply(target, args) |
18 | 27 | if (result && isPromise(result)) {
|
19 | 28 | requestContext.trackBackgroundWork(result)
|
20 | 29 | }
|
|
0 commit comments