Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/borderless/json-rpc
Tiny, type-safe JSON-RPC 2.0 implementation
https://github.com/borderless/json-rpc
json-rpc typescript
Last synced: 7 days ago
JSON representation
Tiny, type-safe JSON-RPC 2.0 implementation
- Host: GitHub
- URL: https://github.com/borderless/json-rpc
- Owner: borderless
- License: mit
- Created: 2020-01-13T18:01:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T06:33:24.000Z (11 months ago)
- Last Synced: 2024-09-18T09:38:12.768Z (about 2 months ago)
- Topics: json-rpc, typescript
- Language: TypeScript
- Homepage:
- Size: 833 KB
- Stars: 20
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON RPC
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]> Tiny, mostly compliant [JSON-RPC 2.0](https://www.jsonrpc.org/specification) implementation.
This package intentionally doesn't implement the "arguments" form of request parameters. This is when the input `params` can be an object or an ordered array representing the object. Instead, you can pass _any_ JSON params over the wire.
## Installation
```sh
npm install @borderless/json-rpc --save
```## Usage
This package makes no assumptions about the transportation layer, for client or server.
### Methods
```ts
type Methods = {
hello: {
request: {};
response: string;
};
echo: {
request: { arg: string };
response: string;
};
};
```### Server
The server accepts a dictionary of resolvers.
```ts
import { createServer } from "@borderless/json-rpc";const server = createServer({
hello: (_) => "Hello World!",
echo: ({ arg }) => arg,
});const res = await server({
jsonrpc: "2.0",
id: "test",
method: "hello",
}); //=> { jsonrpc: "2.0", id: "test", result: "Hello World!" }
```### Client
The client accepts a function to `send` the JSON-RPC request.
```ts
import { createClient } from "@borderless/json-rpc";const client = createClient(async (payload) => {
const res = await fetch("...", {
method: "POST",
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
});return res.json();
});const result = await client({
method: "hello",
params: {},
}); //=> "Hello World!"const results = await client.many([
{
method: "hello",
params: {},
},
{
method: "echo",
params: { arg: "Test" },
},
]); //=> ["Hello World!", "Test"]
```## License
MIT
[npm-image]: https://img.shields.io/npm/v/@borderless/json-rpc.svg?style=flat
[npm-url]: https://npmjs.org/package/@borderless/json-rpc
[downloads-image]: https://img.shields.io/npm/dm/@borderless/json-rpc.svg?style=flat
[downloads-url]: https://npmjs.org/package/@borderless/json-rpc
[travis-image]: https://img.shields.io/travis/borderless/json-rpc.svg?style=flat
[travis-url]: https://travis-ci.org/borderless/json-rpc
[coveralls-image]: https://img.shields.io/coveralls/borderless/json-rpc.svg?style=flat
[coveralls-url]: https://coveralls.io/r/borderless/json-rpc?branch=master