New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@alexmerced/simplerpc-server

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alexmerced/simplerpc-server

server library for simplerpc - RPC API framework

latest
Source
npmnpm
Version
1.1.61
Version published
Weekly downloads
16
128.57%
Maintainers
1
Weekly downloads
 
Created
Source

SimpleRPC - Server

npm install @alexmerced/simplerpc-server

The SimpleRPC framework is a framework for creating very simple lightweight RPC APIs for use with any backend or frontend framework with a redux/useReducer like API.

Actions take the signature of

(payload: Object, context: Object) => response:Any

The client function then can be called like so:

rpcDispatch({type: "actionName", payload: {arg1: 1, arg 2: "hello"}})

Setting up the Server

First we create an handler

const createHandler = require("@alexmerced/simplerpc-server")

// server actions
const actions = {
    getList: (payload, context) => {
        console.log(context)
        return [1,2,3,4,5]
    }

    addToList: (payload, context) => {
        console.log(context)
        return [1,2,3,4,5, payload.num]
    }
}

// context - other data, each action should always have available
const context = {
    user: "Mr. Jones"
}


// create the handler
const handler = createHandler({actions, context})

module.exports = handler

Setting up the Route

The route must be used for a post route, the endpoint url can be anything, we recommend /rpc.

expressjs

app.post("/rpc", (req, res) => {
    const result = handler(req.body)
    res.json(result)
})

fastifyjs

fastify.post('/rpc', async (request, reply) => {
  const result = handler(request.body)
  return result
})

koajs (using koa router & koa-bodyparser)

router.get('/rpc', (ctx, next) => {
  const result = handler(ctx.request.body)
  return result
});

You can now call your actions by:

  • configuring a client with a client library
  • making a post request to the endpoint with a body that looks like so
{
    "type": "actionName",
    "payload":{
        "arg1": 1,
        "arg2": "whatever"
    }
}

FAQs

Package last updated on 02 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts