vulcain-corejs
Version:
Vulcain micro-service framework
274 lines (273 loc) • 6.29 kB
TypeScript
import { IContainer } from '../di/resolvers';
import { IAuthorizationPolicy } from './policy/defaultAuthorizationPolicy';
import { ICommand } from '../commands/command/abstractCommand';
export declare enum Pipeline {
EventNotification = 0,
InProcess = 1,
HttpRequest = 2,
Test = 3,
}
/**
* User context
*
* @export
* @interface UserContext
*/
export interface UserContext {
/**
* User id
*
* @type {string}
* @memberOf UserContext
*/
id: string;
/**
* User display name
*
* @type {string}
* @memberOf UserContext
*/
displayName?: string;
/**
* User email
*
* @type {string}
* @memberOf UserContext
*/
email?: string;
/**
* User name
*
* @type {string}
* @memberOf UserContext
*/
name: string;
/**
* User scopes
*
* @type {Array<string>}
* @memberOf UserContext
*/
scopes: Array<string>;
/**
* Don't use directly - Used requestContext.tenant instead
*
* @type {string}
* @memberOf UserContext
*/
tenant: string;
}
/**
* Logger
*
* @export
* @interface Logger
*/
export interface Logger {
/**
* Log an error
*
* @param {Error} error Error instance
* @param {string} [msg] Additional message
*
*/
error(ctx: RequestContext, error: Error, msg?: string): any;
/**
* Log a message info
*
* @param {string} msg Message format (can include %s, %j ...)
* @param {...Array<string>} params Message parameters
*
*/
info(ctx: RequestContext, msg: string, ...params: Array<any>): any;
/**
* Log a verbose message. Verbose messages 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(ctx: RequestContext, msg: string, ...params: Array<any>): any;
}
/**
* Internal use
*
* @export
* @interface ICustomEvent
*/
export interface ICustomEvent {
action: string;
schema?: string;
params?: any;
}
/**
* Request context
*
* @export
* @class RequestContext
*/
export declare class RequestContext {
pipeline: Pipeline;
static TestTenant: string;
static TestUser: {
id: string;
scopes: string[];
name: string;
displayName: string;
email: string;
tenant: string;
};
startTime: [number, number];
private _customEvents;
/**
* Request correlation id
*
* @type {string}
*/
correlationId: string;
/**
* Request correlation path
*
* @type {string}
*/
correlationPath: string;
_scopePolicy: IAuthorizationPolicy;
/**
* Current user or null
*
* @type {UserContext}
*/
user: UserContext;
private _cache;
private _logger;
/**
* Scoped container
*
* @type {IContainer}
*/
container: IContainer;
/**
* Headers for the current request
*
* @type {{ [name: string]: string }}
*/
headers: {
[name: string]: string;
};
/**
* Current tenant
*
* @type {string}
*/
tenant: string;
/**
* Current locale
*
* @type {string}
* @memberOf RequestContext
*/
locale: string;
/**
* Request host name
*
* @type {string}
* @memberOf RequestContext
*/
hostName: string;
/**
* Send custom event from current service
*
* @param {string} action action event
* @param {*} [params] action parameters
* @param {string} [schema] optional schema
*/
sendCustomEvent(action: string, params?: any, schema?: string): void;
/**
* Get request cache (Cache is only valid during the request lifetime)
*
* @readonly
*/
readonly cache: Map<string, any>;
/**
* Propagated bearer token
*
* @type {string}
* @memberOf RequestContext
*/
bearer: string;
/**
* Do not use directly
* Creates an instance of RequestContext.
*
* @param {IContainer} container
* @param {Pipeline} pipeline
*/
constructor(container: IContainer, pipeline: Pipeline);
dispose(): void;
/**
* Create a request context for testing
*
* @static
* @param {IContainer} [container]
* @param {UserContext} [user]
* @returns
*/
static createMock(container?: IContainer, user?: UserContext): RequestContext;
/**
* Get user scopes
*
* @readonly
* @type {Array<string>}
*/
readonly scopes: Array<string>;
hasScope(handlerScope: string): boolean;
/**
* Check if the current user is an admin
*
* @returns {boolean}
*/
isAdmin(): boolean;
/**
* 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: string, schema?: string): Promise<ICommand>;
/**
* Log an error
*
* @param {Error} error Error instance
* @param {string} [msg] Additional message
*
*/
logError(error: Error, msg?: string): void;
/**
* Log a message info
*
* @param {string} msg Message format (can include %s, %j ...)
* @param {...Array<string>} params Message parameters
*
*/
logInfo(msg: string, ...params: Array<any>): void;
/**
* 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: string, ...params: Array<any>): void;
/**
* Public path used to exposed this service - Set only for public service
*
* @readonly
*
* @memberOf RequestContext
*/
readonly publicPath: string;
}