Responsible for provide data to the web
and mobile
front-ends. Permit to register yourself, login/logout, create and see your spots, approve or reject bookings on your spots and book spots from others. The app has pagination, pagination's link header (to previous, next, first and last page), 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 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
Responsible to store data utilized by the websocket to alert users when books are made, approved or rejected. 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 aircnc-redis -d -p 6379:6379 redis:alpine
Responsible to store almost all application data. 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 aircnc-mongo -d -p 27017:27017 mongo
In this file you may configure your Redis and MongoDB 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_URL | Used to mount spots' thumbnails urls. | http://127.0.0.1 |
APP_PORT | Port number where the app will run. | 3333 |
NODE_ENV | App environment. | development |
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/aircnc-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": 400,
"error": "Bad Request",
"message": "Spot does not exists",
"code": 344,
"docs": "https://github.com/DiegoVictor/aircnc-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 |
---|---|---|
144 | User does not exists | The id sent not references an existing user in the database. |
241 | Token not provided | The JWT token was not sent. |
242 | Token invalid | The JWT token provided is invalid or expired. |
341 | You can not remove spot with bookings approved | Is not allowed to delete spots with approved bookings. |
342 | Only the spot owner can approve bookings | You are trying approved a spot that is not your. |
343 | You didn't request a booking to this spot or is not the spot owner | You are not allowed to reject a book that you not made it or you is not the spot owner. |
344 | Spot does not exists | The id sent not references an existing spot in the database. |
345 | You can only cancel bookings with 24 hours in advance | Is too late to cancel a book. |
This header brings the 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/spots/5e33633397642e0884e90895 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/spots
route | HTTP Method | params | description | auth method |
---|---|---|---|---|
/sessions |
POST | Body with user email . |
Authenticates user, return a Bearer Token and user's email. | ❌ |
/bookings |
GET | - | Lists my bookings. | Bearer |
/bookings/:id/rejection |
POST | id of the rejected booking. |
Reject a booking request. | Bearer |
/bookings/:id/approval |
POST | id of the approved booking. |
Approve a booking request. | Bearer |
/spots/:id/booking |
POST | id of the booked spot. |
Book a spot. | Bearer |
/dashboard |
GET | - | Lists my spots. | Bearer |
/pending |
GET | - | Get my spots' pending requests. | Bearer |
/spots |
GET | tech query parameter. |
Lists available spots by tech. | Bearer |
/spots/:id |
GET | id of the spot. |
Return one spot. | Bearer |
/spots |
POST | Body with new spot form data (See insomnia file for good example). | Create a new spot. | Bearer |
/spots/:id |
PUT | id of the spot, body with spot's thumbnail , techs , company and price (See insomnia file for good example). |
Update a spot. | Bearer |
/spots/:id |
DELETE | id of the spot. |
Delete a spot. | Bearer |
Routes with
Bearer
as auth method expect anAuthorization
header. See Bearer Token section for more information.
POST /session
Request body:
{
"email": "johndoe@example.com"
}
POST /spots/:spot_id/booking
Request body:
{
"date": "2021-11-01T23:16:52"
}
POST /spots
Request body:
"company"="Hackett, Becker and Fadel"
"price"=89
"techs"="ReactJS"
"thumbnail"=<file>
PUT /spots
Request body:
"company"="Becker and Fadel"
"price"=115
"techs"="Node.js, ReactJS"
"thumbnail"=<file>
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.