Custom Edgehancers
Developer preview
Integrations can define custom edgehancers, which let you run fully managed JavaScript at the edge whenever a data connector fetches data. The code executes inside Uniform’s infrastructure, so you don’t need to deploy or maintain any servers.
Use them to enrich responses, enforce business rules, or optimize performance in ways that static HTTP requests can’t.
Typical scenarios include:
- Dynamic caching operations
- Request batching
- OAuth token exchange or renewal
- Real-time data shaping or transformation
Since the logic executes on Uniform’s edge platform, it adds only minimal latency, which is typically around 1 to 2 milliseconds.
tip
Custom edgehancers have to be specifically enabled for your team. Contact Uniform if you are interested in enabling them.
How custom edgehancers work#
Custom edgehancers expose two hook points that you can implement in your integration code:
Hook | When it runs |
---|---|
preRequest | Executes before Uniform makes the HTTP request for a data resource. It receives the request object so you can modify the URL, add headers, inject authentication tokens, or even cancel the call. Uniform will still perform the fetch after this hook completes. |
request | Replaces the default fetch logic. Your hook is responsible for performing the HTTP request, returning the response, and (optionally) adding it to Uniform’s edge cache. |
Request life cycle#
This diagram illustrates how custom edgehancers fit into the request lifecycle of a data resource:
Pre-request hook#
Pre-request hooks run before Uniform makes the HTTP call. They receive a batch of data-resource definitions and must return the same number of resources in the same order.
Key characteristics#
- Purpose: Tweak the request (URL, headers, parameters, body) without executing it. Ideal for computing dynamic cache keys or adding authentication data.
- Runs every time: The hook executes for every request, even when the data resource is fresh in cache.
- No network calls: This hook must not perform HTTP requests itself.
- Variable substitution done: Variable placeholders (e.g.
foo.com/${path}
) are already resolved before the hook runs.
Typical use cases#
- Dynamically calculate URL parameters (for example, using values stored in the
custom
field from the integration’s UI). - Display different content when editing versus publishing (draft vs. live data).
Request hook#
Request hooks replace the default fetch logic. They receive a batch of data-resource definitions and must return the fetched results in the same order.
Key characteristics#
- Purpose: Perform the actual HTTP request(s) and return data, optionally writing to Uniform’s edge cache.
- Runs on cache miss: Only invoked when a data resource is stale. Valid cache entries bypass this hook.
- Stable cache key: The cache key is fixed before this hook runs. If you need dynamic keys, adjust the request in a pre-request hook instead.
Typical use cases#
- Batch multiple requests into a single API call (e.g.,
/entities?ids=1,2,3
). - Exchange OAuth access tokens before calling a protected API.
- Apply custom business logic such as data enrichment, filtering, or re-formatting non-JSON responses.
When are custom edgehancers executed?#
Custom edgehancers are executed in the following scenarios:
- A data type being tested in the Uniform UI.
- Data resources being fetched in the composition or entry editor UI.
- Data resources being fetched as part of the composition, entry, or route APIs.
- Ephemeral data resources being fetched by an integration UI using the Mesh SDK's
getDataResource
function.
Getting started#
To get started with custom integrations, follow the create an integration procedure, and then check out the section on Custom Edgehancers in the README.md
file in the codebase that it creates. The example integration contains full examples of how to build, operationalize, and test custom edgehancers.