UNPKG

@xtr-dev/zod-rpc

Version:

Simple, type-safe RPC library with Zod validation and automatic TypeScript inference

138 lines 3.99 kB
import { ServiceDefinition, ServiceImplementation } from './service'; /** * Configuration options for creating an RPC server. * * @example * ```typescript * const config: RPCServerConfig = { * port: 8080, * host: '0.0.0.0', * serverId: 'my-server', * timeout: 30000 * }; * ``` * * @group Server API */ export interface RPCServerConfig { /** Port number to listen on (for WebSocket servers) */ port?: number; /** Host address to bind to (for WebSocket servers) */ host?: string; /** Unique identifier for this server instance */ serverId?: string; /** Default timeout for RPC calls in milliseconds */ timeout?: number; } /** * RPC Server for hosting type-safe remote procedure call services. * * @example * ```typescript * const server = createRPCServer('ws://localhost:8080') * .implement(userService, { * get: async ({ userId }) => ({ id: userId, name: 'John' }), * create: async ({ name, email }) => ({ id: '123', success: true }) * }); * * await server.start(); * ``` * @group Server API */ export declare class RPCServer { private url; private config; private channel?; private server?; private serverId; private timeout; private services; private implementations; constructor(url: string, config?: RPCServerConfig); implement<T extends Record<string, any>>(service: ServiceDefinition<T>, implementation: ServiceImplementation<T>): this; start(): Promise<void>; private startWebSocketServer; private startHttpServer; private logAvailableServices; stop(): Promise<void>; getServices(): string[]; isRunning(): boolean; } /** * Create a new RPC server instance. * This is the main entry point for creating servers with service implementations. * * @param url - WebSocket or HTTP URL for the server to listen on * @param config - Optional server configuration * @returns A new RPCServer instance ready for service implementation * * @example * ```typescript * const server = createRPCServer('ws://localhost:8080', { * serverId: 'my-server', * timeout: 30000 * }); * * server.implement(userService, { * get: async ({ userId }) => ({ name: `User ${userId}`, email: `user${userId}@example.com` }), * create: async ({ name, email }) => ({ id: '123', success: true }) * }); * * await server.start(); * ``` * * @group Server API */ export declare function createRPCServer(url: string, config?: RPCServerConfig): RPCServer; /** * Fluent builder pattern for creating RPC servers with advanced configurations. * Provides a chainable API for setting server options before building the final server. * * @example * ```typescript * const server = createServer('ws://localhost:8080') * .withId('my-server') * .withTimeout(30000) * .withHost('0.0.0.0') * .withPort(8080) * .build(); * ``` * * @group Server API */ export declare class RPCServerBuilder { private config; private url; constructor(url: string); withId(serverId: string): this; withTimeout(timeout: number): this; withPort(port: number): this; withHost(host: string): this; build(): RPCServer; } /** * Create a new RPC server builder for fluent configuration. * This is the preferred way to create servers with custom settings. * * @param url - WebSocket or HTTP URL for the server to listen on * @returns A new RPCServerBuilder instance for chaining configuration * * @example * ```typescript * const server = createServer('ws://localhost:8080') * .withId('my-server') * .withTimeout(30000) * .withHost('0.0.0.0') * .build(); * * server.implement(userService, userImplementation); * await server.start(); * ``` * * @group Server API */ export declare function createServer(url: string): RPCServerBuilder; export { RPCServerBuilder as RpcServerBuilder }; export { createRPCServer as createRpcServer }; export { RPCServer as RpcServer }; //# sourceMappingURL=server.d.ts.map