public-api-proxy

2.11.5 • Public • Published

Public API

The public api proxy is built with babel. The npm start script will run the compiled code directly, while invoking nodemon app.js will use the babel require hook for simplicity as you don't have to worry about making sure the build step is being ran.

Route definitions

Each route should be in a routes folder under the desired resource. hapi-router will load routes dynamically. Routes should be separated by the action being performed on the resource. Example: Creating a tax has a route for sending the contract, and a route for checking the status of the asynchronous operation. Those routes could potentially be in a create.js file in the taxes/routes directory.

All handler code should typically remain in the handler function of the route object. If extensive logic is needed and would otherwise make the route object hard to read/change, move the handler function into a controller.js file. Schemas, unless trivial, should live in a schemas folder to keep route definitions as minimal and clean as possible.

You should also take note that any route withe the api tag will return a wrapped response on a successful transaction consisting of the url that was called and the response in a data property. Errors will be sent to the user without being wrapped.

Swagger

There are 2 steps required in order for the swagger plugins to pick up new routes and render them correctly:

  1. Specify the following fields (at a minimum) in the route definition:
{
    method: {String|Array<string>},
    path: String,
    config: {
        description: ''
        tags: ['api', ...], // Tags should also include the primary resource (e.g. hub-taxes, mkt-units, etc)
                            // Note: the primary resource tag must also be added to the ui plugin (see step 2)
        validate: {
            /*
            * Any validation for the route, query, or payload parameter/properties  
            */
        }
        response: {
            schema: Joi.object().description('')
        }
    },
    handler: // Any function that takes the req[uest] and the reply
}
  1. Add the unique primary resource route tag to the defaultTags section of the desired api hapi-swaggered-ui plugin config:
  • For Integration Hub API routes, add to: server/plugins/hapiSwaggeredUiHubApi.js
  • For Inventory Marketplace API routes, add to: server/plugins/hapiSwaggeredUiMktApi.js
{
    register: require('@leisurelink/hapi-swaggered-ui'),
    options: {
        ...
        defaultTags: [
            'hub-pmcs',
            'hub-promotions',
            //Add new primary resource route tags here to include in documentation
        ]
        ...
    }
}

Plugins

To add a new plugin, you can take 1 of 2 steps

  1. Add the name of the plugin to the plugins collections in plugins/index.js and then run npm run build. Use this step if the plugin requires no extra configuration
  2. Add a new file that has the same name as the plugin (with camel casing), add any necessary options or configuration, and then follow step 1 using the file you just created

Index files

With trying to keep confusion at a minimum, index.js files only responsibility should be to gather all other files in the directory and export them back out. Think of index files as a manifest for the directory. If a directory contains only one file and only ever contains one file, remove the directory and just name the js file the same as the directory. This helps avoid the scenario when developing of having 10 different index.js files open at once and cycling through all of them to find the one you want A convenience function has been implemented in mod.js that takes a directory, usually __dirname, requires all the .js or .json files in that directory (excluding index.js, and returns an object keyed by the file name. Index files should be as simple as

import load from 'mod.js'

export default load(__dirname);

Tests

Unit Tests should be separated by the same structure as the file they're testing, so if testing taxes routes, the test directory would be tests/supply/v2/pmcs/taxes/{filename}.tests.js The server object is a global defined in tests/server.tests.js to avoid the problem of walking the directory structure and requiring the server in every test file.

Readme

Keywords

Package Sidebar

Install

npm i public-api-proxy

Weekly Downloads

0

Version

2.11.5

License

ISC

Last publish

Collaborators

  • leisurelink-robot