jalehman / ring-transit

Ring middleware for handling transit format

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ring-transit

Standard Ring middleware functions for handling Transit requests and responses.

Disclaimer

This library was created less than a week after Transit was released and was modeled after the very similar ring-json. Since then other, more well-thought-out libraries have emerged that are much more actively maintained. Some of these are:

  • ring-middleware-format: A general-purpose ring middleware for handling lots of different content types. If you're building a ring-based app, ring-middleware-format is recommended over this library.
  • liberator-transit: If you're using liberator, this may be a good choice.

Installation

Add the following in project.clj under :dependencies:

[ring-transit "0.1.6"]

Usage

I've based the api and source on ring/ring-json.

The wrap-transit-response middleware will convert any response with a collection as a body (e.g. map, vector, set, seq, etc) into Transit:

(use '[ring.middleware.transit :only [wrap-transit-response]]
     '[ring.util.response :only [response]])

(defn handler [request]
  (response {:foo "bar"}))

(def app
  (wrap-transit-response handler {:encoding :json, :opts {}}))

:opts is a map of options that will be passed to transit/writer

Note: the :encoding option must be one of #{:json :json-verbose :msgpack}. The default is :json.

The wrap-transit-body middleware will parse the body of any request with a transit content-type into a Clojure data structure:

(use '[ring.middleware.transit :only [wrap-transit-body]]
     '[ring.util.response :only [response]])

(defn handler [request]
  (prn (get-in request [:body "user"]))
  (response "Uploaded user."))

(def app
  (wrap-transit-body handler {:keywords? true :opts {}}))

:opts is a map of options that will be passed to transit/reader

Note: The keywords? option will attempt to recursively convert all keys to keywords (nested maps allowed!).

The wrap-transit-params middleware will parse any request with a transit content-type and body and merge the resulting parameters into a params map:

(use '[ring.middleware.transit :only [wrap-transit-params]]
     '[ring.util.response :only [response]])

(defn handler [request]
  (prn (get-in request [:params "user"]))
  (response "Uploaded user."))

(def app
  (wrap-transit-params handler {:opts {}}))

:opts is a map of options that will be passed to transit/reader

License

Copyright © 2016 Josh Lehman

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

Ring middleware for handling transit format

License:Eclipse Public License 1.0


Languages

Language:Clojure 100.0%