Skip to content

Commit d909bb2

Browse files
authored
feat: add publisherActionAllowed property for canceling publisher action when you change your config (#204)
1 parent 05455ac commit d909bb2

File tree

3 files changed

+89
-38
lines changed

3 files changed

+89
-38
lines changed

README.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,65 @@ module.exports = ({ env }) => ({
4444
'publisher': {
4545
enabled: true,
4646
config: {
47-
hooks: {
48-
beforePublish: async ({ strapi, uid, entity }) => {
49-
console.log('beforePublish');
47+
hooks: {
48+
beforePublish: async ({ strapi, uid, entity }) => {
49+
// Return false to prevent publish; any other value (or no return) allows it
50+
console.log('beforePublish');
51+
// return false
52+
},
53+
afterPublish: async ({ strapi, uid, entity }) => {
54+
console.log('afterPublish');
55+
},
56+
beforeUnpublish: async ({ strapi, uid, entity }) => {
57+
// Return false to prevent unpublish; any other value (or no return) allows it
58+
console.log('beforeUnpublish');
59+
// return false
60+
},
61+
afterUnpublish: async ({ strapi, uid, entity }) => {
62+
console.log('afterUnpublish');
63+
},
5064
},
51-
afterPublish: async ({ strapi, uid, entity }) => {
52-
console.log('afterPublish');
53-
},
54-
beforeUnpublish: async ({ strapi, uid, entity }) => {
55-
console.log('beforeUnpublish');
56-
},
57-
afterUnpublish: async ({ strapi, uid, entity }) => {
58-
console.log('afterUnpublish');
59-
},
60-
},
6165
},
6266
},
6367
// ..
6468
});
6569
```
6670

71+
### TypeScript example (config/plugins.ts)
72+
73+
If you are using a TypeScript Strapi app with ESM and a `config/plugins.ts` file, you can configure the plugin like this:
74+
75+
```ts
76+
import type { Strapi } from '@strapi/strapi';
77+
78+
export default ({ env }: { env: (key: string, def?: any) => any }) => ({
79+
publisher: {
80+
enabled: true,
81+
config: {
82+
hooks: {
83+
beforePublish: async ({ strapi, uid, entity }: { strapi: Strapi; uid: string; entity: any }) => {
84+
// Return false to prevent publish; any other value (or no return) allows it
85+
console.log('beforePublish');
86+
// return false
87+
},
88+
afterPublish: async ({ strapi, uid, entity }: { strapi: Strapi; uid: string; entity: any }) => {
89+
console.log('afterPublish');
90+
// Post-publish side effects
91+
},
92+
beforeUnpublish: async ({ strapi, uid, entity }: { strapi: Strapi; uid: string; entity: any }) => {
93+
// Return false to prevent unpublish; any other value (or no return) allows it
94+
console.log('beforeUnpublish');
95+
// return false
96+
},
97+
afterUnpublish: async ({ strapi, uid, entity }: { strapi: Strapi; uid: string; entity: any }) => {
98+
console.log('afterUnpublish');
99+
},
100+
},
101+
},
102+
},
103+
});
104+
```
105+
67106
### The Complete Plugin Configuration Object
68107

69108
| Property | Description | Type | Default | Required |
@@ -74,9 +113,9 @@ module.exports = ({ env }) => ({
74113
| components.dateTimePicker | Settings associated with the DateTimePicker component used to set action times | Object | {} | No |
75114
| components.dateTimePicker.step | The step between the numbers displayed for the time section of the DateTimePicker | Number | 1 | No |
76115
| components.dateTimePicker.locale | Allows to enforce another locale to change the date layout | String | browser locale | No |
77-
| hooks.beforePublish | An async function that runs before a content type is published | Function | () => {} | No |
116+
| hooks.beforePublish | An async function that runs before a content type is published. Return false to cancel publish. | Function | () => {} | No |
78117
| hooks.afterPublish | An async function that runs after a content type is published | Function | () => {} | No |
79-
| hooks.beforeUnpublish | An async function that runs before a content type is un-published | Function | () => {} | No |
118+
| hooks.beforeUnpublish | An async function that runs before a content type is un-published. Return false to cancel unpublish. | Function | () => {} | No |
80119
| hooks.afterUnpublish | An async function that runs after a content type is un-published | Function | () => {} | No |
81120
| contentTypes | A list of content type uids where the publish actions should be displayed | Array<String> | All content types | No |
82121

server/config/index.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import pluginConfigSchema from './schema';
22

33
export default {
4-
default: () => ({
5-
enabled: true,
6-
actions: {
7-
syncFrequency: '*/1 * * * *',
8-
},
9-
hooks: {
10-
beforePublish: () => {},
11-
afterPublish: () => {},
12-
beforeUnpublish: () => {},
13-
afterUnpublish: () => {},
14-
},
15-
components: {
16-
dateTimePicker: {
17-
step: 5,
4+
default: () => ({
5+
enabled: true,
6+
actions: {
7+
syncFrequency: '*/1 * * * *',
8+
},
9+
// Hooks allow you to run custom logic around publish/unpublish.
10+
// NOTE: If a before* hook explicitly returns false, the action will be cancelled.
11+
hooks: {
12+
beforePublish: () => {},
13+
afterPublish: () => {},
14+
beforeUnpublish: () => {},
15+
afterUnpublish: () => {},
16+
},
17+
components: {
18+
dateTimePicker: {
19+
step: 5,
20+
},
1821
},
22+
}),
23+
validator: async (config) => {
24+
await pluginConfigSchema.validate(config);
1925
},
20-
}),
21-
validator: async (config) => {
22-
await pluginConfigSchema.validate(config);
23-
},
24-
};
26+
};

server/services/publication-service.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export default ({ strapi }) => ({
1818
locale,
1919
});
2020

21-
await hooks.beforePublish({ strapi, uid, entity });
21+
const publisherActionAllowed = await hooks.beforePublish({ strapi, uid, entity });
22+
// If explicitly returned false, abort publish
23+
if (publisherActionAllowed === false) {
24+
strapi.log.info(logMessage(`Publish aborted by beforePublish hook for document id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
25+
return;
26+
}
2227

2328
const publishedEntity = await strapi.documents(uid).publish({
2429
documentId: entityId,
@@ -48,17 +53,22 @@ export default ({ strapi }) => ({
4853
locale,
4954
});
5055

51-
await hooks.beforeUnpublish({ strapi, uid, entity });
56+
const publisherActionAllowed = await hooks.beforeUnpublish({ strapi, uid, entity });
57+
// If explicitly returned false, abort unpublish
58+
if (publisherActionAllowed === false) {
59+
strapi.log.info(logMessage(`Unpublish aborted by beforeUnpublish hook for document id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
60+
return;
61+
}
5262

5363
const unpublishedEntity = await strapi.documents(uid).unpublish({
5464
documentId: entityId,
5565
locale,
5666
});
57-
67+
5868
await getPluginService('emitService').unpublish(uid, unpublishedEntity);
5969

6070
strapi.log.info(logMessage(`Successfully unpublished document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}".`));
61-
71+
6272
await hooks.afterUnpublish({ strapi, uid, entity: unpublishedEntity });
6373
} catch (error) {
6474
strapi.log.error(logMessage(`An error occurred when trying to unpublish document with id "${entityId}"${locale ? ` and locale "${locale}"` : ''} of type "${uid}": "${error}"`));

0 commit comments

Comments
 (0)