# jsonrpc-lite

> Parse and Serialize JSON-RPC2 messages in node.js or browser.

Latest version **2.2.0** (published 2020-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonrpc-lite
pnpm add jsonrpc-lite
yarn add jsonrpc-lite
bun add jsonrpc-lite
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2020-09-02 |
| First published | 2015-04-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 99.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 93 |
| Maintainers | zensh |
| Keywords | json, jsonrpc, json-rpc, jsonrpc2, serialize, parse, message |

## Links

- npm: https://www.npmjs.com/package/jsonrpc-lite
- Repository: https://github.com/teambition/jsonrpc-lite
- Issues: https://github.com/teambition/jsonrpc-lite/issues
- npm.io page: https://npm.io/package/jsonrpc-lite

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.2.0 (latest) — 2020-09-02
- 2.1.1 — 2020-08-06
- 2.1.0 — 2019-10-21
- 2.0.7 — 2019-04-15
- 2.0.6 — 2019-04-01
- 2.0.5 — 2019-03-15
- 2.0.4 — 2019-03-08
- 2.0.3 — 2019-03-02
- 2.0.2 — 2019-01-15
- 2.0.1 — 2018-07-20
- 2.0.0 — 2018-07-14
- 1.3.1 — 2018-05-07
- 1.3.0 — 2017-09-04
- 1.2.4 — 2017-06-29
- 1.2.3 — 2016-06-27
- … 8 more at https://npm.io/package/jsonrpc-lite/versions

## README

# JSON-RPC lite

Parse and Serialize JSON-RPC2 messages in node.js or browser.

Inspired by https://github.com/soggie/jsonrpc-serializer

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]

**A implementation of [JSON-RPC 2.0 specifications](http://jsonrpc.org/specification)**

## Install

```sh
npm install jsonrpc-lite
```

## API

```js
const jsonrpc = require('jsonrpc-lite')
```

### jsonrpc.request(id, method[, params])

Creates a JSON-RPC 2.0 request object, return JsonRpc object.

- `id`: {String|Integer}
- `method`: {String}
- `params`:  {Object|Array}, optional

```js
const requestObj = jsonrpc.request('123', 'update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   id: '123',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }
```

### jsonrpc.notification(method[, params])

Creates a JSON-RPC 2.0 notification object, return JsonRpc object.

- `method`: {String}
- `params`:  {Object|Array}, optional

```js
const notificationObj = jsonrpc.notification('update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }
```

### jsonrpc.success(id, result)

Creates a JSON-RPC 2.0 success response object, return JsonRpc object.

- `id`: {String|Integer}
- `result`:  {Mixed}

```js
const successObj = jsonrpc.success('123', 'OK')
// {
//   jsonrpc: '2.0',
//   id: '123',
//   result: 'OK',
// }
```

### jsonrpc.error(id, error)

Creates a JSON-RPC 2.0 error response object, return JsonRpc object.

- `id`: {String|Integer}
- `error`: {JsonRpcError}

```js
const errorObj = jsonrpc.error('123', new jsonrpc.JsonRpcError('some error', 99))
// {
//   jsonrpc: '2.0',
//   id: '123',
//   error: {code: 99, 'message': 'some error'},
// }
```

### jsonrpc.parse(message)

Takes a JSON-RPC 2.0 payload (string) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

- `message`: {String}

return an array, or an object of this format:

**single parsed request:**

```js
{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: 123,
    method: 'update',
    params: {}
  }
}
```

**batch parsed result:**

```js
[{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    method: 'update',
    params: [1, 2, 3]
  }
}, {
  type: 'notification',
  payload: {
    jsonrpc: '2.0',
    method: 'update',
    params: {_id: 'xxx'}
  }
}, {
  type: 'success',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    result: 'OK'
  }
}, {
  type: 'error',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    error: [jsonrpc.JsonRpcError object]
  }
}, {
  type: 'invalid',
  payload: [jsonrpc.JsonRpcError object]
}]
```

### jsonrpc.parseObject(message)

Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

- `message`: {Object}

return an `JsonRpcParsed` object with `type` and `payload`.

### Class: jsonrpc.JsonRpc()

### Class: jsonrpc.JsonRpcError(message, code[, data])

Create a JsonRpcError instance.

- `message`:  {String}
- `code`:  {Integer}
- `data`: {Mixed} optional

```js
const error = new jsonrpc.JsonRpcError('some error', 999)
```

### Class Method: jsonrpc.JsonRpcError.invalidRequest([data])

### Class Method: jsonrpc.JsonRpcError.methodNotFound([data])

### Class Method: jsonrpc.JsonRpcError.invalidParams([data])

### Class Method: jsonrpc.JsonRpcError.internalError([data])

### Class Method: jsonrpc.JsonRpcError.parseError([data])

[npm-url]: https://npmjs.org/package/jsonrpc-lite
[npm-image]: http://img.shields.io/npm/v/jsonrpc-lite.svg

[travis-url]: https://travis-ci.org/teambition/jsonrpc-lite
[travis-image]: http://img.shields.io/travis/teambition/jsonrpc-lite.svg

[downloads-url]: https://npmjs.org/package/jsonrpc-lite
[downloads-image]: http://img.shields.io/npm/dm/jsonrpc-lite.svg?style=flat-square

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