feat(ghost): add new post published trigger (#1403)
This commit is contained in:
@@ -2,6 +2,7 @@ import defineApp from '../../helpers/define-app';
|
||||
import addAuthHeader from './common/add-auth-header';
|
||||
import setBaseUrl from './common/set-base-url';
|
||||
import auth from './auth';
|
||||
import triggers from './triggers';
|
||||
|
||||
export default defineApp({
|
||||
name: 'Ghost',
|
||||
@@ -14,4 +15,5 @@ export default defineApp({
|
||||
supportsConnections: true,
|
||||
beforeRequest: [setBaseUrl, addAuthHeader],
|
||||
auth,
|
||||
triggers,
|
||||
});
|
||||
|
3
packages/backend/src/apps/ghost/triggers/index.ts
Normal file
3
packages/backend/src/apps/ghost/triggers/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import newPostPublished from './new-post-published';
|
||||
|
||||
export default [newPostPublished];
|
@@ -0,0 +1,55 @@
|
||||
import Crypto from 'crypto';
|
||||
import isEmpty from 'lodash/isEmpty';
|
||||
import defineTrigger from '../../../../helpers/define-trigger';
|
||||
|
||||
export default defineTrigger({
|
||||
name: 'New post published',
|
||||
key: 'newPostPublished',
|
||||
type: 'webhook',
|
||||
description: 'Triggers when a new post is published.',
|
||||
|
||||
async run($) {
|
||||
const dataItem = {
|
||||
raw: $.request.body,
|
||||
meta: {
|
||||
internalId: Crypto.randomUUID(),
|
||||
},
|
||||
};
|
||||
|
||||
$.pushTriggerItem(dataItem);
|
||||
},
|
||||
|
||||
async testRun($) {
|
||||
const lastExecutionStep = await $.getLastExecutionStep();
|
||||
|
||||
if (!isEmpty(lastExecutionStep?.dataOut)) {
|
||||
$.pushTriggerItem({
|
||||
raw: lastExecutionStep.dataOut,
|
||||
meta: {
|
||||
internalId: '',
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
async registerHook($) {
|
||||
const payload = {
|
||||
webhooks: [
|
||||
{
|
||||
event: 'post.published',
|
||||
target_url: $.webhookUrl,
|
||||
name: `Flow ID: ${$.flow.id}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const response = await $.http.post('/admin/webhooks/', payload);
|
||||
const id = response.data.webhooks[0].id;
|
||||
|
||||
await $.flow.setRemoteWebhookId(id);
|
||||
},
|
||||
|
||||
async unregisterHook($) {
|
||||
await $.http.delete(`/admin/webhooks/${$.flow.remoteWebhookId}/`);
|
||||
},
|
||||
});
|
Reference in New Issue
Block a user