@compassdigital/provider

An easy way to get started building data providers for Compass Digital. Provides an easy interface for creating methods that will be called via JSON-RPC 2.0.


License
ISC
Install
npm install @compassdigital/provider@3.1.0

Documentation

Compass Digital Data Provider

An easy way to get started building data providers for Compass Digital. Provides an easy interface for creating methods that will be called via JSON-RPC 2.0.

Includes a built-in HTTP server and AWS Lambda handler.

Requirements

  • node.js 6+

Installation

npm install @compassdigital/provider --save

Usage

var Provider = require("@compassdigital/provider");

var provider = new Provider({type: "menu"});

// Handle the 'get' method
provider.on("get", function(req, callback)
{
    // Do something to get the result
    your_function(req, function(err, res)
    {
        callback(err, res);
    });
});

// Convenience method: respond to several methods using the same handler
provider.on(["get", "get_menu", "get_that_menu"], () => {...});

Context

Sometimes your handler needs to know the context of the method call e.g. the user's id, location, etc. This is accessible via promises from this.context().

For example:

provider.on("get", function(req, callback)
{
    // Get the user's id
    this.context().user().then(function(user_data)
    {
        var user_id = user_data.id;

        callback(err, { message: "Your user id was " + user_id });
    });
});

HTTP Server

// Start on port 3000
provider.start(3000);

// Stop server
provider.stop();

AWS Lambda Handler

Create lambda.js ending with:

var provider = //...
module.exports = provider.lambda;

Configure a Lambda function with the setting Handler:

lambda.handler

Testing

KEY_ARN="SOME PRIVATE AWS KMS KEY ARN" node test