0.5.8 • Published 6 years ago

karmia-lambda-jsonrpc v0.5.8

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

karmia-jsonrpc

Karmia JSON-RPC 2.0 module for AWS Lambda.

Installation

npm install karmia-lambda-jsonrpc

Example

Also see example directory.

API Gateway / Lambda

Request mapping template

#set($parameters = $input.params())
{
    "stage": "$context.stage",
    "user_id": "$context.identity.cognitoIentityId",
    "headers": {
        #foreach($name in $parameters.heaer)
            "$name": "$util.escapeJavaScript($parameters.header.get($name))"
            #if($foreach.hasNext),#end
        #end
    },
    "body": {
        "jsonrpc": "2.0",
        "method": $input.json('$.method'),
        "params": $input.json('$.params'),
        "id": "$context.requestId"
    }
}

Response mapping template

$input.json('$.body');

Example lambda function

// Import modules
const karmia_jsonrpc = require('karmia-lambda-jsonrpc'),
    jsonrpc = new karmia_jsonrpc();

// Add methods
jsonrpc.methods.set('method', (event, context, params) => {
    return Promise.resolve({success: true});
});

/*
// Add multiple method
jsonrpc.methods.set({
    method1: (event, context, parameters) => {},
    method2: (event, context, parameters) => {}
});
*/

// Export handler
exports.handler = async (event) => {
    try {
        return jsonrpc.call(event);
        // You can specify request body instead of to use event.body
        // return jsonrpc.call(event, context, specified_body);
    } catch (error) {
        return Promise.reject({
            jsonrpc: '2.0',
                error: {
                    code: -32603,
                    message: "Internal error",
                    data: error
                },
                id: event.body.id
        });
    }
};
0.5.8

6 years ago

0.5.7

6 years ago

0.5.6

6 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.2

6 years ago

0.4.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago