amiddy
Version:
Middleware server with high configurability for development
1 lines • 990 B
JavaScript
import url from"url";import micromatch from"micromatch";const service={};service.buildUrl=options=>{const urlOptions={hostname:options.ip||options.name,port:options.port,protocol:options.https?"https":"http",slashes:!0};return url.format(urlOptions)},service.buildUrlObject=options=>{const formattedUrl=service.buildUrl(options);return url.parse(formattedUrl)},service.getDependency=(deps,reqUrl)=>deps.find(dep=>{const{patterns}=dep||{};return micromatch.isMatch(reqUrl,patterns||[],{contains:!0})}),service.getMock=(mocks,reqUrl,method)=>mocks&&mocks.find(mock=>{const{disabled,methods,patterns}=mock||{};if(!disabled){const matchedMethod=!methods||methods.includes(method);return matchedMethod&µmatch.isMatch(reqUrl,patterns||[],{contains:!0})}return!1}),service.extendOptions=(proxyOptions,ssl,dependency)=>{dependency&&(proxyOptions.target=service.buildUrl(dependency),dependency.https&&ssl&&(proxyOptions.ssl={cert:ssl.cert,key:ssl.key||ssl["private"]}))};export default service;