Fastify API skeleton

A simple api skeleton using fastify with TypeScript Support, for Node.js.

Core features

  • Docker Support: Docker is guaranteed to be identical on any system
  • Highly performant: Fastify is one of the fastest web frameworks
  • Logging: Fastify has a integration with best logger to almost remove this cost, Pino!
  • TypeScript ready: We work with type declaration, object oriented with classes, interfaces and statically typed like C# or Java
  • Coding Standards: Help you to maintaince a clean code and to find and fix problems in your code

Pre-requisites

  • git
  • docker
  • docker-compose

Misc.

We must pass our commands to the Docker container. This is a really long command:

docker-compose -f docker-compose.cli.yml run --rm yarn install

For this reason, it is recommended to create an alias for docker-compose -f docker-compose.cli.yml run --rm and call it dcli (Docker CLI).

add a new line into your shell alias dcli='docker-compose -f docker-compose.cli.yml run --rm'

Ps.: Oh My Zsh is a powerfull shell framework which features major extensions such as plug-ins and themes for zsh (recomended)

[Recommended] Enable Git hooks

It goes to help you to improve your commits and reviews (PR/MR) like coding standards, unit tests and more.

Install nodejs >= 10:

curl -sL install-node.now.sh/lts | bash

Install husky:

npx husky install

Quick start

Clone this repository:

Inside the project execute the following instructions:

cp .env.dist .env && cp docker-compose.override.yml.dist docker-compose.override.yml
docker-compose pull

Install composer dependencies:

dcli yarn install

Launch the containers with:

docker-compose exec -d

Finally, you can test it with:

curl http://127.0.0.1:4444

[Optional] Setup gitlab token

The first step is to make a gitlab token with following permissions:

  1. Go to Gitlab personal access tokens
  2. Create a new token.
  3. Assign the permissions.
  4. Click on Create personal access token.

Second step is to save gitlab token into .bashrc or .env (see the Quick Start section):

nano ~/.bashrc

add it in the end:

export GITLAB_TOKEN=YOUR_TOKEN

finally reload bashrc

source ~/.bashrc

If you have installed the zsh, save your token into .zshrc.

Tests

yarn tests:

dcli yarn run test

Lint files:

dcli yarn run fix

Console Commands

List commands:

dcli yarn run dev:cli

Command help:

dcli yarn run dev:cli example -h

To register a command, change src/config/commands.config.ts:

import NewExampleCommand from '../commands/new-example.command';

Container.import([
  // ...
  NewExampleCommand,
]);

See the to more examples https://github.com/tj/commander.js

Fastify-decorator has support to Dependency injection (DI), click here to see more exampĺes