Skip to content

"REST API built with Node.js, TypeScript, and Yarn, designed for managing city registrations and user logins, using JSON and SQLite3 for database storage."

Notifications You must be signed in to change notification settings

GabrielNat1/Api-Rest-TypeScript

Repository files navigation

API REST TYPESCRIPT


REST API with Node.js, TypeScript, and Yarn for city registration and user login! πŸ”

API Image

πŸ› οΈ Technologies Used

  • Node.js 🌐
  • TypeScript 🦾
  • Yarn πŸ“¦
  • Database: JSON & SQLite3

πŸš€ Features

  • User authentication (login and password) πŸ”‘
  • Full CRUD for cities πŸ™οΈ
  • Simple database using JSON πŸ’Ύ


🎯 How to Run the Project Locally

  1. Clone the repository:
    git clone https://github.com/your-user/CityListAPI.git  
  2. Install dependencies:
    yarn install  
  3. Run the application:
    yarn start  
  4. Access the API at:
    http://localhost:3333  

πŸ›‘οΈ Security

  • Authentication: Uses JWT (JSON Web Tokens) to ensure that only authenticated users can access protected routes.
  • Data Validation: Input data validation with class-validator and zod to ensure data integrity.


πŸ“‚ Directory Structure

The project directory structure is organized as follows:

api-rest-typescript/  
β”œβ”€β”€ src/  
β”‚   β”œβ”€β”€ server/  
β”‚   β”‚   β”œβ”€β”€ controllers/         # Request and response management  
β”‚   β”‚   β”œβ”€β”€ database/            # Database configuration  
β”‚   β”‚   β”‚   β”œβ”€β”€ knex/            # Database connection and queries using Knex  
β”‚   β”‚   β”‚   β”œβ”€β”€ migrations/      # Files for database versioning  
β”‚   β”‚   β”‚   β”œβ”€β”€ models/          # Data entity and schema definitions  
β”‚   β”‚   β”‚   β”œβ”€β”€ providers/       # Database related services  
β”‚   β”‚   β”‚   └── seeds/           # Initial database data  
β”‚   β”‚   β”œβ”€β”€ routes/              # API route definitions  
β”‚   β”‚   └── shared/  
β”‚   β”‚       β”œβ”€β”€ middleware/      # Middleware functions (authentication, validation)  
β”‚   β”‚       └── services/        # Reusable logic and helper services  
β”œβ”€β”€ tests/  
β”‚   β”œβ”€β”€ cities/                 # Tests for city functionalities  
β”‚   β”œβ”€β”€ people/                 # Tests for people functionalities  
β”‚   └── users/                  # Tests for user functionalities  
β”œβ”€β”€ .env                         # Environment variables file  
β”œβ”€β”€ README.md                    # Project documentation  
β”œβ”€β”€ tsconfig.json                # TypeScript configuration  
β”œβ”€β”€ yarn.lock                    # Yarn dependency lock file  
β”œβ”€β”€ jest.config.ts               # Jest test configuration  
└── package.json                 # Project metadata and dependencies  


πŸ“œ Project Scripts

The package.json file contains the following scripts to automate important tasks in development and running the project:

"scripts": {  
 "start": "ts-node-dev ./src/index.ts",  
 "postinstall": "tsc",  
 "production": "node ./build/index.js",  
 "test": "jest",  
 "knex:migrate": "knex --knexfile ./src/server/database/knex/Environment.ts migrate:latest",  
 "knex:rollback": "knex --knexfile ./src/server/database/knex/Environment.ts migrate:rollback",  
 "knex:rollback-all": "knex --knexfile ./src/server/database/knex/Environment.ts migrate:rollback --all",  
 "knex:seed": "knex --knexfile ./src/server/database/knex/Environment.ts seed:run"  
}  

Explanation of the Scripts

  • start
    Starts the project in development mode using ts-node-dev. This allows TypeScript code to be executed directly with automatic reloading when file changes are detected.

  • postinstall
    Generates JavaScript code from TypeScript files after installing dependencies. Useful for preparing the production environment.

  • production
    Runs the project after transpiling, executing the generated JavaScript code in build/index.js on Node.js. This script is used to start the server in production.

  • test
    Runs the project tests using Jest, a framework for unit and integration tests.

  • knex:migrate
    Applies database migrations defined in migrations/ using Knex. This creates or alters tables as needed to keep the database schema updated.

  • knex:rollback
    Rolls back the last applied migration, useful for fixing recent errors.

  • knex:rollback-all
    Rolls back all applied migrations, restoring the database to its initial state.

  • knex:seed
    Executes seed files in seeds/ to populate the database with initial or test data.

Usage Tips

  • During development, use the start script to save time with automatic reloading.
  • Before deploying to production, run postinstall and validate everything with test.
  • To keep the database updated, run knex:migrate. If needed, use knex:rollback or knex:rollback-all to fix or reset the state.
  • Use knex:seed to test the system with simulated data or reset the database state in development environments.


πŸ§ͺ Automated Tests!

To run automated tests:

  1. Install test dependencies:
    yarn add jest @types/jest ts-jest --dev  
  2. Run the tests:
    yarn test  

πŸ“š Test API Endpoints

You can directly test the API endpoints.

  • Access after opening the server:
    http://localhost:3333/  

🌐 CORS Support

The API is configured to allow requests from different domains using the cors package. This allows the API to be accessed by frontends hosted on different servers.


πŸ”’ Authorization and Permissions

  • The API uses role-based access control. There are two main types of users:
    • Admin: Full access, including the ability to manage users and cities.
    • User: Limited access to CRUD cities and personal resources.

βš™οΈ Configuration and Environment Variables

The API requires some environment variables for correct operation. You must rename the .env.example file with the following keys:

  • IS_LOCALHOST=true
  • NODE_ENV=dev
  • PORT=3333
  • JWT_SECRET=[key_secret] set a secret key

πŸ’‘ Development

  • Performance: Cache for frequently accessed data like city lists.
  • Improvements: Redis caching system to improve response time on routes that perform heavy queries.

πŸ“… Version Roadmap

  • Version 1.0: Initial release with basic CRUD and authentication features.




πŸ“ Available Endpoints


API Endpoints

Cities

- `GET /cidade` - Listar todas as cidades
- `POST /cidade` - Adicionar uma nova cidade
- `PUT /cidade/:id` - Atualizar uma cidade pelo ID
- `DELETE /cidade/:id` - Remover uma cidade pelo ID

People

- `GET /pessoa` - Listar todas as pessoas
- `POST /pessoa` - Adicionar uma nova pessoa
- `PUT /pessoa/:id` - Atualizar uma pessoa pelo ID
- `DELETE /pessoa/:id` - Remover uma pessoa pelo ID

Login

- `POST /post/entrar` - Entrar
- `POST /post/cadastrar` - Cadastrar



API Endpoints Image

About

"REST API built with Node.js, TypeScript, and Yarn, designed for managing city registrations and user logins, using JSON and SQLite3 for database storage."

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages