min-rpc
Minimal rpc server and client using http. <3kb in the browser.
Inspired by vas, but with as few features as possible.
Install
$ npm install --save min-rpc
Usage
person/service.js
moduleexports = path: 'person' methods: { } { }
server.js
var rpcServer = var services = var handler = var app = app app
browser.js
var rpcClient = var api =
Roadmap
- EventSource addon
- Better tests
API
require('min-rpc/server')(services, options)
-> requestHandler
Returns an http request handler that you can mount in your app. Accepts requests from the api client.
services
Each item in the services
array must have a string key path
and an object methods
. Example:
path: 'my/service' methods: { } { }
options
getContext(req, res, callback)
Set the this
that will be used when calling all service methods. Use this for things like getting the current user and checking permissions in your service methods.
Call callback
with (error, context)
. If error
is given, the service method will never be called.
Note: When using options.getContext
, avoid arrow functions in your service methods. this
matters.
var rpcServer =
require('min-rpc/client')(options)
-> apiClient
Returns an apiClient
function, which can talk to the server given at baseUrl
.
The apiClient also exposes a apiClient.request
function, which takes parameters (url, options, callback). Options are passed to xhr. This function is used internally to perform all rpc calls.
options
baseUrl
Type: string
Default: /
The url at which your min-rpc server is hosted.
transformRequest
Type: function
Use this to transform any requests made to the api. Receives options passed to xhr. Change the options and return new options.
You can use this to handle authentication. For example:
var myAuthToken = 'abc123qwert'var api =
License
MIT © Andrew Joslin