From 168adba6a3b081e45f70176dda1ed6b24d9834c4 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Tue, 26 Mar 2024 08:44:54 +0100 Subject: [PATCH] adjust types to better fit webhooks --- lib/models/webhook-models.ts | 80 ++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/lib/models/webhook-models.ts b/lib/models/webhook-models.ts index 7e65909..b26f6a1 100644 --- a/lib/models/webhook-models.ts +++ b/lib/models/webhook-models.ts @@ -1,30 +1,86 @@ -export type WebhookNotification = { - data: WebhookData; - message: WebhookMessage; +type WebhookPublishedDeliverySlot = "published"; +type WebhookPreviewDeliverySlot = "preview"; + +export type WebhookDeliverySlot = WebhookPublishedDeliverySlot | WebhookPreviewDeliverySlot; + +type CommonEvents = "created" | "deleted"; +export type AssetEvents = CommonEvents | "metadata_changed"; +export type ContentItemPreviewEvents = CommonEvents | "workflow_step_changed" | "metadata_changed"; +export type ContentItemPublishedEvents = "published" | "unpublished" | "metadata_changed"; +export type ContentTypeEvents = CommonEvents | "changed"; +export type LanguageEvents = CommonEvents | "changed"; +export type TaxonomyEvents = + | CommonEvents + | "metadata_changed" + | "term_created" + | "term_changed" + | "term_deleted" + | "terms_moved"; + +export type WebhookNotification = WebhookItemNotification | WebhookObjectNotification; + +export type WebhookItemNotification = { + data: { + system: WebhookItemObjectData; + }; + message: ContentItemPreviewMessage | ContentItemPublishedMessage; }; -export type WebhookMessage = { +export type WebhookObjectNotification = { + data: { + system: WebhookObjectData; + }; + message: AssetMesage | ContentTypeMessage | LanguageMessage | TaxonomyMessage; +}; + +type WebhookMessageCommon = { environment_id: string; - object_type: string; - action: string; - delivery_slot: string; + delivery_slot: WebhookDeliverySlot; +}; + +export type AssetMesage = WebhookMessageCommon & { + object_type: "asset"; + action: AssetEvents; +}; + +export type ContentItemPreviewMessage = WebhookMessageCommon & { + object_type: "content_item"; + action: ContentItemPreviewEvents; + delivery_slot: WebhookPreviewDeliverySlot; +}; + +export type ContentItemPublishedMessage = WebhookMessageCommon & { + object_type: "content_item"; + action: ContentItemPublishedEvents; + delivery_slot: WebhookPublishedDeliverySlot; +}; + +export type ContentTypeMessage = WebhookMessageCommon & { + object_type: "content_type"; + action: ContentTypeEvents; +}; + +export type LanguageMessage = WebhookMessageCommon & { + object_type: "language"; + action: LanguageEvents; }; -export type WebhookData = { - system: WebhookObject | WebhookItemObject; +export type TaxonomyMessage = WebhookMessageCommon & { + object_type: "taxonomy"; + action: TaxonomyEvents; }; -export type WebhookObject = { +export type WebhookObjectData = { id: string; name: string; codename: string; last_modified: string; }; -export type WebhookItemObject = { +export type WebhookItemObjectData = { collection: string; workflow: string; workflow_step: string; language: string; type: string; -} & WebhookObject; +} & WebhookObjectData;