|
|
import type { IncomingMessage, ServerResponse } from 'http' |
|
|
import type { PagesAPIRouteDefinition } from '../../route-definitions/pages-api-route-definition' |
|
|
import type { PageConfig } from '../../../types' |
|
|
import type { ParsedUrlQuery } from 'querystring' |
|
|
import { wrapApiHandler, type __ApiPreviewProps } from '../../api-utils' |
|
|
import type { RouteModuleOptions } from '../route-module' |
|
|
|
|
|
import { RouteModule, type RouteModuleHandleContext } from '../route-module' |
|
|
import { apiResolver } from '../../api-utils/node/api-resolver' |
|
|
import type { RevalidateFn } from '../../lib/router-utils/router-server-context' |
|
|
|
|
|
type PagesAPIHandleFn = ( |
|
|
req: IncomingMessage, |
|
|
res: ServerResponse |
|
|
) => Promise<void> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type PagesAPIModule = typeof import('../../../build/templates/pages-api') |
|
|
|
|
|
type PagesAPIUserlandModule = { |
|
|
|
|
|
|
|
|
|
|
|
readonly default: PagesAPIHandleFn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readonly config?: PageConfig |
|
|
} |
|
|
|
|
|
type PagesAPIRouteHandlerContext = RouteModuleHandleContext & { |
|
|
|
|
|
|
|
|
|
|
|
req?: IncomingMessage |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res?: ServerResponse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hostname?: string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
allowedRevalidateHeaderKeys?: string[] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trustHostHeader?: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query: ParsedUrlQuery |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
previewProps: __ApiPreviewProps |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dev: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
propagateError: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
page: string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onError?: Parameters<typeof apiResolver>[8] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
multiZoneDraftMode?: boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internalRevalidate?: RevalidateFn |
|
|
} |
|
|
|
|
|
export type PagesAPIRouteModuleOptions = RouteModuleOptions< |
|
|
PagesAPIRouteDefinition, |
|
|
PagesAPIUserlandModule |
|
|
> |
|
|
|
|
|
export class PagesAPIRouteModule extends RouteModule< |
|
|
PagesAPIRouteDefinition, |
|
|
PagesAPIUserlandModule |
|
|
> { |
|
|
private apiResolverWrapped: typeof apiResolver |
|
|
|
|
|
constructor(options: PagesAPIRouteModuleOptions) { |
|
|
super(options) |
|
|
|
|
|
if (typeof options.userland.default !== 'function') { |
|
|
throw new Error( |
|
|
`Page ${options.definition.page} does not export a default function.` |
|
|
) |
|
|
} |
|
|
|
|
|
this.apiResolverWrapped = wrapApiHandler( |
|
|
options.definition.page, |
|
|
apiResolver |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async render( |
|
|
req: IncomingMessage, |
|
|
res: ServerResponse, |
|
|
context: PagesAPIRouteHandlerContext |
|
|
): Promise<void> { |
|
|
const { apiResolverWrapped } = this |
|
|
await apiResolverWrapped( |
|
|
req, |
|
|
res, |
|
|
context.query, |
|
|
this.userland, |
|
|
{ |
|
|
...context.previewProps, |
|
|
trustHostHeader: context.trustHostHeader, |
|
|
allowedRevalidateHeaderKeys: context.allowedRevalidateHeaderKeys, |
|
|
hostname: context.hostname, |
|
|
multiZoneDraftMode: context.multiZoneDraftMode, |
|
|
dev: context.dev, |
|
|
internalRevalidate: context.internalRevalidate, |
|
|
}, |
|
|
context.propagateError, |
|
|
context.dev, |
|
|
context.page, |
|
|
context.onError |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
export default PagesAPIRouteModule |
|
|
|