Some days ago I wrote about using functions and composition instead of hoarding props to define flexible React components. I would like to dig deeper into this concept outside the scope of React.
In this story we’ll make a refactoring exercise to get to a conclusion: functions and composition can help us build flexible APIs that are easy to understand, maintain and extend.
Let’s start coding a synthetic example: a data fetching operation. It allows us to use options to configure how the data is fetched and the response handled:
The other day I was refactoring some tests so I made a wrapper to access some mocked information with ease. This wrapper contained some getters and one of them retrieved a value asynchronously. Unfortunately, JavasScript syntax does not support asynchronous getters.
async get syntax but you can rely on several workarounds.
As you should know by now:
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.
So, a getter that returns a Promise is an async getter. …
Authorization, frontend-wise, is just showing or hidding stuff. In React apps the usual way to handle authorization is having a component that conditionally renders its children based on some authorization condition (having a specific login or having some role, for example).
Let’s assume that we have some sort of authentication mechanism and using a Hook we can get the current user: a simple object with all its authorization related information.
Using this Hook we can create a
Visible component that receives a prop for each condition we want to check.
And we can use it in our code:
One of our customers gives us user-by-user access to their systems through a proprietary VPN that we cannot set up on our CI infrastructure.
When we need to deploy their Rails application we rely on mina scripts that connect to the deploy machines using SSH connections (over the VPN). To work properly mina needs you to exchange SSH keys with the server. These are legacy projects, so we use Docker to run our local development environment. So, how can we deploy using Docker?
One option would be to create a new shared deploy key for the project using
ssh-keygen, exchange the public part with the server, and use it in our
The React docs say this about Portals:
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
This means that they are the tool to use when we need to render parts of our UI in a different layer. What makes them awesome it’s how portals behave:
Even though a portal can be anywhere in the DOM tree, it behaves like a normal React child in every other way. …
When you want to have real time updates on a web application, you can rely on old-fashioned periodic polling or try to use some push capable modern technology. Your first impulse may be using WebSockets. However, if you just want to receive data from the server you can use Server Sent Events.
Traditionally, a web page has to send a request to the server to receive new data; that is, the page requests data from the server. With server-sent events, it’s possible for a server to send new data to a web page at any time, by pushing messages to the web page. …
Some of our intranet backends use S3 storage and GraphQL APIs. It’s a common scenario nowadays. This story is about how we deal with file attachments in our schemas and how our client code can get hold of the real files.
Let’s start with a very simple server:
When building CLI tools I found two main sources of frustration: options parsing and managing the console I/O.
There are multiple npm packages that ease the options parsing: I’m quite fond of commander, and I’ve used it in several projects as I’ve told in other medium stories. For the console I/O, I’ve approached the problem using different libraries like chalk, inquirer or progress, or using the readline support available in node. Is far easier than tinkering with escape codes, however it always ends in a very imperative and verbose code. …
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world’s most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms.
In this story we’ll see how to set up SSL with HAProxy for one or many domains listening on the same IP/port, and more specifically, when the SSL configuration differs from one domain to another. This is not for the faint of heart. …
Once upon a time I was setting up a complex monorepo. It contained several packages that shared common transpilation, linting and testing configuration. Some of these packages needed to tweak this config a little.
I firmly believe that less is more, so on my first try, I used JSON config inside the
package.json. The shared configuration lived in a shared package that every other package used via the extension mechanism provided by the tool we were targeting (Babel for…