@neoimpulse/cap-js-notifications 1.0.6 → 1.0.7

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.
@@ -2,49 +2,49 @@ const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
2
2
  const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
3
3
  const { getNotificationDestination, readFile, buildNotification } = require("../lib/utils");
4
4
  const cds = require("@sap/cds");
5
- const LOG = cds.log('notifications');
5
+ const LOG = cds.log("notifications");
6
6
  const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
7
7
 
8
+ module.exports = exports = class NotifyToRest extends cds.Service {
9
+ async init() {
10
+ const notificationTypes = readFile(cds.env.requires?.notifications?.types);
11
+ const eventsToListen = notificationTypes.map((notificationType) => notificationType.NotificationTypeKey);
12
+ LOG._info && LOG.info(`Listening to events: ${eventsToListen.join(", ")}`);
13
+ this.on(eventsToListen, (req) => this.postNotification(req));
14
+ return super.init();
15
+ }
8
16
 
9
- module.exports = exports = class NotifyToRest extends cds.Service {
10
- async init() {
11
- const notificationTypes = readFile(cds.env.requires?.notifications?.types);
12
- const eventsToListen = notificationTypes.map(notificationType => notificationType.NotificationTypeKey);
13
- LOG._info && LOG.info(`Listening to events: ${eventsToListen.join(", ")}`);
14
- this.on(eventsToListen, req => this.postNotification(req))
15
- return super.init()
16
- }
17
-
18
- async postNotification(req) {
19
- const type = req.event
20
- let message = req.data
21
- message.type = type
22
- message = buildNotification(message);
17
+ async postNotification(req) {
18
+ const type = req.event;
19
+ let message = req.data;
20
+ message.type = type;
21
+ message = buildNotification(message);
23
22
 
24
- const notificationDestination = await getNotificationDestination();
25
- const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
26
- url: NOTIFICATIONS_API_ENDPOINT,
27
- });
23
+ const notificationDestination = await getNotificationDestination();
24
+ const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
25
+ url: NOTIFICATIONS_API_ENDPOINT
26
+ });
28
27
 
29
- try {
30
- LOG._info && LOG.info(
31
- `Sending notification of key: ${message.NotificationTypeKey} and version: ${message.NotificationTypeVersion}`
32
- );
33
- await executeHttpRequest(notificationDestination, {
34
- url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
35
- method: "post",
36
- data: buildNotification(message),
37
- headers: csrfHeaders,
38
- });
39
- } catch (err) {
40
- const message = err.response.data?.error?.message?.value ?? err.response.message;
41
- const error = new cds.error(message);
28
+ try {
29
+ LOG._info &&
30
+ LOG.info(
31
+ `Sending notification of key: ${message.NotificationTypeKey} and version: ${message.NotificationTypeVersion}`
32
+ );
33
+ await executeHttpRequest(notificationDestination, {
34
+ url: `${NOTIFICATIONS_API_ENDPOINT}/Notifications`,
35
+ method: "post",
36
+ data: message,
37
+ headers: csrfHeaders
38
+ });
39
+ } catch (err) {
40
+ const message = err.response.data?.error?.message?.value ?? err.response.message;
41
+ const error = new cds.error(message);
42
42
 
43
- if (/^4\d\d$/.test(err.response?.status) && err.response?.status != 429) {
44
- error.unrecoverable = true;
45
- }
43
+ if (/^4\d\d$/.test(err.response?.status) && err.response?.status != 429) {
44
+ error.unrecoverable = true;
45
+ }
46
46
 
47
- throw error;
47
+ throw error;
48
+ }
48
49
  }
49
- }
50
- }
50
+ };