MCPcopy
hub / github.com/vercel/next.js / apiResolver

Function apiResolver

packages/next/src/server/api-utils/node/api-resolver.ts:331–489  ·  view source on GitHub ↗
(
  req: IncomingMessage,
  res: ServerResponse,
  query: any,
  resolverModule: any,
  apiContext: ApiContext,
  propagateError: boolean,
  dev?: boolean,
  page?: string,
  onError?: InstrumentationOnRequestError
)

Source from the content-addressed store, hash-verified

329}
330
331export async function apiResolver(
332 req: IncomingMessage,
333 res: ServerResponse,
334 query: any,
335 resolverModule: any,
336 apiContext: ApiContext,
337 propagateError: boolean,
338 dev?: boolean,
339 page?: string,
340 onError?: InstrumentationOnRequestError
341): Promise<void> {
342 const apiReq = req as NextApiRequest
343 const apiRes = res as NextApiResponse
344
345 try {
346 if (!resolverModule) {
347 res.statusCode = 404
348 res.end('Not Found')
349 return
350 }
351 const config: PageConfig = resolverModule.config || {}
352 const bodyParser = config.api?.bodyParser !== false
353 const responseLimit = config.api?.responseLimit ?? true
354 const externalResolver = config.api?.externalResolver || false
355
356 // Parsing of cookies
357 setLazyProp({ req: apiReq }, 'cookies', getCookieParser(req.headers))
358 // Ensure req.query is a writable, enumerable property by using Object.defineProperty.
359 // This addresses Express 5.x, which defines query as a getter only (read-only).
360 Object.defineProperty(apiReq, 'query', {
361 value: { ...query },
362 writable: true,
363 enumerable: true,
364 configurable: true,
365 })
366 // Parsing preview data
367 setLazyProp({ req: apiReq }, 'previewData', () =>
368 tryGetPreviewData(req, res, apiContext, !!apiContext.multiZoneDraftMode)
369 )
370 // Checking if preview mode is enabled
371 setLazyProp({ req: apiReq }, 'preview', () =>
372 apiReq.previewData !== false ? true : undefined
373 )
374 // Set draftMode to the same value as preview
375 setLazyProp({ req: apiReq }, 'draftMode', () => apiReq.preview)
376
377 // Parsing of body
378 if (bodyParser && !apiReq.body) {
379 apiReq.body = await parseBody(
380 apiReq,
381 config.api && config.api.bodyParser && config.api.bodyParser.sizeLimit
382 ? config.api.bodyParser.sizeLimit
383 : '1mb'
384 )
385 }
386
387 let contentLength = 0
388 const maxContentLength = getMaxContentLength(responseLimit)

Callers

nothing calls this directly

Calls 15

setLazyPropFunction · 0.90
getCookieParserFunction · 0.90
tryGetPreviewDataFunction · 0.90
parseBodyFunction · 0.90
sendStatusCodeFunction · 0.90
redirectFunction · 0.90
clearPreviewDataFunction · 0.90
interopDefaultFunction · 0.90
isResSentFunction · 0.90
sendErrorFunction · 0.90
getMaxContentLengthFunction · 0.85
sendDataFunction · 0.85

Tested by

no test coverage detected