2.0.1 • Published 7 years ago

@bb-cli/bb-convert-plugin-orderable v2.0.1

Weekly downloads
14
License
-
Repository
-
Last release
7 years ago

BB-Convert Orderable plugin middleware

This plugin will make any HTTP GET service method from RAML specification which has orderBy and direction query parameters to respect them. So those service methods will return resulting data sorted ascending or descending based on the provided orderBy property.

This plugin will only by applied in mock template.

Install alongside bb-convert (prefer global)

npm i -g @bb-cli/bb-convert-plugin-orderable

Usage

bb-convert raml --transform-plugins @bb-cli/bb-convert-plugin-orderable --template mock-ng
# OR
bb-convert raml --transform-plugins orderable --template mock-ng

Configure in .bbconfig

Orderable plugin can be configured in .bbconfig file:

{
  "default": {
    "convert": {
      "raml": {
        "transform-plugins": "orderable"
      }
    }
  }
}

With this configuration in place, you can simply run

bb-convert raml --template mock-ng

Re-usage

Current implementaion of the hook will only sort on the fields which located on the root properties level of the sortable object. Unfortunately this approach cannot suit all scenarios because in some of RAML specifications it is possible to order data in response by property which is deep in object structure. For such cases custom orderable plugin have to be created.

Since most functionality of the custom plugin for previously describe scenario will be the same, this plugin exports plugin factory function which can simplify new plugin creation. Here is an example of custom plugin code to order objects with the following structure:

// Object structure example
{
  "name": "Example object",
  "id": "1235",
  "ref": {
    "id": "11111",
    "refName": "Some name"
  }
}
// Custom plugin code example
const orderable = require('@bb-cli/bb-convert-plugin-orderable');

const getObjValueFn = (propName, obj) => {
  switch (propName) {
    case 'refName':
      return obj.ref.refName;
    case 'refId':
      return +obj.ref.id;
    default:
      return obj[propName];
  }
};

module.exports = orderable.pluginFnFactory(getObjValueFn);