Responsible for provide data to the web
and mobile
front-ends. Permit to register your class availability and subject, also count the number of teacher connected to users (get contacted by whatsapp). The app has validation and 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 the NGOs and its incidents. For more information to how to setup your database see:
You can find the application’s knexfile.ts
file in the root folder.
Remember to run the SQLite database migrations:
$ yarn knex:migrate
Or:
$ npx knex migrate:latest
See more information on Knex Migrations.
In this file you may configure the environment, your 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 |
NODE_ENV | App environment. The knex’s connection configuration used rely on the this key value, so if the environment is development the knex connection used will bedevelopment . |
development |
DOCS_URL | An url to docs where users can find more information about the app’s internal code errors. | https://github.com/DiegoVictor/proffy-api#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": 429,
"error": "Too Many Requests",
"message": "Too Many Requests",
"code": 549,
"docs": "https://github.com/DiegoVictor/proffy-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 errorscode
description.
code | message | description |
---|---|---|
141 | User not found | Could not found the user of the class. |
144 | Class not found | The id sent does not references an existing class in the database. |
150 | Unexpected error while update new classes | An error ocurred during the updating/creation of the user, classes and schedules. |
240 | Email already in use | The provided email is already used by another user. |
244 | User not found | The id sent does not references an existing user in the database. |
340 | User and/or password not match | User and/or password is incorrect. |
344 | User not exists | The email sent not references an existing user in the database. |
440 | You can not favorite yourself | You provide your own id as favorited_user_id . |
444 | Users not match | Couldn’t found one or both users, the favorited (proffy) and you (you not exists xD!). |
540 | Token invalid or expired | The reset password JWT token is invalid or expired. |
541 | Token invalid or expired | The login JWT token is invalid or expired. |
542 | Invalid token | The login JWT token not contain a valid user id. |
543 | Token not provided | The login JWT token was not sent. |
544 | User does not exists | The provided email not references a user in the database. |
549 | Too Many Requests | You reached at the requests limit. |
550 | An unexpected error while updating the user occured | Was not possible to reset user password. |
All the routes with pagination returns 10 records per page, to navigate to other pages just send the page
query parameter with the number of the page.
GET http://localhost:3333/v1/classes?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:3333/v1/classes?page=7>; rel="last",
<http://localhost:3333/v1/classes?page=4>; rel="next",
<http://localhost:3333/v1/classes?page=1>; rel="first",
<http://localhost:3333/v1/classes?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:3333/v1/classes 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:3333/v1/classes
route | HTTP Method | pagination | params | description | auth method |
---|---|---|---|---|---|
/connections |
GET | ❌ | week_day , from and to query parameters. |
Lists connections total. | Bearer |
/connections |
POST | ❌ | Body with user_id . |
Increase the number of connections. | Bearer |
/classes |
GET | ✔️ | week_day , subject , time , page query parameters. |
Lists classes available. | Bearer |
/classes/:id |
GET | ❌ | id of the class. |
Return the class. | Bearer |
/classes |
POST | ❌ | Body with class subject , cost , user user_id , whatsapp , bio and class schedule schedules.week_day , schedules.from , schedules.to . |
Create new class availability. | Bearer |
/users/:id |
GET | ❌ | id of the user. |
Return one user. | Bearer |
/favorites |
GET | ✔️ | page query parameters. |
Lists favorited proffys. | Bearer |
/favorites |
POST | ❌ | Body with user_id from user that intending to be favorite. |
Set a proffy as favorite. | Bearer |
POST /connections
Request body:
{
"user_id": 76988
}
POST /classes
Request body:
{
"user_id": 76988,
"whatsapp": "39379976591",
"bio": "I have been worked with PHP/Laravel and JavaScript/Node.js for +4 years. Recently I started studying ReactJs and React Native :)",
"cost": 30,
"subject": "Node.js",
"schedule": [
{
"week_day": 0,
"from": "10:00",
"to": "15:00"
},
{
"week_day": 4,
"from": "7:00",
"to": "11:00"
}
]
}
POST /favorites
Request body:
{
"user_id": 76988
}
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.
Author: DiegoVictor
Source Code: https://github.com/DiegoVictor/proffy-api
#nodejs #node #javascript