6

⚓ Async middleware for JavaScript and TypeScript

 3 years ago
source link: https://blog.feathersjs.com/async-middleware-for-javascript-and-typescript-31a0f74c0d30
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

⚓ Async middleware for JavaScript and TypeScript

Today I’d like to share the release of feathersjs/hooks which brings async middleware to any JavaScript or TypeScript class or function. It allows to create composable, reusable and testable workflows for validation, permissions, caching, logging, profiling and more without having to change existing function code.

@feathersjs/hooks has no external dependencies and can be used in many environments like

Image for post
Image for post
Photo by Grant Durr on Unsplash

Why middleware?

The idea of middleware is to create small handlers that each perform a specific task and pass the result to the next step in a chain. The concept has been around for a long time in many different forms like Unix pipelines, Aspect Oriented Programming or Continuation Passing in functional programming. There even is a proposed new pipeline operator for JavaScript itself.

In NodeJS this has become especially popular with low-level web frameworks like Express, Koa, Socket.io and others to handle requests and responses. Layers of HTTP handlers each perform a specific task (like parsing a JSON body) and decide when to pass processing on to the next step. There isn’t really a reason to limit this pattern only to web requests or responses though. FeathersJS uses transport agnostic middleware (called hooks, not to be confused with the later React Hooks) to implement workflows (like sending an email when a new user has been created) for any of its protocol-independent service methods. Over the past 6 years this has proven to be a powerful way to compose API services.

Middleware with @feathersjs/hooks

@feathersjs/hooks expands this middleware pattern so that it can be used with any async function. It is based on the middleware mechanism of KoaJS which gives a middleware function control over the entire flow - before, after, and on errors - by calling an asynchronous next function.

Below is an example that adds a validator that makes sure that the first parameter is valid and logs the total runtime, name and return value of a function. Each middleware wraps around the next one like an onion with the function call in the middle:

Image for post
Image for post

The JavaScript version looks like this:

With TypeScript hooks can also be registered via decorators:

There are many applications for using middleware in a modern application, for example:

Join the Feathers way

This kind of expressive middleware is one of the high level design patterns used by Feathers to create robust and flexible web APIs and real-time applications.

Middleware chains in frameworks like Express or Koa may become long and difficult to follow and test because it is easy to end up mixing application logic with handling HTTP requests and responses. An advantage of being able to use middleware with any function instead of just web requests is that it allows to better organize your application into more self contained and testable units of functionality and re-use them in other places.

While Feathers is not required in order to use this software development design pattern, respectfully, we think our transport agnostic version is yet another improvement in this great composability pattern. 🐦

Get in touch with us on Twitter or in our Slack group.

If you are new to Feathers and looking to create your first app head on over to the docs and get your geek on!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK