https://github.com/ptol/typed-json-rpc
Simple, statically typed and ergonomic way to do async communications (events, workers, http requests) with json-rpc
https://github.com/ptol/typed-json-rpc
json-rpc web-workers
Last synced: 12 months ago
JSON representation
Simple, statically typed and ergonomic way to do async communications (events, workers, http requests) with json-rpc
- Host: GitHub
- URL: https://github.com/ptol/typed-json-rpc
- Owner: ptol
- License: mit
- Created: 2022-04-24T14:07:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-30T09:55:30.000Z (almost 4 years ago)
- Last Synced: 2025-06-01T12:13:54.648Z (about 1 year ago)
- Topics: json-rpc, web-workers
- Language: TypeScript
- Homepage:
- Size: 112 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-json-rpc
>Simple, statically typed and ergonomic way to do async communications (events, workers, http requests) with json-rpc
## Install
```bash
npm install typed-json-rpc
```
## Describe your API
```typescript
export interface Api {
sum: (x: number, y: number) => number
increase: (x: number) => number
}
```
## Create your API implementation as request handler
```typescript
const requestHandler = createRequestHandler({
increase: (x) => {
return x + 1
},
sum: (x, y) => x + y,
})
```
## Create a client
```typescript
const sender = createHttpSender()
const client = createJsonRpcClient(sender)
```
## And call your API
```typescript
const x = await client.increase(100)
const y = await client.sum(1,2)
```
> All this code can be used for any type of async communication. The difference is how you send your requests and responses.
## Http sender example
```typescript
export const createHttpSender = (url: string): RequestSender => ({
sendRequest(request: JsonRpcRequest) {
return axios
.post>(
url,
request,
).then((x) =>
'error' in x.data ? Promise.reject(x.data.error) : x.data.result,
)
},
})
```
## Express responses
```typescript
app.post('/', async (req, res) => {
const response = await requestHandler.handleRequest(req.body)
return res.status(200).json(response)
})
```
## [Full http example](https://github.com/ptol/typed-json-rpc/tree/main/examples/src/http)
## Worker and EventEmitter examples
* [Worker example](https://github.com/ptol/typed-json-rpc/tree/main/examples/src/worker)
* [EventEmitter example](https://github.com/ptol/typed-json-rpc/tree/main/examples/src/events)