Created
February 13, 2018 07:28
-
-
Save leegee/5e44b0205ef820925d2e659cf6f33991 to your computer and use it in GitHub Desktop.
webpackge.devserver.proxy.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.APIS.endpoints.reduce((acc, cv) => { | |
acc[`/onewssi/${cv}/v1`] = { | |
secure: false, | |
headers: config.APIS.proxyHeaders, | |
bypass: (req, res, proxyOptions) => { | |
// May re-write, rather than bypass, to serve mock data basd on req URI | |
let rv = false; | |
let found = true; | |
if (req.url.indexOf('/mock') == -1 | |
&& req.url.indexOf('/onewssi') > -1 | |
) { | |
const requestMethod = req.method.toLowerCase(); | |
rv = req.url.replace( | |
/^\/+onewssi\/+(.*?)(\?.*)?$/, | |
(_, pathPart, queryString) => { | |
if (queryString) { | |
// Original code talking to API has non-encoded QS values: | |
queryString = queryString.replace(/\W/g, '_'); | |
} else { | |
queryString = requestMethod; | |
} | |
let url = '/mock/' + pathPart + '/' + queryString + '.json'; | |
if (!fs.existsSync(proxyContentBase + url)) { | |
url = '/mock/' + pathPart + '/' + requestMethod + '.json'; | |
if (!fs.existsSync(proxyContentBase + url)) { | |
found = false; | |
console.error( | |
'Dev proxy config error - cannot find mock for requested %s URL,\n\t%s\n\t%s', | |
req.method, req.url, url | |
); | |
} | |
} | |
return url; | |
} | |
); | |
} | |
if (found) { | |
if (rv) { | |
rv = rv.replace(/\/+/g, '/'); | |
req.method = 'GET'; | |
console.log('\nProxy: %s %s –> %s\n', req.method, req.url, rv); | |
} else { | |
console.log('Did not rewrite ', req.method, req.url); | |
} | |
} else { | |
console.log('Proxy did not find ', req.method, req.url); | |
} | |
return rv; | |
} | |
}; | |
return acc; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment