tquoctuan97 / foody-api-nestjs

2 stars 0 forks source link

Foody API

Installation

$ yarn install

Running the app

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Quick CLI

nest g resource [name]

Test

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov

Convention

API Version

Always version your API. Version via the URL, not via headers. Versioning APIs always helps to ensure backward compatibility of service while adding new features or updating existing functionality for new clients. The backend will support the old version for a specific amount of time.

Filter, Search and Sort

Use query parameters for advanced filtering, sorting & searching.

Pagination:

GET /stores?page=1&pageSize=10

Sort:

GET /stores?sort=-createdAt

Filter:

GET /stores?cuisine=1|2

Filter by time range:

GET /stores?timeFrom=time&timeTo=time

Search:

GET /stores?search=Mckinsey

Batch Delete

POST /stores/batch_delete

BODY: {
    ids: ["id_1", "id_2"]
}

Auto loading related resource representations (TODO)

There are many cases where an API consumer needs to load data related to (or referenced from) the resource being requested.

In this case, embed would be a separated list of fields to be embedded. Dot-notation could be used to refer to sub-fields.

GET /stores/12?embed=lead.name|assigned_user

Return something useful from POST, PATCH & PUT requests

POST, PUT, or PATCH methods, used to create a resource or update fields in a resource, should always return updated resource representation as a response with appropriate status code as described in further points.

POST, if successful in adding a new resource, should return HTTP status code 201 along with the URI of the newly created resource in the Location header (as per HTTP specification)

HTTP status codes

HTTP defines a bunch of meaningful status codes that can be returned from your API. These can be leveraged to help the API consumers route their responses accordingly. I've curated a shortlist of the ones that you definitely should be using:

Response

Single data entry response

{
  "id": 1,
  "name": "Store",
  "slug": "store"
}

Multi data entries or array

{
  "data": [],
  "metadata": {
    "pageSize": 20,
    "currentPage": 2,
    "totalPages": 15,
    "totalCount": 295,
    "hasNextPage": true
  }
}

Error (TODO)

A JSON error body should provide a few things for the developer - a useful error message, a unique error code (that can be looked up for more details in the docs), and possibly detailed description.

{
  "data": null,
  "error": {
    "status": "", // HTTP status
    "name": "", // error name ('ApplicationError' or 'ValidationError')
    "message": "", // A human readable error message
    "details": {
      // error info specific to the error type
    }
  }
}

CI/CD

Server will auto deploy when push or pull request to branch main

Start an app with pm2

PM2 is a daemon process manager that will help you manage and keep your application online.

pm2 start ./dist/main.js --name foody-api

Restart pm2

pm2 restart foody-api