All Projects → sticksnleaves → versionary

sticksnleaves / versionary

Licence: MIT license
Plug for API versioning

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to versionary

plug rails cookie session store
Rails compatible Plug session store
Stars: ✭ 93 (+132.5%)
Mutual labels:  elixir-phoenix, elixir-plug
Bamboo
Testable, composable, and adapter based Elixir email library for devs that love piping.
Stars: ✭ 1,756 (+4290%)
Mutual labels:  elixir-phoenix
Accent
The first developer-oriented translation tool. True asynchronous flow between translators and your team.
Stars: ✭ 721 (+1702.5%)
Mutual labels:  elixir-phoenix
Shorten api tutorial
🔗How to make a link shortener using Elixir, Phoenix and Mnesia
Stars: ✭ 60 (+50%)
Mutual labels:  elixir-phoenix
Drab
Remote controlled frontend framework for Phoenix.
Stars: ✭ 833 (+1982.5%)
Mutual labels:  elixir-phoenix
Loki
📝 Loki is library that includes helpers for building powerful interactive command line applications, tasks, modules.
Stars: ✭ 71 (+77.5%)
Mutual labels:  elixir-phoenix
Elixir Boilerplate
⚗ The stable base upon which we build our Elixir projects at Mirego.
Stars: ✭ 627 (+1467.5%)
Mutual labels:  elixir-phoenix
Short url
🔗 short url app elixir Phoenix
Stars: ✭ 162 (+305%)
Mutual labels:  elixir-phoenix
Scrivener html
HTML view helpers for Scrivener
Stars: ✭ 112 (+180%)
Mutual labels:  elixir-phoenix
Phoenix In Action
Code snippets and examples from the book Phoenix in Action from Manning and Geoffrey Lessel
Stars: ✭ 60 (+50%)
Mutual labels:  elixir-phoenix
Phoenix Ecto Append Only Log Example
📝 A step-by-step example/tutorial showing how to build a Phoenix (Elixir) App where all data is immutable (append only). Precursor to Blockchain, IPFS or Solid!
Stars: ✭ 58 (+45%)
Mutual labels:  elixir-phoenix
Cog
Bringing the power of the command line to chat
Stars: ✭ 910 (+2175%)
Mutual labels:  elixir-phoenix
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (+80%)
Mutual labels:  elixir-phoenix
Elixir Phoenix Realworld Example App
Exemplary real world application built with Elixir + Phoenix
Stars: ✭ 764 (+1810%)
Mutual labels:  elixir-phoenix
Veil
Simple passwordless authentication for your Phoenix apps
Stars: ✭ 153 (+282.5%)
Mutual labels:  elixir-phoenix
Thesis Phoenix
A lightweight, bolt-on, intuitive content editing system for Elixir/Phoenix websites. Star this repo and follow along with our progress!
Stars: ✭ 645 (+1512.5%)
Mutual labels:  elixir-phoenix
Elixirconf 2018
A collection of links that cover what happened during ElixirConf 2018 🐥 🔥. Please feel free to submit a PR!
Stars: ✭ 56 (+40%)
Mutual labels:  elixir-phoenix
Firebird
Template for Phoenix 1.3 projects
Stars: ✭ 66 (+65%)
Mutual labels:  elixir-phoenix
Statuspal
Statuspal lets you communicate your web apps/services status 📡
Stars: ✭ 179 (+347.5%)
Mutual labels:  elixir-phoenix
Docker Phoenix
A dockerized Phoenix development and runtime environment.
Stars: ✭ 152 (+280%)
Mutual labels:  elixir-phoenix

Versionary

Add versioning to your Elixir Plug and Phoenix built API's.

CI Coverage Status Module Version Hex Docs Total Download License Last Updated

Installation

The package can be installed by adding :versionary to your list of dependencies in mix.exs:

def deps do
  [
    {:versionary, "~> 0.3"}
  ]
end

Usage

def MyAPI.Router do
  use Plug.Router

  plug Versionary.Plug.VerifyHeader, versions: ["application/vnd.app.v1+json"]

  plug Versionary.Plug.EnsureVersion, handler: MyAPI.MyErrorHandler

  plug :match
  plug :dispatch
end

MIME Support

It's possible to verify versions against configured MIME types. If multiple MIME types are passed and at least one matches the version will be considered valid.

config :mime, :types, %{
  "application/vnd.app.v1+json" => [:v1],
  "application/vnd.app.v2+json" => [:v2]
}
plug Versionary.Plug.VerifyHeader, accepts: [:v1, :v2]

Please note that whenever you change media type configurations you must recompile the mime library.

To force mime to recompile run mix deps.clean --build mime.

Identifying Versions

When a version has been verified :version and :raw_version private keys will be added to the conn. These keys will contain version that has been verified.

The :version key may contain either the string version provided by the request or, if configured, the MIME extension. The :raw_version key will always contain the string version provided by the request.

Phoenix

Versionary is just a plug. That means Versionary works with Phoenix out of the box. However, if you'd like Versionary to render a Phoenix error view when verification fails use Versionary.Plug.PhoenixErrorHandler.

defmodule MyAPI.Router do
  use MyAPI.Web, :router

  pipeline :api do
    plug Versionary.Plug.VerifyHeader, accepts: [:v1, :v2]

    plug Versionary.Plug.EnsureVersion, handler: Versionary.Plug.PhoenixErrorHandler
  end

  scope "/", MyAPI do
    pipe_through :api

    get "/my_controllers", MyController, :index
  end
end

Handling Multiple Versions

You can pattern match which version of a controller action to run based on the :version (or :raw_version) private key provided by the conn.

defmodule MyAPI.MyController do
  use MyAPI, :controller

  def index(%{private: %{version: [:v1]}} = conn, _params) do
    render(conn, "index.v1.json", %{})
  end

  def index(%{private: %{version: [:v2]}} = conn, _params) do
    render(conn, "index.v2.json", %{})
  end
end

Plug API

Versionary.Plug.VerifyHeader

Verify that the version passed in to the request as a header is valid. If the version is not valid then the request will be flagged.

This plug will not handle an invalid version. If you would like to halt the request and handle an invalid version please see Versionary.Plug.EnsureVersion.

Options

accepts - a list of strings or atoms representing versions registered as MIME types. If at least one of the registered versions is valid then the request is considered valid.

versions - a list of strings representing valid versions. If at least one of the provided versions is valid then the request is considered valid.

header - the header used to provide the requested version (Default: Accept)

Versionary.Plug.EnsureVersion

Checks to see if the request has been flagged with a valid version. If the version is valid, the request continues, otherwise the request will halt and the handler will be called to process the request.

Options

handler - the module used to handle a request with an invalid version (Default: Versionary.Plug.ErrorHandler)

Versionary.Plug.Handler

Behaviour for handling requests with invalid versions. You can create your own custom handler with this behaviour.

Copyright and License

Copyright (c) 2017 Sticksnleaves

This library is licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].