import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api'; import { LoggerService, RootConfigService, DiscoveryService, AuthService, LifecycleService } from '@backstage/backend-plugin-api'; import { Request } from 'express'; import { ParsedMediaType } from 'content-type'; /** * @public */ interface EventParams { /** * Topic for which this event should be published. */ topic: string; /** * Event payload. */ eventPayload: TPayload; /** * Metadata (e.g., HTTP headers and similar for events received from external). */ metadata?: Record; } /** * Allows a decoupled and asynchronous communication between components. * Components can publish events for a given topic and * others can subscribe for future events for topics they are interested in. * * @public */ interface EventsService { /** * Publishes an event for the topic. * * @param params - parameters for the to be published event. */ publish(params: EventParams): Promise; /** * Subscribes to one or more topics, registering an event handler for them. * * @param options - event subscription options. */ subscribe(options: EventsServiceSubscribeOptions): Promise; } /** * @public */ type EventsServiceSubscribeOptions = { /** * Subscriber ID that is scoped to the calling plugin. Subscribers with the same ID will have events distributed between them. */ id: string; topics: string[]; onEvent: EventsServiceEventHandler; }; /** * @public */ type EventsServiceEventHandler = (params: EventParams) => Promise; /** * @public */ declare const EVENTS_NOTIFY_TIMEOUT_HEADER = "backstage-events-notify-timeout"; /** * Subscribes to a topic and - depending on a set of conditions - * republishes the event to another topic. * * @see {@link https://www.enterpriseintegrationpatterns.com/MessageRouter.html | Message Router pattern}. * @public */ declare abstract class EventRouter { private readonly events; private readonly topics; private subscribed; protected constructor(options: { events: EventsService; topics: string[]; }); protected abstract getSubscriberId(): string; protected abstract determineDestinationTopic(params: EventParams): string | undefined; /** * Subscribes itself to the topic(s), * after which events potentially can be received * and processed by {@link EventRouter.onEvent}. */ subscribe(): Promise; onEvent(params: EventParams): Promise; } /** * @public */ type EventBusMode = 'never' | 'always' | 'auto'; /** * In-process event broker which will pass the event to all registered subscribers * interested in it. * Events will not be persisted in any form. * Events will not be passed to subscribers at other instances of the same cluster. * * @public */ declare class DefaultEventsService implements EventsService { private readonly logger; private readonly localBus; private readonly mode; private constructor(); static create(options: { logger: LoggerService; config?: RootConfigService; useEventBus?: EventBusMode; }): DefaultEventsService; /** * Returns a plugin-scoped context of the `EventService` * that ensures to prefix subscriber IDs with the plugin ID. * * @param pluginId - The plugin that the `EventService` should be created for. */ forPlugin(pluginId: string, options?: { discovery: DiscoveryService; logger: LoggerService; auth: AuthService; lifecycle: LifecycleService; }): EventsService; publish(params: EventParams): Promise; subscribe(options: EventsServiceSubscribeOptions): Promise; } /** * Key-value pairs of query parameters. * * @public */ interface ParsedQueryString { [key: string]: (ParsedQueryString | string)[] | ParsedQueryString | string | undefined; } /** * View on an incoming request that has to be validated. * * @public */ interface RequestDetails { /** * Request body. JSON payloads have been parsed already. */ body: unknown; /** * Key-value pairs of header names and values. Header names are lower-cased. */ headers: Record; /** * Key-value pairs of query parameters. */ query: ParsedQueryString; /** * Raw request details. */ raw: { /** * Raw request body (buffer). */ body: Buffer; /** * Encoding of the raw request body. * Can be used to decode the raw request body like `raw.body.toString(raw.encoding)`. */ encoding: BufferEncoding; }; } /** * Details for how to respond to the rejection * of the received HTTP request transmitting an event payload. * * @public */ interface RequestRejectionDetails { status: number; payload: unknown; contentType?: 'application/json' | 'text/plain'; } /** * Passed context for the validation * at which rejections can be expressed. * * @public */ interface RequestValidationContext { /** * Rejects the validated request * * @param details - Optional details about the rejection which will be provided to the sender. */ reject(details?: Partial): void; } /** * Validator used to check the received HTTP request * transmitting an event payload. * * E.g., it can be used for signature verification like * for GitHub webhook events * (https://docs.github.com/en/developers/webhooks-and-events/webhooks/creating-webhooks#secret) * or other kinds of checks. * * @public */ type RequestValidator = (request: RequestDetails, context: RequestValidationContext) => Promise; /** * @public */ interface HttpPostIngressOptions { topic: string; validator?: RequestValidator; } /** * @public */ type HttpBodyParsed = { bodyParsed: any; bodyBuffer: Buffer; encoding: string; }; /** * @public */ type HttpBodyParser = (request: Request, parsedMediaType: ParsedMediaType, topic: string) => Promise; /** * @public */ interface HttpBodyParserOptions { contentType: string; parser: HttpBodyParser; } /** * Subscribes to the provided (generic) topic * and publishes the events under the more concrete sub-topic * depending on the implemented logic for determining it. * Implementing classes might use information from `metadata` * and/or properties within the payload. * * @public */ declare abstract class SubTopicEventRouter extends EventRouter { protected constructor(options: { events: EventsService; topic: string; }); protected abstract determineSubTopic(params: EventParams): string | undefined; protected determineDestinationTopic(params: EventParams): string | undefined; } /** * Handles received events. * This may include triggering refreshes of catalog entities * or other actions to react on events. * * @public * @deprecated use the `EventsService` via the constructor, setter, or other means instead */ interface EventSubscriber { /** * Supported event topics like "github", "bitbucketCloud", etc. * * @deprecated use the `EventsService` via the constructor, setter, or other means instead */ supportsEventTopics(): string[]; /** * React on a received event. * * @param params - parameters for the to be received event. * @deprecated you are not required to expose this anymore when using `EventsService` */ onEvent(params: EventParams): Promise; } /** * Allows a decoupled and asynchronous communication between components. * Components can publish events for a given topic and * others can subscribe for future events for topics they are interested in. * * @public * @deprecated use `EventsService` instead */ interface EventBroker { /** * Publishes an event for the topic. * * @param params - parameters for the to be published event. */ publish(params: EventParams): Promise; /** * Adds new subscribers for {@link EventSubscriber#supportsEventTopics | interested topics}. * * @param subscribers - interested in events of specified topics. */ subscribe(...subscribers: Array>): void; } /** * Publishes events to be consumed by subscribers for their topic. * The events can come from different (external) sources * like emitted themselves, received via HTTP endpoint (i.e. webhook) * or from event brokers, queues, etc. * * @public * @deprecated use the `EventsService` via the constructor, setter, or other means instead */ interface EventPublisher { /** * @deprecated use the `EventsService` via the constructor, setter, or other means instead */ setEventBroker(eventBroker: EventBroker): Promise; } /** * The {@link EventsService} that allows to publish events, and subscribe to topics. * Uses the `root` scope so that events can be shared across all plugins, modules, and more. * * @public */ declare const eventsServiceRef: _backstage_backend_plugin_api.ServiceRef; /** @public */ declare const eventsServiceFactory: _backstage_backend_plugin_api.ServiceFactory; export { DefaultEventsService, EVENTS_NOTIFY_TIMEOUT_HEADER, EventRouter, SubTopicEventRouter, eventsServiceFactory, eventsServiceRef }; export type { EventBroker, EventBusMode, EventParams, EventPublisher, EventSubscriber, EventsService, EventsServiceEventHandler, EventsServiceSubscribeOptions, HttpBodyParsed, HttpBodyParser, HttpBodyParserOptions, HttpPostIngressOptions, ParsedQueryString, RequestDetails, RequestRejectionDetails, RequestValidationContext, RequestValidator };