Code Generator API

Code Generator API

The manifest, request model and helpers for writing code generators.

Overview

A code generator is a JavaScript file that turns a request into text: a command, a snippet of code in some language, or a file for another tool. Every format in the request editor's Preview panel comes from one, and Relay's built-in generators use the same API as the ones you write.

A generator declares a top-level manifest object and a top-level generate function. Relay calls generate with an ExportRequest, the request with its variables, inherited authentication and settings applied, and with relay, a set of helpers for quoting, encoding and headers. The function returns the code as a string.

var manifest = { id: 'method-and-url', name: 'Method and URL', group: 'Custom' };

function generate(req, r) {
  return req.method + ' ' + r.q.shell(req.url);
}

Generators run in a sandbox: plain JavaScript with JSON, Math, Date and RegExp, the helpers and console, and no network, file system, timers or require. To create and edit generators, see Creating a custom code generator.

Topics

Declaring a generator

  • manifest: The object that names a generator and says which requests it exports.
  • generate: The function that turns a request into code.

Reading the request

  • ExportRequest: The resolved request a generator turns into code.

Helpers

  • relay: The helper functions for building, quoting and encoding generated code.

See Also