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.
#
MetadataMetadata is specialized class contains information about current request metadata, such as controller object graph, action object graph, action parameters etc. It has properties below
actionParams
current action parameters, contains information about action parameters values used to execute the action.controller
current controller object graph, contains information about controller name, decorators, methods, constructor etc.action
current action object graph, contains information about action name, parameters, decorators etc.current
metadata information where the appropriate decorator applied, can be Class metadata, Method metadata, Property metadata or Parameter metadata. For global middleware thecurrent
property will beundefined
.
#
Access The MetadataMetadata object accessible through all custom extension, it accessible by metadata
property.
#
MiddlewareInvocation object has metadata
property, you can access it like below
Or accessible from class style middleware
#
Custom AuthorizerMetadata can be accessed from AuthorizationContext
class like below
Or accessible from class style authorizer
#
Custom ValidatorMetadata can be accessed from ValidatorContext
class like below
Or accessible from class style validator
#
Custom BinderMetadata accessible from the second parameter of custom binder
#
Controller and Action Object GraphMetadata 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 ParamsAction 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