You're reading Mocko v1 (legacy) documentation. Mocko v2 is the current version. View the v2 docs.

Mocko Helpers

In addition to handlebars-helpers, Mocko provides helpers that can modify your response directly.

setStatus

Sets the response status dynamically or conditionally.

{{#is request.params.id '1'}}
{ "id": 1, "name": "George" }
{{else is request.params.id '2'}}
{ "id": 2, "name": "Alice" }
{{else}}
{{setStatus 404}}
{ "error": "Not found error", "message": "Cat not found" }
{{/is}}

setHeader

Sets response headers dynamically or conditionally.

{{#gt (toInt request.params.id) 10}}
{{setStatus 303}}
{{setHeader 'Location' (append '/purchases/' request.params.id)}}
{{else}}
{
"id": {{request.params.id}},
"progress": {{random 0 100}}
}
{{/gt}}

proxy

Proxies the request to the real API behind Mocko. Configure the proxy URL with PROXY_BASE-URI in the env config, or with the --url / -u CLI flag.

{{#is request.query.userId 1}}
[]
{{else}}
{{proxy}}
{{/is}}

You can also override the proxy URI for a specific mock:

{{#is request.params.id '1'}}
{{proxy 'http://localhost:8082'}}
{{else}}
{{proxy 'http://localhost:8081'}}
{{/is}}

append

Concatenates parameters into a string.

{{append 'users:' request.params.id ':name'}}

uuid

Generates a UUID v4. No parameters required.

substring

Returns a substring. Same API as JavaScript's substring.

{{substring 'Lorem ipsum' 0 4}}
{{! Produces 'Lore' }}

setFlag, getFlag, delFlag, hasFlag

See Persistence for documentation.

get, set

See Variables for documentation.