Skip to content

Commit 870d48a

Browse files
authored
update cloud-sdk (#13)
* bug fix * update cloud-sdk to v3
1 parent efae405 commit 870d48a

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

cds-plugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const cds = require("@sap/cds");
22
const { validateNotificationTypes, readFile } = require("./lib/utils");
33
const { createNotificationTypesMap, processNotificationTypes} = require("./lib/notificationTypes");
4+
const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");
45

56
cds.once("served", async () => {
7+
setGlobalLogLevel("error");
68
const profiles = cds.env.profiles ?? [];
79
const production = profiles.includes('production');
810

lib/notificationTypes.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { executeHttpRequest, buildCsrfHeaders } = require("@sap-cloud-sdk/core");
2-
const { getNotificationDestination, doesKeyExist, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils");
1+
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
2+
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
3+
const { getNotificationDestination, doesKeyExist, getPrefix, getNotificationTypesKeyWithPrefix, executeRequest } = require("./utils");
34
const _ = require("lodash");
45

56
const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc";
@@ -68,7 +69,7 @@ async function getNotificationTypes() {
6869

6970
async function createNotificationType(notificationType) {
7071
const notificationDestination = await getNotificationDestination();
71-
const csrfHeaders = await buildCsrfHeaders(notificationDestination, {
72+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
7273
url: NOTIFICATION_TYPES_API_ENDPOINT,
7374
});
7475

@@ -87,7 +88,7 @@ async function createNotificationType(notificationType) {
8788

8889
async function updateNotificationType(id, notificationType) {
8990
const notificationDestination = await getNotificationDestination();
90-
const csrfHeaders = await buildCsrfHeaders(notificationDestination, {
91+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
9192
url: NOTIFICATION_TYPES_API_ENDPOINT,
9293
});
9394

@@ -106,7 +107,7 @@ async function updateNotificationType(id, notificationType) {
106107

107108
async function deleteNotificationType(notificationType) {
108109
const notificationDestination = await getNotificationDestination();
109-
const csrfHeaders = await buildCsrfHeaders(notificationDestination, {
110+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
110111
url: NOTIFICATION_TYPES_API_ENDPOINT,
111112
});
112113

@@ -123,6 +124,10 @@ async function deleteNotificationType(notificationType) {
123124
}
124125

125126
function _createChannelsMap(channels) {
127+
if(channels === null || channels === undefined) {
128+
return {};
129+
}
130+
126131
const channelMap = {};
127132

128133
channels.forEach((channel) => {

lib/notifications.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const { buildCsrfHeaders } = require("@sap-cloud-sdk/core");
1+
const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity");
22
const { getNotificationDestination, executeRequest, buildNotification } = require("./utils");
33
const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc";
44

55
async function postNotification() {
6-
const notificationServiceDestination = await getNotificationDestination();
7-
const csrfHeaders = await buildCsrfHeaders(notificationServiceDestination, {
6+
const notificationDestination = await getNotificationDestination();
7+
const csrfHeaders = await buildHeadersForDestination(notificationDestination, {
88
url: NOTIFICATIONS_API_ENDPOINT,
99
});
1010

1111
const notification = buildNotification(arguments);
1212

1313
if (notification) {
14-
const response = await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notificationServiceDestination, csrfHeaders);
14+
const response = await executeRequest("post", `${NOTIFICATIONS_API_ENDPOINT}/Notifications`, notification, notificationDestination, csrfHeaders);
1515
return response.data?.d ?? response;
1616
}
1717
}

lib/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { existsSync, readFileSync } = require('fs');
22
const { basename } = require('path')
33
const cds = require("@sap/cds");
4-
const { executeHttpRequest, getDestination } = require("@sap-cloud-sdk/core");
4+
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
5+
const { getDestination } = require("@sap-cloud-sdk/connectivity");
56
const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"];
67

78
const messages = {
@@ -51,7 +52,7 @@ function readFile(filePath) {
5152

5253
async function getNotificationDestination() {
5354
const destinationName = cds.env.requires.notifications?.destination ?? "SAP_Notifications"
54-
const notificationDestination = await getDestination(destinationName);
55+
const notificationDestination = await getDestination({ destinationName, useCache: true });
5556
if (!notificationDestination) {
5657
// TODO: What to do if destination isn't found??
5758
throw new Error(messages.DESTINATION_NOT_FOUND + destinationName);
@@ -68,10 +69,10 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) {
6869
return `${prefix}/${notificationTypeKey}`;
6970
}
7071

71-
async function executeRequest(httpMethod, targetUrl, payload, notifServiceDest, csrfHeaders) {
72+
async function executeRequest(httpMethod, targetUrl, payload, notificationDestination, csrfHeaders) {
7273
let response = {};
7374
try {
74-
response = await executeHttpRequest(notifServiceDest, {
75+
response = await executeHttpRequest(notificationDestination, {
7576
url: targetUrl,
7677
method: httpMethod,
7778
data: payload,

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"srv"
1313
],
1414
"dependencies": {
15-
"@sap-cloud-sdk/core": "^1.54.2",
15+
"@sap-cloud-sdk/connectivity": "3.5.0",
16+
"@sap-cloud-sdk/http-client": "3.5.0",
17+
"@sap-cloud-sdk/util": "3.5.0",
1618
"lodash": "4.17.21"
1719
},
1820
"devDependencies": {

0 commit comments

Comments
 (0)