npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-sap

v3.1.0

Published

SAP Anywhere API integration tool

Downloads

6

Readme

node-sap CircleCI

A small wrapper library to easily use SAP Anywhere's API

Installation

$ npm install node-sap

Usage

Include the following lines at the top of your file, where credentials points to a JSON file containing your SAP API credentials (see example below):

// app.js
var nodeSap = require('node-sap');
var credentials = require('./auth');

var sap = new nodeSap(credentials);

// auth.json
{
  "client_id": "123456789",
  "client_secret": "123456789",
  "refresh_token": "123456789"
}

node-sap automatically handles authentication, token expiry and renewal. In case of an error authenticating, the current version will reattempt the authentication request after 1 second and throw an error if the second attempt fails.

Creating resources

For your convenience the module creates resources which have the standard CRUD methods (minus the D, as sap doesn't allow you to delete objects). In order to create a resource, instantiate the module and use the createResource function to return a class which all the methods associated with it. Note that promises are returned from the resource creator. Also added in this module is the ability to cache resources (only applied to findAll method) - by setting the cached option as true you can enable this option, see example below.

Example: Creating a Customer Resource

For example, to instantiate a Customer resource.

var sap = new sapHelper(credentials);
var Customer = sap.createResource("Customers");

Example: Using a resource to create an instance of said resource

Customer.create(body)
.then( function (_id) {
    // do something with id
})
.catch( function (err) {
    // handle error
})

Example: finding all resources with email of "[email protected]"

var filter = "email eq '[email protected]'"
Customer.findAll(filter)
.then( function (_id) {
    // do something with id
})
.catch( function (err) {
    // handle error
})

List of functions - note that the filter parameter is optional

Customer.create(body)

Customer.findAll(filter)

Customer.findById(id, filter)

Customer.updateById(id, body)

a more extensive list of filters can be found here

Example: caching a resource

Use the stdTTL and checkPeriod in order to set how long you want the cache to last. Please make sure you set cache to true if you want to enable caching.

var Customer = sapHelper.createResource("Customers", {cache:true, stdTTL: 120, checkPeriod: 60});

execute()

The module also exposes a public execute method that allows you to send requests to the SAP Anywhere API. The function will automatically fetch an access token based on your credentials.

The execute method takes two parameters, an options object and a callback. The options object can contain the following properties:

  • a request method String (required)
  • an API path String (required)
  • a request parameters Object
  • a request body Object

The execute method passes four arguments to the handler callback:

  • an Error object
  • a data object with the response results
  • a status code integer
  • a headers object

Example: GET request

For example, to fetch a list of all products and expand their skus:

var options = {
  method: 'GET',
  path: '/Products',
  params: {
    expand: 'skus'
  }
};

sap.execute(options, function(err, data, status, headers) {
  // Asynchronously handle error or success
}

Example: POST request

For example, to POST a new Customer:

var options = {
  method: 'POST',
  path: '/Customers',
  body: {
    firstName: 'John',
    lastName: 'Doe',
    // ...
  }
};

sap.execute(options, function(err, data, status, headers) {
  // Asynchronously handle error or success
}

Changelog

Versions < 3.1.0

  • Important: versions prior to 3.1.0 do not handle token expiry/renewal and have been deprecated.
  • Only exposed a class-level execute method. Did not support resource creation and convenience methods.

Versions < 3.0.0

  • The module was initialized when importing:
// app.js
var credentials = require('./auth');
var sap = require('node-sap')(credentials);
  • The execute function only returned error and status objects

Versions < 2.0.0

execute() took four parameters instead of two:

  • the request method (as a String)
  • the API path (as a String)
  • request body parameters (as an Object)
  • a handler callback

Versions < 1.0.0

execute() passed three arguments to the handler callback instead of two:

  • An Error object
  • The HTTP response (as JSON)
  • The HTTP body (as JSON)

Tests

You must add authentication credentials in order to run end-to-end tests. To do so, modify the testCredentials.json file in the tests/support folder with your SAP API credentials.

# Run unit tests
$ `npm test`

# Run e2e tests
$ `npm run e2e-tests`

NOTE: the end-to-end tests purposefully hit the live SAP API, including POSTING and PATCHING. Please make sure you are using test API credentials.

Tests use the mocha framework, chai for BDD-style assertions, nock for mocking HTTP requests, and sinon for mocks, stubs and spies.