FetchEvent
The FetchEvent interface of Azion Runtime: the event a fetch listener receives for each request, its properties, and its methods.
FetchEvent is the event that Azion Runtime passes to a listener registered with addEventListener for the fetch type. The listener defines what code runs when a request arrives, and the event carries the request data to it. The listener reads event.request to route, filter, or transform traffic. It then calls event.respondWith() with a response it builds, or with the response of a fetch() call to an origin. For more information, refer to FetchEvent on MDN Web Docs.
A handler written as export default main(event) receives the same FetchEvent, and azion build marks that shape as deprecated. For the recommended handler shape, refer to Handlers.
Constructor
Azion Runtime creates one FetchEvent for each request and passes it to the listener:
Your code does not construct the event. In a deployed function, new FetchEvent('fetch') throws TypeError: Illegal constructor.
Properties
| Property | Type | Description |
|---|---|---|
event.args | Object | Args of the function instance, one property per key. An instance without Args passes {}. For more information, refer to Args. |
event.console | Object | Console of the event, with methods such as log(). |
event.request | Request | HTTP request that the function received. In a deployed function, event.request.metadata holds the request metadata. |
event.type | String | Type of the event: fetch. |
Methods
| Method | Description |
|---|---|
event.respondWith(response) | Sets the response the function sends to the client. response is a Response or a promise that resolves to one. |
event.waitUntil() | Present on the event. The fetch(request, env, ctx) handler exposes ctx.waitUntil(); for more information, refer to Handlers. |
Example
This listener passes the event to a handleRequest function, and event.respondWith() sends the response that the function returns: