UNPKG

@sprucelabs/mercury-client

Version:

The simple way to interact with the Spruce Experience Platform

97 lines (96 loc) 3.99 kB
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()); }); }; import { SchemaError } from '@sprucelabs/schema'; import { eventContractUtil } from '@sprucelabs/spruce-event-utils'; import { DEFAULT_HOST } from '../constants.js'; import SpruceError from '../errors/SpruceError.js'; import MutableContractClient from './MutableContractClient.js'; class MercuryClientFactory { static Client(connectionOptions) { return __awaiter(this, void 0, void 0, function* () { var _a; const { host: hostOption, contracts, reconnectDelayMs, allowSelfSignedCrt, emitTimeoutMs = this.timeoutMs, shouldReconnect, maxEmitRetries, connectionRetries, } = connectionOptions || {}; const host = hostOption !== null && hostOption !== void 0 ? hostOption : DEFAULT_HOST; if (host.substr(0, 4) !== 'http') { throw new SpruceError({ code: 'INVALID_PROTOCOL', uri: host }); } if (contracts && !Array.isArray(contracts)) { throw new SchemaError({ code: 'INVALID_PARAMETERS', parameters: ['contracts'], }); } const eventContract = !contracts && this.defaultContract ? this.defaultContract : //@ts-ignore eventContractUtil.unifyContracts(contracts !== null && contracts !== void 0 ? contracts : []); let Client = MutableContractClient; if (this.isTestMode) { //TODO, make this something fitxures sets to make the test client available Client = require('../clients/MercuryTestClient.js').default; } const client = new ((_a = MercuryClientFactory.ClientClass) !== null && _a !== void 0 ? _a : //@ts-ignore Client)({ host, reconnection: false, reconnectDelayMs, rejectUnauthorized: !allowSelfSignedCrt, eventContract, emitTimeoutMs, shouldReconnect, maxEmitRetries, connectionRetries, }); yield client.connect(); this.totalClients++; this.clients.push(client); return client; }); } static isInTestMode() { return this.isTestMode; } static setIsTestMode(isTestMode) { this.isTestMode = isTestMode; } static setDefaultContract(contract) { this.defaultContract = contract; } static hasDefaultContract() { return !!this.defaultContract; } static clearDefaultContract() { this.defaultContract = undefined; } static resetTestClient() { const Client = require('../clients/MercuryTestClient.js').default; Client.reset(); } static setDefaultTimeoutMs(ms) { this.timeoutMs = ms; } static getTotalClients() { return this.totalClients; } static reset() { this.totalClients = 0; this.clients = []; this.resetTestClient(); } static getClients() { return this.clients; } } MercuryClientFactory.isTestMode = false; MercuryClientFactory.timeoutMs = 30000; MercuryClientFactory.totalClients = 0; MercuryClientFactory.clients = []; export default MercuryClientFactory;