ZuploContext
The ZuploContext
object provides information about the current request and
runtime environment. It is passed as the second parameter to request handlers
and policies.
Code(ts)
Properties
contextId
A unique identifier for the current execution context. This is different from
requestId
and is useful for correlating multiple operations within the same
execution.
Code(ts)
custom
A mutable object that can be used to store custom data during request processing. This is useful for passing data between policies and handlers.
Code(ts)
incomingRequestProperties
Information about the incoming request such as geolocation data. This is a read-only object with the following properties:
asn
- ASN of the incoming request, for example, 395747.asOrganization
- The organization which owns the ASN of the incoming request, for example, Google Cloud.city
- City of the incoming request, for example, "Austin".continent
- Continent of the incoming request, for example, "NA".country
- The two-letter country code in the request.latitude
- Latitude of the incoming request, for example, "30.27130".longitude
- Longitude of the incoming request, for example, "-97.74260".colo
- The three-letter IATA airport code of the data center that the request hit, for example, "DFW".postalCode
- Postal code of the incoming request, for example, "78701".httpProtocol
- The HTTP protocol of the incoming request.metroCode
- Metro code (DMA) of the incoming request, for example, "635".region
- If known, the ISO 3166-2 name for the first level region associated with the IP address of the incoming request, for example, "Texas".regionCode
- If known, the ISO 3166-2 code for the first-level region associated with the IP address of the incoming request, for example, "TX".timezone
- Timezone of the incoming request, for example, "America/Chicago".
Code(ts)
log
A logger instance for debugging and monitoring. Logs appear in your log tail in the portal and in your integrated log solution (e.g. DataDog). Pre-production environments are typically set to Info log level, while production is set to Error.
As an alternative to using the log
property, you can also use console.log
,
console.error
, etc. to log messages. See the
documentation for more details.
Code(ts)
parentContext
Reference to the parent context when using invokeRoute
. This property is
undefined
for the initial request context. This is useful for detecting
sub-requests and accessing parent context data.
Code(ts)
requestId
A UUID for every request. This is used in logging and can be handy to tie events
together. The requestId
is automatically logged with every use of the logger.
The request ID is also included in the zp-rid
header of the responses from
your API for diagnostic and tracing purposes.
Code(ts)
route
A read-only pointer to the configuration for the matched route. Includes the label, path, methods supported, name of the version, and names of policies. This type is immutable - the routing table cannot be updated at runtime.
Code(ts)
Methods
addResponseSendingHook
Adds a hook that executes before a response is sent to the client. This hook can modify the response before it is sent. Multiple hooks can be added and they execute in the order they were added.
Code(ts)
See Request/Response Hooks for detailed examples and usage patterns.
addResponseSendingFinalHook
Adds a hook that executes after all other response processing is complete but
before the response is sent. Unlike addResponseSendingHook
, this hook cannot
modify the response - it is for monitoring and logging purposes only.
Code(ts)
See Request/Response Hooks for detailed examples and usage patterns.
invokeInboundPolicy
Programmatically executes an inbound policy from your policy library. This is useful for conditionally executing policies based on request attributes.
Code(ts)
Important Notes:
- The method returns either a
Request
orResponse
object - A
Response
indicates the policy wants to short-circuit and stop processing - A
Request
is typically a new request object created by the policy - The original request becomes locked after invoking a policy
Code(ts)
invokeOutboundPolicy
Programmatically executes an outbound policy from your policy library. Outbound policies process responses before they are sent to the client.
Code(ts)
invokeRoute
Invokes another route within the same API gateway. This enables internal routing
and composition of multiple routes. The invoked route runs with a new context
that has parentContext
set to the current context.
Code(ts)
waitUntil
Notifies the runtime to keep the process alive until the provided promise resolves. This is essential for asynchronous work that continues after sending a response, such as logging, analytics, or cleanup tasks.
Code(ts)
Common Patterns
Request Timing
Code(ts)
For more hook examples, see Request/Response Hooks.
Conditional Policy Execution
Code(ts)
See Also
- Request/Response Hooks - Detailed guide on using hooks
- Custom Request Handlers - Building custom handlers
- Policies Overview - Understanding policies
- Logger - Detailed logging API documentation
- Request User - Working with authenticated users
- Routing - How routing works in Zuplo