import { PostEntity } from '@10up/headless-core'; import type { NextApiRequest, NextApiResponse } from 'next'; import { PreviewData } from './types'; /** * The options supported by {@link previewHandler} */ export type PreviewHandlerOptions = { /** * If passed will override the behavior of redirecting to the previewed post. * * If set you should handle the redirect yourself by calling `res.redirect`. * * **Important**: You should not need to override this but if you do, uou must append `-preview=true` to the end of the redirected url. * * This can be used to customize the preview url. * * ```ts * // pages/api/preview.js * import { previewHandler } from '@10up/headless-next'; * * export default async function handler(req, res) { * return previewHandler(req, res, { * onRedirect(req, res, previewData) => { * res.redirect(`/my-custom-preview-route/${previewData.id}-preview=true`); * } * }); * } * ``` */ onRedirect?: (req: NextApiRequest, res: NextApiResponse, previewData: PreviewData, defaultRedirect?: PreviewHandlerOptions['onRedirect']) => NextApiResponse; /** * If passed, this function will be called when the preview data is fetched and allows * for additional preview data to be set. * * The preview data is then passed to `res.setPreviewData`. Be mindful of limitations of this function. * You should not pass full post objects, just ids and other data that can be used to fetch the post. */ preparePreviewData?: (req: NextApiRequest, res: NextApiResponse, post: PostEntity, previewData: PreviewData) => PreviewData; }; /** * The PreviewHandler is responsible for handling preview requests. * * Handling Previews requires the Headless WordPress Plugin. * * **Important**: This function is meant to be used in a api route at `/pages/api/preview`. * * ### Usage * * ```ts * // pages/api/preview.js * import { previewHandler } from '@10up/headless-next'; * * export default async function handler(req, res) { * return previewHandler(req, res); * } * ``` * * @param req The request object, * @param res The response object. * @param options The PreviewHandlerOptions {@link PreviewHandlerOptions} * * @returns A response object. * * @category API handlers */ export declare function previewHandler(req: NextApiRequest, res: NextApiResponse, options?: PreviewHandlerOptions): Promise>;