@yuankui/rpc-express
v0.2.1
Published
Express integration for @yuankui/rpc
Downloads
5
Maintainers
Readme
@yuankui/rpc-express
Express integration for @yuankui/rpc - A type-safe RPC framework.
Installation
npm install @yuankui/rpc-express
# or
yarn add @yuankui/rpc-express
# or
bun add @yuankui/rpc-expressUsage
Basic Setup
import express from 'express';
import { createRPCRouter } from '@yuankui/rpc-express';
const app = express();
app.use(express.json());
// Define your RPC endpoints
const endpoints = {
async getUser(id: number) {
// Your logic here
return { id, name: 'John Doe' };
},
async createUser(name: string, email: string) {
// Your logic here
return { id: 1, name, email };
},
};
// Create RPC router
const rpcRouter = createRPCRouter({
endpoints,
path: '/api/rpc', // Optional, defaults to '/rpc'
});
app.use(rpcRouter);
app.listen(3000, () => {
console.log('Server running on port 3000');
});Using with Express Router
import { Router } from 'express';
import { createRPCMiddleware } from '@yuankui/rpc-express';
const apiRouter = Router();
const rpcMiddleware = createRPCMiddleware({
endpoints: {
async ping() {
return 'pong';
},
},
});
apiRouter.use('/rpc', rpcMiddleware);Custom Serialization
import { createRPCRouter } from '@yuankui/rpc-express';
const rpcRouter = createRPCRouter({
endpoints: {
async getData() {
return new Date(); // Will be properly serialized
},
},
serializer: {
serialize: (data) => JSON.stringify(data),
deserialize: (data) => JSON.parse(data),
},
});API
createRPCRouter(config)
Creates an Express router with RPC endpoint handling.
Parameters
config.endpoints- Object containing your RPC endpoint functionsconfig.path- (Optional) RPC endpoint path, defaults to/rpcconfig.serializer- (Optional) Custom serializer, defaults to superjson
Returns
Express Router instance
createRPCMiddleware(config)
Alias for createRPCRouter for better semantic clarity when using as middleware.
ExpressRPCHandler
Class for more advanced usage scenarios where you need direct control over the handler.
Client Usage
Use with the standard RPC client:
import { RPCClient } from '@yuankui/rpc-express';
type ServerEndpoints = {
getUser: (id: number) => Promise<{ id: number; name: string }>;
createUser: (name: string, email: string) => Promise<{ id: number; name: string; email: string }>;
};
const client = new RPCClient<ServerEndpoints>({
url: 'http://localhost:3000/api/rpc',
});
// Type-safe calls
const user = await client.call('getUser', 123);
const newUser = await client.call('createUser', 'Jane', '[email protected]');License
MIT
