npm init -y
Install dependencies
npm i -D typescript @types/node ts-node-dev eslint rimraf prettier eslint-plugin-prettier eslint-config-prettier
Execute this command for init typescript
npx tsc --init --outDir dist/ --rootDir src
Create file .eslintrc.json
in root folder
- Generate file config for eslint
npx eslint --init
{
"env": {
"es2021": true,
"node": true
},
"extends": ["standard-with-typescript", "plugin:prettier/recommended"],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {}
}
For more information go to the Documentation
Create file .prettierrc.json
in root folder
{
"trailingComma": "all",
"tabWidth": 2,
"printWidth": 120,
"semi": true,
"singleQuote": true
}
For more information go to the Documentation
Add in VSCODE settings.json
and install prettier and eslint plugins in to editor
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.validate": ["javascript", "typescript"]
}
For more information go to the Documentation
- We use Path Alias:
npm install -D tsconfig-paths tsc-alias
- Configure Paths in
tsconfig.json
"baseUrl": "./src",
"paths": {
"@internal/*": ["internal/*"],
"@modules/*": ["modules/*"],
},
For more information go to the Documentation tsc-alias
For more information go to the Documentation tsconfig-paths
Add command to package.json
in section scripts
{
"lint": "eslint --fix ./src/*",
"dev": "npx ts-node-dev -r tsconfig-paths/register --respawn --transpile-only --debug ./src/index.ts",
"start": "npm run build && node dist/index.js",
"build": "rimraf ./dist && tsc && tsc-alias -p tsconfig.json"
}
For more information go to the Documentation
Create image for execute api with the file Dockerfile
docker build -t example-image .
Execute image Two possibilities
- Directly run the container
docker run example-image
- Run with docker compose
docker-compose.yml
docker compose up --build
For more information go to the Documentation
For route management we use Fastify
- Install fastify fastify-cors
npm install fastify @fastify/cors
For more information go to the Documentation
- Install redis
npm install ioredis
- Use
RedisClientAdapter
for cache
For more information go to the Documentation
- Install
@fastify/rate-limit
using cache Redis for multiples server
npm i @fastify/rate-limit
For more information go to the Documentation
- Install
fastify-jwt
npm install @fastify/jwt
- A file is created to extend the FastifyInstance types which allows adding the plugin to the routes without typing errors
- File in
./src/internal/server/fastify.d.ts
- For use add in routes
onRequest: [fastify.authenticate]
for example:
fastify.get(
'/',
{
onRequest: [fastify.authenticate],
},
userController.getUsers.bind(userController),
);
- File of the plugin is
./src/modules/users/infrastructure/http/routes/user.routes.ts
- To generate the token, it is done with the
Reply
object with thejwtSign
method for example:
const token = await reply.jwtSign({ payload });
For more information go to the Documentation
For generate doc use @fastify/swagger
and for UI use @scalar/fastify-api-reference
- Install dependencies
npm install @fastify/swagger @scalar/fastify-api-reference
-
Configure Plugin in
server.ts
-
Example Schemas in module
User
-
Go to URL
http://localhost:{port}/reference
For more information go to the Documentation @fastify/swagger
For more information go to the Documentation @scalar/fastify-api-reference
For validations we use zod
- Install zod
npm install zod
For more information go to the Documentation
For database management we use Prisma
- Install prisma
npm install prisma --save-dev
- Setup Prisma
npx prisma init --datasource-provider mysql
- Run the migrations
npx prisma migrate dev --name init
- To generate the schema automatically if you already have a database, execute:
npx prisma db pull
- Generate primas client (execute every update shema)
npx prisma generate
For more information go to the Documentation
Modify the launch.json
and task.json
file to your liking to run the vscode debug
.vscode/launch.json
.vscode/task.json
Plugins Recommended, read in:
.vscode/extensions.json
For more information go to the Documentation
- Install packages
npm install dotenv env-var
- Rename the
env.example
to.env
- configure your variables in
/src/internal/environment/variables.ts
For more information go to the Documentation