import { JRPCNotification, JRPCRequest, JRPCResponse } from "../interfaces.js"; import { HandleOptions, JRPCMiddlewareV2, JsonRpcCall, MergedContextOf, MiddlewareConstraint, Options } from "./v2interfaces.js"; /** * A JSON-RPC server that handles requests and notifications. * * Essentially wraps a {@link JRPCEngineV2} in order to create a conformant * yet permissive JSON-RPC 2.0 server. * * Note that the server will accept both requests and notifications via {@link handle}, * even if the underlying engine is only able to handle one or the other. * * @example * ```ts * const server = new JRPCServer({ * engine, * onError, * }); * * const response = await server.handle(request); * if ('result' in response) { * // Handle result * } else { * // Handle error * } * ``` */ export declare class JRPCServer { #private; /** * Construct a new JSON-RPC server. * * @param options - The options for the server. * @param options.onError - The callback to handle errors thrown by the * engine. Errors always result in a failed response object, containing a * JSON-RPC 2.0 serialized version of the original error. If you need to * access the original error, use the `onError` callback. * @param options.engine - The engine to use. Mutually exclusive with * `middleware`. * @param options.middleware - The middleware to use. Mutually exclusive with * `engine`. */ constructor(options: Options); static _coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall; /** * Handle a JSON-RPC request. * * This method never throws. For requests, a response is always returned. * All errors are passed to the engine's `onError` callback. * * **WARNING**: This method is unaware of the request type of the underlying * engine. The request will fail if the engine can only handle notifications. * * @param request - The request to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. * @returns The JSON-RPC response. */ handle(request: JRPCRequest, options?: HandleOptions>): Promise>; /** * Handle a JSON-RPC notification. * * This method never throws. For notifications, `undefined` is always returned. * All errors are passed to the engine's `onError` callback. * * **WARNING**: This method is unaware of the request type of the underlying * engine. The request will fail if the engine cannot handle notifications. * * @param notification - The notification to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. */ handle(notification: JRPCNotification, options?: HandleOptions>): Promise; /** * Handle an alleged JSON-RPC request or notification. Permits any plain * object with `{ method: string }`, so long as any present JSON-RPC 2.0 * properties are valid. If the object has an `id` property, it will be * treated as a request, otherwise it will be treated as a notification. * * This method never throws. All errors are passed to the engine's * `onError` callback. A JSON-RPC response is always returned for requests, * and `undefined` is returned for notifications. * * **WARNING**: The request will fail if its coerced type (i.e. request or * response) is not of the type expected by the underlying engine. * * @param rawRequest - The raw request to handle. * @param options - The options for the handle operation. * @param options.context - The context to pass to the middleware. * @returns The JSON-RPC response, or `undefined` if the request is a * notification. */ handle(rawRequest: unknown, options?: HandleOptions>): Promise | void>; }