strapi-plugin-preview-button
Version:
A plugin for Strapi CMS that adds a preview button and live view button to the content manager edit view.
80 lines (79 loc) • 2.06 kB
JavaScript
import has from "lodash/has";
import { errors } from "@strapi/utils";
const defaultConfig = {
contentTypes: null
};
const config = {
default: defaultConfig,
validator(config2) {
if (!has(config2, "contentTypes")) {
return;
}
if (!Array.isArray(config2.contentTypes)) {
throw new errors.ValidationError(`Must define contentTypes as an array.`);
}
config2.contentTypes.forEach((entry) => {
const hasDraft = has(entry, "draft");
const hasPublished = has(entry, "published");
if (!has(entry, "uid")) {
throw new errors.ValidationError("Missing uid prop.");
}
if (!hasDraft && !hasPublished) {
throw new errors.ValidationError(
`The config for ${entry.uid} requires at least a draft or published prop to be defined.`
);
}
if (hasDraft && !has(entry, ["draft", "url"])) {
throw new errors.ValidationError(`Missing draft URL for ${entry.uid}.`);
}
if (hasPublished && !has(entry, ["published", "url"])) {
throw new errors.ValidationError(`Missing published URL for ${entry.uid}.`);
}
});
}
};
const PLUGIN_ID = "preview-button";
const getService = (name) => global.strapi.plugin(PLUGIN_ID).service(name);
const previewButtonController = {
async config(ctx) {
const config2 = await getService("config").get();
ctx.send({ config: config2 });
}
};
const controllers = {
"preview-button": previewButtonController
};
const adminApiRoutes = {
type: "admin",
routes: [
{
method: "GET",
path: "/config",
handler: "preview-button.config",
config: {
policies: ["admin::isAuthenticatedAdmin"]
}
}
]
};
const routes = {
"admin-api": adminApiRoutes
};
const configService = ({ strapi }) => ({
async get() {
const config2 = await strapi.config.get(`plugin::${PLUGIN_ID}`, defaultConfig);
return config2;
}
});
const services = {
config: configService
};
const index = {
config,
controllers,
routes,
services
};
export {
index as default
};