# @happapi/jsonrpc-api

> A simple Json-RPC API framework base on koa.

Latest version **2.0.5** (published 2020-08-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @happapi/jsonrpc-api
pnpm add @happapi/jsonrpc-api
yarn add @happapi/jsonrpc-api
bun add @happapi/jsonrpc-api
```

Provides the command `start-happapi-jsonrpc-server`.

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2020-08-22 |
| First published | 2020-08-08 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tianlin |
| Maintainers | wangtianlin |
| Keywords | koa, jsonrpc, api |

## Links

- npm: https://www.npmjs.com/package/@happapi/jsonrpc-api
- Repository: https://github.com/tianlinle/happapi-framework
- Homepage: https://github.com/tianlinle/happapi-framework#readme
- Issues: https://github.com/tianlinle/happapi-framework/issues
- npm.io page: https://npm.io/package/@happapi/jsonrpc-api

## Dependencies (7)

- [ajv](https://npm.io/package/ajv.md) ^6.12.3
- [koa](https://npm.io/package/koa.md) ^2.13.0
- [glob](https://npm.io/package/glob.md) ^7.1.6
- [raw-body](https://npm.io/package/raw-body.md) ^2.4.1
- [koa-router](https://npm.io/package/koa-router.md) ^9.1.0
- [content-type](https://npm.io/package/content-type.md) ^1.0.4
- [@happapi/jsonrpc-core](https://npm.io/package/@happapi/jsonrpc-core.md) ^1.0.6

## 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.0.5 (latest) — 2020-08-22
- 2.0.4 — 2020-08-19
- 2.0.3 — 2020-08-18
- 2.0.2 — 2020-08-18
- 2.0.1 — 2020-08-17
- 2.0.0 — 2020-08-16
- 1.1.1 — 2020-08-09
- 1.1.0 — 2020-08-09
- 1.0.4 — 2020-08-08
- 1.0.3 — 2020-08-08
- 1.0.2 — 2020-08-08
- 1.0.1 — 2020-08-08

## README

# `jsonrpc-api`

A simple Json-RPC API framework base on koa.

## Usage

### 1 Create a folder where controllers will be placed.
```
mkdir controllers
```

### 2 Create controllers under the folder created above.

#### 2.1 Example of using javascript:
```javascript
// controllers/A.js
const { ControllerBase } = require('@happapi/jsonrpc-api');

class A extends ControllerBase { // Classname should be the same as filename
  static paramsSchema() {
    // Return a json-schema object used to varify jsonprc params. Empty object by default.
    return {};
  }

  async main() {
    // Do stuff and return result
    return { message: 'success', requestBody: this.body, jsonrpcParams: this.params };
  }
}
module.exports = { A }; // Make class A under the exported object
```

#### 2.2 Example of using typescript:
```typescript
// controllers/A.ts
import { ControllerBase } from '@happapi/jsonrpc-api';

export class A extends ControllerBase {
  async main() {
    // Do stuff and return result
    return { message: 'success' };
  }
}
```

### 3 Start server.
```shell
CONTROLLER_DIR=./controllers npx start-happapi-jsonrpc-server
```
After that, to get all avalible jsonrpc methods:
```
Request: GET /
```
to call method `A`
```
Request: POST /
Request body: {"method": "A", "jsonrpc": "2.0", "id": "1"}
```

## Configurations or `start-happapi-jsonrpc-server`
+ `CONTROLLER_DIR`: Required. Where are controller files placed.
+ `ROUTER_PATH`: Optional. Request path, / by default.
+ `PORT`: Optional. The port the server is listening on, 3003 by default.
+ `BODY_LIMIT`: Optional. The maximun length of body, 10k by default.

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