API Project Template, Written in Typescript

arifintahu

Miftahul Arifin

Posted on October 10, 2021

API Project Template, Written in Typescript

Building project with standardized structure could save much our time. We could focus on business process without considering too much on project structure. On the other hand, a good project structure should be clean, nice refactored, and easy to maintain.

Here's why:

  • Your time should be focused on creating something amazing. A project that solves a problem and helps others.
  • You shouldn't be doing the same tasks over and over like structuring project
  • You should implement dry principles to the rest of your life

This time, I created complete project template for API written in Typescript which is available in this github. Of course, no one template will serve all projects since your needs may be different.

Built With

This project structure is built using

Features

  • Everything is modular and unit testable
  • Typescript everything
  • Express API with dependency injected routes, controllers, repositories, middleware, and services
  • Centralized configuration loading and validation
  • API Documentation using swagger

Folder Structure

├── .husky/                     # Pre-commit config for lint staged
├── src/                        # All application source
├──── @types/                   # Type definition
|
├──── api/                       
├────── controllers/            # Define all controllers
├────── helpers/                # Define all helpers
├────── lib/                    
├──────── logger.ts             # Define logger
├────── middlewares/
├──────── authorization.ts      # Define authorization using JWT
├──────── morgan.ts             # Define morgan   
├────── models/                 # Define all schema models
├────── repositories/           # Define all repositories
├────── routes/                 
├──────── v1/                   # Define all v1 routes
├────── services/               # Define all services
├────── validations/            # Define all controller validations
|
├──── config/
├────── swagger/                # Define swagger configuration
├────── database.ts             # Define postgres database connection
├────── express.ts              # Define express configuration
|
├──── constants/                # Define all constants                   
├──── server.ts                 # Create express config
├──── index.ts                  # ENTRYPOINT - Start server
|
├── test/                       # End-to-end test
|
└── Lots of random build-related files
Enter fullscreen mode Exit fullscreen mode

See full project here. Let me know if you have any suggestions😄.

💖 💪 🙅 🚩
arifintahu
Miftahul Arifin

Posted on October 10, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related