SAP-archive / yaas-nodejs-client-sdk

YaaS.js, a Node.js client library for SAP Hybris as a Service (YaaS)
Apache License 2.0
15 stars 5 forks source link

added headers to response payload and hybris-count if provided, added… #1

Closed maxmarkus closed 8 years ago

maxmarkus commented 8 years ago
maxmarkus commented 8 years ago

Updates based on your suggestions, not totally sure how extensionModules work, but added my assumption.

abrain commented 8 years ago

Great, thanks a lot!

I guess we need to provide more detailed documentation about the extension mechanism anyway. The current description should be fine for now.

georgkreimer commented 8 years ago

Some clarification on the extension mechanism

I have a service (store-appliances) that is available on the YaaS market. As it's not part of any official product (== package) that is sold we wouldn't want to add this to this client SDK. So I think who ever wants to consume services from package that are not from hybris could use same technique.

So my second app is consuming some core YaaS services and also this 'store-appliances' service. In the app.js I do the following to tell where the app can load the client extension from:

var yaasApplianceExtension = {'serviceName':'appliances', 'path': path.join(__dirname, 'lib/store-appliances')};

Later in the the app when I initialize the client I supply it the array with the extension info:

yaas.init(config.clientId, config.clientSecret, config.scopes, config.projectId, [yaasApplianceExtension])
  .then(function(response) {
    // init successful
    console.log("YaaS client initialized.");
  }, function(reason) {
    // init not successful
    console.log("Could not initialize YaaS client!");
  }
);

In my lib/store-appliances.js I have implemented the client for the 'store-appliances' service. It's very similar to the core services that we have covered already:

var pathStoreApplianceBase = '/hybrislabs/store-appliances/v1';

var StoreAppliance = function(rh) {
  this.requestHelper = rh;

  this.getAppliances = function(params) {
    return this.requestHelper.get(pathStoreApplianceBase + '/appliances/', params);
  };

  this.getAppliance = function(applianceId) {
    return this.requestHelper.get(pathStoreApplianceBase + '/appliances/' + applianceId);
  };

  this.createAppliance = function(appliance) {
    return this.requestHelper.post(pathStoreApplianceBase + '/appliances/', 'application/json', appliance);
  };
};

module.exports = StoreAppliance;

In my routes I use it like this:

/* GET appliances for appliance owner. */
router.get('/:applianceOwner', function(req, res) {
  yaas.appliances
    .getAppliances({applianceOwner: req.params.applianceOwner})
    .then(function(appliancesResponse) {
      res.render('index', {
        applianceOwner: req.params.applianceOwner,
        appliances: appliancesResponse.body
      });
    })
    .catch(function(err) {
      res.json(err);
    });
});

The extension mechanism is a first shot as I needed it for a prototype that I was building and wanted to keep a client extension specific to my services outside of the official client SDK.

I hope the info above helps. Improvements are always welcome. We will add more documentation for the client SDK soon. Thanks for your support!