Skip to content

Commit

Permalink
kebab camel interceptor for all request type (#263)
Browse files Browse the repository at this point in the history
* Fix swagger endpoints and refactor code

* Fix kebab-camel-interceptor to all request type

* Apply cljstyle

* Fix tests for new interceptor
  • Loading branch information
gmsvalente authored Nov 22, 2023
1 parent 112275c commit 7219841
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/xiana/interceptor/kebab_camel.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
[camel-snake-kebab.extras :as cske]
[clojure.core.memoize :as mem]))

(def request-type-params [:params :body-params :query-params :path-params :form-params :multipart-params])

(def camel-to-kebab
(fn [resp]
(cske/transform-keys
(mem/fifo csk/->kebab-case {} :fifo/threshold 512) resp)))

(def kebab-to-camel
(fn [resp]
(cske/transform-keys
(mem/fifo csk/->camelCase {} :fifo/threshold 512) resp)))

(def interceptor
"The purpose is to make Js request compatible with clojure, and response compatible with Javascript.
:request - {:params { "
{:name ::camel-to-kebab-case
:enter (fn [state]
(update-in state [:request :params]
(fn [resp]
(cske/transform-keys
(mem/fifo csk/->kebab-case {} :fifo/threshold 512) resp))))
(reduce
(fn [state type-param]
(update-in state [:request type-param] camel-to-kebab))
state
request-type-params))
:leave (fn [state]
(update-in state [:response :body]
(fn [resp]
(cske/transform-keys
(mem/fifo csk/->camelCase {} :fifo/threshold 512) resp))))})
(update-in state [:response :body] kebab-to-camel))})
24 changes: 17 additions & 7 deletions test/xiana/interceptor/kebab_camel_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@

(deftest req->kebab-resp->camel-test
(testing "Transforms keys of request params to kebab case"
(let [state {:request {:params {:paramKey1 1 :paramKey2 2 :paramKey3 3}}}
expected {:request {:params {:param-key-1 1 :param-key-2 2 :param-key-3 3}}}
enter (:enter kc/interceptor)
result (enter state)]
(let [state {:request {:params {:paramKey1 1 :paramKey2 2 :paramKey3 3}
:body-params {:bodyParamKey1 1 :bodyParamKey2 2 :bodyParamKey3 3}
:query-params {:queryParamKey1 1 :queryParamKey2 2 :queryParamKey3 3}
:path-params {:pathParamKey1 1 :pathParamKey2 2 :pathParamKey3 3}
:form-params {:formParamKey1 1 :formParamKey2 2 :formParamKey3 3}
:multipart-params {:multipartParamKey1 1 :multipartParamKey2 2 :multipartParamKey3 3}}}
expected {:request {:params {:param-key-1 1 :param-key-2 2 :param-key-3 3}
:body-params {:body-param-key-1 1 :body-param-key-2 2 :body-param-key-3 3}
:query-params {:query-param-key-1 1 :query-param-key-2 2 :query-param-key-3 3}
:path-params {:path-param-key-1 1 :path-param-key-2 2 :path-param-key-3 3}
:form-params {:form-param-key-1 1 :form-param-key-2 2 :form-param-key-3 3}
:multipart-params {:multipart-param-key-1 1 :multipart-param-key-2 2 :multipart-param-key-3 3}}}
enter (:enter kc/interceptor)
result (enter state)]
(is (= expected result))))

(testing "Transform keys of response body to Camel case"
(let [state {:response {:body {:param-key-1 1 :param-key-2 2 :param-key-3 3}}}
(let [state {:response {:body {:param-key-1 1 :param-key-2 2 :param-key-3 3}}}
expected {:response {:body {:paramKey1 1 :paramKey2 2 :paramKey3 3}}}
leave (:leave kc/interceptor)
result (leave state)]
leave (:leave kc/interceptor)
result (leave state)]
(is (= expected result)))))

0 comments on commit 7219841

Please sign in to comment.