Orchestr

Middleware

Middleware is a way to intercept and modify the input, output or context of an action. It is a way to add additional functionality to an action without having to modify the action-handler.

Middleware is a way to intercept and modify the input, output or context of an action. It is a way to add additional functionality to an action without having to modify the action-handler.

Middleware Types

Action Middleware

server/orchestr/newsletter/subscribe.ts
import { 
SubscribeAction
} from '@laioutr-core/canonical-types/newsletter';
// The return-type of the use-method is a new Orchestr instance with the middleware applied const
defineMyPackage
=
defineOrchestr
.
use
((
args
,
next
) => {
return
next
({
context
: {
client
:
createCrmClient
() }
}) }) export default
defineMyPackage
.
actionHandler
(
SubscribeAction
, async ({
input
,
context
}) => {
// Now we can use the client to subscribe to the newsletter await
context
.
client
.
subscribe
(
input
.
email
);
return {
status
: 'success' as
const
};
});

Middleware can also be used before and after an action-handler. An example would be a logger middleware that logs the input and output of an action.

server/orchestr/defineWithLogger.ts
export const 
defineWithLogger
=
defineOrchestr
.
use
(async (
args
,
next
) => {
console
.
log
('Action Input',
args
.
input
,
args
.
clientEnv
);
const
response
= await
next
({});
console
.
log
('Action Output',
response
.
output
);
return
response
;
});