UNPKG

@stoqey/ib

Version:

Interactive Brokers TWS/IB Gateway API client library for Node.js (TS)

134 lines 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = get; /* eslint-disable @typescript-eslint/no-explicit-any */ const fs_1 = require("fs"); const path = __importStar(require("path")); const dotenv_1 = __importDefault(require("dotenv")); dotenv_1.default.config(); let configuration = null; const envsToInclude = [ "ci", "env_config_test", "ib_host", "ib_port", "ib_test_account", "client_version", "max_req_per_second", ]; function readJson(readPath) { try { const data = (0, fs_1.readFileSync)(readPath, "utf8"); return JSON.parse(data); } catch (error) { if (error.code !== "ENOENT" || (error.errno !== undefined && error.errno !== -4058 && error.errno !== -2)) { throw error; } } return {}; } function read(file) { const filePath = path.resolve(__dirname, "..", "..", "config", `${file}.json`); return readJson(filePath); } function assignEnvironment(config) { const newConfig = config; envsToInclude.forEach((key) => { const lc = key.toLowerCase(); const uc = key.toUpperCase(); newConfig[lc] = process.env[uc] || config[lc]; }); return newConfig; } function loadEnvironmentSpecific(config, environment) { let newConfig = config; if (environment) { const conf = read(environment); if (conf) { newConfig = { ...newConfig, ...conf, }; } } return newConfig; } const ensureInteger = (fields, config) => fields.forEach((field) => { const value = config[field]; if (typeof value === "string") { config[field] = parseInt(value, 10); } }); function load() { const nodeEnvironment = process.env.NODE_ENV; // load default config let config = read("default"); // load local config config = loadEnvironmentSpecific(config, "local"); // load environment specific config config = loadEnvironmentSpecific(config, nodeEnvironment); // load config from env variables config = assignEnvironment(config); config.environment = nodeEnvironment || "local"; config.isProduction = nodeEnvironment === "production"; config.isStaging = nodeEnvironment === "staging"; config.isDevelopment = nodeEnvironment === "development"; config.isTest = nodeEnvironment === "test"; config.isLocal = !nodeEnvironment; config.ci = config.ci || process.env.CIRCLECI; const intFields = [ "ib_port", "default_client_id", "client_version", "max_req_per_second", ]; ensureInteger(intFields, config); return config; } function get() { if (!configuration) { configuration = load(); } return configuration; } configuration = load(); exports.default = configuration; //# sourceMappingURL=configuration.js.map