API with NestJS #57. Composing classes with the mixin pattern

Uncategorized

This entry is part 57 of 85 in the API with NestJS

Inheritance is one of the four pillars of object-oriented programming. JavaScript has the prototype-based inheritance, and with it, one object can acquire properties of another object. Even though JavaScript has the class keyword, it still uses prototypes under the hood.

If you want to know to know more about prototypes, check out this article I wrote a few years ago.

In this article, we explain the mixin pattern in the context of TypeScript and provide examples of how we can use it with NestJS.

Creating mixins with TypeScript

Besides inheritance, JavaScript and TypeScript support another way of attaching methods and properties to classes. Following the TypeScript 2.2 RC announcement, the mixin as a function that:

  1. takes a constructor
  2. declares a class that extends that constructor
  3. adds members to that new class
  4. and returns the class itself.

The mixin pattern is also popular in languages such as Scala.

Let’s create a fundamental example of a mixin:

The above type is provided in the official documentation and makes sure that what we pass to the function is a class.

In a nutshell, whatever class we pass to the function, it becomes a person.

Constrained mixins

Above, we’ve created a type that doesn’t contain any information about the class we pass to the mixin. Sometimes, we would like the mixin to use some of the properties from the class it extends.

We can deal with the above problem by creating types that are more constrained.

Thanks to creating the above types, we can define a mixin that uses the property.

Multiple inheritance

Some programming languages implement multiple inheritance where a class can inherit from more than one parent class. Unfortunately, neither JavaScript nor TypeScript doesn’t include it.

error TS1174: Classes can only extend a single class.

We can use the mixin pattern to combine multiple classes. To do that, we first need to define the function suggested in the official documentation:

I’ve made slight adjustments to the example from the documentation by writing more descriptive variable names and returning the .

There is a significant drawback to using this approach, even though it is provided in the official documentation.  Using the type in the function is a big disadvantage, and we should avoid using it as much as possible.

Instead, we can compose multiple mixins and achieve a similar result.

Mixins with NestJS

There is a good chance that you don’t find mixins very appealing. When using NestJS, they can be pretty helpful, though.

Extending the existing mixins

A big part of it is that NestJS uses mixins under the hood. A good example is the FileInterceptor. If we ever want to extend its functionalities for any reason, we need to create our mixin.

The above code comes from API with NestJS #55. Uploading files to the server

There are a few essential things to notice above. First, we create our class and return it using the function from the package. The mixin function applies the decorator under the hood.

Unfortunately, NestJS currently experiences an issue where not using directly causes the reflect-metadata library not to pick it up. Because of that, not using at the top would cause the dependency injection to fail, and our would stay undefined. Hopefully, this bug will be resolved at some point.

Thanks to creating the above mixin, we can now easily use it in one of our controllers:

Passing additional arguments

Sometimes, we would like to pass additional arguments to structures that don’t usually allow us to do that.

The official NestJS docs for authorization suggest creating separate for setting a role and for checking them. Fortunately, we can solve that by creating a mixin that creates a custom guard with an additional argument.

You can find the above code in API with NestJS #56. Authorization with roles and claims

Using the above approach, we can pass arguments when using our . It wouldn’t be possible without using a mixin.

Summary

Most of the time, using mixins to extend our classes might not be a fitting option. Even though that’s the case, mixins have some uses when working with the NestJS framework. Since it uses them under the hood, we might need to create our mixins to extend them. Also, mixins can come in handy when we want to pass additional arguments to structures such as guards.

Series Navigation<< API with NestJS #56. Authorization with roles and claimsAPI with NestJS #58. Using ETag to implement cache and save bandwidth >>
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Shamim
6 months ago

In the code above, instead of PermissionGuard, it should be RoleGuard.

In the code above, c is missing on the first line.