未验证 提交 33234ca1 编写于 作者: A Asher Foster 提交者: GitHub

Attach previewData to API Route request (#13373)

Co-authored-by: NJoe Haddad <timer150@gmail.com>
Co-authored-by: NTim Neutkens <timneutkens@me.com>
上级 38bd1a02
......@@ -199,6 +199,12 @@ export interface NextApiRequest extends IncomingMessage {
body: any
env: Env
preview?: boolean
/**
* Preview data set on the request, if any
* */
previewData?: any
}
/**
......
......@@ -46,7 +46,16 @@ export async function apiResolver(
setLazyProp({ req: apiReq }, 'cookies', getCookieParser(req))
// Parsing query string
setLazyProp({ req: apiReq, params }, 'query', getQueryParser(req))
// // Parsing of body
// Parsing preview data
setLazyProp({ req: apiReq }, 'previewData', () =>
tryGetPreviewData(req, res, apiContext)
)
// Checking if preview mode is enabled
setLazyProp({ req: apiReq }, 'preview', () =>
apiReq.previewData !== false ? true : undefined
)
// Parsing of body
if (bodyParser) {
apiReq.body = await parseBody(
apiReq,
......
export default (req, res) => {
const { preview, previewData } = req
res.json({
preview,
previewData,
})
}
......@@ -182,6 +182,27 @@ function runTests(startServer = nextStart) {
expect(cookies[1]).not.toHaveProperty('Max-Age')
})
it('should pass undefined to API routes when not in preview', async () => {
const res = await fetchViaHTTP(appPort, `/api/read`)
const json = await res.json()
expect(json).toMatchObject({})
})
it('should pass the preview data to API routes', async () => {
const res = await fetchViaHTTP(
appPort,
'/api/read',
{},
{ headers: { Cookie: previewCookieString } }
)
const json = await res.json()
expect(json).toMatchObject({
preview: true,
previewData: { lets: 'goooo' },
})
})
afterAll(async () => {
await killApp(app)
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册