Making APIs with Express & Typescript using OOPs

Making APIs with Express & Typescript using OOPs

posted Originally published at medium.com 3 min read

This blog doesn't cover concepts involving the technologies used to
create the project. To know about the core technical concepts used in
this learn from here: https://devxp.in/

Before building an API, two key aspects need to be clarified: data modeling and API routes. Here's how we can proceed:

  1. Define Sample Data: Start by identifying sample data to understand
    the entities and their relationships.
  2. Create an ER Model: Use the sample data to design an
    Entity-Relationship (ER) model that visually represents the
    structure and relationships between different entities.
  3. Generate Prisma Schema: Convert the ER model into a Prisma schema to
    define your database structure programmatically.
  4. Seed the Database: Populate the PostgreSQL database with a
    substantial amount of sample data (to use Redis) using seeding
    scripts. These scripts, available in your GitHub repository, will
    ensure the database is ready for development and testing.

Now we need to make our Api routes and for that we will divide it into two parts: Main routes and sub routes

Now as we know the flow of our logic lets make an instance of our own custom server which will include an instance of express app, middle-wares, initialisation of our routes, and connection to Redis running on docker container and Postgres. Also we will create a custom class of CLIENT which holds are connection to Redis, database. (We can create more client connections if we want in CLIENT class)


Now we have our Server instance now we need to setup AllRoutes function which is nothing but an instance of our Main Routes Class:

Now for the sub-routes CLASS (we will see Api_user route towards the end):

And finally user route will be (& so will be the other routes):

Now we can see middleware ie auth.decryptJWT which is nothing but a method of another custom class JWT as to encapsulate two related functions ie generate a JWT token and then decrypt it to enable auth. Also we see middleware argument has an array type which means we can add more middle-wares if we want such as rate-limitter, logging etc.

Also we can see that the handler function is a method of the user object, which is an instance of the Data class. For each route (e.g., /todos, /images), a route-specific instance of the Data class is created. The Data class contains various methods that are executed when the user requests a specific route. These methods include CRUD operations and Redis caching to enhance performance. Please refer my Github for the code.

Now lets talk about Api_user route. So this route basically allows users to sign in & login to our API for them to access our other routes. So for that another class is created called USER. It includes methods for signup and login. Please note this class is independent of Data Class. Please refer my Github for the code.


We can enhance this simple API further by incorporating features like
rate limiters, Prometheus monitoring, queues, pub/sub mechanisms, and
more. These additions can be integrated similarly to ensure
scalability and optimized performance.

If you read this far, tweet to the author to show them you care. Tweet a Thanks
Very insightful!
I would appreciate if the code was pasted instead of screenshot, its easier for user to search it in website if they are looking for same topic and code!! Can you Siddhant?  Cheers
Great Read. I think I might try this.
Nice article

More Posts

Understanding Equality in C# with IEquatable, Not Equals and ==

Spyros - Feb 6

Building Robust API Clients in C# with Refit

Odumosu Matthew - Jan 6

Learn how to build a user-friendly, conversational Telegram bot with python

Astra Bertelli - Apr 30, 2024

Breaking into System Design: Here’s What I Learned Today

Riya Sharma - Jan 16

Create a python Telegram bot, plain, simple and production-ready

Astra Bertelli - Apr 24, 2024
chevron_left