New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: webhooks #20129
Merged
Merged
feat: webhooks #20129
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
37aedc5
feat: webhooks
Marc-Roig 58c5623
feat: ignore entry.publish findOne
Marc-Roig bb2529f
chore: type entry
Marc-Roig 87c535a
feat: refactor publication methods to not trigger entry.delete webhoo…
Marc-Roig c18cfd4
feat: emit document. as webhook
Marc-Roig 98834b7
feat: register legacy events
Marc-Roig f324cfb
feat: only trigger entry webhooks
Marc-Roig 556c80c
feat: rename webhook to event
Marc-Roig 87c8689
feat: event manager
Marc-Roig 400ee93
Merge branch 'v5/main' into v5/webhooks
Marc-Roig a6f2ba6
fix: repository
Marc-Roig ad2831a
feat: webhook tests
Marc-Roig 83fe9de
fix: api tests
Marc-Roig 2aefc18
fix: non localized fields api test
Marc-Roig a791b36
fix: refactor event hub
Marc-Roig 844b7a6
fix: eventhub type
Marc-Roig 219d497
fix: event hub unit test
Marc-Roig 3597c45
fix: remove all subscribers
Marc-Roig 60977e2
fix: webhook api test
Marc-Roig 000b112
Merge branch 'v5/main' into v5/webhooks
Marc-Roig fd2643a
Merge branch 'v5/main' into v5/webhooks
Marc-Roig ed8cc94
Merge branch 'v5/main' into v5/webhooks
Marc-Roig File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum I wonder if we should replace this with just a listeners.clear & add a removeAllSubscribers. & make destroy do
wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:cheff-kiss: sounds great to me, will do that