karmia-jsonrpc
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

karmia-jsonrpc

Karmia JSON-RPC 2.0 library

Installation

npm install karmia-jsonrpc

Example

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');

Sample function

// Import modules
const karmia_context = require('karmia-context'),
    karmia_jsonrpc = require('karmia-jsonrpc'),
    jsonrpc = karmia_jsonrpc();

// Add methods
jsonrpc.methods.set('method', function () {
    return Promise.resolve({success: true});
});

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

// Export handler
exports.handler = (event, context, callback) => {
    const context = karmia_context();
    jsonrpc.call(context, event.body).then((result) => {
        callback(null, result);
    }).catch((error) => {
        const result = {
            jsonrpc: '2.0',
            error: {
                code: -32603,
                message: "Internal error",
                data: error
            },
            id: event.body.id
        };

        callback(JSON.stringify(result));
    });
};

Readme

Keywords

none

Package Sidebar

Install

npm i karmia-jsonrpc

Weekly Downloads

1

Version

0.3.2

License

MIT

Unpacked Size

15.5 kB

Total Files

10

Last publish

Collaborators

  • fujimaki-k