DEV Community

Cover image for API Project Template, Written in Typescript
Miftahul Arifin
Miftahul Arifin

Posted on

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😄.

Oldest comments (8)

Collapse
 
the_riz profile image
Rich Winter

Very impressive!

(I hope whatever .env code for the JWT is ok to be public.)

Collapse
 
arifintahu profile image
Miftahul Arifin

Thanks for your suggestion

Collapse
 
razorcell profile image
razorcell

Clean work. I checked all files one by one :) Thanks for sharing

Collapse
 
arifintahu profile image
Miftahul Arifin

Thank you!

Collapse
 
dev_emmy profile image
nshimiye_emmy

did a great job, thanks

Collapse
 
ausmurp profile image
Austin Murphy

Highly recommend you look into NestJS for Node/TypeScript API.

Collapse
 
arifintahu profile image
Miftahul Arifin

Thanks! I'll find it out

Collapse
 
abrahamlawson profile image
Lawson

Great article.
I think it's would be better if you make an example such as a blog app for it.That will help people understand more details about its applicability