-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: webhooks * feat: ignore entry.publish findOne * chore: type entry * feat: refactor publication methods to not trigger entry.delete webhook (#20136) * feat: refactor publication methods * fix: remove unnecessary chain op in db query * feat: emit document. as webhook * feat: register legacy events * feat: only trigger entry webhooks * feat: rename webhook to event * feat: event manager * fix: repository * feat: webhook tests * fix: api tests * fix: non localized fields api test * fix: refactor event hub * fix: eventhub type * fix: event hub unit test * fix: remove all subscribers * fix: webhook api test
- Loading branch information
Showing
11 changed files
with
420 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/core/core/src/services/document-service/events.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { UID, Utils, Modules, Core } from '@strapi/types'; | ||
import { sanitize } from '@strapi/utils'; | ||
|
||
import { getDeepPopulate } from './utils/populate'; | ||
|
||
const EVENTS = { | ||
ENTRY_CREATE: 'entry.create', | ||
ENTRY_UPDATE: 'entry.update', | ||
ENTRY_DELETE: 'entry.delete', | ||
ENTRY_PUBLISH: 'entry.publish', | ||
ENTRY_UNPUBLISH: 'entry.unpublish', | ||
ENTRY_DRAFT_DISCARD: 'entry.draft-discard', | ||
}; | ||
|
||
type EventName = Utils.Object.Values<typeof EVENTS>; | ||
|
||
/** | ||
* Manager to trigger entry related events | ||
* | ||
* It will populate the entry if it is not a delete event. | ||
* So the event payload will contain the full entry. | ||
*/ | ||
const createEventManager = (strapi: Core.Strapi, uid: UID.Schema) => { | ||
const populate = getDeepPopulate(uid, {}); | ||
const model = strapi.getModel(uid); | ||
|
||
const emitEvent = async (eventName: EventName, entry: Modules.Documents.AnyDocument) => { | ||
// There is no need to populate the entry if it has been deleted | ||
let populatedEntry = entry; | ||
if (![EVENTS.ENTRY_DELETE, EVENTS.ENTRY_UNPUBLISH].includes(eventName)) { | ||
populatedEntry = await strapi.db.query(uid).findOne({ where: { id: entry.id }, populate }); | ||
} | ||
|
||
const sanitizedEntry = await sanitize.sanitizers.defaultSanitizeOutput( | ||
{ | ||
schema: model, | ||
getModel: (uid) => strapi.getModel(uid as UID.Schema), | ||
}, | ||
populatedEntry | ||
); | ||
|
||
await strapi.eventHub.emit(eventName, { | ||
model: model.modelName, | ||
uid: model.uid, | ||
entry: sanitizedEntry, | ||
}); | ||
}; | ||
|
||
return { | ||
/** | ||
* strapi.db.query might reuse the transaction used in the doc service request, | ||
* so this is executed after that transaction is committed. | ||
*/ | ||
emitEvent(eventName: EventName, entry: Modules.Documents.AnyDocument) { | ||
strapi.db.transaction(({ onCommit }) => { | ||
onCommit(() => emitEvent(eventName, entry)); | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
export { createEventManager }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.