Programming API
Runtime Extensions
While most configuration in your Zuplo gateway is set on a per-route or
per-policy basis, there are times when behaviors need to be modified globally.
To plug into the global initialization of your gateway, create a file called
zuplo.runtime.ts
in the modules
folder with the following code.
Caution
Any error thrown in the runtimeInit
method will prevent the gateway from
starting and yield a 500 error for all requests. Be sure to add only reliable
code here and use try/catch
as appropriate to handle any recoverable
exceptions.
Note
The name of the export must be runtimeInit
and must conform to the above
function signature.
The following configurations are available.
Custom Problem (Error) Response Formatter#
Zuplo includes built-in error handling that returns errors in the format of the Problem Details for HTTP APIs proposed standard. This means that HTTP errors (or other exceptions) will return responses that look like the following.
If you want to customize this format, you can configure the
problemResponseFormat
function and return a Response
in the format of your
choice.
Hooks#
Hooks allow code to be run as part of the request/response pipeline. Hooks can
be created at the API level in zuplo.runtime.ts
as shown below or can be added
via a plugin.
Tip
All hooks can be either synchronous or asynchronous. To make your hook
asynchronous simply add the async
keyword on the function.
The following hooks can be set globally in the zuplo.runtime.ts
:
Hook: OnRequest#
Runs when a request is received, before any plugins or handlers.
Hooks: OnResponseSending#
Runs before a response is sent. Response can be modified. More details.
Hooks: OnResponseSendingFinal#
Runs before a response is sent. The response cannot be modified. More details.
Plugin and Handler Extensions#
Built-in and custom plugins and handlers can expose their own extensibility. For example, AWS Lambda handler exposes the ability to customize the event that is sent when invoking the Lambda function.
The example below shows how to use a route's custom property to set the path on the outgoing event to a custom value.