Skip to content

louiscavalcante/ts-node-boilerplate-2024

Repository files navigation

Usage

Install the packages.

$ npm i

Install the requirements.

$ npm run dev:requirements

Tips:

  • This project separates prettier just for formatting and eslint just for linting, as it's advised by the developers. That's why I don't use eslint-plugin-prettier

  • When you commit something, husky, mrm and lint-stage are set to run prettier, eslint and jest.
    This will fix everything and let you know if you forgot about any error.
    It will also run tests that were created or modified.

  • Every time you update the openapi.yaml file, you need to run the npm run update:openapi script, to keep openapi-schema.d.ts updated, so you can use the openapi's schemas as types for this application.

  • The error-handler.middleware will handle all errors, this also includes any unknown routes by the openapi.yaml and the errors thrown by the classes DomainError and AxiosRequestError.
    When NODE_ENV is set to production. It will NOT interfere with the logs, but will change the error response to:
    http status 500 and the body response to: Something bad happened
    Unless it's a DomainError that the client must know about!

  • Unknown exceptions by the application, will use the Erlang's let it crash philosophy.
    Basically it lets the app crash so the orchestrator can restart the application.
    But how this really works?
    First the app will stop receiving request, then finish the requests that were already in progress and gracefully shuts down everything.
    This approach improves system reliability.
    If you're on Kubernetes, you can use Ingress or other load balancer strategies for your application.

  • There's a rateLimiter method configured to limit request per user's ip.
    You can delete it if you decide to use this project as a private API.

  • dotenv is set just for Jest.
    For development it uses --env-file ./environments/.env

  • Inside the tsconfig.json under paths: {} you can set custom paths.
    You don't need to set them anywhere else, this project already does that for you.

  • There are two debuggers configured with auto reload to facilitate your workflow.
    So you don't need to keep restarting your debugger manually.
    One for Node and the other for Jest.

  • You can run/debug just one test at a time with the extension Jest Runner from the author firsttris
    The project is already set to run it without any problems.
    PS: I've found a bug in this extension on linux, but the bug just occurs if you set your it(``, () => {}) with template strings that has multiple lines. The bug happens when the first line has a similar description with another test before the line breaks.
    Besides that it works great!

WYP

  • Unit tests