File size: 3,851 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
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>
/**
* The PagesAPIModule is the type of the module exported by the bundled Pages
* API module.
*/
export type PagesAPIModule = typeof import('../../../build/templates/pages-api')
type PagesAPIUserlandModule = {
/**
* The exported handler method.
*/
readonly default: PagesAPIHandleFn
/**
* The exported page config.
*/
readonly config?: PageConfig
}
type PagesAPIRouteHandlerContext = RouteModuleHandleContext & {
/**
* The incoming server request in non-edge runtime.
*/
req?: IncomingMessage
/**
* The outgoing server response in non-edge runtime.
*/
res?: ServerResponse
/**
* The hostname for the request.
*/
hostname?: string
/**
* Keys allowed in the revalidate call.
*/
allowedRevalidateHeaderKeys?: string[]
/**
* Whether to trust the host header.
*/
trustHostHeader?: boolean
/**
* The query for the request.
*/
query: ParsedUrlQuery
/**
* The preview props used by the `preview` API.
*/
previewProps: __ApiPreviewProps
/**
* True if the server is in development mode.
*/
dev: boolean
/**
* Whether errors should be left uncaught to handle
* higher up
*/
propagateError: boolean
/**
* The page that's being rendered.
*/
page: string
/**
* The error handler for the request.
*/
onError?: Parameters<typeof apiResolver>[8]
/**
* whether multi-zone flag is enabled for draft mode
*/
multiZoneDraftMode?: boolean
/**
* Internal revalidate function to avoid revalidating
* over the network
*/
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
)
}
/**
*
* @param req the incoming server request
* @param res the outgoing server response
* @param context the context for the render
*/
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
|