Skip to main content

Module template

Module template 

Source
Expand description

Minimal template rendering for mockd responses.

Templates are expressions of the form {{ namespace.key }} embedded in string values inside a JSON response body. The supported namespaces are:

  • path.<name> — a captured path parameter, e.g. {{path.id}}.
  • query.<name> — a query parameter, e.g. {{query.role}}.
  • header.<name> — a request header, e.g. {{header.x-tenant-id}}.
  • body.<json.path> — a value extracted from the JSON request body using dot navigation through objects and array indices, e.g. {{body.user.name}} or {{body.items.0.id}}.

In addition, the following helper functions are available (called without a namespace):

  • {{uuid}} — a fresh UUIDv4 string, e.g. 550e8400-e29b-41d4-a716-446655440000.
  • {{now}} — the current UTC time as an ISO 8601 string, e.g. 2024-01-15T12:34:56Z.
  • {{randomInt(min,max)}} — a random integer in the inclusive range [min, max], e.g. {{randomInt(1,100)}}. Useful for generating IDs.

§Interpolation vs. coercion

When a string value consists exactly of a single expression, the result is coerced into the most appropriate JSON type:

  • numeric strings become JSON numbers ({{path.id}} with id = 4242),
  • true / false become booleans,
  • null becomes JSON null,
  • anything else stays a string.

When an expression is part of a larger string, it is interpolated as text:

"user-{{path.id}}" with id = 42"user-42".

Unknown or missing variables resolve to an empty string during interpolation, and to JSON null when used as a whole-value coercion.

Note: helper functions such as {{uuid}} produce a fresh value on every render and therefore never coerce to null.

Structs§

TemplateContext
Lookup tables used while rendering templates.

Functions§

render
Render every string value inside value, returning a new Value.