Skip to content

A RestFull API, made with Express.js and Typescript. In it you can create a user and create, update, delete and get your own ToDos, having everything stored in an SQL database.

License

Notifications You must be signed in to change notification settings

aalexborges/ToDo-api

Repository files navigation

ToDo - API

GitHub language count GitHub top language Package License Coverage Status

Description

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.

Installation

$ npm install

Running the app

First, 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 deploy

Now you can run the app with the command:

# development
$ npm run dev

Test

$ npx run test

# test coverage
$ npx run test --coverage

REST API

All API routes:

Get a user

  • 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",
      }

Create a new user

  • 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"
      }

Delete a User

  • 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",
      }

Authenticate User

  • 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"
      }

Refresh Token

  • 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"
      }

Get all user ToDos

  • 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"
      }
    ]
  • **Error Response

    • Code: 401 UNAUTHORIZED

    • Content:

      { "errorCode": "token.invalid" }

Create a new ToDo

  • 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"
      }

Update a ToDo

  • 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" }

Delete a ToDo

  • 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" }

About

A RestFull API, made with Express.js and Typescript. In it you can create a user and create, update, delete and get your own ToDos, having everything stored in an SQL database.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages