Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Demo Custom Remote Functions

From Leeroopedia
Knowledge Sources
Domains API_Design, Extensibility, Server_Side_Logic
Last Updated 2026-10-14 00:00 GMT

Overview

Extensibility mechanism that allows client-side JSON requests to invoke server-side Java methods or multi-language scripts, enabling dynamic computation and business logic without predefined endpoints.

Description

Custom Remote Functions let APIJSON clients call server-side logic directly from JSON requests using a key() or key-() syntax. The server resolves the function name to a registered Java method (via the Method database table) or a script executor (JavaScript, Lua), executes it with the provided arguments, and inserts the result into the JSON response.

This solves:

  • Fixed endpoint limitation: Traditional REST APIs require a new endpoint for each business operation. Remote functions allow extending the API without new routes.
  • Client-side computation gaps: Some values (current user ID, computed lists, validation) cannot be expressed in pure SQL — remote functions fill this gap.
  • Multi-language scripting: Business logic may be written in JavaScript (GraalVM/Nashorn) or Lua, enabling non-Java developers to contribute logic.

The function invocation flow:

  1. Client sends JSON with "key()":"functionName(args)"
  2. Framework looks up function name in the Method database table
  3. If Java method: invoke via reflection with curObj and arguments
  4. If script: delegate to registered ScriptExecutor (GraalJS, Nashorn, Lua)
  5. Result is inserted at the key position in the response JSON

Usage

Use this principle when:

  • Clients need server-computed values (current user, ID lists, cross-table validation) in their queries.
  • Business logic should be extensible without redeploying the server (via database-registered scripts).
  • You want to support multiple scripting languages for server-side extensions.

Theoretical Basis

Function Invocation Pipeline:

Client Request:
    {
        "User": {
            "id@": "getCurrentUserId()",     // Remote function call
            "Comment[]": {
                "Comment": {
                    "userId@": "User/id"     // Reference to function result
                }
            }
        }
    }

Server Resolution:
    1. Parser encounters "getCurrentUserId()" as function call
    2. Look up in Method table: package, method, arguments
    3. Invoke:
       - Java: DemoFunctionParser.getCurrentUserId(curObj) → Long
       - Script: GraalJavaScriptExecutor.execute(parser, curObj, name, args) → Object
    4. Insert result: "id" = 82001
    5. Continue parsing with resolved value

Function types:

  • Java functions: Methods on DemoFunctionParser annotated with @NotNull JSONObject curObj parameter. Registered via Method database table.
  • Script functions: Loaded via ScriptExecutor.load(name, script), executed with _meta context (version, tag, args) and optional extParam binding.
  • Validation functions: Return void, throw exception on failure (e.g., verifyIdList, verifyURLList).

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment