ProtoRune.XRPC.DSL (proto_rune v0.1.2)
The XRPC.DSL module provides macros to define queries and procedures for interacting with the XRPC system, simplifying the creation of API methods for querying or performing procedures. It supports building custom XRPC queries and procedures by encoding method names and dynamically generating functions based on user-defined parameters.
Overview
The primary purpose of this module is to offer a simple DSL for defining queries and procedures in Elixir projects using XRPC. It automates the generation of functions that initiate XRPC requests, reducing boilerplate code for developers.
Key Features:
- Query Definition: Use the
defquery/1macro to define an XRPC query method. - Procedure Definition: Use the
defprocedure/2macro to define an XRPC procedure method. - Parameter Handling: The
param/2macro allows specifying parameters and their types for queries and procedures. - Automatic Method Name Encoding: Converts method names to function names by snakelizing the last segment of the method.
Macros
defquery/1
Defines a query function based on the given method name.
defquery("app.bsky.actor.getProfile")This creates a function that returns a new query for the app.bsky.actor.getProfile method.
defquery/2
Defines a query with parameters.
defquery("app.bsky.feed.getFeed", do: block)Within the block, you can specify parameters using the param/2 macro. The generated function will include these parameters in the query.
defprocedure/2
Defines a procedure with parameters.
defprocedure("app.bsky.actor.mute", do: block)Similar to defquery/2, you can specify parameters in the block using the param/2 macro. This will generate a function that returns a new procedure with the given method and parameters.
param/2
Defines a parameter for queries or procedures.
param(:actor_id, :string)This specifies a parameter with a key of actor_id and a type of :string, which will be included in the final query or procedure.
Usage Example
defmodule MyApp.Bsky do
import XRPC.DSL
defquery "app.bsky.actor.getProfile"
defprocedure "app.bsky.actor.mute" do
param :actor_id, :string
end
endIn this example:
get_profile/0is generated as a function that creates a query forapp.bsky.actor.getProfile.mute/0is generated as a function that creates a procedure forapp.bsky.actor.mutewith the parameter:actor_idof type:string.