@backstage/plugin-events-node 0.2.22 → 0.3.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +110 -13
- package/README.md +81 -2
- package/alpha/package.json +1 -1
- package/dist/alpha.cjs.js.map +1 -1
- package/dist/alpha.d.ts +9 -0
- package/dist/index.cjs.js +126 -16
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +135 -40
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,25 +1,122 @@
|
|
|
1
1
|
# @backstage/plugin-events-node
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.3.0-next.1
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
7
|
- Updated dependencies
|
|
8
|
-
- @backstage/backend-plugin-api@0.6.
|
|
8
|
+
- @backstage/backend-plugin-api@0.6.14-next.1
|
|
9
9
|
|
|
10
|
-
## 0.
|
|
10
|
+
## 0.3.0-next.0
|
|
11
11
|
|
|
12
|
-
###
|
|
13
|
-
|
|
14
|
-
- Updated dependencies
|
|
15
|
-
- @backstage/backend-plugin-api@0.6.12
|
|
16
|
-
|
|
17
|
-
## 0.2.20
|
|
18
|
-
|
|
19
|
-
### Patch Changes
|
|
12
|
+
### Minor Changes
|
|
20
13
|
|
|
21
|
-
-
|
|
22
|
-
|
|
14
|
+
- eff3ca9: BREAKING CHANGE: Migrate `EventRouter` implementations from `EventBroker` to `EventsService`.
|
|
15
|
+
|
|
16
|
+
`EventRouter` uses the new `EventsService` instead of the `EventBroker` now,
|
|
17
|
+
causing a breaking change to its signature.
|
|
18
|
+
|
|
19
|
+
All of its extensions and implementations got adjusted accordingly.
|
|
20
|
+
(`SubTopicEventRouter`, `AzureDevOpsEventRouter`, `BitbucketCloudEventRouter`,
|
|
21
|
+
`GerritEventRouter`, `GithubEventRouter`, `GitlabEventRouter`)
|
|
22
|
+
|
|
23
|
+
Required adjustments were made to all backend modules for the new backend system,
|
|
24
|
+
now also making use of the `eventsServiceRef` instead of the `eventsExtensionPoint`.
|
|
25
|
+
|
|
26
|
+
**Migration:**
|
|
27
|
+
|
|
28
|
+
Example for implementations of `SubTopicEventRouter`:
|
|
29
|
+
|
|
30
|
+
```diff
|
|
31
|
+
import {
|
|
32
|
+
EventParams,
|
|
33
|
+
+ EventsService,
|
|
34
|
+
SubTopicEventRouter,
|
|
35
|
+
} from '@backstage/plugin-events-node';
|
|
36
|
+
|
|
37
|
+
export class GithubEventRouter extends SubTopicEventRouter {
|
|
38
|
+
- constructor() {
|
|
39
|
+
- super('github');
|
|
40
|
+
+ constructor(options: { events: EventsService }) {
|
|
41
|
+
+ super({
|
|
42
|
+
+ events: options.events,
|
|
43
|
+
+ topic: 'github',
|
|
44
|
+
+ });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
+ protected getSubscriberId(): string {
|
|
48
|
+
+ return 'GithubEventRouter';
|
|
49
|
+
+ }
|
|
50
|
+
+
|
|
51
|
+
// ...
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Example for a direct extension of `EventRouter`:
|
|
56
|
+
|
|
57
|
+
```diff
|
|
58
|
+
class MyEventRouter extends EventRouter {
|
|
59
|
+
- constructor(/* ... */) {
|
|
60
|
+
+ constructor(options: {
|
|
61
|
+
+ events: EventsService;
|
|
62
|
+
+ // ...
|
|
63
|
+
+ }) {
|
|
64
|
+
- super();
|
|
65
|
+
// ...
|
|
66
|
+
+ super({
|
|
67
|
+
+ events: options.events,
|
|
68
|
+
+ topics: topics,
|
|
69
|
+
+ });
|
|
70
|
+
}
|
|
71
|
+
+
|
|
72
|
+
+ protected getSubscriberId(): string {
|
|
73
|
+
+ return 'MyEventRouter';
|
|
74
|
+
+ }
|
|
75
|
+
-
|
|
76
|
+
- supportsEventTopics(): string[] {
|
|
77
|
+
- return this.topics;
|
|
78
|
+
- }
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Patch Changes
|
|
83
|
+
|
|
84
|
+
- 56969b6: Add new `EventsService` as well as `eventsServiceRef` for the new backend system.
|
|
85
|
+
|
|
86
|
+
**Summary:**
|
|
87
|
+
|
|
88
|
+
- new:
|
|
89
|
+
`EventsService`, `eventsServiceRef`, `TestEventsService`
|
|
90
|
+
- deprecated:
|
|
91
|
+
`EventBroker`, `EventPublisher`, `EventSubscriber`, `DefaultEventBroker`, `EventsBackend`,
|
|
92
|
+
most parts of `EventsExtensionPoint` (alpha),
|
|
93
|
+
`TestEventBroker`, `TestEventPublisher`, `TestEventSubscriber`
|
|
94
|
+
|
|
95
|
+
Add the `eventsServiceRef` as dependency to your backend plugins
|
|
96
|
+
or backend plugin modules.
|
|
97
|
+
|
|
98
|
+
**Details:**
|
|
99
|
+
|
|
100
|
+
The previous implementation using the `EventsExtensionPoint` was added in the early stages
|
|
101
|
+
of the new backend system and does not respect the plugin isolation.
|
|
102
|
+
This made it not compatible anymore with the new backend system.
|
|
103
|
+
|
|
104
|
+
Additionally, the previous interfaces had some room for simplification,
|
|
105
|
+
supporting less exposure of internal concerns as well.
|
|
106
|
+
|
|
107
|
+
Hereby, this change adds a new `EventsService` interface as replacement for the now deprecated `EventBroker`.
|
|
108
|
+
The new interface does not require any `EventPublisher` or `EventSubscriber` interfaces anymore.
|
|
109
|
+
Instead, it is expected that the `EventsService` gets passed into publishers and subscribers,
|
|
110
|
+
and used internally. There is no need to expose anything of that at their own interfaces.
|
|
111
|
+
|
|
112
|
+
Most parts of `EventsExtensionPoint` (alpha) are deprecated as well and were not usable
|
|
113
|
+
(by other plugins or their modules) anyway.
|
|
114
|
+
|
|
115
|
+
The `DefaultEventBroker` implementation is deprecated and wraps the new `DefaultEventsService` implementation.
|
|
116
|
+
Optionally, an instance can be passed as argument to allow mixed setups to operate alongside.
|
|
117
|
+
|
|
118
|
+
- Updated dependencies
|
|
119
|
+
- @backstage/backend-plugin-api@0.6.13-next.0
|
|
23
120
|
|
|
24
121
|
## 0.2.19
|
|
25
122
|
|
package/README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
# plugin-events-node
|
|
1
|
+
# `@backstage/plugin-events-node`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package defined basic types for event-based interactions inside of Backstage.
|
|
4
|
+
|
|
5
|
+
Additionally, it provides the core event service `eventsServiceRef` of type `EventsService`
|
|
6
|
+
with its default implementation that uses the `DefaultEventsService` implementation.
|
|
7
|
+
|
|
8
|
+
`DefaultEventsService` is a simple in-memory implementation
|
|
9
|
+
that requires the co-deployment of producers and consumers of events.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add `@backstage/plugin-events-node` as dependency to your plugin or plugin module package
|
|
14
|
+
to which you want to add event support.
|
|
15
|
+
|
|
16
|
+
Use `eventsServiceRef` as a dependency at your plugin or plugin module.
|
|
17
|
+
|
|
18
|
+
### Legacy Backend System
|
|
19
|
+
|
|
20
|
+
Create an `EventsService` instance and add it to the environment.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// packages/backend/src/plugins/events.ts
|
|
24
|
+
import { DefaultEventsService } from '@backstage/plugin-events-node';
|
|
25
|
+
|
|
26
|
+
// ...
|
|
27
|
+
|
|
28
|
+
function makeCreateEnv(config: Config) {
|
|
29
|
+
// ...
|
|
30
|
+
const eventsService = DefaultEventsService.create({ logger: root });
|
|
31
|
+
// ...
|
|
32
|
+
return (plugin: string): PluginEnvironment => {
|
|
33
|
+
// ...
|
|
34
|
+
return {
|
|
35
|
+
// ...
|
|
36
|
+
events: eventsService,
|
|
37
|
+
// ...
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Use the `events` from the `PluginEnvironment` as desired:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// packages/backend/src/plugins/events.ts
|
|
47
|
+
export default async function createPlugin(
|
|
48
|
+
env: PluginEnvironment,
|
|
49
|
+
): Promise<Router> {
|
|
50
|
+
// ...
|
|
51
|
+
env.events; // ...
|
|
52
|
+
// ...
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Use Case
|
|
57
|
+
|
|
58
|
+
### Exchange service implementation
|
|
59
|
+
|
|
60
|
+
Create your custom service factory implementation:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { eventsServiceRef } from '@backstage/plugin-events-node';
|
|
64
|
+
// ...
|
|
65
|
+
export const customEventsServiceFactory = createServiceFactory({
|
|
66
|
+
service: eventsServiceRef,
|
|
67
|
+
deps: {
|
|
68
|
+
// add needed dependencies here
|
|
69
|
+
},
|
|
70
|
+
async factory({ logger }) {
|
|
71
|
+
// add your custom logic here
|
|
72
|
+
return customEventsService;
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
and your custom implementation:
|
|
78
|
+
|
|
79
|
+
```diff
|
|
80
|
+
// packages/backend/src/index.ts
|
|
81
|
+
+ backend.add(customEventsServiceFactory());
|
|
82
|
+
```
|
package/alpha/package.json
CHANGED
package/dist/alpha.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alpha.cjs.js","sources":["../src/extensions.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createExtensionPoint } from '@backstage/backend-plugin-api';\nimport {\n EventBroker,\n EventPublisher,\n EventSubscriber,\n HttpPostIngressOptions,\n} from '@backstage/plugin-events-node';\n\n/**\n * @alpha\n */\nexport interface EventsExtensionPoint {\n setEventBroker(eventBroker: EventBroker): void;\n\n addPublishers(\n ...publishers: Array<EventPublisher | Array<EventPublisher>>\n ): void;\n\n addSubscribers(\n ...subscribers: Array<EventSubscriber | Array<EventSubscriber>>\n ): void;\n\n addHttpPostIngress(options: HttpPostIngressOptions): void;\n}\n\n/**\n * @alpha\n */\nexport const eventsExtensionPoint = createExtensionPoint<EventsExtensionPoint>({\n id: 'events',\n});\n"],"names":["createExtensionPoint"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"alpha.cjs.js","sources":["../src/extensions.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createExtensionPoint } from '@backstage/backend-plugin-api';\nimport {\n EventBroker,\n EventPublisher,\n EventSubscriber,\n HttpPostIngressOptions,\n} from '@backstage/plugin-events-node';\n\n/**\n * @alpha\n */\nexport interface EventsExtensionPoint {\n /**\n * @deprecated use `eventsServiceRef` and `eventsServiceFactory` instead\n */\n setEventBroker(eventBroker: EventBroker): void;\n\n /**\n * @deprecated use `EventsService.publish` instead\n */\n addPublishers(\n ...publishers: Array<EventPublisher | Array<EventPublisher>>\n ): void;\n\n /**\n * @deprecated use `EventsService.subscribe` instead\n */\n addSubscribers(\n ...subscribers: Array<EventSubscriber | Array<EventSubscriber>>\n ): void;\n\n addHttpPostIngress(options: HttpPostIngressOptions): void;\n}\n\n/**\n * @alpha\n */\nexport const eventsExtensionPoint = createExtensionPoint<EventsExtensionPoint>({\n id: 'events',\n});\n"],"names":["createExtensionPoint"],"mappings":";;;;;;AAqDO,MAAM,uBAAuBA,qCAA2C,CAAA;AAAA,EAC7E,EAAI,EAAA,QAAA;AACN,CAAC;;;;"}
|
package/dist/alpha.d.ts
CHANGED
|
@@ -5,8 +5,17 @@ import { EventBroker, EventPublisher, EventSubscriber, HttpPostIngressOptions }
|
|
|
5
5
|
* @alpha
|
|
6
6
|
*/
|
|
7
7
|
interface EventsExtensionPoint {
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated use `eventsServiceRef` and `eventsServiceFactory` instead
|
|
10
|
+
*/
|
|
8
11
|
setEventBroker(eventBroker: EventBroker): void;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated use `EventsService.publish` instead
|
|
14
|
+
*/
|
|
9
15
|
addPublishers(...publishers: Array<EventPublisher | Array<EventPublisher>>): void;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated use `EventsService.subscribe` instead
|
|
18
|
+
*/
|
|
10
19
|
addSubscribers(...subscribers: Array<EventSubscriber | Array<EventSubscriber>>): void;
|
|
11
20
|
addHttpPostIngress(options: HttpPostIngressOptions): void;
|
|
12
21
|
}
|
package/dist/index.cjs.js
CHANGED
|
@@ -2,46 +2,156 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
|
|
5
|
+
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
6
|
+
|
|
7
|
+
var __defProp$1 = Object.defineProperty;
|
|
8
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __publicField$1 = (obj, key, value) => {
|
|
10
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
11
|
return value;
|
|
10
12
|
};
|
|
11
13
|
class EventRouter {
|
|
12
|
-
constructor() {
|
|
13
|
-
__publicField(this, "
|
|
14
|
+
constructor(options) {
|
|
15
|
+
__publicField$1(this, "events");
|
|
16
|
+
__publicField$1(this, "topics");
|
|
17
|
+
__publicField$1(this, "subscribed", false);
|
|
18
|
+
this.events = options.events;
|
|
19
|
+
this.topics = options.topics;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Subscribes itself to the topic(s),
|
|
23
|
+
* after which events potentially can be received
|
|
24
|
+
* and processed by {@link EventRouter.onEvent}.
|
|
25
|
+
*/
|
|
26
|
+
async subscribe() {
|
|
27
|
+
if (this.subscribed) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.subscribed = true;
|
|
31
|
+
await this.events.subscribe({
|
|
32
|
+
id: this.getSubscriberId(),
|
|
33
|
+
topics: this.topics,
|
|
34
|
+
onEvent: this.onEvent.bind(this)
|
|
35
|
+
});
|
|
14
36
|
}
|
|
15
37
|
async onEvent(params) {
|
|
16
|
-
var _a;
|
|
17
38
|
const topic = this.determineDestinationTopic(params);
|
|
18
39
|
if (!topic) {
|
|
19
40
|
return;
|
|
20
41
|
}
|
|
21
|
-
|
|
42
|
+
await this.events.publish({
|
|
22
43
|
...params,
|
|
23
44
|
topic
|
|
24
45
|
});
|
|
25
46
|
}
|
|
26
|
-
|
|
27
|
-
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var __defProp = Object.defineProperty;
|
|
50
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
51
|
+
var __publicField = (obj, key, value) => {
|
|
52
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
53
|
+
return value;
|
|
54
|
+
};
|
|
55
|
+
class DefaultEventsService {
|
|
56
|
+
constructor(logger) {
|
|
57
|
+
this.logger = logger;
|
|
58
|
+
__publicField(this, "subscribers", /* @__PURE__ */ new Map());
|
|
59
|
+
}
|
|
60
|
+
static create(options) {
|
|
61
|
+
return new DefaultEventsService(options.logger);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns a plugin-scoped context of the `EventService`
|
|
65
|
+
* that ensures to prefix subscriber IDs with the plugin ID.
|
|
66
|
+
*
|
|
67
|
+
* @param pluginId - The plugin that the `EventService` should be created for.
|
|
68
|
+
*/
|
|
69
|
+
forPlugin(pluginId) {
|
|
70
|
+
return {
|
|
71
|
+
publish: (params) => {
|
|
72
|
+
return this.publish(params);
|
|
73
|
+
},
|
|
74
|
+
subscribe: (options) => {
|
|
75
|
+
return this.subscribe({
|
|
76
|
+
...options,
|
|
77
|
+
id: `${pluginId}.${options.id}`
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async publish(params) {
|
|
83
|
+
var _a;
|
|
84
|
+
this.logger.debug(
|
|
85
|
+
`Event received: topic=${params.topic}, metadata=${JSON.stringify(
|
|
86
|
+
params.metadata
|
|
87
|
+
)}, payload=${JSON.stringify(params.eventPayload)}`
|
|
88
|
+
);
|
|
89
|
+
if (!this.subscribers.has(params.topic)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const onEventPromises = [];
|
|
93
|
+
(_a = this.subscribers.get(params.topic)) == null ? void 0 : _a.forEach((subscription) => {
|
|
94
|
+
onEventPromises.push(
|
|
95
|
+
(async () => {
|
|
96
|
+
try {
|
|
97
|
+
await subscription.onEvent(params);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
this.logger.warn(
|
|
100
|
+
`Subscriber "${subscription.id}" failed to process event for topic "${params.topic}"`,
|
|
101
|
+
error
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
})()
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
await Promise.all(onEventPromises);
|
|
108
|
+
}
|
|
109
|
+
async subscribe(options) {
|
|
110
|
+
options.topics.forEach((topic) => {
|
|
111
|
+
if (!this.subscribers.has(topic)) {
|
|
112
|
+
this.subscribers.set(topic, []);
|
|
113
|
+
}
|
|
114
|
+
this.subscribers.get(topic).push({
|
|
115
|
+
id: options.id,
|
|
116
|
+
onEvent: options.onEvent
|
|
117
|
+
});
|
|
118
|
+
});
|
|
28
119
|
}
|
|
29
120
|
}
|
|
30
121
|
|
|
31
122
|
class SubTopicEventRouter extends EventRouter {
|
|
32
|
-
constructor(
|
|
33
|
-
super(
|
|
34
|
-
|
|
123
|
+
constructor(options) {
|
|
124
|
+
super({
|
|
125
|
+
events: options.events,
|
|
126
|
+
topics: [options.topic]
|
|
127
|
+
});
|
|
35
128
|
}
|
|
36
129
|
determineDestinationTopic(params) {
|
|
37
130
|
const subTopic = this.determineSubTopic(params);
|
|
38
131
|
return subTopic ? `${params.topic}.${subTopic}` : void 0;
|
|
39
132
|
}
|
|
40
|
-
supportsEventTopics() {
|
|
41
|
-
return [this.topic];
|
|
42
|
-
}
|
|
43
133
|
}
|
|
44
134
|
|
|
135
|
+
const eventsServiceRef = backendPluginApi.createServiceRef({
|
|
136
|
+
id: "events.service",
|
|
137
|
+
scope: "plugin",
|
|
138
|
+
defaultFactory: async (service) => backendPluginApi.createServiceFactory({
|
|
139
|
+
service,
|
|
140
|
+
deps: {
|
|
141
|
+
pluginMetadata: backendPluginApi.coreServices.pluginMetadata,
|
|
142
|
+
rootLogger: backendPluginApi.coreServices.rootLogger
|
|
143
|
+
},
|
|
144
|
+
async createRootContext({ rootLogger }) {
|
|
145
|
+
return DefaultEventsService.create({ logger: rootLogger });
|
|
146
|
+
},
|
|
147
|
+
async factory({ pluginMetadata }, eventsService) {
|
|
148
|
+
return eventsService.forPlugin(pluginMetadata.getId());
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
exports.DefaultEventsService = DefaultEventsService;
|
|
45
154
|
exports.EventRouter = EventRouter;
|
|
46
155
|
exports.SubTopicEventRouter = SubTopicEventRouter;
|
|
156
|
+
exports.eventsServiceRef = eventsServiceRef;
|
|
47
157
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/api/EventRouter.ts","../src/api/SubTopicEventRouter.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EventBroker } from './EventBroker';\nimport { EventParams } from './EventParams';\nimport { EventPublisher } from './EventPublisher';\nimport { EventSubscriber } from './EventSubscriber';\n\n/**\n * Subscribes to a topic and - depending on a set of conditions -\n * republishes the event to another topic.\n *\n * @see {@link https://www.enterpriseintegrationpatterns.com/MessageRouter.html | Message Router pattern}.\n * @public\n */\nexport abstract class EventRouter implements EventPublisher, EventSubscriber {\n private eventBroker?: EventBroker;\n\n protected abstract determineDestinationTopic(\n params: EventParams,\n ): string | undefined;\n\n async onEvent(params: EventParams): Promise<void> {\n const topic = this.determineDestinationTopic(params);\n\n if (!topic) {\n return;\n }\n\n // republish to different topic\n this.eventBroker?.publish({\n ...params,\n topic,\n });\n }\n\n async setEventBroker(eventBroker: EventBroker): Promise<void> {\n this.eventBroker = eventBroker;\n }\n\n abstract supportsEventTopics(): string[];\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EventParams } from './EventParams';\nimport { EventRouter } from './EventRouter';\n\n/**\n * Subscribes to the provided (generic) topic\n * and publishes the events under the more concrete sub-topic\n * depending on the implemented logic for determining it.\n * Implementing classes might use information from `metadata`\n * and/or properties within the payload.\n *\n * @public\n */\nexport abstract class SubTopicEventRouter extends EventRouter {\n protected constructor(private readonly topic: string) {\n super();\n }\n\n protected abstract determineSubTopic(params: EventParams): string | undefined;\n\n protected determineDestinationTopic(params: EventParams): string | undefined {\n const subTopic = this.determineSubTopic(params);\n return subTopic ? `${params.topic}.${subTopic}` : undefined;\n }\n\n supportsEventTopics(): string[] {\n return [this.topic];\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;AA4BO,MAAe,WAAuD,CAAA;AAAA,EAAtE,WAAA,GAAA;AACL,IAAQ,aAAA,CAAA,IAAA,EAAA,aAAA,CAAA,CAAA;AAAA,GAAA;AAAA,EAMR,MAAM,QAAQ,MAAoC,EAAA;AAnCpD,IAAA,IAAA,EAAA,CAAA;AAoCI,IAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,yBAAA,CAA0B,MAAM,CAAA,CAAA;AAEnD,IAAA,IAAI,CAAC,KAAO,EAAA;AACV,MAAA,OAAA;AAAA,KACF;AAGA,IAAK,CAAA,EAAA,GAAA,IAAA,CAAA,WAAA,KAAL,mBAAkB,OAAQ,CAAA;AAAA,MACxB,GAAG,MAAA;AAAA,MACH,KAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF;AAAA,EAEA,MAAM,eAAe,WAAyC,EAAA;AAC5D,IAAA,IAAA,CAAK,WAAc,GAAA,WAAA,CAAA;AAAA,GACrB;AAGF;;AC1BO,MAAe,4BAA4B,WAAY,CAAA;AAAA,EAClD,YAA6B,KAAe,EAAA;AACpD,IAAM,KAAA,EAAA,CAAA;AAD+B,IAAA,IAAA,CAAA,KAAA,GAAA,KAAA,CAAA;AAAA,GAEvC;AAAA,EAIU,0BAA0B,MAAyC,EAAA;AAC3E,IAAM,MAAA,QAAA,GAAW,IAAK,CAAA,iBAAA,CAAkB,MAAM,CAAA,CAAA;AAC9C,IAAA,OAAO,WAAW,CAAG,EAAA,MAAA,CAAO,KAAK,CAAA,CAAA,EAAI,QAAQ,CAAK,CAAA,GAAA,KAAA,CAAA,CAAA;AAAA,GACpD;AAAA,EAEA,mBAAgC,GAAA;AAC9B,IAAO,OAAA,CAAC,KAAK,KAAK,CAAA,CAAA;AAAA,GACpB;AACF;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/api/EventRouter.ts","../src/api/DefaultEventsService.ts","../src/api/SubTopicEventRouter.ts","../src/service.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EventParams } from './EventParams';\nimport { EventsService } from './EventsService';\n\n/**\n * Subscribes to a topic and - depending on a set of conditions -\n * republishes the event to another topic.\n *\n * @see {@link https://www.enterpriseintegrationpatterns.com/MessageRouter.html | Message Router pattern}.\n * @public\n */\nexport abstract class EventRouter {\n private readonly events: EventsService;\n private readonly topics: string[];\n private subscribed: boolean = false;\n\n protected constructor(options: { events: EventsService; topics: string[] }) {\n this.events = options.events;\n this.topics = options.topics;\n }\n\n protected abstract getSubscriberId(): string;\n\n protected abstract determineDestinationTopic(\n params: EventParams,\n ): string | undefined;\n\n /**\n * Subscribes itself to the topic(s),\n * after which events potentially can be received\n * and processed by {@link EventRouter.onEvent}.\n */\n async subscribe(): Promise<void> {\n if (this.subscribed) {\n return;\n }\n\n this.subscribed = true;\n\n await this.events.subscribe({\n id: this.getSubscriberId(),\n topics: this.topics,\n onEvent: this.onEvent.bind(this),\n });\n }\n\n async onEvent(params: EventParams): Promise<void> {\n const topic = this.determineDestinationTopic(params);\n\n if (!topic) {\n return;\n }\n\n // republish to different topic\n await this.events.publish({\n ...params,\n topic,\n });\n }\n}\n","/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { EventParams } from './EventParams';\nimport { EventsService, EventsServiceSubscribeOptions } from './EventsService';\n\n/**\n * In-process event broker which will pass the event to all registered subscribers\n * interested in it.\n * Events will not be persisted in any form.\n * Events will not be passed to subscribers at other instances of the same cluster.\n *\n * @public\n */\n// TODO(pjungermann): add opentelemetry? (see plugins/catalog-backend/src/util/opentelemetry.ts, etc.)\nexport class DefaultEventsService implements EventsService {\n private readonly subscribers = new Map<\n string,\n Omit<EventsServiceSubscribeOptions, 'topics'>[]\n >();\n\n private constructor(private readonly logger: LoggerService) {}\n\n static create(options: { logger: LoggerService }): DefaultEventsService {\n return new DefaultEventsService(options.logger);\n }\n\n /**\n * Returns a plugin-scoped context of the `EventService`\n * that ensures to prefix subscriber IDs with the plugin ID.\n *\n * @param pluginId - The plugin that the `EventService` should be created for.\n */\n forPlugin(pluginId: string): EventsService {\n return {\n publish: (params: EventParams): Promise<void> => {\n return this.publish(params);\n },\n subscribe: (options: EventsServiceSubscribeOptions): Promise<void> => {\n return this.subscribe({\n ...options,\n id: `${pluginId}.${options.id}`,\n });\n },\n };\n }\n\n async publish(params: EventParams): Promise<void> {\n this.logger.debug(\n `Event received: topic=${params.topic}, metadata=${JSON.stringify(\n params.metadata,\n )}, payload=${JSON.stringify(params.eventPayload)}`,\n );\n\n if (!this.subscribers.has(params.topic)) {\n return;\n }\n\n const onEventPromises: Promise<void>[] = [];\n this.subscribers.get(params.topic)?.forEach(subscription => {\n onEventPromises.push(\n (async () => {\n try {\n await subscription.onEvent(params);\n } catch (error) {\n this.logger.warn(\n `Subscriber \"${subscription.id}\" failed to process event for topic \"${params.topic}\"`,\n error,\n );\n }\n })(),\n );\n });\n\n await Promise.all(onEventPromises);\n }\n\n async subscribe(options: EventsServiceSubscribeOptions): Promise<void> {\n options.topics.forEach(topic => {\n if (!this.subscribers.has(topic)) {\n this.subscribers.set(topic, []);\n }\n\n this.subscribers.get(topic)!.push({\n id: options.id,\n onEvent: options.onEvent,\n });\n });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EventParams } from './EventParams';\nimport { EventRouter } from './EventRouter';\nimport { EventsService } from './EventsService';\n\n/**\n * Subscribes to the provided (generic) topic\n * and publishes the events under the more concrete sub-topic\n * depending on the implemented logic for determining it.\n * Implementing classes might use information from `metadata`\n * and/or properties within the payload.\n *\n * @public\n */\nexport abstract class SubTopicEventRouter extends EventRouter {\n protected constructor(options: { events: EventsService; topic: string }) {\n super({\n events: options.events,\n topics: [options.topic],\n });\n }\n\n protected abstract determineSubTopic(params: EventParams): string | undefined;\n\n protected determineDestinationTopic(params: EventParams): string | undefined {\n const subTopic = this.determineSubTopic(params);\n return subTopic ? `${params.topic}.${subTopic}` : undefined;\n }\n}\n","/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n createServiceRef,\n} from '@backstage/backend-plugin-api';\nimport { EventsService, DefaultEventsService } from './api';\n\n/**\n * The {@link EventsService} that allows to publish events, and subscribe to topics.\n * Uses the `root` scope so that events can be shared across all plugins, modules, and more.\n *\n * @public\n */\nexport const eventsServiceRef = createServiceRef<EventsService>({\n id: 'events.service',\n scope: 'plugin',\n defaultFactory: async service =>\n createServiceFactory({\n service,\n deps: {\n pluginMetadata: coreServices.pluginMetadata,\n rootLogger: coreServices.rootLogger,\n },\n async createRootContext({ rootLogger }) {\n return DefaultEventsService.create({ logger: rootLogger });\n },\n async factory({ pluginMetadata }, eventsService) {\n return eventsService.forPlugin(pluginMetadata.getId());\n },\n }),\n});\n"],"names":["__publicField","createServiceRef","createServiceFactory","coreServices"],"mappings":";;;;;;;;;;;;AA0BO,MAAe,WAAY,CAAA;AAAA,EAKtB,YAAY,OAAsD,EAAA;AAJ5E,IAAiBA,eAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AACjB,IAAiBA,eAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AACjB,IAAAA,eAAA,CAAA,IAAA,EAAQ,YAAsB,EAAA,KAAA,CAAA,CAAA;AAG5B,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA,CAAA;AACtB,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA,CAAA;AAAA,GACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,MAAM,SAA2B,GAAA;AAC/B,IAAA,IAAI,KAAK,UAAY,EAAA;AACnB,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AA;AAAA,MAC7B,YAAYA,6BAAa,CAAA,UAAA;AAAA,KAC3B;AAAA,IACA,MAAM,iBAAA,CAAkB,EAAE,UAAA,EAAc,EAAA;AACtC,MAAA,OAAO,oBAAqB,CAAA,MAAA,CAAO,EAAE,MAAA,EAAQ,YAAY,CAAA,CAAA;AAAA,KAC3D;AAAA,IACA,MAAM,OAAA,CAAQ,EAAE,cAAA,IAAkB,aAAe,EAAA;AAC/C,MAAA,OAAO,aAAc,CAAA,SAAA,CAAU,cAAe,CAAA,KAAA,EAAO,CAAA,CAAA;AAAA,KACvD;AAAA,GACD,CAAA;AACL,CAAC;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
|
|
2
|
+
import { LoggerService } from '@backstage/backend-plugin-api';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* @public
|
|
3
6
|
*/
|
|
@@ -16,26 +19,6 @@ interface EventParams<TPayload = unknown> {
|
|
|
16
19
|
metadata?: Record<string, string | string[] | undefined>;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
/**
|
|
20
|
-
* Handles received events.
|
|
21
|
-
* This may include triggering refreshes of catalog entities
|
|
22
|
-
* or other actions to react on events.
|
|
23
|
-
*
|
|
24
|
-
* @public
|
|
25
|
-
*/
|
|
26
|
-
interface EventSubscriber {
|
|
27
|
-
/**
|
|
28
|
-
* Supported event topics like "github", "bitbucketCloud", etc.
|
|
29
|
-
*/
|
|
30
|
-
supportsEventTopics(): string[];
|
|
31
|
-
/**
|
|
32
|
-
* React on a received event.
|
|
33
|
-
*
|
|
34
|
-
* @param params - parameters for the to be received event.
|
|
35
|
-
*/
|
|
36
|
-
onEvent(params: EventParams): Promise<void>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
22
|
/**
|
|
40
23
|
* Allows a decoupled and asynchronous communication between components.
|
|
41
24
|
* Components can publish events for a given topic and
|
|
@@ -43,7 +26,7 @@ interface EventSubscriber {
|
|
|
43
26
|
*
|
|
44
27
|
* @public
|
|
45
28
|
*/
|
|
46
|
-
interface
|
|
29
|
+
interface EventsService {
|
|
47
30
|
/**
|
|
48
31
|
* Publishes an event for the topic.
|
|
49
32
|
*
|
|
@@ -51,24 +34,27 @@ interface EventBroker {
|
|
|
51
34
|
*/
|
|
52
35
|
publish(params: EventParams): Promise<void>;
|
|
53
36
|
/**
|
|
54
|
-
*
|
|
37
|
+
* Subscribes to one or more topics, registering an event handler for them.
|
|
55
38
|
*
|
|
56
|
-
* @param
|
|
39
|
+
* @param options - event subscription options.
|
|
57
40
|
*/
|
|
58
|
-
subscribe(
|
|
41
|
+
subscribe(options: EventsServiceSubscribeOptions): Promise<void>;
|
|
59
42
|
}
|
|
60
|
-
|
|
61
43
|
/**
|
|
62
|
-
* Publishes events to be consumed by subscribers for their topic.
|
|
63
|
-
* The events can come from different (external) sources
|
|
64
|
-
* like emitted themselves, received via HTTP endpoint (i.e. webhook)
|
|
65
|
-
* or from event brokers, queues, etc.
|
|
66
|
-
*
|
|
67
44
|
* @public
|
|
68
45
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
46
|
+
type EventsServiceSubscribeOptions = {
|
|
47
|
+
/**
|
|
48
|
+
* Identifier for the subscription. E.g., used as part of log messages.
|
|
49
|
+
*/
|
|
50
|
+
id: string;
|
|
51
|
+
topics: string[];
|
|
52
|
+
onEvent: EventsServiceEventHandler;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
type EventsServiceEventHandler = (params: EventParams) => Promise<void>;
|
|
72
58
|
|
|
73
59
|
/**
|
|
74
60
|
* Subscribes to a topic and - depending on a set of conditions -
|
|
@@ -77,12 +63,49 @@ interface EventPublisher {
|
|
|
77
63
|
* @see {@link https://www.enterpriseintegrationpatterns.com/MessageRouter.html | Message Router pattern}.
|
|
78
64
|
* @public
|
|
79
65
|
*/
|
|
80
|
-
declare abstract class EventRouter
|
|
81
|
-
private
|
|
66
|
+
declare abstract class EventRouter {
|
|
67
|
+
private readonly events;
|
|
68
|
+
private readonly topics;
|
|
69
|
+
private subscribed;
|
|
70
|
+
protected constructor(options: {
|
|
71
|
+
events: EventsService;
|
|
72
|
+
topics: string[];
|
|
73
|
+
});
|
|
74
|
+
protected abstract getSubscriberId(): string;
|
|
82
75
|
protected abstract determineDestinationTopic(params: EventParams): string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Subscribes itself to the topic(s),
|
|
78
|
+
* after which events potentially can be received
|
|
79
|
+
* and processed by {@link EventRouter.onEvent}.
|
|
80
|
+
*/
|
|
81
|
+
subscribe(): Promise<void>;
|
|
83
82
|
onEvent(params: EventParams): Promise<void>;
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* In-process event broker which will pass the event to all registered subscribers
|
|
87
|
+
* interested in it.
|
|
88
|
+
* Events will not be persisted in any form.
|
|
89
|
+
* Events will not be passed to subscribers at other instances of the same cluster.
|
|
90
|
+
*
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
declare class DefaultEventsService implements EventsService {
|
|
94
|
+
private readonly logger;
|
|
95
|
+
private readonly subscribers;
|
|
96
|
+
private constructor();
|
|
97
|
+
static create(options: {
|
|
98
|
+
logger: LoggerService;
|
|
99
|
+
}): DefaultEventsService;
|
|
100
|
+
/**
|
|
101
|
+
* Returns a plugin-scoped context of the `EventService`
|
|
102
|
+
* that ensures to prefix subscriber IDs with the plugin ID.
|
|
103
|
+
*
|
|
104
|
+
* @param pluginId - The plugin that the `EventService` should be created for.
|
|
105
|
+
*/
|
|
106
|
+
forPlugin(pluginId: string): EventsService;
|
|
107
|
+
publish(params: EventParams): Promise<void>;
|
|
108
|
+
subscribe(options: EventsServiceSubscribeOptions): Promise<void>;
|
|
86
109
|
}
|
|
87
110
|
|
|
88
111
|
/**
|
|
@@ -156,11 +179,83 @@ interface HttpPostIngressOptions {
|
|
|
156
179
|
* @public
|
|
157
180
|
*/
|
|
158
181
|
declare abstract class SubTopicEventRouter extends EventRouter {
|
|
159
|
-
|
|
160
|
-
|
|
182
|
+
protected constructor(options: {
|
|
183
|
+
events: EventsService;
|
|
184
|
+
topic: string;
|
|
185
|
+
});
|
|
161
186
|
protected abstract determineSubTopic(params: EventParams): string | undefined;
|
|
162
187
|
protected determineDestinationTopic(params: EventParams): string | undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Handles received events.
|
|
192
|
+
* This may include triggering refreshes of catalog entities
|
|
193
|
+
* or other actions to react on events.
|
|
194
|
+
*
|
|
195
|
+
* @public
|
|
196
|
+
* @deprecated use the `EventsService` via the constructor, setter, or other means instead
|
|
197
|
+
*/
|
|
198
|
+
interface EventSubscriber {
|
|
199
|
+
/**
|
|
200
|
+
* Supported event topics like "github", "bitbucketCloud", etc.
|
|
201
|
+
*
|
|
202
|
+
* @deprecated use the `EventsService` via the constructor, setter, or other means instead
|
|
203
|
+
*/
|
|
163
204
|
supportsEventTopics(): string[];
|
|
205
|
+
/**
|
|
206
|
+
* React on a received event.
|
|
207
|
+
*
|
|
208
|
+
* @param params - parameters for the to be received event.
|
|
209
|
+
* @deprecated you are not required to expose this anymore when using `EventsService`
|
|
210
|
+
*/
|
|
211
|
+
onEvent(params: EventParams): Promise<void>;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Allows a decoupled and asynchronous communication between components.
|
|
216
|
+
* Components can publish events for a given topic and
|
|
217
|
+
* others can subscribe for future events for topics they are interested in.
|
|
218
|
+
*
|
|
219
|
+
* @public
|
|
220
|
+
* @deprecated use `EventsService` instead
|
|
221
|
+
*/
|
|
222
|
+
interface EventBroker {
|
|
223
|
+
/**
|
|
224
|
+
* Publishes an event for the topic.
|
|
225
|
+
*
|
|
226
|
+
* @param params - parameters for the to be published event.
|
|
227
|
+
*/
|
|
228
|
+
publish(params: EventParams): Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Adds new subscribers for {@link EventSubscriber#supportsEventTopics | interested topics}.
|
|
231
|
+
*
|
|
232
|
+
* @param subscribers - interested in events of specified topics.
|
|
233
|
+
*/
|
|
234
|
+
subscribe(...subscribers: Array<EventSubscriber | Array<EventSubscriber>>): void;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Publishes events to be consumed by subscribers for their topic.
|
|
239
|
+
* The events can come from different (external) sources
|
|
240
|
+
* like emitted themselves, received via HTTP endpoint (i.e. webhook)
|
|
241
|
+
* or from event brokers, queues, etc.
|
|
242
|
+
*
|
|
243
|
+
* @public
|
|
244
|
+
* @deprecated use the `EventsService` via the constructor, setter, or other means instead
|
|
245
|
+
*/
|
|
246
|
+
interface EventPublisher {
|
|
247
|
+
/**
|
|
248
|
+
* @deprecated use the `EventsService` via the constructor, setter, or other means instead
|
|
249
|
+
*/
|
|
250
|
+
setEventBroker(eventBroker: EventBroker): Promise<void>;
|
|
164
251
|
}
|
|
165
252
|
|
|
166
|
-
|
|
253
|
+
/**
|
|
254
|
+
* The {@link EventsService} that allows to publish events, and subscribe to topics.
|
|
255
|
+
* Uses the `root` scope so that events can be shared across all plugins, modules, and more.
|
|
256
|
+
*
|
|
257
|
+
* @public
|
|
258
|
+
*/
|
|
259
|
+
declare const eventsServiceRef: _backstage_backend_plugin_api.ServiceRef<EventsService, "plugin">;
|
|
260
|
+
|
|
261
|
+
export { DefaultEventsService, EventBroker, EventParams, EventPublisher, EventRouter, EventSubscriber, EventsService, EventsServiceEventHandler, EventsServiceSubscribeOptions, HttpPostIngressOptions, RequestDetails, RequestRejectionDetails, RequestValidationContext, RequestValidator, SubTopicEventRouter, eventsServiceRef };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-events-node",
|
|
3
3
|
"description": "The plugin-events-node module for @backstage/plugin-events-backend",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0-next.1",
|
|
5
5
|
"main": "./dist/index.cjs.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -40,10 +40,11 @@
|
|
|
40
40
|
"postpack": "backstage-cli package postpack"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@backstage/backend-plugin-api": "^0.6.
|
|
43
|
+
"@backstage/backend-plugin-api": "^0.6.14-next.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@backstage/
|
|
46
|
+
"@backstage/backend-common": "^0.21.4-next.1",
|
|
47
|
+
"@backstage/cli": "^0.25.3-next.1"
|
|
47
48
|
},
|
|
48
49
|
"files": [
|
|
49
50
|
"dist",
|