A RestFul API made in Typescript using the Express library, and the Prism ORM library to make all calls to the SQL database. In it you can create your own user, keeping all your ToDos private, with authentication being done through a JWT token.
$ npm installFirst, create a .env file at the root of the project, containing the same properties as the .env.example file, changing only their values.
After creating the .env file, run the migration to create the database, for that, run the command:
$ npx prisma migrate deployNow you can run the app with the command:
# development
$ npm run dev$ npx run test
# test coverage
$ npx run test --coverageAll API routes:
-
USER
-
SESSION
-
ToDos
-
URL:
/user -
Method:
GET -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Success Response:
-
Code:
200 OK -
Content:
{ "name": "Example User", "email": "user@email.com" }
-
-
ErrorResponse:
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "description": "Invalid token", "errorCode": "token.invalid", }
-
-
URL:
/users -
Method:
POST -
Body data:
{ name: { type: String, required: true, min: 2, trim: true }, email: { type: String, required: true, email: true, trim: true }, password: { type: String, required: true, min: 8, max: 16, trim: true }, }
-
Success Response:
-
Code:
201 CREATED -
Content:
{ "message": "User created successfully" }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "name": ["name is required", "..."], "email": ["email is required", "..."], "password": ["password is required", "..."] }, "errorCode": "data.invalid" }
OR
-
Code:
400 BAD REQUEST -
Content:
{ "error": "User already exist", "identifier": "email", "errorCode": "user.already_exist" }
-
-
URL:
/users/delete -
Method:
POST -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Body data:
{ password: { type: String, required: true, min: 8, max: 16, trim: true } }
-
Success Response:
-
Code:
200 OK -
Content:
{ "message": "User deleted successfully" }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "password": ["password is required", "..."] }, "errorCode": "data.invalid" } -
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Unauthorized action", "description": "Invalid password", "errorCode": "user.delete_not_authorized" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "description": "User not found", "errorCode": "user.not_found", }
-
-
URL:
/session/signIn -
Method:
POST -
Body data:
{ email: { type: String, required: true, email: true, trim: true }, password: { type: String, required: true, email: true, trim: true } }
-
Success Response:
-
Code:
200 OK -
Content:
{ "token": "<Your token>", "user": { "name": "Example User", "email": "user@email.com" } }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "email": ["email is required", "..."], "password": ["password is required", "..."] }, "errorCode": "data.invalid" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "description": "Invalid email or password", "errorCode": "session.signIn" }
-
-
URL:
/session/token/refresh -
Method:
POST -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Success Response:
-
Code:
200 OK -
Content:
{ "message": "A new token was successfully generated", "token": "<Your new token>" }
-
-
Error Response:
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "errorCode": "token.invalid" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "errorCode": "token.expired" }
-
-
URL:
/toDos -
Method:
GET -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Success Response:
- Code:
200 OK - COntent:
[ { "id": "a767d63a-f411-49d5-b4bc-91652c114ce7", "task": "Document the API", "completed": false, "completedAt": null, "createdAt": "2021-10-29T19:51:53.534Z" } ] - Code:
-
**Error Response
-
Code:
401 UNAUTHORIZED -
Content:
{ "errorCode": "token.invalid" }
-
-
URL:
/toDos -
Method:
POST -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Body Data:
{ task: { type: String, required: true, min: 2, trim: true } }
-
Success Response:
-
Code:
200 OK -
Content:
{ "message": "ToDo created successfully", "toDo": { "id": "a767d63a-f411-49d5-b4bc-91652c114ce7", "task": "Document the API", "completed": false, "completedAt": null, "createdAt": "2021-10-29T19:51:53.534Z" } }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "task": ["task is required", "..."] }, "errorCode": "data.invalid" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "error": "Session Error", "description": "Invalid token", "errorCode": "token.invalid" }
-
-
URL:
/toDos/:id -
Method:
PATCH -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Params Data:
{ id: { type: String, required: true, uuid: true } }
-
Body Data:
{ task: { type: String, required: false, min: 2, trim: true }, completed: { type: Boolean, required: false } }
-
Success Response:
-
Code:
200 OK -
Content:
{ "message": "ToDo updated successfully", "toDO": { "id": "a767d63a-f411-49d5-b4bc-91652c114ce7", "task": "Updated Task", "completed": false, "completedAt": null, "createdAt": "2021-10-29T19:51:53.534Z" } }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "id": ["id is required", "..."] }, "errorCode": "data.invalid" }
OR
-
Code:
404 NOT FOUND -
Content:
{ "error": "ToDo not found", "errorCode": "toDo.not_found" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "errorCode": "token.invalid" }
-
-
URL:
/toDos/:id -
Method:
DELETE -
Headers:
{ "Authorization": "Bearer <Your token>" } -
Params Data:
{ id: { type: String, required: true, uuid: true } }
-
Success Response:
-
Code:
200 OK -
Content:
{ "message": "ToDo deleted successfully" }
-
-
Error Response:
-
Code:
400 BAD REQUEST -
Content:
{ "error": "Validation fails", "errors": { "id": ["id is required", "..."] }, "errorCode": "data.invalid" }
OR
-
Code:
404 NOT FOUND -
Content:
{ "error": "ToDo not found", "errorCode": "toDo.not_found" }
OR
-
Code:
401 UNAUTHORIZED -
Content:
{ "errorCode": "token.invalid" }
-