DEV Community

benhason1
benhason1

Posted on

nest.js enhanced http module

If you are node.js developer you must heard about nest.js (unless you lived under a rock).

In short this library help you make your code more organized by split your code into what they called "modules" that you can then import in another modules.

for example:

lets say i want to build an app that let you fetch pokemons from this api https://pokeapi.co/ and let you save your favorite ones.

The way that you would split your code in nest.js is by creating:
HttpModule: let you run http methods
DbModule : let you insert items into the db
PokemonModule : the module that discover your api to the consumers.

then you will need to import the DbModule and the HttpModule into the PokemonModule so he will be able to use it.
the PokemonModule.ts will look something like this:

import DbModule from 'src/db-module'
import HttpModule from 'src/http-module'

@module({
    import: [HttpModule, DbModule]
})
Enter fullscreen mode Exit fullscreen mode

Existing Modules

nest.js already provides set of common modules that we can use instead of writing our own.
one of those is the HttpModule, all you need to do is to import it in this way:

import DbModule from 'src/db-module'
import { HttpModule } from '@nestjs/common'

@module({
    import: [HttpModule, DbModule]
})
Enter fullscreen mode Exit fullscreen mode

the problem

the problem that I faced while trying to use this very common and simple library is its implementation, nest.js implemented this library in the observable way with the help of rxjs.
although that this library provide very powerful features like operators, most of the time we don't need it and that just add some complexity to our code which we could avoid by using Promises.

the solution

There were 2 solutions to this problem:

  • implement it on your own
  • add .toPromise() to the end of every http call - kind of patchy way that also make your code unreadable and strange.

there is a third solution

when I faced this problem I checked 2 things:

  • am I the only one that encouter this problem?
  • is there a library that implement the same HttpModule but in the promise way?

The answer to both of those questions is no.

In order to answer those questions i asked developers who use nest.js if they facing the same issue (and how they solve it) and also checked if there are questions on github/stack overflow about it.
the answer was clearly that there isn't library to solve this problem, and they have to implement it on their own or use the patchy .toPromise() everywhere.

nestjs-http-promise

My solution to this issue was to create library that have the same features (easy to migrate from the old one) as the existing HttpModule but in Promise way.

features of the library:

  • retry mechanism - one of the most common things to do when using http request to call an external api is to retry failed requests
  • promise based
  • axios - the most used http library in js
  • typescript

features for the future:

  • add option to make this module global like nest.js ConfigModule
  • make the errors more readable - axiosError override the stacktrace, you can read more about it here https://github.com/axios/axios/issues/2387 (this problem exist also in nest.js HttpModule)

take a look at this library and hit the ⭐️ if you liked it :)
https://github.com/benhason1/nestjs-http-promise

If you have any issues or reviews on this library I will be glad if you could open issue on the repo or just message me.

Top comments (0)