Context lets you create an object to pass around your app. An easy way to give more predictable access to things like credentials, headers, services, … to the rest of your app by passing around a single object.

The context middleware for example, injects such a context on req.context that automatically includes req and res in the context.

Kind: global class

new Context(context)

Context constructor

Returns: Context - the constructed context

Param Type Description
context Object Initial contents of the context. A dictionairy of key-value pairs where the keys are strings of the names by which the value will be accessible on the context object.


const Context = require('@ambassify/expressify/context');
const ctx = new Context({ foo: 1 });

ctx.define('bar', _ctx =>;
ctx.define('baz', _ctx =>;

// They all return 1;;

context.define(key, getter)

Define intelligent getters on your context, they will have access to the current context when evaluated and their values will be cached on the context afterwards.

Kind: instance method of Context

Param Type Description
key String The name by which the value will be accessible on context
getter function A getter function that gets the context as argument and should return the value to be returned when key is accessed.

context.reload([key]) ⇒ \*

Reload the value for a specific key or all keys on context. Will clear the internal cache and re-evaluate the getter(s) to retrieve a fresh value. Handy if a key’s value is computed against another value on context that has been updated.

Kind: instance method of Context
Returns: \* - The value for key or the entire context when no key was provided.

Param Type Description
[key] String The key to reload. Leave empty to reload all keys.

context.clone() ⇒ Context

Create a copy of your context with a fresh cache and its getters intact

Kind: instance method of Context
Returns: Context - the copy of this context