RequestRocketRequestRocketDocs
Govern

Response transforms

Run code after filters and before the caller receives the response.

A response transform is a default-exported function. It runs after filters and before the response is returned. transformType is response. Path, header, query, body, and token matches are on Predicates.

The function receives (proxyContext, proxyRequest, targetResponse). Return an object with a numeric status. Its headers and data become the response sent to the caller.

Use a filter when you only need deterministic retain or destroy operations on a successful JSON response. Use a response transform when the output requires code.

Add a response transform

Open a saved proxy, open the Govern tab, and expand Response Transforms.

Saved proxy, Govern tab, Response Transforms section expanded, with the code editor visible

Rename a response field

export default function transform(proxyContext, proxyRequest, targetResponse) {
  const { customer_name, ...rest } = targetResponse.data ?? {};

  return {
    ...targetResponse,
    data: {
      ...rest,
      customerName: customer_name,
    },
  };
}

Add a computed response header

export default function transform(proxyContext, proxyRequest, targetResponse) {
  return {
    ...targetResponse,
    headers: {
      ...targetResponse.headers,
      "x-request-path": proxyContext.path,
    },
  };
}

Test the transform

Save the transform, then send one call through the proxy. Check the returned JSON or header and use Request logs to find the call.

Runtime rules

  • The source must default-export a function.
  • Imports, require, fetch, and process are rejected.
  • The sandbox has no network or filesystem access. It has a five-second execution limit and a 32 MB memory limit.
  • The returned object must include a numeric status.
  • Only the highest-priority applicable response transform runs.

Next

On this page