//
// $Id: cnContext.js 8638 2025-09-09 15:26:51Z jochen.hanff $
//
// /**
// * @fileoverview Infohub context class
// * Central holder for the current user/session context and service endpoints.
// */
// import { clearCache as __clearCache } from "./cache.internal.js";
/**
@category COLNEO infohub
@classdesc Class to configure access to COLNEO infohub. <br>
Keeps track of user identity, project/scope, and base URLs for services.
*/
export class cnContext {
/**
* Supported web services.
*
* HUB | CONFIG | USR | IDP | IAM | RENDER | RPC | TYPES
*
* @example
* ...
* const url = `${ctx.getServiceUrl(cnContext.SERVICE.IDP)}/users/${userid}`;
* @readonly
*
*/
static SERVICE = {
HUB: "hub",
CONFIG: "config",
ADMIN: "admin",
USR: "usr",
IDP: "idp",
IAM: "iam",
RENDER: "render",
RPC: "rpc",
TYPES: "types"
};
static #_servicesnames_ = [
this.SERVICE.HUB,
this.SERVICE.CONFIG,
this.SERVICE.ADMIN,
this.SERVICE.USR,
this.SERVICE.IDP,
this.SERVICE.IAM,
this.SERVICE.RENDER,
this.SERVICE.RPC,
this.SERVICE.TYPES
];
/**
Get list of names of supported web services.
@example
let names = Infohub.getServiceNames()
@returns {string[]} Array of names as string
@since 08.2025, jh
*/
static getServiceNames() {
return [...this.#_servicesnames_];
}
/**
*
* Create an COLNEO infohub context with service endpoints set to null and empty user state.
* @example
*
* // create context
* const ctx = new cnContext();
*
* ctx.setServiceUrls({
* hub : "https://app.colneo.services/hub",
* admin : "https://app.colneo.services/admin",
* usr : "https://app.colneo.services/usr",
* idp : "https://idp.colneo.services/service"
* });
*
* ctx.setUserAndToken( "user@example.com" , "... jwt_token ...");
* ctx.setScopeAndProject( "cn_c00", "proj_123");
*
* ctx.getScope(); // => "project"
* ctx.getProjectShortId(); // => "proj_123"
* ctx.getUser(); // => "user@example.com"
* ctx.getToken(); // => "jwt_token"
*
* console.log(cntx.getUser()); // => "user@example.com"
*
* @since 08.2025, jh
*
*/
constructor() {
this._userid = null;
this._token = null;
this._serviceurl = {}
for( const srv of cnContext.getServiceNames() ) {
this._serviceurl[srv] = null;
}
this._scope = null;
this._project = null;
}
/**
* Print current context to the console (for debugging).
* @returns {void}
*/
dump() {
console.log("cnContext: {")
console.log(` USER: ${this._userid}`);
console.log(` TOKEN: ${this._token}`);
console.log(` SCOPE: ${this._scope}`);
console.log(` PROJECT: ${this._project}`);
for (const srv of cnContext.getServiceNames()) {
console.log(` ${srv} = ${this.getServiceUrl(srv)} `)
}
console.log("} cnContext")
}
/**
* Reset scope and project to null.
* @returns {void}
*/
resetProjectAndScope() {
this._scope = null;
this._project = null;
}
/**
*
* Set current user id and authentication token.
*
* @param {string|null} userid
* @param {string|null} token
*
* @returns {void}
*
*/
setUserAndToken(userid, token) {
this._userid = userid;
this._token = token;
}
/**
*
* Set scope and project short id.
*
* @param {string} scope
* @param {string} project_sid
*
* @returns {void}
*
*/
setScopeAndProjectShortId(scope, project_sid) {
this._project = project_sid;
this._scope = scope;
}
/**
*
* Get current user id.
*
* @returns {string} User ID (Email)
*
*/
getUserId() {
return this._userid;
}
/**
*
* Get current access token.
*
* @returns {string}
*
*/
getToken() {
return this._token;
}
/**
* Get current scope.
* @returns {string}
*/
getScope() {
return this._scope;
}
/**
* Set current scope.
* @param {string} scope
* @returns {void}
*/
setScope(scope) {
this._scope = scope;
}
/**
*
* Set multiple service URLs at once.
*
* @example
*
* const ctx = new cnContext();
*
* ctx.setServiceUrls({
* hub : "https://app.colneo.services/hub",
* admin : "https://app.colneo.services/admin",
* usr : "https://app.colneo.services/usr"
* });
*
* console.log(ctx.getServiceUrl("hub")); // https://app.colneo.services/hub
* console.log(ctx.getServiceUrl("config")); // null (not set yet)
*
* @param {Record<string, string>} cfg - Object mapping service names to URLs
*
* @throws {TypeError} If cfg is not a non-null object
*/
setServiceUrls(cfg) {
if (typeof cfg !== "object" || cfg === null) {
throw new TypeError("cfg must be a non-null object");
}
for (const [srv, url] of Object.entries(cfg)) {
if (srv in this._serviceurl) {
this._serviceurl[srv] = url;
}
else {
console.warn(`Unknown service name: ${srv}`);
}
}
}
/**
*
* Get URL of supported webservice.
*
* @param {string} servicename
*
* @returns {string | null} Url of service, null if not found
*
* @since 08.2025, jh
*/
getServiceUrl(servicename) {
if (servicename in this._serviceurl) {
return this._serviceurl[servicename];
}
return null;
}
/**
* Set current project short id.
* @param {string} sid
* @returns {void}
*/
setProjectShortId(sid) {
this._project = sid;
}
/**
*
* @returns Project ShortId
*
* @since
* 1.0, 09.2025, jh
*/
getProjectShortId() {
return this._project;
}
// /**
// * Immediately clear all caches (eager clear).
// * @returns {void}
// */
// clearCache() {
// __clearCache();
// }
}
Source