Skip to main content

cloudevents

@webda/cloudevents

CloudEvents v1.0 producer and consumer utilities for Webda — validate, emit, and subscribe to CloudEvents over HTTP or AMQP transports, with a store-backed subscription model.

When to use it​

  • You need to publish events from a Webda store save/update as CloudEvents to an external sink (webhook, AMQP broker, MQTT).
  • You want to receive and validate incoming CloudEvents from third-party producers.
  • You are building an event-driven integration that must conform to the CNCF CloudEvents v1.0 specification.

Install​

pnpm add @webda/cloudevents

Configuration​

@webda/cloudevents exports model and utility classes — it does not register a service bean with the Webda DI container. Wire it up by using the exported CloudEvent, Subscription, and isCloudEvent helpers directly in a custom service.

Usage​

import { isCloudEvent } from "@webda/cloudevents";
import { CloudEvent, emitterFor, httpTransport } from "cloudevents";
import { Subscription } from "@webda/cloudevents";

// Validate an incoming payload
const body = await ctx.getRequestBody();
if (!isCloudEvent(body, true /* strict */)) {
throw new Error("Invalid CloudEvent");
}

// Publish a CloudEvent via HTTP to an external sink
const event = new CloudEvent({
type: "com.myapp.order.created",
source: "/orders",
data: { orderId: "abc-123" }
});
const emit = emitterFor(httpTransport("https://sink.example.com/events"));
await emit(event);

// Use a Subscription model to persist a subscriber's endpoint
const sub = new Subscription();
sub.sink = "https://receiver.example.com/hook";
sub.types = ["com.myapp.order.created"];
await sub.save();

Reference​

Interfaces​

Functions​