Coder Social home page Coder Social logo

mirzaakhena / clean-architecture-nodejs Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 140 KB

Sample implementation of Clean Architecture in NodeJS using expressJS as the web framework and TypeORM as the database client

TypeScript 98.00% Dockerfile 1.35% Procfile 0.04% Shell 0.61%

clean-architecture-nodejs's Introduction

Clean Architecture NodeJS

This project inspired from https://github.com/mirzaakhena/gogen

I am experimenting using the function closure except using a class.

Installation

git clone the repo then install required dependencies

$ npm install 

Run the application

$ npm run start

Application will run in port 3000

Use the application

To use the API, you can use the src/controller/request.http file to test the api request invocation.

Code Structure

src
├── controller
│   ├── controller.ts
│   ├── handle_addproduct.ts
│   ├── handle_authorization.ts
│   ├── handle_createorder.ts
│   ├── handle_error.ts
│   ├── handle_getallproduct.ts
│   ├── handle_login.ts
│   └── request.http
├── gateway
│   ├── gateway.ts
│   ├── impl_order.ts
│   ├── impl_product.ts
│   ├── impl_trx.ts
│   └── impl_user.ts
├── index.ts
├── model
│   ├── entity
│   │   ├── error.ts
│   │   ├── order.ts
│   │   ├── product.ts
│   │   └── user.ts
│   └── repository
│       ├── order.ts
│       ├── product.ts
│       ├── transaction.ts
│       └── user.ts
├── usecase
│   ├── execute_add_product.test.ts
│   ├── execute_add_product.ts
│   ├── execute_create_order.test.ts
│   ├── execute_create_order.ts
│   ├── execute_getall_product.test.ts
│   ├── execute_getall_product.ts
│   ├── execute_login.test.ts
│   ├── execute_login.ts
│   └── usecase.ts
└── utility
    ├── application.ts
    └── logger.ts

What is the feature of this code ?

  1. Written with concept clean architecture that promote the separation of concern between logic code and infrastructure code.
  2. Basically just use common frameworks like ExpressJS and TypeORM. And not using complex frameworks like NestJS
  3. All the controller, use case (service in common terms) and gateway written by function closure instead of class
    export const ImplFindOneUserByUsername = (ds: DataSource): FindOneUserByUsername => {
        return async (ctx: Context, username: string): Promise<User | null> => {
            return await getManager(ctx, ds.manager).findOneBy(User, {username})
        }
    }
    
    export const ImplValidatePassword = (): ValidatePassword => {
        return async (ctx: Context, username: string, password: string): Promise<boolean> => {
            // TODO: temporary implementation
            return (username === password)
        }
    }
    
    export const ImplFindAllUserRoles = (ds: DataSource): FindAllUserRoles => {
        return async (ctx: Context, username: string): Promise<string[]> => {
            return (await getManager(ctx, ds.manager).findBy(UserRole, {username})).map(ur => {
                return ur.role
            })
        }
    }
    
  4. have a login function which generate the jwt token
    export const handleLogin = (executable: Inport<Request, Response>): HandlerFuncWithNext => {
    
        return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    
            try {
    
                const ctx = getContext(handleLogin.name)
    
                logger.info(ctx, `called with ${JSON.stringify(req.body)}`)
    
                const result = await executable(ctx, req.body)
    
                logger.info(ctx, `done with id ${result.user}`)
    
                const payload = {data: result.user}
                const secretKey = process.env.SECRET_KEY as string
                const expiration = {expiresIn: process.env.TOKEN_EXPIRATION}
                const token = jwt.sign(payload, secretKey, expiration);
    
                res.json({token: token})
    
            } catch (err) {
                next(err)
            }
    
        };
    
    }
    
  5. have a middleware to intercept and check the token
    export const handleAuthorization = (): HandlerFuncWithNext => {
    
        return async (req: DecodedRequest, res: express.Response, next: express.NextFunction) => {
    
            try {
    
                const authHeader = req.headers.authorization;
                if (!authHeader) {
                    res.sendStatus(401);
                    return
                }
    
                const token = authHeader.split(' ');
                if (token.length !== 2) {
                    res.sendStatus(401);
                    return
                }
    
                const secretKey = process.env.SECRET_KEY as string
                const dataDecoded = jwt.verify(token[1], secretKey) as JwtPayload
                req.user = dataDecoded.data as User
    
                next();
    
            } catch (e) {
                next(e);
            }
    
        }
    
    }   
    
  6. have a simple user access control verified on every method
    if (!hasOneOfRoles(user,["admin", "operator"])) {
        res.sendStatus(403);
        return
    }
    
  7. have sample transactions database handling
    export const executeCreateOrder = (o: Outport): Inport<Request, Response> => {
    
        const [saveProduct, saveOrder, withTransaction,] = o
    
        return async (ctx: Context, req: Request): Promise<Response> => {
    
            return await withTransaction(ctx, async (ctx: Context): Promise<Response> => {
    
                const objOrder = new Order()
                objOrder.id = `order-${req.id}`
                objOrder.username = req.username
                await saveOrder(ctx, objOrder)
    
                const objProduct = new Product()
                objProduct.id = `product-${req.id}`
                objProduct.name = req.name
                objProduct.price = req.price
                objProduct.validate()
    
                await saveProduct(ctx, objProduct)
    
                return {
                    orderId: objOrder.id,
                    productId: objProduct.id,
                }
    
            })
    
        }
    }
    
  8. Use manual and simple dependency injection
    const m = getDataSource()
    
    const findOneUserByUsername = ImplFindOneUserByUsername(m)
    const validatePassword = ImplValidatePassword()
    
    const withTrx = ImplWithTransaction(m)
    const saveOrder = ImplSaveOrder(m)
    const saveProduct = ImplSaveProduct(m)
    const findAllProducts = ImplFindAllProducts(m)
    const findAllUserRoles = ImplFindAllUserRoles(m)
    
    const router = express.Router()
    
    router.use("/api/v1", handleAuthorization());
    router.post("/api/v1/products", handleAddProduct(executeAddProduct([saveProduct])))
    router.get("/api/v1/products", handleGetAllProduct(executeGetAllProduct([findAllProducts])))
    router.post("/api/v1/order", handleCreateOrder(executeCreateOrder([saveProduct, saveOrder, withTrx])))
    router.post("/login", handleLogin(executeLogin([findOneUserByUsername, validatePassword, findAllUserRoles])))
    

clean-architecture-nodejs's People

Contributors

mirzaakhena avatar

Stargazers

Carlos Narez avatar  avatar Muhammad Zakir Ramadhan  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.