Skip to content

Commit

Permalink
GitHub stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryjappinen committed Aug 5, 2023
1 parent 8ab5a8c commit 0634632
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util/sendGithubApiRequest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ky from 'ky'

const baseUrl = 'https://api/github.com/'
const baseUrl = 'https://api.github.com/'
const method = 'get'

export default async (endpoint, params, body, token) => {
Expand Down
3 changes: 3 additions & 0 deletions vercel/endpoints/index.js
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'
66 changes: 66 additions & 0 deletions vercel/endpoints/proxy.js
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)

Check warning on line 62 in vercel/endpoints/proxy.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

return apiProxy(req, res)
}
}

0 comments on commit 0634632

Please sign in to comment.