# `Restlax.Resource`
[🔗](https://github.com/princemaple/restlax/blob/v1.0.0/lib/restlax/resource.ex#L1)

## 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

# `action`

```elixir
@type action() :: :index | :show | :create | :update | :delete
```

# `action_body`

```elixir
@type action_body() ::
  map()
  | keyword()
  | %Stream{accs: term(), done: term(), enum: term(), funs: term()}
  | binary()
```

# `action_options`

```elixir
@type action_options() :: [keyword() | {:client, module()} | {:params, keyword()}]
```

# `option`

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

# `handle_options`

```elixir
@spec handle_options(opts :: action_options()) :: keyword()
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
