Comparing version 4.1.3 to 4.2.0
125
api/Api.js
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -33,21 +42,23 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
let cachedAuthHeaders = {}; | ||
async function getAuthHeaders(useCachedHeaders = true) { | ||
if (process.env.NODE_ENV === 'test') { | ||
return {}; | ||
} | ||
const timestamp = Date.now(); | ||
if (useCachedHeaders) { | ||
if (timestamp - lastAuthHeaders < 300000) { | ||
return cachedAuthHeaders; | ||
function getAuthHeaders(useCachedHeaders = true) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (process.env.NODE_ENV === 'test') { | ||
return {}; | ||
} | ||
} | ||
lastAuthHeaders = timestamp; | ||
const auth = system_1.default.getLibModule(system_1.LibModule.AUTH); | ||
const organization = await auth.getOrganization(); | ||
cachedAuthHeaders = { | ||
[Consts_1.X_ORGANIZATION]: organization.module_id, | ||
[Consts_1.X_CLIENT_TZ]: Intl.DateTimeFormat().resolvedOptions().timeZone, | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
}; | ||
return cachedAuthHeaders; | ||
const timestamp = Date.now(); | ||
if (useCachedHeaders) { | ||
if (timestamp - lastAuthHeaders < 300000) { | ||
return cachedAuthHeaders; | ||
} | ||
} | ||
lastAuthHeaders = timestamp; | ||
const auth = system_1.default.getLibModule(system_1.LibModule.AUTH); | ||
const organization = yield auth.getOrganization(); | ||
cachedAuthHeaders = { | ||
[Consts_1.X_ORGANIZATION]: organization.module_id, | ||
[Consts_1.X_CLIENT_TZ]: Intl.DateTimeFormat().resolvedOptions().timeZone, | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
}; | ||
return cachedAuthHeaders; | ||
}); | ||
} | ||
@@ -75,37 +86,39 @@ function rejectedBecauseAuth(e) { | ||
} | ||
async function request(method, uri, data, headers, canRetryAuth = true) { | ||
let authFailed = false; | ||
const requestSettings = { | ||
method, | ||
withCredentials: true, | ||
}; | ||
if (data) { | ||
if (method === Consts_1.GET || method === Consts_1.DELETE) { | ||
requestSettings.params = data; | ||
function request(method, uri, data, headers, canRetryAuth = true) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let authFailed = false; | ||
const requestSettings = { | ||
method, | ||
withCredentials: true, | ||
}; | ||
if (data) { | ||
if (method === Consts_1.GET || method === Consts_1.DELETE) { | ||
requestSettings.params = data; | ||
} | ||
else { | ||
requestSettings.data = data; | ||
} | ||
} | ||
else { | ||
requestSettings.data = data; | ||
const authHeaders = yield getAuthHeaders(canRetryAuth); | ||
requestSettings.headers = Object.assign(Object.assign(Object.assign({}, authHeaders), system_1.default.getHttpHeaders()), headers); | ||
try { | ||
const url = `${system_1.default.nexus.getUrl()}/${sanitizeUri(uri)}`; | ||
const response = (yield axios_1.default(url, requestSettings)); | ||
if (!response || !response.data) { | ||
return null; | ||
} | ||
return response.data; | ||
} | ||
} | ||
const authHeaders = await getAuthHeaders(canRetryAuth); | ||
requestSettings.headers = { ...authHeaders, ...system_1.default.getHttpHeaders(), ...headers }; | ||
try { | ||
const url = `${system_1.default.nexus.getUrl()}/${sanitizeUri(uri)}`; | ||
const response = (await axios_1.default(url, requestSettings)); | ||
if (!response || !response.data) { | ||
return null; | ||
catch (e) { | ||
authFailed = rejectedBecauseAuth(e); | ||
} | ||
return response.data; | ||
} | ||
catch (e) { | ||
authFailed = rejectedBecauseAuth(e); | ||
} | ||
if (authFailed && canRetryAuth) { | ||
// TODO: consider setting an amount of times to retry auth | ||
const user = await system_1.default.nexus.reconnect(); | ||
if (user) { | ||
return request(method, uri, data, headers, false); | ||
if (authFailed && canRetryAuth) { | ||
// TODO: consider setting an amount of times to retry auth | ||
const user = yield system_1.default.nexus.reconnect(); | ||
if (user) { | ||
return request(method, uri, data, headers, false); | ||
} | ||
} | ||
} | ||
return null; | ||
return null; | ||
}); | ||
} | ||
@@ -124,10 +137,12 @@ function get(uri, params, headers) { | ||
} | ||
async function search(domain, module, filters, options) { | ||
common_1.validateDomainAndModule(domain, module); | ||
const domainUri = common_1.domainToUri(domain); | ||
const moduleUri = common_1.moduleToUri(module); | ||
const response = await request(Consts_1.PUT, `${domainUri}/${moduleUri}/search`, { ...options, filters }, options?.headers); | ||
return response ? response : []; | ||
function search(domain, module, filters, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
common_1.validateDomainAndModule(domain, module); | ||
const domainUri = common_1.domainToUri(domain); | ||
const moduleUri = common_1.moduleToUri(module); | ||
const response = yield request(Consts_1.PUT, `${domainUri}/${moduleUri}/search`, Object.assign(Object.assign({}, options), { filters }), options === null || options === void 0 ? void 0 : options.headers); | ||
return response ? response : []; | ||
}); | ||
} | ||
const api = Object.freeze(Utils_1.makeCallable(request, { get, delete: del, post, put, search })); | ||
exports.default = system_1.default.sealModule(api); |
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -26,33 +35,43 @@ const bf_types_1 = require("bf-types"); | ||
const system_1 = __importStar(require("../system")); | ||
async function getUser() { | ||
return system_1.default.nexus.getUser(); | ||
function getUser() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return system_1.default.nexus.getUser(); | ||
}); | ||
} | ||
async function getUserDoc() { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const userId = system_1.default.nexus.getUser().sub; | ||
const user = await api.get(`core/user/entity/${userId}`); | ||
if (!user) { | ||
throw new Error('Failed to retrieve the document for the authenticated user.'); | ||
} | ||
return user; | ||
function getUserDoc() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const userId = system_1.default.nexus.getUser().sub; | ||
const user = yield api.get(`core/user/entity/${userId}`); | ||
if (!user) { | ||
throw new Error('Failed to retrieve the document for the authenticated user.'); | ||
} | ||
return user; | ||
}); | ||
} | ||
async function getUserDocs() { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const users = await api.get('users'); | ||
if (!users || !Array.isArray(users)) { | ||
return []; | ||
} | ||
return users; | ||
function getUserDocs() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const users = yield api.get('users'); | ||
if (!users || !Array.isArray(users)) { | ||
return []; | ||
} | ||
return users; | ||
}); | ||
} | ||
async function getOrganization() { | ||
return common_1.moduleLink(bf_types_1.CORE_MODULES.ORGANIZATION, system_1.default.nexus.getUser().organization[0]); | ||
function getOrganization() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return common_1.moduleLink(bf_types_1.CORE_MODULES.ORGANIZATION, system_1.default.nexus.getUser().organization[0]); | ||
}); | ||
} | ||
async function getOrganizationDoc() { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const organizationId = system_1.default.nexus.getUser().organization[0]; | ||
const organization = await api.get(`core/organization/entity/${organizationId}`); | ||
if (!organization) { | ||
throw new Error("failed to retrieve the document for the authenticated user's organization"); | ||
} | ||
return organization; | ||
function getOrganizationDoc() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const api = system_1.default.getLibModule(system_1.LibModule.API); | ||
const organizationId = system_1.default.nexus.getUser().organization[0]; | ||
const organization = yield api.get(`core/organization/entity/${organizationId}`); | ||
if (!organization) { | ||
throw new Error("failed to retrieve the document for the authenticated user's organization"); | ||
} | ||
return organization; | ||
}); | ||
} | ||
@@ -59,0 +78,0 @@ function logOut() { |
@@ -58,6 +58,3 @@ "use strict"; | ||
function withType(type) { | ||
return (value) => ({ | ||
type, | ||
...value, | ||
}); | ||
return (value) => (Object.assign({ type }, value)); | ||
} |
@@ -27,7 +27,22 @@ "use strict"; | ||
function removeField(field) { | ||
return (obj) => Object.fromEntries(Object.keys(obj) | ||
.filter(notEquals(field)) | ||
.map((field) => [field, obj[field]])); | ||
return (obj) => { | ||
const good = {}; | ||
const objAny = obj; | ||
for (const key of Object.keys(obj)) { | ||
if (field !== key) { | ||
good[key] = objAny[key]; | ||
} | ||
} | ||
return good; | ||
}; | ||
} | ||
exports.removeField = removeField; | ||
/* export function removeField<K extends PropertyKey>(field: K) { | ||
return <T extends Partial<Record<K, unknown>>>(obj: T): Omit<T, K> => | ||
Object.fromEntries( | ||
Object.keys(obj) | ||
.filter(notEquals<PropertyKey>(field)) | ||
.map((field) => [field, obj[field as keyof typeof obj]]), | ||
) as Omit<T, K>; | ||
}*/ | ||
exports.removeId = removeField('id'); | ||
@@ -40,3 +55,3 @@ /** | ||
function addField(field) { | ||
return (value) => (obj) => ({ ...obj, [field]: value }); | ||
return (value) => (obj) => (Object.assign(Object.assign({}, obj), { [field]: value })); | ||
} | ||
@@ -50,3 +65,13 @@ exports.addField = addField; | ||
function getFields(...fields) { | ||
return (obj) => Object.fromEntries(fields.map((field) => [field, obj[field]])); | ||
return (obj) => { | ||
const good2 = {}; | ||
const obj2 = obj; | ||
const fiedls2 = fields; | ||
for (const key of Object.keys(obj2)) { | ||
if (fiedls2.includes(key)) { | ||
good2[key] = obj2[key]; | ||
} | ||
} | ||
return good2; | ||
}; | ||
} | ||
@@ -53,0 +78,0 @@ exports.getFields = getFields; |
41
index.js
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,19 +36,21 @@ exports.ClientType = void 0; | ||
const system_1 = __importStar(require("./system")); | ||
async function bflib(settings) { | ||
await system_1.default.init(settings); | ||
return Object.freeze({ | ||
get api() { | ||
return system_1.default.getLibModule(system_1.LibModule.API); | ||
}, | ||
get auth() { | ||
return system_1.default.getLibModule(system_1.LibModule.AUTH); | ||
}, | ||
get module() { | ||
return system_1.default.getLibModule(system_1.LibModule.MODULE); | ||
}, | ||
get multitool() { | ||
return system_1.default.getLibModule(system_1.LibModule.MULTITOOL); | ||
}, | ||
function bflib(settings) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield system_1.default.init(settings); | ||
return Object.freeze({ | ||
get api() { | ||
return system_1.default.getLibModule(system_1.LibModule.API); | ||
}, | ||
get auth() { | ||
return system_1.default.getLibModule(system_1.LibModule.AUTH); | ||
}, | ||
get module() { | ||
return system_1.default.getLibModule(system_1.LibModule.MODULE); | ||
}, | ||
get multitool() { | ||
return system_1.default.getLibModule(system_1.LibModule.MULTITOOL); | ||
}, | ||
}); | ||
}); | ||
} | ||
exports.default = bflib; |
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -45,6 +54,8 @@ const common_1 = require("../common"); | ||
} | ||
async function baseBulkCreate(api, domain, module, data, headers) { | ||
const bulkCreateUri = entityUri(domain, module, 'bulkCreate'); | ||
const response = await api.post(bulkCreateUri, data, headers); | ||
return response ? response : []; | ||
function baseBulkCreate(api, domain, module, data, headers) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const bulkCreateUri = entityUri(domain, module, 'bulkCreate'); | ||
const response = yield api.post(bulkCreateUri, data, headers); | ||
return response ? response : []; | ||
}); | ||
} | ||
@@ -57,3 +68,3 @@ function baseCreate(api, domain, module, data, headers) { | ||
const uri = entityUri(domain, module, 'delete'); | ||
return api.post(uri, { ...data, id }, headers); | ||
return api.post(uri, Object.assign(Object.assign({}, data), { id }), headers); | ||
} | ||
@@ -65,3 +76,3 @@ function baseSearch(api, domain, module, filters, options) { | ||
const uri = entityUri(domain, module, 'view'); | ||
return api.post(uri, { ...data, id }, headers); | ||
return api.post(uri, Object.assign(Object.assign({}, data), { id }), headers); | ||
} | ||
@@ -113,5 +124,7 @@ function entity(domain, module) { | ||
} | ||
async function bulkCreate(data, headers) { | ||
const response = await api.post(bulkCreateUri, data, headers); | ||
return response ? response : []; | ||
function bulkCreate(data, headers) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield api.post(bulkCreateUri, data, headers); | ||
return response ? response : []; | ||
}); | ||
} | ||
@@ -124,5 +137,7 @@ function create(data, headers) { | ||
} | ||
async function search(filters, options) { | ||
const response = await api.put(searchUri, { ...options, filters, module_name: moduleName }); | ||
return response ? response : []; | ||
function search(filters, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield api.put(searchUri, Object.assign(Object.assign({}, options), { filters, module_name: moduleName })); | ||
return response ? response : []; | ||
}); | ||
} | ||
@@ -129,0 +144,0 @@ function addCustomAttribute(moduleId, attribute, headers) { |
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -33,29 +42,33 @@ const bf_types_1 = require("bf-types"); | ||
} | ||
async function executeCommand(command) { | ||
const results = await getApi().post('core/multitoolCommand/command/execute', { | ||
command, | ||
function executeCommand(command) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const results = yield getApi().post('core/multitoolCommand/command/execute', { | ||
command, | ||
}); | ||
if (!Array.isArray(results) || results.length < 1) { | ||
const result = { | ||
action: bf_types_1.MultitoolCommandAction.CREATED, | ||
result: bf_types_1.MultitoolCommandResult.FAILED, | ||
reason: 'no response was given', | ||
}; | ||
return result; | ||
} | ||
return results[0]; | ||
}); | ||
if (!Array.isArray(results) || results.length < 1) { | ||
const result = { | ||
action: bf_types_1.MultitoolCommandAction.CREATED, | ||
result: bf_types_1.MultitoolCommandResult.FAILED, | ||
reason: 'no response was given', | ||
}; | ||
return result; | ||
} | ||
return results[0]; | ||
} | ||
async function executeCommands(commands) { | ||
const results = await getApi().post('core/multitoolCommand/command/execute', { | ||
command: commands.join('\n'), | ||
function executeCommands(commands) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const results = yield getApi().post('core/multitoolCommand/command/execute', { | ||
command: commands.join('\n'), | ||
}); | ||
if (!Array.isArray(results) || results.length < 1) { | ||
const result = { | ||
action: bf_types_1.MultitoolCommandAction.CREATED, | ||
result: bf_types_1.MultitoolCommandResult.FAILED, | ||
reason: 'no response was given', | ||
}; | ||
return [result]; | ||
} | ||
return results; | ||
}); | ||
if (!Array.isArray(results) || results.length < 1) { | ||
const result = { | ||
action: bf_types_1.MultitoolCommandAction.CREATED, | ||
result: bf_types_1.MultitoolCommandResult.FAILED, | ||
reason: 'no response was given', | ||
}; | ||
return [result]; | ||
} | ||
return results; | ||
} | ||
@@ -62,0 +75,0 @@ function isCommandFailedResult(result) { |
{ | ||
"name": "bf-lib", | ||
"version": "4.1.3", | ||
"version": "4.2.0", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "the standard client library for block-5 software", |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
async function nexus(system, config, clientAuth) { | ||
let user = null; | ||
let baseUrl = config.url; | ||
if (baseUrl[baseUrl.length - 1] === '/') { | ||
baseUrl = baseUrl.slice(0, -1); | ||
} | ||
function getUrl() { | ||
return baseUrl; | ||
} | ||
function getUser() { | ||
return user; | ||
} | ||
function getLoginUrl(returnUrl) { | ||
if (typeof returnUrl !== 'string') { | ||
return `${baseUrl}/login`; | ||
function nexus(system, config, clientAuth) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let user = null; | ||
let baseUrl = config.url; | ||
if (baseUrl[baseUrl.length - 1] === '/') { | ||
baseUrl = baseUrl.slice(0, -1); | ||
} | ||
return `${baseUrl}/login?returnUrl=${returnUrl}`; | ||
} | ||
async function disconnect() { | ||
await clientAuth.disconnect(system); | ||
if (clientAuth.afterDisconnect) { | ||
clientAuth.afterDisconnect(system); | ||
function getUrl() { | ||
return baseUrl; | ||
} | ||
} | ||
async function reconnect() { | ||
await disconnect(); | ||
const result = await clientAuth.reconnect(system); | ||
if (!result) { | ||
return null; | ||
function getUser() { | ||
return user; | ||
} | ||
user = result; | ||
if (clientAuth.afterConnect) { | ||
clientAuth.afterConnect(system); | ||
function getLoginUrl(returnUrl) { | ||
if (typeof returnUrl !== 'string') { | ||
return `${baseUrl}/login`; | ||
} | ||
return `${baseUrl}/login?returnUrl=${returnUrl}`; | ||
} | ||
return result; | ||
} | ||
const instance = { | ||
getUrl, | ||
getLoginUrl, | ||
disconnect, | ||
reconnect, | ||
}; | ||
try { | ||
user = await clientAuth.connect(baseUrl); | ||
if (!user) { | ||
throw new Error('was not able to authenticate the user'); | ||
function disconnect() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield clientAuth.disconnect(system); | ||
if (clientAuth.afterDisconnect) { | ||
clientAuth.afterDisconnect(system); | ||
} | ||
}); | ||
} | ||
return Object.freeze({ ...instance, getUser }); | ||
} | ||
catch (e) { | ||
throw new Error(`failed to establish a proper connection with a nexus node: ${e.message}`); | ||
} | ||
function reconnect() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield disconnect(); | ||
const result = yield clientAuth.reconnect(system); | ||
if (!result) { | ||
return null; | ||
} | ||
user = result; | ||
if (clientAuth.afterConnect) { | ||
clientAuth.afterConnect(system); | ||
} | ||
return result; | ||
}); | ||
} | ||
const instance = { | ||
getUrl, | ||
getLoginUrl, | ||
disconnect, | ||
reconnect, | ||
}; | ||
try { | ||
user = yield clientAuth.connect(baseUrl); | ||
if (!user) { | ||
throw new Error('was not able to authenticate the user'); | ||
} | ||
return Object.freeze(Object.assign(Object.assign({}, instance), { getUser })); | ||
} | ||
catch (e) { | ||
throw new Error(`failed to establish a proper connection with a nexus node: ${e.message}`); | ||
} | ||
}); | ||
} | ||
exports.default = nexus; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -30,65 +39,67 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
async function init(settings) { | ||
if (initialized) { | ||
return; | ||
} | ||
const { logger, mask } = settings.logging; | ||
const debugLoggingEnabled = !mask.includes('debug'); | ||
function log(level, message) { | ||
switch (level) { | ||
case 'debug': { | ||
if (debugLoggingEnabled) { | ||
function init(settings) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (initialized) { | ||
return; | ||
} | ||
const { logger, mask } = settings.logging; | ||
const debugLoggingEnabled = !mask.includes('debug'); | ||
function log(level, message) { | ||
switch (level) { | ||
case 'debug': { | ||
if (debugLoggingEnabled) { | ||
logger[level](message); | ||
} | ||
break; | ||
} | ||
default: { | ||
logger[level](message); | ||
break; | ||
} | ||
break; | ||
} | ||
default: { | ||
logger[level](message); | ||
break; | ||
} | ||
log('debug', 'beginning to initialize the internal bf-lib system'); | ||
Instrumentor_1.SetInstrumentorLogger(log); | ||
const httpHeaders = {}; | ||
const libModuleMap = new Map(); | ||
const nexus = yield Nexus_1.default(system, settings.nexus, settings.auth); | ||
const eventBus = EventBus_1.default(); | ||
function getEventBus() { | ||
return eventBus; | ||
} | ||
function getHttpHeaders() { | ||
return Object.assign({}, httpHeaders); | ||
} | ||
function setHttpHeader(key, value) { | ||
httpHeaders[key] = value; | ||
} | ||
function getLibModule(type) { | ||
const libModule = libModuleMap.get(type); | ||
if (!libModule) { | ||
throw new Error('An attempt was mad to access an instance for a missing Lib Module.'); | ||
} | ||
return libModule(seal); | ||
} | ||
} | ||
log('debug', 'beginning to initialize the internal bf-lib system'); | ||
Instrumentor_1.SetInstrumentorLogger(log); | ||
const httpHeaders = {}; | ||
const libModuleMap = new Map(); | ||
const nexus = await Nexus_1.default(system, settings.nexus, settings.auth); | ||
const eventBus = EventBus_1.default(); | ||
function getEventBus() { | ||
return eventBus; | ||
} | ||
function getHttpHeaders() { | ||
return { ...httpHeaders }; | ||
} | ||
function setHttpHeader(key, value) { | ||
httpHeaders[key] = value; | ||
} | ||
function getLibModule(type) { | ||
const libModule = libModuleMap.get(type); | ||
if (!libModule) { | ||
throw new Error('An attempt was mad to access an instance for a missing Lib Module.'); | ||
log('debug', 'beginning to initialize all system lib modules'); | ||
libModuleMap.set(Types_1.LibModule.AUTH, require('../auth/Auth').default); | ||
libModuleMap.set(Types_1.LibModule.API, require('../api/Api').default); | ||
libModuleMap.set(Types_1.LibModule.MODULE, require('../module/Module').default); | ||
libModuleMap.set(Types_1.LibModule.MULTITOOL, require('../multitool/Multitool').default); | ||
log('debug', 'finished initializing all system lib modules'); | ||
const instanceMethods = { | ||
getEventBus, | ||
getHttpHeaders, | ||
setHttpHeader, | ||
getLibModule, | ||
nexus, | ||
}; | ||
Object.assign(instance, instanceMethods); | ||
if (settings.auth.afterConnect) { | ||
settings.auth.afterConnect(instance); | ||
} | ||
return libModule(seal); | ||
} | ||
log('debug', 'beginning to initialize all system lib modules'); | ||
libModuleMap.set(Types_1.LibModule.AUTH, require('../auth/Auth').default); | ||
libModuleMap.set(Types_1.LibModule.API, require('../api/Api').default); | ||
libModuleMap.set(Types_1.LibModule.MODULE, require('../module/Module').default); | ||
libModuleMap.set(Types_1.LibModule.MULTITOOL, require('../multitool/Multitool').default); | ||
log('debug', 'finished initializing all system lib modules'); | ||
const instanceMethods = { | ||
getEventBus, | ||
getHttpHeaders, | ||
setHttpHeader, | ||
getLibModule, | ||
nexus, | ||
}; | ||
Object.assign(instance, instanceMethods); | ||
if (settings.auth.afterConnect) { | ||
settings.auth.afterConnect(instance); | ||
} | ||
initialized = true; | ||
log('debug', 'finished initializing the internal bf-lib system'); | ||
initialized = true; | ||
log('debug', 'finished initializing the internal bf-lib system'); | ||
}); | ||
} | ||
const [instance, system] = Utils_1.proxyWrap({}, { init, sealModule }); | ||
exports.default = system; |
115242
2663