UNPKG

@flexvertex/flexvertex-driver

Version:

The official FlexVertex Node.js driver

99 lines (77 loc) 2.84 kB
import { FlexCommons } from '../commons/FlexCommons.js'; import { FlexSchema } from '../schema/FlexSchema.js'; import axios from 'axios'; import { Buffer } from 'node:buffer'; import { JSONParse, JSONStringify } from 'json-with-bigint'; /** * The primary class used for connecting to a FlexVertex instance. */ export class FlexSession { /** * @param {Object} options * @param {string} [options.protocol=https] - The protocol used for the FlexVertex REST endpoints * @param {string} [options.hostname=localhost] - The hostname of the FlexVertex REST endpoints * @param {string} [options.port=8000] - If hostname is 'localhost', then port defaults to 8000 * @param {string} options.username - Either the full /D/N/S/Username or an email address * @param {string} options.password - An unencoded password * @param {string} [options.timeout=30000] - How long (in milliseconds) each REST call has before timing out (defaults to 30000) */ constructor(options) { if(options === undefined) throw "FlexSession() options is undefined!"; if(options.username === undefined) throw "FlexSession() The username option is required!"; if(options.password === undefined) throw "FlexSession() The password option is required!"; let hostname = 'localhost'; if(FlexCommons.isDefined(options.hostname)) hostname = options.hostname; let protocol = undefined; let port = undefined; if(hostname === 'localhost') { port = 8000; protocol = 'http'; } else { protocol = 'https'; } if(FlexCommons.isDefined(options.port)) port = options.port; if(FlexCommons.isDefined(options.protocol)) protocol = options.protocol; let baseURL = undefined; if(FlexCommons.isDefined(port)) baseURL = protocol + '://' + hostname + ':' + port + '/rest/'; else baseURL = protocol + '://' + hostname + '/rest/'; // let baseURL = "http://localhost:8000/rest/"; // if(!(options.baseURL === undefined)) baseURL = options.baseURL; let token = options.username + ':' + options.password; // console.log("token = " + token); var encodedToken = Buffer.from(token).toString('base64'); // console.log("encodedToken = " + encodedToken); let timeout = 30000; if(!(options.timeout === undefined)) timeout = options.timeout; console.log("baseURL = " + baseURL); this.axInstance = axios.create( { baseURL: baseURL, timeout: timeout, headers: {'Authorization': 'Basic ' + encodedToken } }); } /** * Called when the FlexSession is no longer needed. */ close() { // console.log("--FlexSession.close()"); } /** * * @param {string} dnsPath The /Domain/Nexus/Schema path to open * @returns {FlexSchema} */ openSchema(dnsPath) { return new FlexSchema(this, dnsPath); } }