https://github.com/gabrielnat1/api-rest-typescript
"REST API built with Node.js, TypeScript, and Yarn, designed for managing city registrations and user logins, using JSON and SQLite3 for database storage."
https://github.com/gabrielnat1/api-rest-typescript
api api-client api-rest api-server apirest express jest-tests jwt-authentication sqlite3 typescript yarn
Last synced: 12 months ago
JSON representation
"REST API built with Node.js, TypeScript, and Yarn, designed for managing city registrations and user logins, using JSON and SQLite3 for database storage."
- Host: GitHub
- URL: https://github.com/gabrielnat1/api-rest-typescript
- Owner: GabrielNat1
- Created: 2024-11-16T18:25:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-18T23:57:54.000Z (over 1 year ago)
- Last Synced: 2025-04-03T12:52:16.689Z (over 1 year ago)
- Topics: api, api-client, api-rest, api-server, apirest, express, jest-tests, jwt-authentication, sqlite3, typescript, yarn
- Language: TypeScript
- Homepage:
- Size: 383 KB
- Stars: 19
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
API REST TYPESCRIPT
REST API with Node.js, TypeScript, and Yarn for city registration and user login! ๐
---
## ๐ ๏ธ 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:**
```bash
git clone https://github.com/your-user/CityListAPI.git
```
2. **Install dependencies:**
```bash
yarn install
```
3. **Run the application:**
```bash
yarn start
```
4. **Access the API at:**
```bash
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:
```bash
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:
```json
"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:**
```bash
yarn add jest @types/jest ts-jest --dev
```
2. **Run the tests:**
```bash
yarn test
```
---
## ๐ Test API Endpoints
You can directly test the API endpoints.
- Access after opening the server:
```bash
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
### Cities
```bash
- `GET /cidade` - Listar todas as cidades
```
```bash
- `POST /cidade` - Adicionar uma nova cidade
```
```bash
- `PUT /cidade/:id` - Atualizar uma cidade pelo ID
```
```bash
- `DELETE /cidade/:id` - Remover uma cidade pelo ID
```
### People
```bash
- `GET /pessoa` - Listar todas as pessoas
```
```bash
- `POST /pessoa` - Adicionar uma nova pessoa
```
```bash
- `PUT /pessoa/:id` - Atualizar uma pessoa pelo ID
```
```bash
- `DELETE /pessoa/:id` - Remover uma pessoa pelo ID
```
### Login
```bash
- `POST /post/entrar` - Entrar
```
```bash
- `POST /post/cadastrar` - Cadastrar
```