@backstage/plugin-notifications-node 0.0.1-next.0

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 ADDED
@@ -0,0 +1,14 @@
1
+ # @backstage/plugin-notifications-node
2
+
3
+ ## 0.0.1-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - fb8fc24: Initial notifications system for backstage
8
+ - Updated dependencies
9
+ - @backstage/backend-common@0.21.0-next.2
10
+ - @backstage/plugin-signals-node@0.0.1-next.2
11
+ - @backstage/backend-plugin-api@0.6.10-next.2
12
+ - @backstage/plugin-notifications-common@0.0.1-next.0
13
+ - @backstage/catalog-client@1.6.0-next.1
14
+ - @backstage/catalog-model@1.4.4-next.0
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @backstage/plugin-notifications-node
2
+
3
+ Welcome to the Node.js library package for the notifications plugin!
4
+
5
+ ## Getting Started
6
+
7
+ To be able to send notifications from other backend plugins, the `NotificationService` must be initialized for the
8
+ environment. Add notification service to your `plugin.ts` as a dependency for init
9
+
10
+ ```ts
11
+ import { notificationService } from '@backstage/plugin-notifications-node';
12
+
13
+ export const myPlugin = createBackendPlugin({
14
+ pluginId: 'myPlugin',
15
+ register(env) {
16
+ env.registerInit({
17
+ deps: {
18
+ config: coreServices.rootConfig,
19
+ logger: coreServices.logger,
20
+ httpRouter: coreServices.httpRouter,
21
+ notificationService: notificationService,
22
+ },
23
+ async init({ config, logger, httpRouter, notificationService }) {
24
+ httpRouter.use(
25
+ await createRouter({
26
+ config,
27
+ logger,
28
+ permissions,
29
+ notificationService,
30
+ }),
31
+ );
32
+ },
33
+ });
34
+ },
35
+ });
36
+ ```
37
+
38
+ You also need to set up the `@backstage/plugin-notifications-backend` and `@backstage/plugin-notifications`
39
+ to be able to show notifications in the UI.
40
+
41
+ ## Sending notifications
42
+
43
+ To send notifications from backend plugin, use the `NotificationService::send` functionality. This function will
44
+ save the notification and optionally signal the frontend to show the latest status for users.
45
+
46
+ When sending notifications, you can specify the entity reference of the notification. If the entity reference is
47
+ a user, the notification will be sent to only that user. If it's a group, the notification will be sent to all
48
+ members of the group. If it's some other entity, the notification will be sent to the owner of that entity.
49
+
50
+ If the notification has `scope` set and user already has notification with that scope, the existing notification
51
+ will be updated with the new notification values and moved to inbox as unread.
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var backendPluginApi = require('@backstage/backend-plugin-api');
6
+
7
+ class DefaultNotificationService {
8
+ constructor(discovery, tokenManager, pluginId) {
9
+ this.discovery = discovery;
10
+ this.tokenManager = tokenManager;
11
+ this.pluginId = pluginId;
12
+ }
13
+ static create({
14
+ tokenManager,
15
+ discovery,
16
+ pluginId
17
+ }) {
18
+ return new DefaultNotificationService(discovery, tokenManager, pluginId);
19
+ }
20
+ async send(notification) {
21
+ try {
22
+ const baseUrl = await this.discovery.getBaseUrl("notifications");
23
+ const { token } = await this.tokenManager.getToken();
24
+ const response = await fetch(`${baseUrl}/`, {
25
+ method: "POST",
26
+ body: JSON.stringify({
27
+ ...notification,
28
+ // TODO: Should retrieve this in the backend from service auth instead
29
+ origin: `plugin-${this.pluginId}`
30
+ }),
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ Accept: "application/json",
34
+ Authorization: `Bearer ${token}`
35
+ }
36
+ });
37
+ if (!response.ok) {
38
+ throw new Error(`Request failed with status ${response.status}`);
39
+ }
40
+ } catch (error) {
41
+ throw new Error(`Failed to send notifications: ${error}`);
42
+ }
43
+ }
44
+ }
45
+
46
+ const notificationService = backendPluginApi.createServiceRef({
47
+ id: "notifications.service",
48
+ scope: "plugin",
49
+ defaultFactory: async (service) => backendPluginApi.createServiceFactory({
50
+ service,
51
+ deps: {
52
+ discovery: backendPluginApi.coreServices.discovery,
53
+ tokenManager: backendPluginApi.coreServices.tokenManager,
54
+ pluginMetadata: backendPluginApi.coreServices.pluginMetadata
55
+ },
56
+ factory({ discovery, tokenManager, pluginMetadata }) {
57
+ return DefaultNotificationService.create({
58
+ discovery,
59
+ tokenManager,
60
+ pluginId: pluginMetadata.getId()
61
+ });
62
+ }
63
+ })
64
+ });
65
+
66
+ const notificationsProcessingExtensionPoint = backendPluginApi.createExtensionPoint({
67
+ id: "notifications.processing"
68
+ });
69
+
70
+ exports.DefaultNotificationService = DefaultNotificationService;
71
+ exports.notificationService = notificationService;
72
+ exports.notificationsProcessingExtensionPoint = notificationsProcessingExtensionPoint;
73
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/service/DefaultNotificationService.ts","../src/lib.ts","../src/extensions.ts"],"sourcesContent":["/*\n * Copyright 2023 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 */\nimport { TokenManager } from '@backstage/backend-common';\nimport { NotificationService } from './NotificationService';\nimport { DiscoveryService } from '@backstage/backend-plugin-api';\nimport { NotificationPayload } from '@backstage/plugin-notifications-common';\n\n/** @public */\nexport type NotificationServiceOptions = {\n discovery: DiscoveryService;\n tokenManager: TokenManager;\n pluginId: string;\n};\n\n/** @public */\nexport type NotificationRecipients = {\n type: 'entity';\n entityRef: string | string[];\n};\n\n// TODO: Support for broadcast messages\n// | { type: 'broadcast' };\n\n/** @public */\nexport type NotificationSendOptions = {\n recipients: NotificationRecipients;\n payload: NotificationPayload;\n};\n\n/** @public */\nexport class DefaultNotificationService implements NotificationService {\n private constructor(\n private readonly discovery: DiscoveryService,\n private readonly tokenManager: TokenManager,\n private readonly pluginId: string,\n ) {}\n\n static create({\n tokenManager,\n discovery,\n pluginId,\n }: NotificationServiceOptions): DefaultNotificationService {\n return new DefaultNotificationService(discovery, tokenManager, pluginId);\n }\n\n async send(notification: NotificationSendOptions): Promise<void> {\n try {\n const baseUrl = await this.discovery.getBaseUrl('notifications');\n const { token } = await this.tokenManager.getToken();\n const response = await fetch(`${baseUrl}/`, {\n method: 'POST',\n body: JSON.stringify({\n ...notification,\n // TODO: Should retrieve this in the backend from service auth instead\n origin: `plugin-${this.pluginId}`,\n }),\n headers: {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n Authorization: `Bearer ${token}`,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Request failed with status ${response.status}`);\n }\n } catch (error) {\n // TODO: Should not throw in optimal case, see BEP\n throw new Error(`Failed to send notifications: ${error}`);\n }\n }\n}\n","/*\n * Copyright 2023 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 */\nimport {\n coreServices,\n createServiceFactory,\n createServiceRef,\n} from '@backstage/backend-plugin-api';\nimport { DefaultNotificationService } from './service';\nimport { NotificationService } from './service/NotificationService';\n\n/** @public */\nexport const notificationService = createServiceRef<NotificationService>({\n id: 'notifications.service',\n scope: 'plugin',\n defaultFactory: async service =>\n createServiceFactory({\n service,\n deps: {\n discovery: coreServices.discovery,\n tokenManager: coreServices.tokenManager,\n pluginMetadata: coreServices.pluginMetadata,\n },\n factory({ discovery, tokenManager, pluginMetadata }) {\n return DefaultNotificationService.create({\n discovery,\n tokenManager,\n pluginId: pluginMetadata.getId(),\n });\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 */\nimport { createExtensionPoint } from '@backstage/backend-plugin-api';\nimport { Notification } from '@backstage/plugin-notifications-common';\n\n/**\n * @public\n */\nexport interface NotificationProcessor {\n /**\n * Decorate notification before sending it\n *\n * @param notification - The notification to decorate\n * @returns The same notification or a modified version of it\n */\n decorate?(notification: Notification): Promise<Notification>;\n\n /**\n * Send notification using this processor.\n *\n * @param notification - The notification to send\n */\n send?(notification: Notification): Promise<void>;\n}\n\n/**\n * @public\n */\nexport interface NotificationsProcessingExtensionPoint {\n addProcessor(\n ...processors: Array<NotificationProcessor | Array<NotificationProcessor>>\n ): void;\n}\n\n/**\n * @public\n */\nexport const notificationsProcessingExtensionPoint =\n createExtensionPoint<NotificationsProcessingExtensionPoint>({\n id: 'notifications.processing',\n });\n"],"names":["createServiceRef","createServiceFactory","coreServices","createExtensionPoint"],"mappings":";;;;;;AA2CO,MAAM,0BAA0D,CAAA;AAAA,EAC7D,WAAA,CACW,SACA,EAAA,YAAA,EACA,QACjB,EAAA;AAHiB,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA,CAAA;AACA,IAAA,IAAA,CAAA,YAAA,GAAA,YAAA,CAAA;AACA,IAAA,IAAA,CAAA,QAAA,GAAA,QAAA,CAAA;AAAA,GAChB;AAAA,EAEH,OAAO,MAAO,CAAA;AAAA,IACZ,YAAA;AAAA,IACA,SAAA;AAAA,IACA,QAAA;AAAA,GACyD,EAAA;AACzD,IAAA,OAAO,IAAI,0BAAA,CAA2B,SAAW,EAAA,YAAA,EAAc,QAAQ,CAAA,CAAA;AAAA,GACzE;AAAA,EAEA,MAAM,KAAK,YAAsD,EAAA;AAC/D,IAAI,IAAA;A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AA,KAC/B;AAAA,IACA,OAAQ,CAAA,EAAE,SAAW,EAAA,YAAA,EAAc,gBAAkB,EAAA;AACnD,MAAA,OAAO,2BAA2B,MAAO,CAAA;AAAA,QACvC,SAAA;AAAA,QACA,YAAA;AAAA,QACA,QAAA,EAAU,eAAe,KAAM,EAAA;AAAA,OAChC,CAAA,CAAA;AAAA,KACH;AAAA,GACD,CAAA;AACL,CAAC;;ACOM,MAAM,wCACXC,qCAA4D,CAAA;AAAA,EAC1D,EAAI,EAAA,0BAAA;AACN,CAAC;;;;;;"}
@@ -0,0 +1,69 @@
1
+ import { TokenManager } from '@backstage/backend-common';
2
+ import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
3
+ import { DiscoveryService } from '@backstage/backend-plugin-api';
4
+ import { NotificationPayload, Notification } from '@backstage/plugin-notifications-common';
5
+
6
+ /** @public */
7
+ interface NotificationService {
8
+ send(options: NotificationSendOptions): Promise<void>;
9
+ }
10
+
11
+ /** @public */
12
+ type NotificationServiceOptions = {
13
+ discovery: DiscoveryService;
14
+ tokenManager: TokenManager;
15
+ pluginId: string;
16
+ };
17
+ /** @public */
18
+ type NotificationRecipients = {
19
+ type: 'entity';
20
+ entityRef: string | string[];
21
+ };
22
+ /** @public */
23
+ type NotificationSendOptions = {
24
+ recipients: NotificationRecipients;
25
+ payload: NotificationPayload;
26
+ };
27
+ /** @public */
28
+ declare class DefaultNotificationService implements NotificationService {
29
+ private readonly discovery;
30
+ private readonly tokenManager;
31
+ private readonly pluginId;
32
+ private constructor();
33
+ static create({ tokenManager, discovery, pluginId, }: NotificationServiceOptions): DefaultNotificationService;
34
+ send(notification: NotificationSendOptions): Promise<void>;
35
+ }
36
+
37
+ /** @public */
38
+ declare const notificationService: _backstage_backend_plugin_api.ServiceRef<NotificationService, "plugin">;
39
+
40
+ /**
41
+ * @public
42
+ */
43
+ interface NotificationProcessor {
44
+ /**
45
+ * Decorate notification before sending it
46
+ *
47
+ * @param notification - The notification to decorate
48
+ * @returns The same notification or a modified version of it
49
+ */
50
+ decorate?(notification: Notification): Promise<Notification>;
51
+ /**
52
+ * Send notification using this processor.
53
+ *
54
+ * @param notification - The notification to send
55
+ */
56
+ send?(notification: Notification): Promise<void>;
57
+ }
58
+ /**
59
+ * @public
60
+ */
61
+ interface NotificationsProcessingExtensionPoint {
62
+ addProcessor(...processors: Array<NotificationProcessor | Array<NotificationProcessor>>): void;
63
+ }
64
+ /**
65
+ * @public
66
+ */
67
+ declare const notificationsProcessingExtensionPoint: _backstage_backend_plugin_api.ExtensionPoint<NotificationsProcessingExtensionPoint>;
68
+
69
+ export { DefaultNotificationService, NotificationProcessor, NotificationRecipients, NotificationSendOptions, NotificationService, NotificationServiceOptions, NotificationsProcessingExtensionPoint, notificationService, notificationsProcessingExtensionPoint };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@backstage/plugin-notifications-node",
3
+ "description": "Node.js library for the notifications plugin",
4
+ "version": "0.0.1-next.0",
5
+ "main": "dist/index.cjs.js",
6
+ "types": "dist/index.d.ts",
7
+ "license": "Apache-2.0",
8
+ "publishConfig": {
9
+ "access": "public",
10
+ "main": "dist/index.cjs.js",
11
+ "types": "dist/index.d.ts"
12
+ },
13
+ "backstage": {
14
+ "role": "node-library"
15
+ },
16
+ "scripts": {
17
+ "build": "backstage-cli package build",
18
+ "lint": "backstage-cli package lint",
19
+ "test": "backstage-cli package test",
20
+ "clean": "backstage-cli package clean",
21
+ "prepack": "backstage-cli package prepack",
22
+ "postpack": "backstage-cli package postpack"
23
+ },
24
+ "devDependencies": {
25
+ "@backstage/cli": "^0.25.2-next.2",
26
+ "@backstage/test-utils": "^1.5.0-next.2",
27
+ "msw": "^1.0.0"
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "dependencies": {
33
+ "@backstage/backend-common": "^0.21.0-next.2",
34
+ "@backstage/backend-plugin-api": "^0.6.10-next.2",
35
+ "@backstage/catalog-client": "^1.6.0-next.1",
36
+ "@backstage/catalog-model": "^1.4.4-next.0",
37
+ "@backstage/plugin-notifications-common": "^0.0.1-next.0",
38
+ "@backstage/plugin-signals-node": "^0.0.1-next.2",
39
+ "knex": "^3.0.0",
40
+ "uuid": "^8.0.0"
41
+ }
42
+ }