Skip to content

DiegoVictor/tindev-api

Repository files navigation

[API] Tindev

AppVeyor mongodb redis nodemon eslint airbnb-style jest coverage MIT License PRs Welcome
Run in Insomnia}

Responsible for provide data to the web and mobile front-ends. Allows users to register yourself, see other registered users, like or dislake them and see past matches. The app has friendly errors, use JWT to logins, validation, also a simple versioning was made.

Table of Contents

Installing

Easy peasy lemon squeezy:

$ yarn

Or:

$ npm install

Was installed and configured the eslint and prettier to keep the code clean and patterned.

Configuring

The application uses two databases: MongoDB and Redis. For the fastest setup is recommended to use docker-compose, you just need to up all services:

$ docker-compose up -d

Redis

Responsible to store data utilized by the websocket to alert users when a match occurs. If for any reason you would like to create a Redis container instead of use docker-compose, you can do it by running the following command:

$ docker run --name tindev-redis -d -p 6379:6379 redis:alpine

MongoDB

Store all data utilized by the application. If for any reason you would like to create a MongoDB container instead of use docker-compose, you can do it by running the following command:

$ docker run --name tindev-mongo -d -p 27017:27017 mongo

.env

In this file you may configure your Redis database connection, JWT settings, the environment, app's port and a url to documentation (this will be returned with error responses, see error section). Rename the .env.example in the root directory to .env then just update with your settings.

key description default
APP_PORT Port number where the app will run. 3333
JWT_SECRET A alphanumeric random string. Used to create signed tokens. -
JWT_EXPIRATION_TIME How long time will be the token valid. See jsonwebtoken repo for more information. 7d
MONGO_URL MongoDB connection url. mongodb://mongo:27017/tindev
REDIS_HOST Redis host. redis
REDIS_PORT Redis port. 6379
DOCS_URL An url to docs where users can find more information about the app's internal code errors. https://github.com/DiegoVictor/tindev-api#errors-reference

Usage

To start up the app run:

$ yarn dev:server

Or:

npm run dev:server

Error Handling

Instead of only throw a simple message and HTTP Status Code this API return friendly errors:

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Developer not exists",
  "code": 240,
  "docs": "https://github.com/DiegoVictor/tindev-api#errors-reference"
}

Errors are implemented with @hapi/boom. As you can see a url to error docs are returned too. To configure this url update the DOCS_URL key from .env file. In the next sub section (Errors Reference) you can see the errors code description.

Errors Reference

code message description
141 Token not provided The JWT token was not sent.
142 Token invalid The JWT token provided is invalid or expired.
240 Developer not exists The id sent does not references an existing developer in the database.

X-Total-Count

A header returned in routes of listing, this bring the total records amount.

Bearer Token

A few routes expect a Bearer Token in an Authorization header.

You can see these routes in the routes section.

GET http://localhost:3333/v1/developers Authorization: Bearer <token>

To achieve this token you just need authenticate through the /developers route and it will return the token key with a valid Bearer Token.

Versioning

A simple versioning was made. Just remember to set after the host the /v1/ string to your requests.

GET http://localhost:3333/v1/developers

Routes

route HTTP Method params description auth method
/developers POST Body with Github username. Authenticates users, return a Bearer Token and user's data.
/developers GET - List developers not liked or disliked yet. Bearer
/developers/:id GET id of the developer. Return one developer. Bearer
/developers/:liked_user_id/like POST liked_user_id of the developer to like. Like a developer. Bearer
/developers/:disliked_user_id/like POST disliked_user_id of the developer to dislike. Dislike a developer. Bearer
/matches GET - List developers you liked in the past. Bearer

Routes with Bearer as auth method expect an Authorization header. See Bearer Token section for more information.

Requests

  • POST /developers

Request body:

{
  "username": "diegovictor"
}

Running the tests

Jest was the choice to test the app, to run:

$ yarn test

Or:

$ npm run test

Coverage report

You can see the coverage report inside tests/coverage. They are automatically created after the tests run.