1

Created a MTAR application in VS Code with app router . made the changes in start file of approuter to a different file let say custominfo.js as below

enter image description here

below is the code in custominfo.js

const approuter = require("@sap/approuter");
const jwtDecode = require("jwt-decode");
const rp = require("request-promise");
const xsenv = require("@sap/xsenv");
xsenv.loadEnv();
const dest_service = xsenv.getServices({ dest: { tag: "destination" } }).dest;
const uaa_service = xsenv.getServices({ uaa: { tag: "xsuaa" } }).uaa;
const sUaaCredentials = dest_service.clientid + ":" + dest_service.clientsecret;
const sDestinationName = "devapi";
let ar = approuter();
ar.beforeRequestHandler.use("/data", (req, res) => {

let email = jwtDecode(req.user.token.accessToken).email;

return rp({
    uri: uaa_service.url + "/oauth/token",
    method: "POST",
    headers: {
        "Authorization": "Basic " + Buffer.from(sUaaCredentials).toString("base64"),
        "Content-type": "application/x-www-form-urlencoded"
    },
    form: {
        "client_id": dest_service.clientid,
        "grant_type": "client_credentials"
    }
}).then((data) => {
    const token = JSON.parse(data).access_token;
    return rp({
        uri: dest_service.uri + "/destination-configuration/v1/destinations/" + sDestinationName,
        headers: {
            "Authorization": "Bearer " + token
        }
    });
}).then((data) => {
    const oDestination = JSON.parse(data);
    const url = oDestination.destinationConfiguration.URL;

// need to send email address and app router url to fetch some json data in the API c 
   configured in destination 
 
}).then((result) => {
   
}).catch((error) => {
   
  });
  });
  ar.start();

In the above node js script i need to send email address and app router URL to other REST api to fetch some data .

so question was

How would i get email address from JWT Token in first Request Promise(rp) and destination URL from destination service in second Request Promise(rp) and pass these thing to third Request promise (rp) .

Third Request promise should trigger a REST API (Url is extracted from the destination service in second RP ) and should send the email address and the app router url in the header part to that REST API returns custom JSON .

i was able to get url and email in separate Request promise but couldn't get the app router URL.

Regards

Prasad

user3349850
  • 225
  • 1
  • 3
  • 21

0 Answers0