Files
herolib/lib/schemas/openrpc
Mahmoud-Emad 0bfb5cfdd0 refactor: Update JSON parsing and schema inflation
- Use `json2.decode[json2.Any]` instead of `json2.raw_decode`
- Add `@[required]` to procedure function signatures
- Improve error handling for missing JSONRPC fields
- Update `encode` to use `prettify: true`
- Add checks for missing schema and content descriptor references
2025-10-22 21:14:29 +03:00
..
...
2025-09-14 11:57:11 +02:00
...
2025-03-24 06:44:39 +01:00
2025-09-28 10:38:45 +03:00
2025-09-28 10:38:45 +03:00
2025-09-28 10:38:45 +03:00

OpenRPC Module

This module provides a complete implementation of the OpenRPC specification for V, enabling structured JSON-RPC 2.0 API development with schema-based validation and automatic documentation.

Purpose

  • Define and validate JSON-RPC APIs using OpenRPC schema definitions
  • Handle JSON-RPC requests/responses over HTTP or Unix sockets
  • Automatic discovery endpoint (rpc.discover) for API documentation
  • Type-safe request/response handling
  • Support for reusable components (schemas, parameters, errors, examples)

Usage

1. Create an OpenRPC Handler

Create a handler with your OpenRPC specification:

import incubaid.herolib.schemas.openrpc

// From file path
mut handler := openrpc.new_handler('path/to/openrpc.json')!

// From specification text
mut handler := openrpc.new(text: spec_json)!

2. Register Methods

Register your method handlers to process incoming JSON-RPC requests:

fn my_method(request jsonrpc.Request) !jsonrpc.Response {
    // Decode parameters
    mut params := json.decode(MyParams, request.params) or { 
        return jsonrpc.invalid_params 
    }
    
    // Process logic
    result := process_my_method(params)
    
    // Return response
    return jsonrpc.new_response(request.id, json.encode(result))
}

// Register the method
handler.register_procedure_handle('my.method', my_method)

3. Start Server

Launch the server using either HTTP or Unix socket transport:

// HTTP server
mut controller := openrpc.new_http_controller(handler)
controller.run(port: 8080)

// Unix socket server
mut server := openrpc.new_unix_server(handler)!
server.start()