Metaprogramming
Key feature that make Plumier different than other TypeScript backend framework is its ability to perform metaprogramming. Plumier has its own reflection (introspection) library named @plumier/reflect, that makes metaprogramming possible.
Metaprogramming in Plumier increases reusability of custom extension, because it can access information about current controller and action handles the request and the location of custom extension applied.
Metadata#
Metadata is specialized class contains information about current request metadata, such as controller object graph, action object graph, action parameters etc. It has properties below
actionParamscurrent action parameters, contains information about action parameters values used to execute the action.controllercurrent controller object graph, contains information about controller name, decorators, methods, constructor etc.actioncurrent action object graph, contains information about action name, parameters, decorators etc.currentmetadata information where the appropriate decorator applied, can be Class metadata, Method metadata, Property metadata or Parameter metadata. For global middleware thecurrentproperty will beundefined.
Access The Metadata#
Metadata object accessible through all custom extension, it accessible by metadata property.
Middleware#
Invocation object has metadata property, you can access it like below
Or accessible from class style middleware
Custom Authorizer#
Metadata can be accessed from AuthorizationContext class like below
Or accessible from class style authorizer
Custom Validator#
Metadata can be accessed from ValidatorContext class like below
Or accessible from class style validator
Custom Binder#
Metadata accessible from the second parameter of custom binder
Controller and Action Object Graph#
Metadata object contains information of current controller and action handle the request, Access them like below
Access the parameter names of the action
Access decorator applied to the Controller or Action
Action Params#
Action parameter useful to get information about current action parameters and their values. For example with controller below
With the request GET /animal/12345?breed=canine you can access the values of the parameter from metadata object like below
You can access the parameter by its index like below
To increase your app robustness it is necessary to check if current action handles the request has specific parameter. You can do that like below
