Skip to content

JoseLuis21/skeleton-hexagonal-api-ts

Repository files navigation

SKELETON API TS HEXAGONAL

npm version

Dependencies

  • fastify
  • @fastify/cors
  • @fastify/jwt
  • @fastify/rate-limit
  • @prisma/client
  • zod
  • dotenv
  • env-var
  • bcryptjs
  • ioredis
  • @fastify/swagger
  • @scalar/fastify-api-reference

Development dependencies

  • @types/node
  • eslint
  • prettier
  • prisma
  • rimraf
  • ts-node-dev
  • typescript
  • tsc-alias
  • tsconfig-paths

Init project

  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

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

PATH ALIAS

  1. We use Path Alias:
  npm install -D tsconfig-paths tsc-alias
  1. 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

SCRIPTS FOR USE

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

DOCKER

Create image for execute api with the file Dockerfile

  docker build -t example-image .

Execute image Two possibilities

  1. Directly run the container
  docker run example-image
  1. Run with docker compose docker-compose.yml
  docker compose up --build

For more information go to the Documentation

SERVER

For route management we use Fastify

  1. Install fastify fastify-cors
  npm install fastify @fastify/cors

For more information go to the Documentation

CACHE REDIS

  1. Install redis
  npm install ioredis
  1. Use RedisClientAdapter for cache

For more information go to the Documentation

RATE LIMIT

  1. Install @fastify/rate-limit using cache Redis for multiples server
  npm i @fastify/rate-limit

For more information go to the Documentation

PRIVATE ROUTES

  1. Install fastify-jwt
  npm install @fastify/jwt
  1. 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
  1. 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
  1. To generate the token, it is done with the Reply object with the jwtSign method for example:
const token = await reply.jwtSign({ payload });

For more information go to the Documentation

DOCUMENTATION API

For generate doc use @fastify/swagger and for UI use @scalar/fastify-api-reference

  1. Install dependencies
  npm install @fastify/swagger @scalar/fastify-api-reference
  1. Configure Plugin in server.ts

  2. Example Schemas in module User

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

VALIDATIONS

For validations we use zod

  1. Install zod
  npm install zod

For more information go to the Documentation

DATABASE

For database management we use Prisma

  1. Install prisma
  npm install prisma --save-dev
  1. Setup Prisma
  npx prisma init --datasource-provider mysql
  1. Run the migrations
  npx prisma migrate dev --name init
  1. To generate the schema automatically if you already have a database, execute:
  npx prisma db pull
  1. Generate primas client (execute every update shema)
  npx prisma generate

For more information go to the Documentation

DEBUG IN VSCODE

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

ENVIRONMENT VARIABLES

  1. Install packages
  npm install dotenv env-var
  1. Rename the env.example to .env
  2. configure your variables in /src/internal/environment/variables.ts

For more information go to the Documentation

About

Api hexagonal with typescript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published