-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates api-gateway method to use deftype
- Loading branch information
Showing
6 changed files
with
60 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |