|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | +-- |
| 4 | +-- Idris2 ABI type definitions for the v-trpc protocol. |
| 5 | +-- Procedure types (query/mutation), router, middleware chain. |
| 6 | + |
| 7 | +module Types |
| 8 | + |
| 9 | +import Data.List |
| 10 | + |
| 11 | +||| Kind of tRPC procedure: read-only query or state-changing mutation. |
| 12 | +public export |
| 13 | +data ProcedureKind : Type where |
| 14 | + Query : ProcedureKind -- Idempotent read (GET or POST) |
| 15 | + Mutation : ProcedureKind -- State-changing write (POST only) |
| 16 | + |
| 17 | +||| Opaque value type for procedure input/output at the ABI level. |
| 18 | +public export |
| 19 | +data TrpcValue : Type where |
| 20 | + ValNull : TrpcValue |
| 21 | + ValBool : Bool -> TrpcValue |
| 22 | + ValInt : Integer -> TrpcValue |
| 23 | + ValStr : String -> TrpcValue |
| 24 | + ValArray : List TrpcValue -> TrpcValue |
| 25 | + ValObject : List (String, TrpcValue) -> TrpcValue |
| 26 | + |
| 27 | +||| Error returned when a procedure invocation fails. |
| 28 | +public export |
| 29 | +record TrpcError where |
| 30 | + constructor MkTrpcError |
| 31 | + code : String -- Machine-readable code (e.g. "NOT_FOUND") |
| 32 | + message : String |
| 33 | + errData : Maybe TrpcValue |
| 34 | + |
| 35 | +||| Result of invoking a procedure: success or typed error. |
| 36 | +public export |
| 37 | +data ProcedureResult : Type where |
| 38 | + ProcSuccess : (output : TrpcValue) -> ProcedureResult |
| 39 | + ProcError : (err : TrpcError) -> ProcedureResult |
| 40 | + |
| 41 | +||| A registered procedure with name, kind, and validation flag. |
| 42 | +public export |
| 43 | +record Procedure where |
| 44 | + constructor MkProcedure |
| 45 | + name : String |
| 46 | + kind : ProcedureKind |
| 47 | + hasValidator : Bool |
| 48 | + |
| 49 | +||| A middleware step in the request processing chain. |
| 50 | +public export |
| 51 | +record Middleware where |
| 52 | + constructor MkMiddleware |
| 53 | + name : String -- Human-readable name for logging |
| 54 | + priority : Nat -- Lower runs first |
| 55 | + |
| 56 | +||| Per-request context threaded through middleware and handlers. |
| 57 | +public export |
| 58 | +record Context where |
| 59 | + constructor MkContext |
| 60 | + headers : List (String, String) |
| 61 | + meta : List (String, String) |
| 62 | + |
| 63 | +||| Router collecting procedures and middleware into a unit. |
| 64 | +public export |
| 65 | +record Router where |
| 66 | + constructor MkRouter |
| 67 | + procedures : List Procedure |
| 68 | + middleware : List Middleware |
0 commit comments