A Generic Handler For AWS SDK Operations in Elm
The code in this post is a work in progress.
I’m working on an app that uses the AWS SDK for JavaScript to query DynamoDB directly from the browser. Yes, there are ways to securely do this.
Since the app is written in Elm, I need a driver in native JavaScript to perform the operation. The naive way to do this is to write one handler per operation I want to expose in the SDK. These functions are easily 10 lines of code. Obviously, this does not scale. Obviously, that’s what I did first.
What I really want is a generic handler. Here’s my first attempt (in TypeScript):
The wire
function does all the work of subscribing to requests and sending responses back to Elm.
It expects an object with operation
, client
, and params
fields.
It then dynamically invokes the SDK function.
Here’s how that works.
This is what’s in the clients object:
If the elm code passes in ‘DynamoDB’ as the value of the client parameter, this function will invoke operationName
on an instance of DynamoDB
.
The params
argument is passed unmodified to the operation.
When the operation invokes its callback, I send that data unmodified back to the elm handler.
It’s up to the Elm to parse and dispatch the response as it sees fit.
That’s a topic for another post though.
Finally, I need an entry-point to set all this up:
If you liked this post, your followers might also. Please give it a share!
Did this article help?
blog comments powered by Disqus