@bavenir/spade-node-js-client
Version:
A client-side JavaScript library that can be used to iteract with Data Broker's API.
161 lines (160 loc) • 6.41 kB
JavaScript
/**
* Copyright (C) 2024 bAvenir
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
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 });
exports.DataBroker = void 0;
const client_1 = require("./client");
const misc_1 = require("./misc");
const error_1 = require("./utils/error");
class DataBroker {
constructor(config) {
try {
this._initConfig(config);
this._initClients();
}
catch (error) {
console.error('Failed to initialize Data Broker', error);
throw new error_1.DataBrokerInitError();
}
}
/**
* Retrieves the health status of the Data Broker.
*
* @method healthcheck
* @returns {Promise<Healthcheck>} - A promise that resolves to a `Healthcheck` object containing information about the status of the Data Broker.
* @throws {DataBrokerHealthcheckError} - If the healthcheck fails.
*/
healthcheck() {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._sbClient.get('admin/healthcheck', {
responseType: 'json',
headers: { 'Content-Type': 'application/json' },
});
}
catch (error) {
throw new error_1.DataBrokerHealthcheckError(error);
}
});
}
/**
* Runs a local discovery on the Data Broker and returns a list of items, where each item represents a Thing Description (TD).
*
* @method items
* @param {string} query - JSON Path query to filter items. Use '$' for all items.
* @returns {Promise<ItemTD[]>} A promise that resolves to an array of Thing Descriptions (`ItemTD[]`).
* @throws {DataBrokerDiscoveryError} - If the discovery fails.
*/
discovery(query) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._sbClient.post('discovery', {
data: query,
responseType: 'json',
headers: { 'Content-Type': 'text/plain' },
});
}
catch (error) {
throw new error_1.DataBrokerDiscoveryError('Failed to run local discovery', error);
}
});
}
/**
* Runs a remote discovery on the Data Broker and returns a list of items from your partners, where each item represents a Thing Description (TD).
*
* @method remoteItems
* @param {string} query - JSON Path query to filter items. Use '$' for all items.
* @returns {Promise<ItemTD[]>} A promise that resolves to an array of Thing Descriptions (`ItemTD[]`).
* @throws {DataBrokerDiscoveryError} - If the discovery fails.
*/
remoteDiscovery(query) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._sbClient.post('discovery/remote', {
data: query,
responseType: 'json',
headers: { 'Content-Type': 'text/plain' },
});
}
catch (error) {
throw new error_1.DataBrokerDiscoveryError('Failed to run remote discovery', error);
}
});
}
/**
* Retrieves the value read from a property of an Item.
*
* @method consumption
* @param {string} oid - The unique identifier of the Item to which the property belongs.
* @param {string} pid - The unique string representing the property name (key) to be read.
* @param {any} [params] - Optional parameters to be passed to the consumption.
* @param {any} [data] - Optional data to be passed to the consumption.
* @param {DataBrokerResponseType} [responseType] - Optional response type for the consumption.
* @returns {Promise<any>} - A promise that resolves to the value of the specified property from the Item.
* @throws {DataBrokerConsumptionError} - If the consumption fails.
*/
consumption(oid, pid, params, data, responseType) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._sbClient.get(`consumption/properties/${oid}/${pid}`, {
data,
responseType,
params,
headers: { 'Content-Type': 'application/json' },
});
}
catch (error) {
throw new error_1.DataBrokerConsumptionError(undefined, error);
}
});
}
/**
* Returns Data Broker base URL.
*
* @returns {string} The Data Broker base URL.
*/
get url() {
return `https://${this._config.url}`;
}
/**
* Returns South Bound URL of Data Broker.
*
* @returns {string} The South Bound URL.
*/
get sbUrl() {
return `${this.url}/api/sb`;
}
/**
* Returns North Bound URL of Data Broker.
*
* @returns {string} The North Bound URL.
*/
get nbUrl() {
return `${this.url}/api/nb`;
}
_initConfig(config) {
this._config = config;
this._config.url = (0, misc_1.sanitazeUrl)(config.url);
}
_initClients() {
this._sbClient = new client_1.Client(Object.assign(Object.assign({}, this._config), { url: this.sbUrl }));
this._nbClient = new client_1.Client(Object.assign(Object.assign({}, this._config), { url: this.nbUrl }));
}
}
exports.DataBroker = DataBroker;
;