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.
81 lines (80 loc) • 2.34 kB
JavaScript
const has = require("lodash/has");
const utils = require("@strapi/utils");
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
const has__default = /* @__PURE__ */ _interopDefault(has);
const defaultConfig = {
contentTypes: null
};
const config = {
default: defaultConfig,
validator(config2) {
if (!has__default.default(config2, "contentTypes")) {
return;
}
if (!Array.isArray(config2.contentTypes)) {
throw new utils.errors.ValidationError(`Must define contentTypes as an array.`);
}
config2.contentTypes.forEach((entry) => {
const hasDraft = has__default.default(entry, "draft");
const hasPublished = has__default.default(entry, "published");
if (!has__default.default(entry, "uid")) {
throw new utils.errors.ValidationError("Missing uid prop.");
}
if (!hasDraft && !hasPublished) {
throw new utils.errors.ValidationError(
`The config for ${entry.uid} requires at least a draft or published prop to be defined.`
);
}
if (hasDraft && !has__default.default(entry, ["draft", "url"])) {
throw new utils.errors.ValidationError(`Missing draft URL for ${entry.uid}.`);
}
if (hasPublished && !has__default.default(entry, ["published", "url"])) {
throw new utils.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
};
module.exports = index;
;