Contexts
A large amount of information about operations and requests are shared across several systems. @simply-openapi/controllers passes this information in Context objects, which inherit from each other as the execution progresses from building operations to handling requests.
OperationContext
This context provides detailed information about an OpenAPI Operation.
Properties
spec
- Gets the root OpenAPI specification passed tocreateMethodHandlerFromSpec
.path
- Gets the request / operation path as it exists in the OpenAPI spec.method
- Gets the request / operation method.pathItem
- Gets the Path Item Object this operation is defined in.operation
- Gets the Operation Object this operation is defined in.securitySchemes
- Gets a record of all resolved Security Schemes defined in the OpenAPI specification, keyed by scheme name.securities
- Gets the resolved Security Requirements for this operation. This will either be those defined by the operation itself, or if the operation does not specify any, then the security schemes of the OpenAPI document will be used.parameters
- Gets an array of the resolved Parameter Objects of this operation. These may come from the operation itself, or the path item.requestBody
- Gets the resolved Request Body Object for this operation.
OperationHandlerContext
This context provides information about a specific method handler on a controller. As methods implement operations, it inherits from OperationContext.
Properties
In addition to all properties from the OperationContext, the following properties are available:
controller
- The resolved controller class instance the handler is attached to.handler
- The resolved handler function that will handle the operation.handlerArgs
- An array of argument definitions describing the purpose of each argument of the handler.
OperationMiddlewareFactoryContext
This context is used for producing middleware to handle methods. As middleware factories run on specific methods, this context inherits from OperationHandlerContext
Properties
In addition to all all properties from OperationHandlerContext, the following properties are available:
validators
- An object containing functions which will take an OpenAPI 3.1 Schema object, and return a function that attempts to validate and coerce its argument. If the data is valid, the cosersed value will be returned. If the data is invalid, an AJVValidationError
will be thrown with an array of AJV Errors in itserrors
property. For an example of this, see Schema Based Validation. The following functions are available by default. They may be modified, or new validators may be added, through thevalidatorFactories
option ofcreateRouterFromSpec
. See here for more details.createParameterValidator
- Creates a validator function that validates parameter objects. The validation will coerce types and apply defaults. Coercion is done in accordance with the AJV Type Coercion RulescreateBodyValidator
- Creates a validator function that validates parameter objects. The validation will coerce types and apply defaults. Coercion is done in accordance with the AJV Type Coercion RulescreateResponseValidator
- Creates a validator function that validates responses. No type coersion is done; types must match exactly.
OperationRequestContext
This context is used when handling a specific network request. It inherits from the OperationHandlerContext.
Properties
In addition to all properties from the OperationHandlerContext, the following properties are available:
req
- The express request object of this request.res
- The express response object of this request.
Methods
The following methods are available to simplify working with the request:
getPathParam(name)
- Gets the value of the path parameter of the given name.getHeader(name)
- Gets the value of the header of the given name. This method is case insensitive.getQuery(name)
- Gets the value of the query parameter with the given name.getCookie(name)
- Gets the value of the cookie with the given name.
The following methods are available for working with Request Data:
hasRequestData(key)
- Returns true if the request data specified by the key was ever set, or false if it was not. This will returntrue
in cases where the request data was explicitly set toundefined
.getRequestData(key)
- Returns the request data for the specified key if set, orundefined
if not.setRequestData(key, value)
- Sets the request data for the specified key.
Last updated