@kontent-ai/webhook-helper
Version:
This utility helps with webhook notifications from Kontent.ai
151 lines • 5.18 kB
JavaScript
import { z } from "zod";
export const webhookDeliverySlotSchema = z.enum(["published", "preview"]);
export const assetEventsSchema = z.enum(["created", "deleted", "metadata_changed"]);
export const contentItemPreviewEventsSchema = z.enum([
"created",
"deleted",
"workflow_step_changed",
"metadata_changed",
]);
export const contentItemPublishedEventsSchema = z.enum([
"published",
"unpublished",
"metadata_changed",
]);
export const contentTypeEventsSchema = z.enum(["created", "deleted", "changed"]);
export const languageEventsSchema = z.enum(["created", "deleted", "changed"]);
export const taxonomyEventsSchema = z.enum([
"created",
"deleted",
"metadata_changed",
"term_created",
"term_changed",
"term_deleted",
"terms_moved",
]);
export const webhookObjectDataSchema = z.object({
id: z.string(),
name: z.string(),
codename: z.string(),
last_modified: z.string(),
});
export const webhookItemObjectDataSchema = webhookObjectDataSchema.extend({
collection: z.string(),
workflow: z.string(),
workflow_step: z.string(),
language: z.string(),
type: z.string(),
});
const webhookMessageCommonSchema = z.object({
environment_id: z.string(),
delivery_slot: webhookDeliverySlotSchema,
});
export const assetMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("asset"),
action: assetEventsSchema,
});
export const contentItemPreviewMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("content_item"),
delivery_slot: z.literal("preview"),
action: z.enum(["created", "deleted", "metadata_changed"]),
});
export const contentItemWorkflowChangedPreviewMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("content_item"),
delivery_slot: z.literal("preview"),
action: z.literal("workflow_step_changed"),
action_context: z.object({
previous_workflow: z.string(),
previous_workflow_step: z.string(),
}),
});
export const contentItemPublishedMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("content_item"),
action: contentItemPublishedEventsSchema,
delivery_slot: z.literal("published"),
});
export const contentTypeMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("content_type"),
action: contentTypeEventsSchema,
});
export const languageMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("language"),
action: languageEventsSchema,
});
export const taxonomyMessageSchema = webhookMessageCommonSchema.extend({
object_type: z.literal("taxonomy"),
action: taxonomyEventsSchema,
});
export const webhookMessageSchema = z.discriminatedUnion("object_type", [
contentItemPreviewMessageSchema,
contentItemWorkflowChangedPreviewMessageSchema,
contentItemPublishedMessageSchema,
assetMessageSchema,
contentTypeMessageSchema,
languageMessageSchema,
taxonomyMessageSchema,
]);
const assetNotificationSchema = z
.object({
data: z.object({
system: webhookObjectDataSchema,
}),
message: assetMessageSchema,
})
.transform((notification) => (Object.assign(Object.assign({}, notification), { object_type: "asset" })));
const contentItemNotificationSchema = z
.object({
data: z.object({
system: webhookItemObjectDataSchema,
}),
message: z.union([
contentItemPreviewMessageSchema,
contentItemPublishedMessageSchema,
contentItemWorkflowChangedPreviewMessageSchema,
]),
})
.transform((notification) => (Object.assign(Object.assign({}, notification), { object_type: "content_item" })));
const contentTypeNotificationSchema = z
.object({
data: z.object({
system: webhookObjectDataSchema,
}),
message: contentTypeMessageSchema,
})
.transform((notification) => (Object.assign(Object.assign({}, notification), { object_type: "content_type" })));
const languageNotificationSchema = z
.object({
data: z.object({
system: webhookObjectDataSchema,
}),
message: languageMessageSchema,
})
.transform((notification) => (Object.assign(Object.assign({}, notification), { object_type: "language" })));
const taxonomyNotificationSchema = z
.object({
data: z.object({
system: webhookObjectDataSchema,
}),
message: taxonomyMessageSchema,
})
.transform((notification) => (Object.assign(Object.assign({}, notification), { object_type: "taxonomy" })));
const knownNotificationSchema = z.union([
assetNotificationSchema,
contentItemNotificationSchema,
contentTypeNotificationSchema,
languageNotificationSchema,
taxonomyNotificationSchema,
]);
export const webhookNotificationSchema = z.unknown().transform((input) => {
const result = knownNotificationSchema.safeParse(input);
if (result.success) {
return result.data;
}
return {
object_type: "unknown",
original_notification: input,
};
});
export const webhookResponseSchema = z.object({
notifications: z.array(webhookNotificationSchema),
});
//# sourceMappingURL=webhook-schemas.js.map