reserve

Custom handler

🔝 REserve documentation

A custom handler is an object with predefined properties.

Optional properties

method

A comma separated string or an array of HTTP verbs that indicates which methods are supported by the handler.

[!IMPORTANT] When REserve validates a mapping, it matches the mapping’s method with the handler’s list of supported methods. In case of mismatch, an error is raised.

When no value is provided, REserve considers that all verbs are supported by the handler.

schema

A mapping validation schema.

The schema syntax is designed to be short and self-explanatory. It consists in a dictionary mapping a property name to its specification.

The specification can be either:

For instance, the following schema specification defines:

{
  "schema": {
    "custom": ["function", "string"],
    "watch": {
      "type": "boolean",
      "defaultValue": false
    }
  }
}

Example of a schema definition for two properties

If provided, the schema is applied on the mapping before the validate function. If you need better validation, implement the validate function.

validate

An asynchronous method that validates mapping definition.

[!WARNING] The mapping will also contain REserve’s mapping properties such as cwd, match… The function should focus only on handler’s specific settings.

The method is called with two parameters:

If validation fails, the method should throw an error.

Required properties

redirect

A method that may return a Promise and that will be called with an object containing :

Capturing groups and interpolation

By default, the handler prefix is interpolated. Identified placeholders are substituted with values coming from the capturing groups of the matching regular expression.

Several syntaxes are accepted for placeholders :

It is possible to reuse the mechanism by importing the interpolate helper.