Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/edragonconnect/plug_response_replace
- Owner: edragonconnect
- License: mit
- Created: 2021-08-10T03:36:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-11T02:36:30.000Z (about 3 years ago)
- Last Synced: 2024-04-24T10:18:32.441Z (7 months ago)
- Language: Elixir
- Homepage: https://hex.pm/packages/plug_response_replace
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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.Builderplug 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.Builderplug 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.Builderplug 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.Builderplug PlugResponseReplace,
status: 401# ... rest of the pipeline
end
```## License
MIT