payload
Version:
Node, React, Headless CMS and Application Framework built on Next.js
38 lines (37 loc) • 1.36 kB
JavaScript
// @ts-strict-ignore
import { status as httpStatus } from 'http-status';
import { extractJWT } from '../../auth/extractJWT.js';
import { getRequestCollectionWithID } from '../../utilities/getRequestEntity.js';
import { headersWithCors } from '../../utilities/headersWithCors.js';
import { isNumber } from '../../utilities/isNumber.js';
import { findByIDOperation } from '../operations/findByID.js';
export const previewHandler = async (req)=>{
const { id, collection } = getRequestCollectionWithID(req);
const { searchParams } = req;
const depth = searchParams.get('depth');
const doc = await findByIDOperation({
id,
collection,
depth: isNumber(depth) ? Number(depth) : undefined,
draft: searchParams.get('draft') === 'true',
req
});
let previewURL;
const generatePreviewURL = req.payload?.collections?.[collection.config.slug]?.config?.admin?.preview;
const token = extractJWT(req);
if (typeof generatePreviewURL === 'function') {
previewURL = await generatePreviewURL(doc, {
locale: req.locale,
req,
token
});
}
return Response.json(previewURL, {
headers: headersWithCors({
headers: new Headers(),
req
}),
status: httpStatus.OK
});
};
//# sourceMappingURL=preview.js.map