amiddy
Version:
Middleware server with high configurability for development
1 lines • 3.94 kB
JavaScript
import stripComments from"strip-json-comments";import merge from"lodash.merge";import chalk from"chalk";import file from"../file.js";import debug from"../debug.js";const privateApi={};privateApi.loadFile=filePath=>{debug.log(`Loading file: ${filePath}`);try{return stripComments(file.read(filePath))}catch(e){throw debug.log(`Error reading file: ${filePath}`),e.message=`Cannot read file: ${filePath}\nError: ${e.message}`,e.messageData={message:e.message,path:filePath},e}},privateApi.loadJSONFile=filePath=>{debug.log(`Loading JSON file: ${filePath}`);try{return JSON.parse(privateApi.loadFile(filePath))}catch(e){throw debug.log(`Error reading JSON file: ${filePath}`),e.message=`Cannot read JSON file: ${filePath}\nError: ${e.message}`,e.messageData={message:e.message,path:filePath},e}},privateApi.validate=configObj=>{const vhostConf=configObj.vhost;if(!vhostConf)throw Error("Missing `vhost` property");if(!vhostConf.name)throw Error("Missing `vhost.name` property");const source=configObj.source;if(source&&("object"!=typeof source||Array.isArray(source)))throw Error("`source` property should be an object");const deps=configObj.deps;if(!deps)throw Error("Missing `deps` property");if(!Array.isArray(deps))throw Error("`deps` property should be an array");const depErrors=[];if(deps.forEach((dep,index)=>{dep.ip||dep.name||depErrors.push(`Dependency with index ${index} is missing "ip" or "name"`),dep.patterns&&Array.isArray(dep.patterns)&&!(1>dep.patterns.length)||depErrors.push(`Dependency with index ${index} should have patterns defined as array of strings`)}),0<depErrors.length)throw Error(depErrors.join(" \n"))},privateApi.getPort=(https,defaultPort)=>defaultPort?defaultPort:https?443:80,privateApi.setSourceDefaults=configObj=>{configObj.source=configObj.source||{},configObj.source.ip=configObj.source.ip||"127.0.0.1",configObj.source.https=configObj.source.https||!1,configObj.source.port=configObj.source.port||privateApi.getPort(configObj.source.https,3e3)},privateApi.setVhostDefaults=configObj=>{configObj.vhost.https=configObj.vhost.https||!1,configObj.vhost.port=configObj.vhost.port||privateApi.getPort(configObj.vhost.https)},privateApi.setDepsDefaults=configObj=>{configObj.deps.forEach(dep=>{dep.https=dep.https||!1,dep.port=dep.port||privateApi.getPort(dep.https)})},privateApi.setProxyDefaults=configObj=>{configObj.proxy=merge({options:{changeOrigin:!1,secure:!1,ws:!1},response:{headers:{}}},configObj.proxy)},privateApi.setOptionDefaults=configObj=>{configObj.options=merge({mock:{enabled:!0},recorder:{enabled:!1,fileNamePattern:"{METHOD}-{PATH}.{EXT}",ignorePatterns:["**favicon*"],path:"__amiddy__/records"}},configObj.options)},privateApi.setDefaults=configObj=>{privateApi.setSourceDefaults(configObj),privateApi.setVhostDefaults(configObj),privateApi.setDepsDefaults(configObj),privateApi.setProxyDefaults(configObj),privateApi.setOptionDefaults(configObj)},privateApi.generateConfig=(config,tokensObj)=>{const warnColored=chalk.hex("#C03D29"),updatedConfig=Object.keys(tokensObj).reduce((acc,token)=>{const regExp=new RegExp(token,"g"),hasMatch=regExp.test(acc);return hasMatch?acc.replace(regExp,tokensObj[token]):(console.warn(`${warnColored("Token")} ${warnColored.bold(token)} ${warnColored("was not wound in base configuration")}`),acc)},config);try{return JSON.parse(updatedConfig)}catch(e){throw debug.log("Error parsing config as JSON"),e.message=`${"Error parsing config as JSON"} ${e.message}`,e}};const service={get:props=>{let configObj;const tokens=props.tokens,configPath=file.getAbsolutePath(props.config);if(tokens){const tokensPath=file.getAbsolutePath(tokens),tokensObj=privateApi.loadJSONFile(tokensPath),config=privateApi.loadFile(configPath);configObj=privateApi.generateConfig(config,tokensObj)}else configObj=privateApi.loadJSONFile(configPath);return privateApi.validate(configObj),privateApi.setDefaults(configObj),configObj}};export{privateApi};export default service;