Giter Site home page Giter Site logo

tsoa-sequelize-jwt-app's Introduction

tsoa-sequelize-jwt-app

Boilerplate project for a TypeScript API with TSOA, ExpressJS, Sequelize and Swagger UI

The api endpoint: http://localhost:8080/api/v1/ Swagger UI: http://localhost:8080/docs

Motivation

For the last few months I was contemplating whether I should start using typescript for our company's online microservices. In the past my approach was to create the Swagger specification in YAML first and then generate javascript code, write the controllers etc.

Then I found TSOA and was a bit reluctant to use it because it reverses the order and I never really liked code annotations. After a few hours of trying I found it quite easy to use and decided to go with it. Especially after I found Jesus Felipe Chavarro Muñoz's repo https://github.com/jesusfchavarro/tsoa-app

I always try to keep things simple. While searching for boilerplate code with typescript support for Swagger I found several solutions but they felt over-engineered, probably the authors came from java and applied the same enterprise philosophy to javascript. Jesus' code is very simple and it uses almost the latest package versions, so I took his repo and added what I needed.

Instead of grouping files by type like models/controllers/services I grouped them by endpoint functionality, so that every file related to a particular endpoint is in the same directory. Easy to navigate.

There is an implementation for jwt token authorization for the project endpoint. The token is generated by the login endpoint that takes an email address and password. The password hash generator and comparison method is translated from http://web2py.com/ which is considered a highly secure encryption.

When you implement a new endpoint there is not much boilerplate code to write. The model is being used by Sequelize and also by TSOA for the response type. There is only 3 files needed for a new endpoint:

  • controller with the annotations: that is transformed into an express API endpoint
  • model: representing a database table and also the response type of the API
  • service: the actual implementation of the business logic, fetching records from DB and eventually postprocessing, manipulating them

If you need to return a different response type which does not match the database record, then obviously you will need to define an additional model for that.

Ingredients

This boilerplate code is based on:

Dotenv sample

DATABASE=missionImpossible
DIALECT=postgres
DB_USER=tomCruise
DB_PASS=chooseToAccept
JWT_SECRET=rabbitsfoot
PORT=8080

Build and start server

npm start

Build

// generate routes, swagger.json and compile typescript
npm run build

// generate swagger.json
npm run swagger-gen

// generate routes
npm run routes-gen

tsoa-sequelize-jwt-app's People

Contributors

ridly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

adrek anietz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.