vulcain-corejs
Version:
Vulcain micro-service framework
139 lines (137 loc) • 4.19 kB
JavaScript
"use strict";
const containers_1 = require("../di/containers");
const commandFactory_1 = require("../commands/command/commandFactory");
const annotations_1 = require("../di/annotations");
var Pipeline;
(function (Pipeline) {
Pipeline[Pipeline["EventNotification"] = 0] = "EventNotification";
Pipeline[Pipeline["InProcess"] = 1] = "InProcess";
Pipeline[Pipeline["HttpRequest"] = 2] = "HttpRequest";
Pipeline[Pipeline["Test"] = 3] = "Test";
})(Pipeline = exports.Pipeline || (exports.Pipeline = {}));
/**
* Request context
*
* @export
* @class RequestContext
*/
class RequestContext {
/**
* Do not use directly
* Creates an instance of RequestContext.
*
* @param {IContainer} container
* @param {Pipeline} pipeline
*/
constructor(container, pipeline) {
this.pipeline = pipeline;
this._logger = container.get(annotations_1.DefaultServiceNames.Logger);
this.container = new containers_1.Container(container, this);
this._scopePolicy = container.get(annotations_1.DefaultServiceNames.AuthorizationPolicy);
}
/**
* Get request cache (Cache is only valid during the request lifetime)
*
* @readonly
*/
get cache() {
if (!this._cache) {
this._cache = new Map();
}
return this._cache;
}
dispose() {
this.container.dispose();
}
/**
* Create a request context for testing
*
* @static
* @param {IContainer} [container]
* @param {UserContext} [user]
* @returns
*/
static createMock(container, user) {
let ctx = new RequestContext(container || new containers_1.Container(), Pipeline.Test);
ctx.user = user || RequestContext.TestUser;
ctx.user.tenant = ctx.tenant = RequestContext.TestTenant;
return ctx;
}
/**
* Get user scopes
*
* @readonly
* @type {Array<string>}
*/
get scopes() {
return this._scopePolicy.scopes(this);
}
hasScope(handlerScope) {
this.logVerbose(`Check scopes [${this.scopes}] for user ${this.user && this.user.name} to handler scope ${handlerScope}`);
return this._scopePolicy.hasScope(this, handlerScope);
}
/**
* Check if the current user is an admin
*
* @returns {boolean}
*/
isAdmin() {
return this._scopePolicy.isAdmin(this);
}
/**
* Create a new command
* Throws an exception if the command is unknown
*
* @param {string} name Command name
* @param {string} [schema] Optional schema used to initialize the provider
* @returns {ICommand} A command
*/
getCommandAsync(name, schema) {
return commandFactory_1.CommandFactory.getAsync(name, this, schema);
}
/**
* Log an error
*
* @param {Error} error Error instance
* @param {string} [msg] Additional message
*
*/
logError(error, msg) {
this._logger.error(this, error, msg);
}
/**
* Log a message info
*
* @param {string} msg Message format (can include %s, %j ...)
* @param {...Array<string>} params Message parameters
*
*/
logInfo(msg, ...params) {
this._logger.info(this, msg, ...params);
}
/**
* Log a verbose message. Verbose message are enable by service configuration property : enableVerboseLog
*
* @param {any} requestContext Current requestContext
* @param {string} msg Message format (can include %s, %j ...)
* @param {...Array<string>} params Message parameters
*
*/
logVerbose(msg, ...params) {
this._logger.verbose(this, msg, ...params);
}
/**
* Public path used to exposed this service - Set only for public service
*
* @readonly
*
* @memberOf RequestContext
*/
get publicPath() {
return this.headers["X-VULCAIN-PUBLICPATH"];
}
}
RequestContext.TestTenant = "TesT";
RequestContext.TestUser = { id: "test", scopes: ["*"], name: "test", displayName: "test", email: "test", tenant: RequestContext.TestTenant };
exports.RequestContext = RequestContext;
//# sourceMappingURL=requestContext.js.map