Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

API Pipeline Middleware

Thomas O'Connor edited this page Oct 23, 2017 · 3 revisions

API Pipeline Middleware

Creating Request handlers and Pre-processors

As it stands the OpenFin API Protocol goes through a process pipeline, each step in the pipeline has access to the API Message package, this includes the requesting identity, the data as well as the ability to ack or nack the API Message:

MessagePackage as described in api_transport_base.ts

export interface MessagePackage {
    identity: any; // of the caller 
    data: any;
    ack: any;
    nack: any;
    e?: any;
    strategyName: any; // ws / elipc 
}

Pre-Processors

Pre-Processors enter the pipeline before Handlers, In reverse order of insertion.

Creating a Pre-processor

in api_protocol_base.ts

requestHandler.addPreProcessor((msg: MessagePackage, next: () => void) => {
    log.writeToLog('info', `API Request from: ${JSON.stringify(msg.identity)}, Action: ${msg.data.action}`);
    
    //We need to call the next step in the pipeline.
    next();
});

Handlers

Handlers enter the pipeline at the very tail end. If you are behind the default handlers then you will not be called.

Creating a Handler

in api_protocol_base.ts

requestHandler.addHandler((msg: MessagePackage, next: () => void) => {
    if (msg.data.action === 'get-version') {
        msg.nack(new Error('get version not allowed'));
    } else {
        //We call the next step in the pipeline.
        next();
    }
});

Middleware Locals

To add data to the message package in a Pre-processor for use in a handler or later pre-processor call next() with the desired data in an object with a named property. See example below from mesh_middleware.ts:

   const locals = { aggregate: externalRuntimeData };
   next(locals);

This object is added to the message package in the locals property in the mkNext function in base_handler.ts:

private mkNext(fn: any, msg: any) {
    return (locals?: any) => {
        // Add any middleware data to the message in locals property to be utilized in the api handlers
        if (locals) {
            msg.data.locals = msg.data.locals ? Object.assign(msg.data.locals, locals) : locals;
        }