DiegoVictor / chatter

Project built during Rocketseat's Next Level Week #5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chatter

GitHub Workflow Status typescript eslint airbnb-style jest coverage MIT License PRs Welcome
Run in Insomnia}

Permit to register users and its settings, also manage websocket connections and messages. The app has friendly errors, 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 use just one database: SQLite. For the fastest setup is recommended to use docker-compose, you just need to up all services:

$ docker-compose up -d

SQLite

Store the users, settings, messages and connections. For more information to how to setup your database see:

You can find the application's ormconfig.js file in the root folder.

Migrations

Remember to run the database migrations:

$ yarn ts-node-dev ./node_modules/typeorm/cli.js migration:run

Or:

$ yarn typeorm migration:run

See more information on TypeORM Migrations.

.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
PORT Port number where the app will run. 3333
DOCS_URL An url to docs where users can find more information about the app's internal code errors. https://github.com/DiegoVictor/chatter#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": "BadRequest",
  "message": "Setting already exists",
  "code": 140,
  "docs": "https://github.com/DiegoVictor/chatter#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
140 Setting already exists Already exists a settings to provided id.
144 Setting not found The provided id not references an existing setting in the database.
244 User not found The user id sent does not references an existing user in the database.
245 User not found Is not possible to retrieve messages from a user not found in the database.

Versioning

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

POST http://localhost:3333/v1/settings

Routes

route HTTP Method params description
/settings/:id GET id of the setting. Return the user's setting.
/settings POST Body with settings user_id and chat property. Create new setting.
/settings/:id PUT id of the setting and body with chat new value. Update an user's setting.
/users POST Body with user's email. Create a new user.
/users/:id/messages GET id of the user. Return user's messages.
/messages POST Body with message data. Save user new message.

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

Requests

  • POST /settings

Request body:

{
  "user_id": "17c6f66d-a24b-45e0-8c3f-2b3411e51fe8",
  "chat": true
}
  • PUT /settings/:id

Request body:

{
  "chat": true
}
  • POST /users

Request body:

{
  "email": "johndoe@example.com"
}
  • POST /messages

Request body:

{
  "user_id": "d01bc88b-15cb-4478-830f-edc44577d707",
  "admin_id": null,
  "text": "Lorem ipsum doolor sit amet"
}

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.

About

Project built during Rocketseat's Next Level Week #5

License:MIT License


Languages

Language:TypeScript 73.6%Language:CSS 10.2%Language:HTML 8.7%Language:JavaScript 7.3%Language:Dockerfile 0.2%