npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hexa-three-levels

v1.2.0

Published

TypeScript library that helps you to implement hexagonal architecture best practises.

Downloads

5

Readme

Description

This is a library that implement Three Levels Hexagonal Architecture for Back-end, using Typescript, Nest and mongoose for MongoDB. The library has typescript code and nestjs is only used in the tests, since it was originally intended to be used with nest.

The Clean Architectures and specifically the Hexagonal Architecture can help us make our code more maintainable, testable and scalable. The library is to show a way to implement the architecture and provide some useful interfaces and classes for your implementation.

Global Architecture Draft

Main concepts for this architecture Hexa Three Levels are around codebase scalability using Arquitectura hexagonal and DDD. The goal is to provide a clean architecture while flexible for implementing and growing functionalities into the codebase.

Hexa3-clean-architecture.png

Hexagonal three levels

The hexagonal architecture is based on three principles and techniques:

  • Explicitly separate User-Side (app), Business Logic (domain), and Server-Side (infra).
  • Dependencies are going from User-Side and Server-Side to the Business Logic (domain).
  • We isolate the boundaries by using Ports (interfaces) and Adapters (implementations).

Hexa3-general_idea

Folder structure for three levels

.
└── src
    ├── application # Layer that exposes application to external world and users, and configure and launch the application module(s)
    │     ├── middleware # called before the route handler or controllers
    │     ├── filter
    │     ├── guard # Authorizator that determine whether a given request will be handled by the route handler or not
    │     ├── dto # Data Transfer Objects 
    │     └── controller # API Controllers responsible for handling incoming requests and returning responses to the client (routing)
    ├── domain # Layer for the domain to Business Logic
    │     ├── incoming # input-port, services interfaces 
    │     ├── service # Layer that composes application use cases 
    │     ├── model # Business domain classes and everything that composes domain model (Entities and Value Objects)
    │     └── outgoing # output-port to infrastructure interfaces
    │
    └── infrastructure # Layer for communication with what is external of application and infrastructure
        ├── database # output-port to infrastructure interfaces
        │     ├── repository # implementation of repository pattern
        │     └── schema # Model schema for database
        └── etc 

Hexa-three-levels, proposal implementation diagram

In this diagram, the light blue elements are the ones that this library contains, the others are the ones that can be created for particular implementations. Generic types are used where T indicates the name of the type of the entity to implement.

Hexa3-class-diagram

Errors Management Strategy

The error management strategy used is to have the individual services in the domain throw a new subclass of DomainError(), and have the controller catch them and then, via an error handler or an interceptor, throw the appropriate type of HttpException (BadRequestException, ForbiddenException, etc. ). Having Http related stuff in domain layer (services) just seems wrong. For this reason, the domain layer does not handle http exceptions and will only attach an error code included in Domain Error to help the app layer determine which http exception corresponds.

Hexa3-error-manager

The app layer (controllers, middleware, etc.) will throw exceptions in the response with the following JSON format:

{
    "statusCode": 400,
    "timestamp": "2022-11-03T20:38:09.613Z",
    "path": "/api/webshop/v1/profiles/all",
    "payload": {
        "code": 400,
        "detail": "Not authorized by the Auth Guard Middleware because no authorization data in Header.",
        "data": {
            "method": "GET",
            "url": "/api/webshop/v1/profiles/all"
        },
        "name": "HeadersAuthorizationError"
    },
    "message": "The headers Authorization in HTTP Request has a format error."
}

Installation

$ npm install

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Build the lib

# build
$ npm run build

# create pack hexa-three-levels-X.Y.Z.tgz
$ npm pack   

Lib manual and local installation

The project where you want to import the library:

$ npm install hexa-three-levels-X.Y.Z.tgz

The library is installed in:

.
└── node_modules
    ├── hexa-three-levels # lib

And use:

import { IRepository } from "hexa-three-levels";

Lib manual and local installation with Link

Install lib in a folder parallel to your project folder.

.
└── hexa-three-levels # lib project
├── my-project 

Go hexa-three-levels folder and use: npm link Go to the libconsumer (my-project) directory and run: npm link hexa-three-levels

And use.

import { IRepository } from "hexa-three-levels";
...

To unbind: npm unlink hexa-three-levels

References

  • https://blog.octo.com/hexagonal-architecture-three-principles-and-an-implementation-example/#:~:text=The%20hexagonal%20architecture%20is%20based,by%20using%20Ports%20and%20Adapters
  • https://www.tsmean.com/articles/how-to-write-a-typescript-library/
  • https://www.npmjs.com/package/eslint-plugin-hexagonal-architecture

Stay in touch