Skip to main content

Compiler

Stentor API Reference


Stentor API Reference / stentor-utils/src / Compiler

Class: Compiler

Defined in: packages/stentor-utils/src/compilers/compiler.ts:56

Compiles the templated response based on the provided request and context with some configurability.

You can provide custom macros that can be used to modify the templated response and also additional context that will be used as possible replacements.

Implements​

Constructors​

Constructor​

new Compiler(props?): Compiler

Defined in: packages/stentor-utils/src/compilers/compiler.ts:64

Parameters​

props?​

CompilerProps

Returns​

Compiler

Properties​

replaceWhenUndefined?​

readonly optional replaceWhenUndefined: boolean

Defined in: packages/stentor-utils/src/compilers/compiler.ts:58

When true, it will replace the ${foo} with undefined or null if the value for foo cannot be found. Default behavior will leave ${foo} if it does not have a value.

A caveat to this is when the foo is in a string such as "${foo}" then it will replace it with an empty string instead of undefined, thus "" instead of "undefined". The reason for this is "" is falsey and "undefined" is not. If you are doing string comparisons then this will be a problem.

Implementation of​

CompilerProps.replaceWhenUndefined


macros?​

readonly optional macros: MacroMap

Defined in: packages/stentor-utils/src/compilers/compiler.ts:60

When provided it overrides the DEFAULT_MACROS and are used when compiling the templates.

You can include the DEFAULT_MACROS if you include them:

macros: {
...DEFAULT_MACROS,
...myMacros
}

Implementation of​

CompilerProps.macros


additionalContext?​

readonly optional additionalContext: Record\<string, unknown> = {}

Defined in: packages/stentor-utils/src/compilers/compiler.ts:62

When provided, the additional context will be used when compiling the templates. This is an opportunity to inject more information beyond the provided request and context objects.

Implementation of​

CompilerProps.additionalContext

Methods​

compile()​

Call Signature​

compile(input, request, context): string

Defined in: packages/stentor-utils/src/compilers/compiler.ts:259

Compiles the provided response output or string based on the provided request and context.

It is used for compiling conditions on a response or injecting variables into responses.

Parameters​
input​

string

Either a string or a set of responses.

request​

Request

The request

context​

Context

Context object

Returns​

string

Call Signature​

compile(input, request, context): ResponseOutput

Defined in: packages/stentor-utils/src/compilers/compiler.ts:260

Compiles the provided response output or string based on the provided request and context.

It is used for compiling conditions on a response or injecting variables into responses.

Parameters​
input​

ResponseOutput

Either a string or a set of responses.

request​

Request

The request

context​

Context

Context object

Returns​

ResponseOutput