-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ab5a8c
commit 0634632
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export { default as contentfulCreate } from './contentfulCreate' | ||
export { default as contentfulEntries } from './contentfulEntries' | ||
export { default as githubOrg } from './githubOrg' | ||
export { default as githubUser } from './githubUser' | ||
export { default as proxy } from './proxy' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Create a proxy to redirect requests of the "/api/*" | ||
// https://mmazzarolo.com/blog/2022-02-05-creating-and-deploying-a-proxy-server-in-5-minutes/ | ||
// https://github.com/chimurai/http-proxy-middleware | ||
// | ||
// Examples: | ||
// GET /api/hello → GET https://example.org/hello | ||
// POST /api/test?color=red → POST https://example.org/test?color=red | ||
import isAbsoluteUrl from 'bonn/util/isAbsoluteUrl' | ||
|
||
import isFunction from 'lodash-es/isFunction' | ||
|
||
import { createProxyMiddleware } from 'http-proxy-middleware' | ||
|
||
export default (targetDomain, optionsInput) => { | ||
function modify (obj, newObj) { | ||
Object.keys(obj).forEach(function (key) { | ||
delete obj[key] | ||
}) | ||
|
||
Object.keys(newObj).forEach(function (key) { | ||
obj[key] = newObj[key] | ||
}) | ||
} | ||
|
||
const options = isFunction(optionsInput) ? { callback: optionsInput } : (optionsInput || {}) | ||
const headersToAdd = options.headersToAdd || {} | ||
const headersToRemove = options.headersToRemove || [] | ||
const callback = options.callback | ||
|
||
let target = targetDomain | ||
if (!isAbsoluteUrl(target)) { | ||
target = 'https://' + target | ||
} | ||
|
||
const apiProxy = createProxyMiddleware({ | ||
target, | ||
changeOrigin: true, | ||
|
||
// Strip "/api" from the URL | ||
pathRewrite: { | ||
'^/api': '' | ||
}, | ||
|
||
onProxyRes (proxyRes) { | ||
// Add headers to response | ||
for (const key in headersToAdd) { | ||
proxyRes.headers[key] = headersToAdd[key] | ||
} | ||
|
||
// Remove header from response | ||
for (let i = 0; i < headersToRemove.length; i++) { | ||
delete proxyRes.headers[headersToRemove[i]] | ||
} | ||
|
||
if (callback) { | ||
modify(proxyRes, callback(proxyRes)) | ||
} | ||
} | ||
}) | ||
|
||
return (req, res) => { | ||
console.log(req) | ||
|
||
return apiProxy(req, res) | ||
} | ||
} |