open-index/open-npm huggingface dataset:
npm package: @happapi/jsonrpc-api
Description: A simple Json-RPC API framework base on koa.
Author: tianlin
Latest version: 2.0.5

# `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.
npm package: @happapi/jsonrpc-core
Description: A simple implementation of jsonrpc2
Author: tianlin
Latest version: 1.0.6

# jsonrpc
A simple implementation of jsonrpc2

### example

```js
const { JsonRpcHandler, JsonRpcError } = require('tian-jsonrpc');

let jsonRpc = new JsonRpcHandler();

//add a single method like this...
jsonRpc.setMethodHandler('sayHello', function ({ name }) {
    if (!name) {
        throw JsonRpcError.InvalidParams('parameter `name` is required');
    }
    return `hello ${name}`;
});

//now you can handle jsonrpc request
jsonRpc.handle('{"jsonrpc":"2.0","method":"doSomething","id":"1"}');

//await to get result
(async () => {
    let result2 = await jsonRpc.handle([
        { jsonrpc: '2.0', method: 'unexisted method', id: 1 },
        { jsonrpc: '2.0', method: 'sayHello', id: 2, params: { name: 'world' } }
    ]);
})();
```

### api
+ JsonRpcError
JsonRpcError.constructor(code, message, data?)
    + JsonRpcError.toJSON()
    + JsonRpcError.ParseError.constructor(data?)
    + JsonRpcError.InvalidRequest.constructor(data?)
    + JsonRpcError.MethodNotFound.constructor(data?)
    + JsonRpcError.InvalidParams.constructor(data?)
    + JsonRpcError.InternalError.constructor(data?)
+ JsonRpcResult
    + static JsonRpcResult.success(id, result)
    + static JsonRpcResult.error(id, error)
+ JsonRpcHandler
    + JsonRpcHandler.constructor()
    + JsonRpcHandler.setMethodHandler(method: string, handler: Function, context?: Object)
    + async JsonRpcHandler.handle(body)
npm package: @happapi/logger
Description: A simple and configurable logger using async_hooks.
Author: tianlin
Latest version: 1.0.2

# `logger`

A simple and configurable logger using async_hooks.

## Usage

```javascript
const Logger = require('@happapi/logger');

const logger = new Logger({ level: 'info', meta: { pid: process.pid } });

logger.info('process started'); // {"level":"info","time":"2020-08-08T05:24:45.295Z","message":"process started","meta":{"pid":8412},"data":{}}

const a = async (data) => {
  await Promise.resolve();
  logger.info('a ended');
}

logger.attach({ sub: 'a' });

a('1'); // {"level":"info","time":"2020-08-08T05:24:45.297Z","message":"a ended","meta":{"pid":8412,"sub":"a"},"data":"1"}
```