Plugin Builder API Documentation

This is the Plugin Builder API Documentation for the Caesar.js secure microservices node framework.

Plugin Builder API Documentation

Caesar.js server is extensible and so you can build your own plugins. Caesar.js Plugin Builder API Documentation provide an helper class to build plugins:

const server = require('caesarjs').server;
const pluginBuilder = require('caesarjs-plugin-builder').pluginBuilder;

The Caesar.js plugin builder is also a separate npm package. This allows plugin developers to download it as separate package:

const PluginBuilder = require('caesarjs-plugin-builder');

Let’s go through the documentation for the pluginBuilder class:

Constructor( pluginId: string, pluginName: string, authorName: string, version: string)

You must instantiate the pluginBuilder class which is chainable and returns an instance of pluginBuilder.

const pb = new pluginBuilder('my-plugin-id', 'my plugin', 'my name', '0.0.1')

The other chainable methods are:

  • addRoute
  • addRouteWithPatternObject
  • addMiddleware

The method finally returning the plugin object as accepted by the server object is:

  • buildPlugin

 

Let’s discuss these in this section then.

 

addRoute(routeUrlPath: string, expressCallBack: function)

addRoute method allows adding a route with personalized URL. The expressCallback has the following parameters:

  1. req
  2. res

For example

const pb = new pluginBuilder('my-plugin-id', 'my plugin', 'my name', '0.0.1')
.addRoute('/some/url', function(req, res) {
// do something in here
})

addRouteWithPatternObject( patternMatchingObject: object, expressCallBack )

addRouteWithPatternObject creates a route with a matching pattern. The expressCallback has the following parameters:

  1. req
  2. res

For example:

const pb = new pluginBuilder('my-plugin-id', 'my plugin', 'my name', '0.0.1')
.addRouteWithPatternObject({ role: 'something'}, function(req, res) {
// do something in here
})

addMiddleware( expressMiddlewareFunction: function )

addMiddleware allows to add a middleware function which is executed before the route functions. The expressMiddlewareFunction has the following parameters:

  1. req
  2. res
  3. next

const pb = new pluginBuilder('my-plugin-id', 'my plugin', 'my name', '0.0.1') 
.addMiddleware( function( req, res, next) {
//do something i.e.
req.superCool = true;
next();
})

buildPlugin()

The buildPlugin method returns the object in a format that’s accepted by the server through the server’s method installPlugin

For example:

const server = require('caesarjs').server; 
const pluginBuilder = require('caesarjs').pluginBuilder;
const encryptorKey = '33a24560-a43b-44cc-b2bc-9cc0de14a093';
const pb = new pluginBuilder('my-plugin-id', 'my plugin', 'my name', '0.0.1') 
.addMiddleware( function( req, res, next) {
//do something i.e.
req.superCool = true;
next();
});
new server(3000, {encryptorKey})
.installPlugin( pb.buildPlugin() )
.listen();

It is worthwhile noting that all the expressRoutes & middleware functions inherit the Server API Middleware

Leave a Reply

Required fields are marked*

*