# min-rpc

> Minimal rpc server and client using http.

Latest version **1.0.2** (published 2017-02-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install min-rpc
pnpm add min-rpc
yarn add min-rpc
bun add min-rpc
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-02-21 |
| First published | 2017-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Andrew Joslin |
| Maintainers | andytjoslin |
| Keywords | rpc, http, server, client |

## Links

- npm: https://www.npmjs.com/package/min-rpc
- Repository: https://github.com/ajoslin/min-rpc
- Homepage: https://github.com/ajoslin/min-rpc#readme
- Issues: https://github.com/ajoslin/min-rpc/issues
- npm.io page: https://npm.io/package/min-rpc

## Dependencies (6)

- [xhr](https://npm.io/package/xhr.md) ~2.3.3
- [xtend](https://npm.io/package/xtend.md) ~4.0.1
- [express](https://npm.io/package/express.md) ^4.14.1
- [url-join](https://npm.io/package/url-join.md) ^1.1.0
- [body-parser](https://npm.io/package/body-parser.md) ~1.16.1
- [http-status-error](https://npm.io/package/http-status-error.md) ^1.1.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2017-02-21
- 1.0.1 — 2017-02-21
- 1.0.0 — 2017-02-21
- 0.0.2 — 2017-02-15
- 0.0.1 — 2017-02-15

## README

# min-rpc [![Build Status](https://travis-ci.org/ajoslin/min-rpc.svg?branch=master)](https://travis-ci.org/ajoslin/min-rpc)

> Minimal rpc server and client using http. <3kb in the browser.

Inspired by [vas](https://github.com/ahdinosaur/vas), but with as few features as possible.

## Install

```
$ npm install --save min-rpc
```

## Usage

**person/service.js**
```js
module.exports = {
  path: 'person',
  methods: {
    shout: function (phrase, callback) {
      callback(null, phrase.toUpperCase() + '!')
    },
    shoutUser: function (phrase, callback) {
      callback(null, this.user.name + ': ' + phrase.toUpperCase() + '!')
    }
  }
}
```

**server.js**
```js
var rpcServer = require('min-rpc/server')
var services = [
  require('./person/service')
]

var handler = rpcServer(services, {
  getContext: function (req, res, callback) {
    // ... Get the user from cookies or headers here.
    callback(null, {
      user: {name: 'Bob'}
    })
  }
})

var app = require('express')()
app.use('/api', handler)

app.listen(3000)
```

**browser.js**
```js
var rpcClient = require('min-rpc/client')

var api = rpcClient({
  baseUrl: 'http://localhost:3000/api'
})

api('person/shout', 'hey', function (error, data) {
  console.log(error) // => null
  console.log(data) // => 'HEY!'
})

api('person/shoutUser', 'hey', function (error, data) {
  console.log(error) // => null
  console.log(data) // => 'Bob: HEY!'
})
```

## Roadmap

- [EventSource addon](https://github.com/ajoslin/min-rpc/issues/2)
- 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:

```js
{
  path: 'my/service',
  methods: {
    method1: function (data, callback) { callback(null, 'result') },
    method2: function (data, callback) { callback(new Error('Nope!')) }
  }
}
```

##### 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.

```js
var rpcServer = RpcServer(services, {
  getContext: function (req, res, callback) {
    db.getUser(req.session.userId, function (error, user) {
      if (error) return callback(error)

      // Now `this` of service methods is equal to `{user: user}`
      callback(null, {user: user})
    })
  }
})
```

### `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](https://github.com/raynos/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](https://github.com/raynos/xhr). Change the options and return new options.

You can use this to handle authentication. For example:

```js
var myAuthToken = 'abc123qwert'
var api = rpcClient(services, {
  transformRequest: function (options) {
    options.headers.Authorization = 'Bearer ' + myAuthToken
    return options
  }
})
```


## License

MIT © [Andrew Joslin](http://ajoslin.com)

---
_Source: https://npm.io/package/min-rpc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
