-
Notifications
You must be signed in to change notification settings - Fork 50
API Pipeline Middleware
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 enter the pipeline before Handlers, In reverse order of insertion.
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 enter the pipeline at the very tail end. If you are behind the default handlers then you will not be called.
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();
}
});
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;
}