Then run the following command to install the project's dependencies:
npm install
That's it! You can now start developing your TypeScript Project by running the command below. Happy coding!
npm run dev
Error Handling and Logging
While dealing with errors in a Layered Structure Project enviroment, you may notice that the project's debugging complexity scales beyond common console.log() usage. The AppLog Object and AppError Object structures were set to counter that exact issue, by trying to keep the Development process as clean and concise as possible. Both are frequently referenced in the code, but do have a specific usage.
▸ AppError
An AppError Object is used to handle errors in the application. It that takes four parameters:
log: A string containing a simplified error message, for Server side use. This is the message that will be used by the AppLog Object
statusCode: An integer containing the HTTP status code.
message: A string containing a simplified error message, for Client side use. This is the message that will be displayed to the user.
details: A string containing a detailed error message, for Client side use. Can be used to provide more information about the error, such as the stack trace, or suggestions on how to counter the error.
Example Usage
// ..../middlewares/auth.middleware.tsimport*asrepositoryfrom'./../repositories/auth.repository.ts';importAppErrorfrom'./../events/AppError';
...
..asyncfunctionusersExists(req: Request,...){
...
..constuser=awaitrepository.findbyId(req.body.id);if(!user){thrownewAppError('User not found',404,'User not found','Ensure to provide a valid user ID.');}..
...
}
▸ AppLog
An AppLog Object is used to handle logs in the application. It takes two parameters:
type: A string containing the main Layer Structure that contains the log. There are seven allowed values: Error, Server, Controller, Middleware, Repository, Service, and Util.
text: A descriptive string containing the log message. Generally, a short message that describes the output event of the function that generated the log.
While aiming to provide a reusable, modular and extensible architecture, the middlewares are generally the first structures to be refactored into self-contained modules. The validateSchema(), processHeader() and requireToken() middlewares were set in order to achieve that goal. The following section describes useMiddleware(), which incorporates the forementioned functions as key–value pairs in an Object, along with their structure and usage.
‣ UseMiddleware
The useMiddleware() function takes two parameters:
middlewares: An Object containing the key–value pairs of the middlewares to be used, takes one to three parameters:
schema: A Joi Schema Object that will be used to validate the data provided by the client. If the data provided by the client is not valid, an AppError Object will be thrown.
header: A string containing the name of the header that will be used to authenticate the action. If the client-provided header is missing, an AppError Object will be thrown.
token: A boolean indicating whether the token provided by the client will be verified or not. If the token is not valid, an AppError Object will be thrown.
endpoint: A string that will be used to identify the endpoint at which the client–api interaction is undergoing, which will be logged to console by the AppLog Object.
In this section, you will find the example API's endpoints and their respective descriptions, along with the request and response examples, as well as the MongoDBBSON types for each entity, that can be used as guide for data formatting. All data is sent and received as JSON.
Models
User model User
_id: A unique identifier for each user. ObjectId
full_name: The user's full name. Stringrequiredmax(100)
username: The user's username. Stringrequireduniquemax(25)
password: The user's password. Stringrequiredmax(50)
last_update: The date and time when the user was last updated. Date
created_at: The date and time when the user was created. Date
Company model Company
_id: A unique identifier for each company. ObjectId
name: The companys's name. Stringrequireduniquemax(100)
units: An array containing the company's units. Unit[]
users: An array containing the company's users. User[]
x-api-key: The company's API key. Stringrequired
last_update: The date and time when the company was last updated. Date
created_at: The date and time when the company was created. Date
Unit model Unit
_id: A unique identifier for each unit. ObjectId
name: The units's name. Stringrequireduniquemax(50)
street: The unit's street. Stringmax(100)
number: The unit's number. Stringmax(10)
city: The unit's city. Stringrequiredmax(50)
state: The unit's state. Stringrequiredmax(50)
postal_code: The unit's postal code. Stringmax(20)
assets: An array containing the unit's assets. Asset[]
opens_at: The date and time when the unit opens. Stringrequiredlength(5)
closes_at: The date and time when the unit closes. Stringrequiredlength(5)
last_update: The date and time when the unit was last updated. Date
created_at: The date and time when the unit was created. Date
Asset model Asset
_id: A unique identifier for each asset. ObjectId
name: The assets's name. Stringrequiredmax(50)
description: The assets's description. String
model: The assets's model. Stringrequiredmax(100)
owner: The assets owner's user. Userrequired
image: The assets's image URL. String
status: The assets's status. Stringrequiredenum('RUNNING', 'ALERTING', 'STOPPED')
health: The assets's healthscore. Numberrequiredmin(0)max(100)
last_update: The date and time when the asset was last updated. Date
created_at: The date and time when the asset was created. Date
{
"name": "Acme Inc. - Unit 1",
"description": "Now at a new location!",
"address": "Main Street",
"number": "123",
"city": "New York",
"state": "NY",
"postal_code": "12345",
"opens_at": "08:00",
"closes_at": "18:00"
}
{
"name": "Assembly Machine - Now with more assembly",
"description": "This is a machine for assembly, but now we use the Assembly programming language",
"model": "AM-123",
"status": "RUNNING",
"health": 81
}
Very simple full-stack application using React, Java Spring Boot, and PostgreSQL. The API was built following the N-Tier architecture. The goal was to explore and learn more in-depth the development of APIs, the use of Docker and deploying with AWS.
The frontend of a full stack application of a personal wallet made with React, Node and MongoDB that allows you to add inputs, outputs and see all your extract.
My first full stack application with the concept of a personal wallet that allows you to create a personal account to keep track of your entire statement by adding incoming and outgoing transactions, as well as calculating the total balance and being able to edit and delete old transactions.
A POC of a Discord.js bot that sends 3D rendering instructions to a Go server through gRPC which responds with the image bytes which are then sent back on Discord.