vulcain-corejs
Version:
Vulcain micro-service framework
183 lines (181 loc) • 5.42 kB
JavaScript
"use strict";
const containers_1 = require('../di/containers');
const commandFactory_1 = require('../commands/command/commandFactory');
const annotations_1 = require('../di/annotations');
(function (Pipeline) {
Pipeline[Pipeline["EventNotification"] = 0] = "EventNotification";
Pipeline[Pipeline["InProcess"] = 1] = "InProcess";
Pipeline[Pipeline["HttpRequest"] = 2] = "HttpRequest";
Pipeline[Pipeline["Test"] = 3] = "Test";
})(exports.Pipeline || (exports.Pipeline = {}));
var 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.container.injectInstance(this, annotations_1.DefaultServiceNames.RequestContext);
}
/**
* Do not use
*
* @returns
*/
getResponseHeaders() {
return this._responseHeaders;
}
/**
* Add a custom header value to the response
*
* @param {string} name
* @param {string} value
*/
addHeader(name, value) {
if (!this._responseHeaders)
this._responseHeaders = new Map();
this._responseHeaders.set(name, value);
}
/**
* 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.user && this.user.scopes) || [];
}
/**
* Check if the current user has a specific scope
*
* Rules:
* scope userScope Result
* null/?/* true
* null false
* * true
* x x true
* x-yz x-* true
*
* @param {string} scope
* @returns {number}
*/
hasScope(scope) {
if (this.user && this.user.tenant && this.user.tenant !== this.tenant)
return false;
if (!scope || scope === "?")
return true;
if (!this.user)
return false;
if (scope === "*")
return true;
const scopes = this.scopes;
if (!scopes || scopes.length == 0)
return false;
if (scopes[0] === "*")
return true;
for (let userScope of this.user.scopes) {
for (let sc of scopes) {
if (userScope === sc)
return true;
// admin-* means all scope beginning by admin-
if (userScope.endsWith("*") && sc.startsWith(userScope.substr(0, userScope.length - 1)))
return true;
}
}
return false;
}
/**
* Check if the current user is an admin
*
* @returns {boolean}
*/
isAdmin() {
return this.scopes && this.scopes.length > 0 && this.scopes[0] === "*";
}
/**
* Create a new command
* Throws an execption if the command is unknown
*
* @param {string} name Command name
* @param {string} [schema] Optional schema used to initialize the provider
* @returns {ICommand} A command
*/
getCommand(name, schema) {
return commandFactory_1.CommandFactory.get(name, this, schema);
}
/**
* Log an error
*
* @param {Error} error Error instance
* @param {string} [msg] Additional message
*
*/
error(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
*
*/
info(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
*
*/
verbose(msg, ...params) {
this._logger.verbose(this, msg, ...params);
}
}
RequestContext.TestTenant = "_test_";
RequestContext.TestUser = { id: "test", scopes: ["*"], name: "test", displayName: "test", email: "test", tenant: RequestContext.TestTenant };
exports.RequestContext = RequestContext;
//# sourceMappingURL=requestContext.js.map