diff --git a/src/xiana/interceptor/kebab_camel.clj b/src/xiana/interceptor/kebab_camel.clj index 37d97ce4..00373114 100644 --- a/src/xiana/interceptor/kebab_camel.clj +++ b/src/xiana/interceptor/kebab_camel.clj @@ -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))}) diff --git a/test/xiana/interceptor/kebab_camel_test.clj b/test/xiana/interceptor/kebab_camel_test.clj index 4e0a4a47..84680542 100644 --- a/test/xiana/interceptor/kebab_camel_test.clj +++ b/test/xiana/interceptor/kebab_camel_test.clj @@ -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)))))