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.
Easy peasy lemon squeezy:
$ yarn
Or:
$ npm install
Was installed and configured the
eslint
andprettier
to keep the code clean and patterned.
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
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.
Remember to run the database migrations:
$ yarn ts-node-dev ./node_modules/typeorm/cli.js migration:run -- -d ./src/database/datasource.ts
Or:
$ yarn typeorm migration:run -- -d ./src/database/datasource.ts
See more information on TypeORM Migrations.
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 |
To start up the app run:
$ yarn dev:server
Or:
npm run dev:server
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 errorscode
description.
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. |
A simple versioning was made. Just remember to set after the host
the /v1/
string to your requests.
POST http://localhost:3333/v1/settings
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. |
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": "[email protected]"
}
POST /messages
Request body:
{
"user_id": "d01bc88b-15cb-4478-830f-edc44577d707",
"admin_id": null,
"text": "Lorem ipsum doolor sit amet"
}
Jest was the choice to test the app, to run:
$ yarn test
Or:
$ npm run test
You can see the coverage report inside tests/coverage
. They are automatically created after the tests run.