- Node.js π
- TypeScript π¦Ύ
- Yarn π¦
- Database: JSON & SQLite3
- User authentication (login and password) π
- Full CRUD for cities ποΈ
- Simple database using JSON πΎ
- Clone the repository:
git clone https://github.com/your-user/CityListAPI.git
- Install dependencies:
yarn install
- Run the application:
yarn start
- Access the API at:
http://localhost:3333
- Authentication: Uses JWT (JSON Web Tokens) to ensure that only authenticated users can access protected routes.
- Data Validation: Input data validation with
class-validator
andzod
to ensure data integrity.
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
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"
}
-
start
Starts the project in development mode usingts-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 inbuild/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 inmigrations/
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 inseeds/
to populate the database with initial or test data.
- During development, use the
start
script to save time with automatic reloading. - Before deploying to production, run
postinstall
and validate everything withtest
. - To keep the database updated, run
knex:migrate
. If needed, useknex:rollback
orknex: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.
To run automated tests:
- Install test dependencies:
yarn add jest @types/jest ts-jest --dev
- Run the tests:
yarn test
You can directly test the API endpoints.
- Access after opening the server:
http://localhost:3333/
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.
- 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.
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
- Performance: Cache for frequently accessed data like city lists.
- Improvements: Redis caching system to improve response time on routes that perform heavy queries.
- Version 1.0: Initial release with basic CRUD and authentication features.
- `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
- `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
- `POST /post/entrar` - Entrar
- `POST /post/cadastrar` - Cadastrar