An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          


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:**
```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



API 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
```


API Endpoints Image