https://github.com/platformatic/rpc
@platformatic/rpc
https://github.com/platformatic/rpc
Last synced: 6 months ago
JSON representation
@platformatic/rpc
- Host: GitHub
- URL: https://github.com/platformatic/rpc
- Owner: platformatic
- License: apache-2.0
- Created: 2025-08-06T08:12:04.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-08-06T09:23:01.000Z (8 months ago)
- Last Synced: 2025-09-17T09:18:05.036Z (6 months ago)
- Language: JavaScript
- Homepage: https://platformatic.dev
- Size: 56.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Platformatic RPC
Fastify plugin to generate a server RPC api for a Fastify application.
> [!WARNING]
> Platformatic RPC API is in the experimental stage. The feature is not subject to semantic versioning rules.
> Non-backward compatible changes or removal may occur in any future release.
> Use of the feature is not recommended in production environments.
## Installation
```bash
npm install @platformatic/rpc-cli
npm install --save-dev @platformatic/rpc-cli
```
## Usage
1. Register the plugin in your Fastify typescript application. Pass the OpenAPI schema as an option.
OpenAPI schema will be automatically generated by the CLI command.
```ts
import { join } from 'node:path'
import { readFile } from 'node:fs/promises'
import fastify from 'fastify'
import fastifyRpc from '@platformatic/rpc'
const app = fastify()
app.register(async app => {
const openapiSchemaPath = join(__dirname, 'openapi.json')
const openapiSchemaFile = await readFile(openapiSchemaPath, 'utf8')
const openapiSchema = JSON.parse(openapiSchemaFile)
await app.register(fastifyRpc, {
openapi: openapiSchema,
prefix: '/rpc'
})
})
app.listen({ port: 3042 }, err => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening on http://localhost:3042`)
})
```
2. Define RPC handlers in your Fastify application. An RPC handler is a function that takes an optional options object.
All parameters will be passed as properties of the options object.
```ts
import { join } from 'node:path'
import { readFile } from 'node:fs/promises'
import fastify from 'fastify'
import fastifyRpc from '@platformatic/rpc'
const app = fastify()
app.register(async app => {
const openapiSchemaPath = join(__dirname, 'openapi.json')
const openapiSchemaFile = await readFile(openapiSchemaPath, 'utf8')
const openapiSchema = JSON.parse(openapiSchemaFile)
await app.register(fastifyRpc, {
openapi: openapiSchema,
prefix: '/rpc'
})
app.register(async app => {
type User = {
name: string
age: number
}
const users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 35 }
]
app.rpc('getUsers', async (options: { maxAge: number }): Promise => {
return users.filter(user => user.age <= options.maxAge)
})
})
})
app.listen({ port: 3042 }, err => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening on http://localhost:3042`)
})
```
3. Run the CLI command to generate the OpenAPI schema.
```bash
npx plt-rpc --ts-config ./tsconfig.json --path ./openapi.json
```
4. Start the Fastify application.
Your RPC handlers are exposed as http routes under the `/rpc` prefix. All RPC routes are POST routes.
```bash
curl -X POST http://localhost:3042/rpc/getUsers -H 'Content-Type: application/json' -d '{"maxAge": 30}'
```