gleamrpc

Package Version Hex Docs

Remote Procedure Calls in Gleam !

Note that this library isn’t a standalone library, you will need a client and a server implementations.
See gleamrpc_http_client and gleamrpc_http_server for HTTP implementations.

gleam add gleamrpc@1
import convert
import gleam/option
import gleamrpc

pub fn main() {
  let id_query_convert = convert.object({
	  use id <- convert.field("id", fn(v) { Ok(v.id) }, convert.string())
    convert.success(IdQuery(id))
  })
  |> convert.map(
    fn (v: IdQuery) { v.id },
    fn (v: String) { Ok(IdQuery(v)) },
    ""
  )
  let user_convert = convert.object({
    use id <- convert.field("id", fn(v) { Ok(v.id) }, convert.string())
    use name <- convert.field("name", fn(v) { Ok(v.name) }, convert.string())
    use age <- convert.field("age", fn(v) { Ok(v.age) }, convert.int())
    convert.success(User(id, name, age))
  })

  let users_router = gleamrpc.Router("users", option.None) // /api/gleamRPC/users
  let get_user = gleamrpc.query("get_user", option.Some(users_router))
    |> gleamrpc.params(id_query_convert) // /api/gleamRPC/users/get_user?id=<ID>
    |> gleamrpc.returns(user_convert)

  // user is of type Result(User, gleamrpc.GleamRPCError)
  let user = get_user
  |> gleamrpc.with_client(client) // client is provided by the implementation you choose
  |> gleamrpc.call("1")
}

Further documentation can be found at https://hexdocs.pm/gleamrpc.

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document