Published on

Awesome Nestjs Usability

Authors

Today I had to take a Node app someone else wrote in NestJS and try work out how it operated. Firstly to get the routes I simply ran the app and on start up got a mapping of the different routes. Something like:

...
2018-11-07T14:25:31.012Z - info: [app] ControlleProducts {/api/v1/products}:
2018-11-07T14:25:31.014Z - info: [app] Mapped {/, GET} route
2018-11-07T14:25:31.015Z - info: [app] Mapped {/:id, GET} route
2018-11-07T14:25:31.015Z - info: [app] Mapped {/, POST} route
2018-11-07T14:25:31.015Z - info: [app] Mapped {/:id, DELETE} route
...

Then when trying to hit some of the more complex endpoints for example the POST one, I simply hit it with nothing initially and got back a really cool and descriptive error message:

{
  "message": "Request POST /api/v1/products with query {} and body {} failed validation. Details: [{\"target\":{},\"property\":\"name\",\"children\":[],\"constraints\":{\"isNotEmpty\":\"name should not be empty\",\"isString\":\"name must be a string\",\"length\":\"name must be longer than or equal to 4 characters\"}},{\"target\":{},\"property\":\"schema\",\"children\":[],\"constraints\":{\"isNotEmpty\":\"schema should not be empty\",\"isString\":\"schema must be a string\",\"length\":\"schema must be longer than or equal to 1 characters\"}},{\"target\":{},\"property\":\"price\",\"children\":[],\"constraints\":{\"isNotEmpty\":\"price should not be empty\",\"isPositiveNumberString\":\"Must be zero or a positive number less than 9007199254740991\"}}]",
  "errors": [
    {
      "target": {},
      "property": "name",
      "children": [],
      "constraints": {
        "isNotEmpty": "name should not be empty",
        "isString": "name must be a string",
        "length": "name must be longer than or equal to 4 characters"
      }
    },
    {
      "target": {},
      "property": "schema",
      "children": [],
      "constraints": {
        "isNotEmpty": "schema should not be empty",
        "isString": "schema must be a string",
        "length": "schema must be longer than or equal to 1 characters"
      }
    },
    {
      "target": {},
      "property": "price",
      "children": [],
      "constraints": {
        "isNotEmpty": "price should not be empty",
        "isPositiveNumberString": "Must be zero or a positive number less than 9007199254740991"
      }
    }
  ]
}

Working with an app like this was a pleasure and just glancing briefly at the NestJS site this is one of the design philosophies:

Nest aims to provide an application architecture out of the box which allows for effortless creation of highly testable, scalable, loosely coupled and easily maintainable applications.