next-acms
Version:
Tools for building decoupled front-ends for Acquia CMS
46 lines (43 loc) • 1.57 kB
JavaScript
async function revalidate(request, response) {
let slug = request.query.slug;
const secret = request.query.secret;
// Validate secret.
if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
return response.status(401).json({
message: 'Invalid secret.'
});
}
// Validate slug.
if (!slug) {
return response.status(400).json({
message: 'Invalid slug.'
});
}
// Fix for home slug.
if (slug === process.env.NEXT_PUBLIC_DRUPAL_FRONT_PAGE) {
slug = '/';
}
try {
await response.revalidate(slug);
return response.json({});
} catch (error) {
return response.status(404).json({
message: error.message
});
}
}
async function testApiCompatibility(contentTypes, drupal) {
let index;
try {
index = await drupal.getIndex();
} catch (e) {
throw new Error(e.message + '\n\n' + 'Failed to connect to the backend. See here for documentation about common reasons: \nhttps://github.com/acquia/next-acms/wiki/Debugging-common-errors-with-Next.js-and-Drupal-integration#incompatible-web-server-setup-or-incorrect-environment-variables-generated');
}
for (const type of contentTypes) {
if (!Object.keys(index.links).includes(type)) {
throw new Error(`Content type ${type} does not exist in the backend. \nSee here for documentation on content model mismatch: https://github.com/acquia/next-acms/wiki/Debugging-common-errors-with-Next.js-and-Drupal-integration#content-model-mismatch`);
}
}
}
export { revalidate, testApiCompatibility };
//# sourceMappingURL=index.modern.mjs.map