Skip to content

Commit

Permalink
Updates api-gateway method to use deftype
Browse files Browse the repository at this point in the history
  • Loading branch information
dehli committed Jan 20, 2020
1 parent cd9fcfd commit a6e8bed
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

- 0.1.16
- Updates `serverless.aws.api-gateway` to use a `deftype`
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
},
"homepage": "https://github.com/dehli/serverless#readme",
"devDependencies": {
"aws-sdk": "^2.429.0"
"aws-sdk": "^2.536.0"
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>dehli</groupId>
<artifactId>serverless</artifactId>
<version>0.1.15</version>
<version>0.1.16</version>
<name>serverless</name>

<dependencies>
Expand Down
44 changes: 29 additions & 15 deletions src/serverless/aws/api_gateway.cljs
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
(ns serverless.aws.api-gateway
(:require [goog.object :as gobj]
[serverless.aws.sdk :refer [AWS js-call]]
[serverless.aws.sdk :refer [AWS js-call-v2]]
[serverless.core :refer [def-]]
[serverless.json :refer [json->clj clj->json]]))

(defonce ^:private ManagementApi (gobj/get AWS "ApiGatewayManagementApi"))
(def- ApiGatewayManagementApi (gobj/get AWS "ApiGatewayManagementApi"))

(defn management-api [endpoint]
(new ManagementApi (clj->js {:apiVersion "2018-11-29" :endpoint endpoint})))
;; Protocol
(defprotocol ManagementApiProtocol
"A protocol to interact with ApiGatewayManagementApi"
(delete-connection [this params] "Delete a connection")
(get-connection [this params] "Get a connection")
(post-to-connection [this params] "Post data to a connection"))

(defonce request-context :requestContext)
(defonce connection-id (comp :connectionId request-context))
(defonce authorizer (comp :authorizer request-context))
(defonce sub (comp :principalId authorizer))
(defonce body #(-> % :body (json->clj :keywordize-keys true)))
(defonce endpoint (comp #(str (:domainName %) "/" (:stage %)) request-context))
(deftype ManagementApi [api]
ManagementApiProtocol
(delete-connection [_ params]
(js-call-v2 api "deleteConnection" params))
(get-connection [_ params]
(js-call-v2 api "getConnection" params))
(post-to-connection [_ params]
(js-call-v2 api "postToConnection" (update params :data clj->json))))

(defn post-to-connection [client {:keys [connection-id data]}]
(js-call client
"postToConnection"
{:ConnectionId connection-id
:Data (clj->json data)}))
(defn management-api [^String endpoint]
(->> (clj->js {:apiVersion "2018-11-29" :endpoint endpoint})
(new ApiGatewayManagementApi)
->ManagementApi))

;; Accessors
(def request-context :requestContext)
(def connection-id (comp :connectionId request-context))
(def authorizer (comp :authorizer request-context))
(def sub (comp :principalId authorizer))
(def body #(-> % :body (json->clj :keywordize-keys true)))
(def endpoint (comp #(str (:domainName %) "/" (:stage %)) request-context))
29 changes: 15 additions & 14 deletions src/serverless/aws/sdk.cljs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
(ns serverless.aws.sdk
(:require [cljs.core.async :refer [promise-chan put! close!]]))
(:require [cljs.core.async :refer [promise-chan put! close!]]
[serverless.case-styles :refer [kebab->pascal]]))

(defonce AWS (js/require "aws-sdk"))
(def AWS (js/require "aws-sdk"))

(defn- callback [channel]
(fn [error response]
(cond
(some? error) (put! channel error)
(some? response) (put! channel (js->clj response :keywordize-keys true))
:else (close! channel))))

(defn js-call [client method args]
(let [c (promise-chan)]
(js-invoke client
method
(clj->js args)
(fn [error response]
(cond
(some? error)
(put! c error)

(some? response)
(put! c (js->clj response :keywordize-keys true))
(js-invoke client method (clj->js args) (callback c))
c))

:else
(close! c))))
(defn js-call-v2 [client method args]
(let [c (promise-chan)]
(js-invoke client method (clj->js (kebab->pascal args)) (callback c))
c))

0 comments on commit a6e8bed

Please sign in to comment.