Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.41.x.
What you'll learn
  • how to intercept the request
  • what you can do with the request at this point

Introduction
anchor

In the 5.34.0 version we added a possibility for users to intercept the Fastify request in it’s initial phase. It can be done via the HandlerOnRequestPluginexternal link.

With this plugin you can do anything with the Fastify’s Requestexternal link object, even stop the request from being processed. To find out why you can stop the request from being processed, read about it in the Fastify Lifecycleexternal link and Fastify Hooksexternal link documentation.

Creating and Registering the Plugin to Intercept the Request
anchor

Of course, as with our other plugins, you must register it in the Lambda handler plugins array:

apps/api/graphql/src/index.ts

Intercepting theOPTIONSRequest and Stopping the Request From Being Processed
anchor

By default, Webiny intercepts the OPTIONS request and stop all of the Fastify lifecycles, after the onRequest, from being ran. We do this because we have no need to run all the code we usually run for, eg., POST request. You can see the code hereexternal link.

Via the HandlerOnRequestPlugin, users can prevent our default behavior.

Why Would the User Prevent the Webiny Default Behavior?
anchor

The most simple answer would be: to output their own headers in the OPTIONS request.

How Can Users Prevent the Webiny Default Behavior onOPTIONSRequest to Be Executed?
anchor

To prevent the Webiny default behavior on OPTIONS request, user would need to add a simple plugin which returns false as the result.

Here is an example of sending a custom header myCustomHeader, and prevent the Webiny default behavior, on OPTIONS request:

Conclusion
anchor

Simply, with the HandlerOnRequestPlugin user can, in the initial stage of the Fastify Request Lifecycle:

  • intercept the request and prevent it to go further down the lifecycle line
  • modify the Webiny default OPTIONS headers output to suit their own need
  • log the request to some outside service

Users must be careful on which path are they when executing the plugin code. Webiny has two built-in paths: /cms and /graphql.