@xtr-dev/zod-rpc
Version:
Simple, type-safe RPC library with Zod validation and automatic TypeScript inference
72 lines • 2.06 kB
TypeScript
import { RPCMessage, Transport } from '../types';
import type { Channel } from '../channel';
interface Request {
method: string;
body?: any;
}
interface Response {
status(code: number): Response;
json(data: any): Response;
}
type NextFunction = () => void;
/**
* @group Transport Layer
*/
export interface HTTPTransportConfig {
baseUrl: string;
headers?: Record<string, string>;
timeout?: number;
fetch?: typeof globalThis.fetch;
}
/**
* @group Transport Layer
*/
export declare class HTTPTransport implements Transport {
private config;
private messageHandler?;
private connected;
constructor(config: HTTPTransportConfig);
send(message: RPCMessage): Promise<void>;
onMessage(handler: (message: RPCMessage) => void): void;
connect(): Promise<void>;
disconnect(): Promise<void>;
isConnected(): boolean;
}
/**
* Creates Express middleware for handling zod-rpc calls with a channel.
*
* @param channel - Channel with registered service implementations
* @returns Express middleware function
*
* @example
* ```typescript
* import express from 'express';
* import { zodRpc, Channel } from '@xtr-dev/zod-rpc';
* import { implementService } from '@xtr-dev/zod-rpc';
*
* const app = express();
* app.use(express.json());
*
* // Create channel for local method invocation (no transport needed)
* const channel = new Channel('server');
*
* // Implement and publish service (simplified API)
* channel.publishService(userService, {
* get: async ({ userId }) => ({ id: userId, name: 'John' }),
* create: async ({ name, email }) => ({ id: '123', success: true })
* });
*
* // Use middleware
* app.use('/rpc', zodRpc(channel));
* ```
* @group HTTP Middleware
*/
export declare function zodRpc(channel: Channel | {
invoke: Function;
}): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* @group Transport Layer
*/
export declare function createHTTPTransport(config: HTTPTransportConfig): HTTPTransport;
export {};
//# sourceMappingURL=http.d.ts.map