GuideMock responses

JS Hook responses

Write a function that reads the request and builds the response dynamically.

Dynamic JavaScript responses are a Pro feature.

Switch the body tab from JSON to JS Hook and write a function body. The returned value becomes the response body; async / await is supported.

JS Hook js
// Single argument: context (remote modules cannot be imported in the sandbox)
//   context.req         — { url, method, headers, query, body, params }
//   context.loadFaker() — loads Faker (en / fr / de / ru)
//   context.loadScript(url) — loads a UMD / IIFE script
const body = context.req.body || {};
if (body.coupon === 'BLACKFRIDAY') {
  return {
    code: 0,
    data: { orderId: 'A20261012', discount: 0.3, message: 'Coupon applied' },
  };
}
return {
  code: 0,
  data: { orderId: 'A20261012', discount: 0, message: 'No coupon' },
};

Available variables

VariableFieldsNotes
context.requrl, method, headers, query, body, params, gqlbody is parsed when it is JSON; params holds the path params; gql is only available for GraphQL requests
context.loadFaker()Faker instancesconst { faker, fakerEN, fakerFR, fakerDE, fakerRU } = await context.loadFaker(); faker is the English (en) instance
context.loadScript(url)Loads a UMD / IIFE scriptThe sandbox CSP only allows the extension’s own files, so a remote import() never works; scripts are cached by URL

Click Try it to run the hook against an editable req and inspect the return value without sending a request. Errors are reported with the thrown message in the panel, and the request falls through to the network.

Try it runs the hook locally — edit the fake request on the left, read the return value on the right.
Try it runs the hook locally — edit the fake request on the left, read the return value on the right.

Limits

  • No `import()`. The sandbox page’s CSP is script-src 'self'; remote, data: and blob: imports always fail. Load a UMD build with context.loadScript(url) instead.
  • ES modules are not supported by loadScript — pass an IIFE / UMD build (*.min.js).
  • No page access. Hooks cannot read the DOM, window globals or localStorage of the page; only the request context is available.
  • Execution has a timeout — a hook that blocks forever is aborted and the request passes through.
Hooks run as function bodies inside an isolated extension sandbox. Importing a rule file containing JS code shows a trust warning — only import files from sources you trust.

Stuck, or need something clarified? support@mockrew.com·Terms of Service