exframe-context
v1.3.7
Published
Exframe context
Downloads
4,498
Readme
Exframe Context Module
installation
npm install -s exframe-context
usage
const { Context, register } = require('exframe-context');
const start = async () => {
const { Context, use } = require('exframe-context');
const { someMiddleware } = require('some-package');
use((context) => {
context.test = 'abc';
});
use(someMiddleware);
const context = new Context({
accessToken: 'SYSTEMUSER',
requestId: '123'
});
await context.initialize();
// context.test is now populated and someMiddleware has executed and whatever it
// populates has been added to the context, for example the user object from
// exframe-security or the log from exframe-logger
}
const start2 = async () => {
const exframeContext = require('exframe-context');
// identical to new Context()
const context = exframeContext.createContext();
exframeContext.use(async (ctx) => {
ctx.myStuff = await getSomeData()
});
exframeContext.use(() => ({
accessToken: 'SYSTEMUSER',
requestId: '123'
}));
console.log(context.requestId); // prints undefined
console.log(context.myStuff); // prints undefined;
await context.initialize();
console.log(context.requestId); // prints '123'
console.log(context.myStuff); // prints the result of getSomeData;
}
class Context
constructor(options?) => Context
async initialize() => Promise<void>
Initializes all registered middleware
use (options: function || object) => boolean;
Registers a middleware for usage in exframe context. If callback registration was successful, true
is returned.
type options
|field|type|required|default|description|
|-----|----|--------|-------|-----------|
|callback
|function|true|null|Async method to execute when context.initialize() is called|
|name
|string|false|function name|name to register the middleware under (used to create middleware uniqueness)|
|priority
|integer|false|9999|used to determine what order to initialize middleware in|
|force
|boolean|false|false|in the event of name collision, if this is true override the existing middleware with this name|
|optionss
|object|false|{}|An optionable customizeable object with options can be set at registration for the middleware to use|