Project built during Rocketseat's Next Level Week #6
MIT License
Allow to register common users and admin users, tags and compliments between users. The app has friendly errors, use JWT to logins, 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.
Store all application data. 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 database connection, JWT settings, app's port and an 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. | 3000 |
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. | 15m |
DOCS_URL | An url to docs where users can find more information about the app's internal code errors. | https://github.com/DiegoVictor/nlw-valoriza#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": "Bad Request",
"message": "Tag already exists",
"code": 240,
"docs": "https://github.com/DiegoVictor/nlw-valoriza#errors-reference"
}
Errors are implemented with @hapi/boom. As you can see an 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 | User already exists | An user with the same email already is registered. |
240 | Tag already exists | The provided tag name already exists in the database. |
340 | Is not allowed create a compliment from and to the same user | You can not create a compliment using the same user id in the sender and receiver. |
344 | Receiver user not found | The receiver user id provided not matches any registry in the database. |
440 | Email and/or password is incorret | The provided email not match a existing user in the database. |
441 | Email and/or password is incorret | Wrong password . |
541 | You are not authorized | The current user is not admin. |
640 | Token not provided | The bearer token was not sent |
641 | Token invalid | The provided token is expired or invalid |
All the routes with pagination returns 20 records per page, to navigate to other pages just send the page
query parameter with the number of the page.
GET http://localhost:3000/v1/users?page=3
Also in the headers of every route with pagination the Link
header is returned with links to first
, last
, next
and prev
(previous) page.
<http://localhost:3000/v1/tags?page=7>; rel="last",
<http://localhost:3000/v1/tags?page=4>; rel="next",
<http://localhost:3000/v1/tags?page=1>; rel="first",
<http://localhost:3000/v1/tags?page=2>; rel="prev"
See more about this header in this MDN doc: Link - HTTP.
Another header returned in routes with pagination, this bring the total records amount.
A few routes expect a Bearer Token in an Authorization
header.
You can see these routes in the routes section.
GET http://localhost:3000/v1/users?page=1 Authorization: Bearer <token>
To achieve this token you just need authenticate through the
/sessions
route and it will return thetoken
key with a valid Bearer Token.
A simple versioning was made. Just remember to set after the host
the /v1/
string to your requests.
GET http://localhost:3000/v1/tags
route | HTTP Method | pagination | params | description | auth method |
---|---|---|---|---|---|
/sessions |
POST | ❌ | Body with user email and password . |
Authenticates user and return an access token. | ❌ |
/users |
GET | ✔️ |
page query parameter. |
List users. | Bearer |
/users |
POST | ❌ | Body with user name , admin (flag), email and password . |
Creates a new user. | ❌ |
/tags |
GET | ✔️ |
page query parameter. |
List tags. | Bearer |
/tags |
POST | ❌ | Body with tag name . |
Creates a new tag. | Bearer |
/compliments/sent |
GET | ✔️ |
page query parameter. |
List compliments sent. | Bearer |
/compliments/received |
GET | ✔️ |
page query parameter. |
List compliments received. | Bearer |
/compliments |
POST | ❌ | Body with compliments message , tag_id and receiver_id (user's id). |
Creates a new compliments. | Bearer |
Routes with
Bearer
as auth method expect anAuthorization
header. See Bearer Token section for more information.
POST /session
Request body:
{
"email": "[email protected]",
"password": "123456"
}
POST /users
Request body:
{
"name": "John Doe",
"email": "[email protected]",
"password": "123456",
"admin": true
}
POST /tags
Request body:
{
"name": "Inspiration",
}
POST /compliment
Request body:
{
"tag_id": "a7e96379-e1e0-4b01-954c-38175d3df012",
"receiver_id": "38015cdf-c70a-420c-9efa-b71b8f7fae1e",
"message": "Assumenda ducimus quia distinctio eaque"
}
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.