Restlax.Resource (Restlax v0.1.0) View Source

Rest Resource builder

Options

  • :endpoint - required, string
  • :singular - defaults to false, boolean
  • :only - list of default actions to generate, defaults to [:index, :show, :create, :update, :delete]
  • :except - list of default actions to exclude, defaults to []
  • :create_method - HTTP verb to use for create action, defaults to :post, sometimes :put is used
  • :update_method - HTTP verb to use for update action, defaults to :put, a common alternative is :patch

Example

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "my-resource"
end

^ This creates

  • GET MyResource.index(opts)
  • GET MyResource.show(id, opts)
  • POST MyResource.create(body, opts)
  • PUT MyResource.update(id, body, opts)
  • DELETE MyResource.delete(id, opts)

Change what actions to generate

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "my-resource",
    only: [:show, :update]
end

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "my-resource",
    except: [:delete]
end

Scoped by other resources, :parent_id will be interpolated later

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "parent-resource/:parent_id/my-resource"
end

Singular resources only have :show and :update, and don't have ID in their url

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "my-resource",
    singular: true
end

^ This only generates

  • GET MyResource.show(opts)
  • PUT MyResource.update(body, opts)

Use PATCH for update (and use PUT for create ╮(╯-╰)╭)

defmodule MyResource do
  use Restlax.Resource,
    endpoint: "my-resource",
    update_method: :patch,
    create_method: :put
end

Link to this section Summary

Link to this section Types

Specs

action() :: :index | :show | :create | :update | :delete

Specs

action_body() ::
  map()
  | keyword()
  | Tesla.Multipart.t()
  | %Stream{accs: term(), done: term(), enum: term(), funs: term()}
  | binary()

Specs

action_options() :: [
  Tesla.option() | {:client, module()} | {:params, keyword()}
]

Specs

option() ::
  {:endpoint, String.t()}
  | {:singular, boolean()}
  | {:only, [action()]}
  | {:except, [action()]}
  | {:create_method, :post | :put}
  | {:update_method, :put | :patch}

Link to this section Functions

Specs

handle_options(opts :: action_options()) :: [Tesla.option()]