Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fabiospampinato/picorpc
A tiny RPC library and spec, inspired by JSON-RPC 2.0 and tRPC.
https://github.com/fabiospampinato/picorpc
json pico rpc tiny typescript
Last synced: 3 months ago
JSON representation
A tiny RPC library and spec, inspired by JSON-RPC 2.0 and tRPC.
- Host: GitHub
- URL: https://github.com/fabiospampinato/picorpc
- Owner: fabiospampinato
- License: mit
- Created: 2023-02-19T05:27:44.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-07T17:46:05.000Z (10 months ago)
- Last Synced: 2024-06-30T16:49:51.811Z (4 months ago)
- Topics: json, pico, rpc, tiny, typescript
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 93
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# PicoRPC
A tiny RPC library and [spec](./spec.md), inspired by [JSON-RPC 2.0](https://www.jsonrpc.org/specification) and [tRPC](https://trpc.io).
## Install
```sh
npm install --save picorpc
```## Usage
```ts
import {createAbstractClient, createAbstractServer} from 'picorpc';
import {createMemoryClient, createMemoryServer} from 'picorpc';
import {createHttpClient, createHttpServer} from 'picorpc';// Custom map of procedures to expose to the client
const Procedures = {
ctx () { // This procedure reads something from the context, which will be attached to "this"
return this.value;
},
sum ( a, b ) { // This procedure is a simple pure function that doesn't use the context
return a + b;
},
throw () { // This procedure throws, with a custom error code and data value
const error = new Error ( 'Some custom error' );
error.code = 1;
error.data = 'Some data';
throw error;
}
};// Create an in-memory client/server
// An in-memory server behind a proper HTTP server is the recommended way to expose procedures via an API
// An in-memory client is mostly useful for testingconst server = createMemoryServer ({
procedures: Procedures
});const client = createMemoryClient ({
server, // The in-memory server to pass requests to
context: () => ({ // Custom, optional, context object, which will be attached to every request automatically
value: 123
})
});await client.sum ( 1, 2 ); // => 3
await client.ctx (); // => 123
await client.throw (); // => This will throw// Create an HTTP client/server
// The HTTP server is intended for simple internal inter-process communication
// The HTTP client makes request with the fetch API, so it works everywhereconst client = createHttpClient ({
context: () => ({ // Custom, optional, context object, which will be attached to every request automatically
value: 123
}),
serializer: JSON.stringify, // Custom, optional, serializer
deserializer: JSON.parse, // Custom, optional, deserializer
url: 'http://localhost:6000' // Required endpoint URL
});const server = createHttpServer ({
serializer: JSON.stringify, // Custom, optional, serializer
deserializer: JSON.parse, // Custom, optional, deserializer
port: 6000, // The port to start listening at
procedures: Procedures
});await client.sum ( 1, 2 ); // => 3
await client.ctx (); // => 123
await client.throw (); // => This will throwserver.close (); // Close the server, stopping listening
// Create an abstract server, to use your own transport protocol
const client = createAbstractClient ({
handler: request => {
//TODO: Send the request somewhere and read the result back
}
});const client = createAbstractServer ({
procedures: Procedures,
handler: response => {
//TODO: Do something with the response object, optionally
}
});
```## License
MIT © Fabio Spampinato