Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/edragonconnect/plug_response_replace

A tiny plug for replacing response fields
https://github.com/edragonconnect/plug_response_replace

Last synced: about 6 hours ago
JSON representation

A tiny plug for replacing response fields

Awesome Lists containing this project

README

        

# PlugResponseReplace

[![Module Version](https://img.shields.io/hexpm/v/plug_response_replace.svg)](https://hex.pm/packages/plug_response_replace)

A tiny plug for replacing [response fields](https://hexdocs.pm/plug/Plug.Conn.html#module-response-fields).

## Installation

Add a dependency to your application's mix.exs file:

```elixir
def deps do
[
{:plug_response_replace, "~> 0.2"}
]
end
```

then run `mix deps.get`.

## Usage

Replace `:resp_body` of `Plug.Conn`, the `iodata()` response body with the following `:pattern`, `:replacement`, and `options`
fields are completely used to `String.replace/4`.

```
defmodule MyHandler do
use Plug.Builder

plug PlugResponseReplace,
resp_body: [
pattern: "...",
replacement: "...",
options: [] # optional
]

# ... rest of the pipeline
end
```

Replace `:resp_cookies` of `Plug.Conn`, the item of the following `:resp_cookies` is a tuple contains a pair of response cookie key
and value used to `Plug.Conn.put_resp_cookie/4`, the item may contain an optional options, see `Plug.Conn.put_resp_cookie/4` for details.

```
defmodule MyHandler do
use Plug.Builder

plug PlugResponseReplace,
resp_cookies: [
{"key1", "new"},
{"key1", %{"session" => "abcdef", "timestamp" => 123}, [sign: true]}
]

# ... rest of the pipeline
end
```

Replace `:resp_headers` of `Plug.Conn`, the item of the following `:resp_headers` is a tuple contains a pair of header key and value
used to `Plug.Conn.put_resp_header/3`.

```
defmodule MyHandler do
use Plug.Builder

plug PlugResponseReplace,
resp_headers: [
{"content-type", "text/plain"}
]

# ... rest of the pipeline
end
```

Replace `:status` of `Plug.Conn`, override the status of repsonse with a proper value.

```
defmodule MyHandler do
use Plug.Builder

plug PlugResponseReplace,
status: 401

# ... rest of the pipeline
end
```

## License

MIT