Skip to content

Commit

Permalink
🔀Merge pull request #292 from algodex/fix-cors
Browse files Browse the repository at this point in the history
🔧 Adds Rewrites to prevent CORS
  • Loading branch information
PhearZero committed Jan 27, 2022
2 parents 8b2ebed + 4f3e42a commit d074394
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NEXT_PUBLIC_ENV=production
NEXT_PUBLIC_API=https://api-testnet-public.algodex.com

GEO_PASSWORD=Password
GEO_PASSWORD=Password
10 changes: 8 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ const getDefaultAsset = () => {
// Default to LAMP (available on Testnet only)
return process.env.NEXT_PUBLIC_DEFAULT_ASSET || 15322902
}

const PUBLIC_API = process.env.NEXT_PUBLIC_API

if (typeof PUBLIC_API === 'undefined') throw new Error('Must have Public API!')

const defaultAsset = getDefaultAsset()
const nextTranslate = require('next-translate')
const nextPWA = require('next-pwa')

const moduleExports = nextPWA(
nextTranslate({
pwa: {
Expand All @@ -22,8 +28,8 @@ const moduleExports = nextPWA(
return {
beforeFiles: [
{
source: '/api/:path*',
destination: 'https://cloud.algodex.com/algodex-backend/:path*'
source: '/algodex-backend/:path*',
destination: `${PUBLIC_API}/algodex-backend/:path*`
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"lodash": "^4.17.21",
"millify": "^4.0.0",
"moment": "^2.29.1",
"next": "^11.1.2",
"next": "^11.0.1",
"next-pwa": "^5.4.0",
"next-translate": "^1.0.7",
"polished": "^4.1.3",
Expand Down
16 changes: 11 additions & 5 deletions services/algodex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
*
* Includes all responses from the publicly exposed routes
*
* TODO: Make deterministic in @algodex/sdk or @algodex/common.
* Refactor the api client to accept a URL. The consumers of the api client should handle their
* own ENV variables. For this project it would be ./hooks/useAlgodex
*
* @author Alexander Trefonas
* @author Michael Feher
* @copyright Algodev Inc
*/

import axios from 'axios'
import Router from 'next/router'

// TODO: Implement getLogger() from '@algodex/common'
const DEBUG = process.env.NEXT_PUBLIC_DEBUG || process.env.DEBUG || false

export const PUBLIC_API = process.env.NEXT_PUBLIC_API || 'https://api-testnet-public.algodex.com'
export const PUBLIC_API =
typeof window === 'undefined' && process.env.NEXT_PUBLIC_API
? process.env.NEXT_PUBLIC_API
: `${window.location.protocol}//${window.location.host}`

export const API_HOST = `${PUBLIC_API}/algodex-backend`

Expand Down Expand Up @@ -46,7 +52,7 @@ async function getEtagResponse(url) {
const authToken = process.env.GEO_PASSWORD
const authHeader = `Bearer ${authToken}`

let headers = { headers: {Authorization: authHeader} }
let headers = { headers: { Authorization: authHeader } }
if (urlToEtag[url]) {
headers = { headers: { Authorization: authHeader, 'if-none-match': urlToEtag[url] } }
}
Expand All @@ -55,7 +61,7 @@ async function getEtagResponse(url) {
delete headers.headers.Authorization
}

DEBUG && console.debug({headers})
DEBUG && console.debug({ headers })
DEBUG && console.debug('url: ' + url)
return await axios
.get(url, headers)
Expand All @@ -76,7 +82,7 @@ async function getEtagResponse(url) {
} else if (error && !errorResp) {
console.debug('preflight failing?')
} else if (errorResp && errorResp.status === 451) {
console.debug('Error 451!');
console.debug('Error 451!')
} else {
throw new Error(`Invalid response: ${error.message}`)
}
Expand Down
100 changes: 51 additions & 49 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1742,20 +1742,20 @@
resolved "https://registry.npmjs.org/@napi-rs/triples/-/triples-1.1.0.tgz"
integrity sha512-XQr74QaLeMiqhStEhLn1im9EOMnkypp7MZOwQhGzqp2Weu5eQJbpPxWxixxlYRKWPOmJjsk6qYfYH9kq43yc2w==

"@next/env@11.1.3":
version "11.1.3"
resolved "https://registry.npmjs.org/@next/env/-/env-11.1.3.tgz"
integrity sha512-5+vaeooJuWmICSlmVaAC8KG3O8hwKasACVfkHj58xQuCB5SW0TKW3hWxgxkBuefMBn1nM0yEVPKokXCsYjBtng==
"@next/env@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/env/-/env-11.1.4.tgz#ff6090c2c38ca08e7d73eed616ff0899dd3fcc41"
integrity sha512-vEW+fSulzZams4nYmcX9LByb1moMBlkwOAVf0eF+44u+1N/h7HDeznPBWIjEfihzTku8rdLB0k7u8VT8AGtNkQ==

"@next/polyfill-module@11.1.3":
version "11.1.3"
resolved "https://registry.npmjs.org/@next/polyfill-module/-/polyfill-module-11.1.3.tgz"
integrity sha512-7yr9cr4a0SrBoVE8psxXWK1wTFc8UzsY8Wc2cWGL7qA0hgtqACHaXC47M1ByJB410hFZenGrpE+KFaT1unQMyw==
"@next/polyfill-module@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/polyfill-module/-/polyfill-module-11.1.4.tgz#9b4a4ccf3a783244c132bfb2d04d3980dcd3d4c1"
integrity sha512-CY3bOSQf9Dy3+34dFjFbOdg3DRXIGfujb54D/AVO83ajyQczRZ3xdU0i5VV0eSR6B56ktVy3/aelOffpTUq6LA==

"@next/react-dev-overlay@11.1.3":
version "11.1.3"
resolved "https://registry.npmjs.org/@next/react-dev-overlay/-/react-dev-overlay-11.1.3.tgz"
integrity sha512-zIwtMliSUR+IKl917ToFNB+0fD7bI5kYMdjHU/UEKpfIXAZPnXRHHISCvPDsczlr+bRsbjlUFW1CsNiuFedeuQ==
"@next/react-dev-overlay@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/react-dev-overlay/-/react-dev-overlay-11.1.4.tgz#a0b911e799389b4ca6a30fb129c49071150c7a22"
integrity sha512-8/9JflJwRXEvVb6cKCWgRTOmALzDJHpWD5diRbtXWsllqxcMBjtscgnO4PaK+9QyZnSYSUbn0zZUZvxOXOTE1Q==
dependencies:
"@babel/code-frame" "7.12.11"
anser "1.4.9"
Expand All @@ -1769,30 +1769,30 @@
stacktrace-parser "0.1.10"
strip-ansi "6.0.0"

"@next/react-refresh-utils@11.1.3":
version "11.1.3"
resolved "https://registry.npmjs.org/@next/react-refresh-utils/-/react-refresh-utils-11.1.3.tgz"
integrity sha512-144kD8q2nChw67V3AJJlPQ6NUJVFczyn10bhTynn9o2rY5DEnkzuBipcyMuQl2DqfxMkV7sn+yOCOYbrLCk9zg==
"@next/react-refresh-utils@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/react-refresh-utils/-/react-refresh-utils-11.1.4.tgz#08d3427b7a97f225d3cf31ee8605722c267176c0"
integrity sha512-jTme207yEV4On9Gk0QJYK2N3kfKVBx17lLOL3qSjqNbqk1TnE51xvzogOCQXNABbzQlBY+J/NN+eylPS4QOKwA==

"@next/swc-darwin-arm64@11.1.3":
version "11.1.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-11.1.3.tgz#062eb7871048fdb313304e42ace5f91402dbc39f"
integrity sha512-TwP4krjhs+uU9pesDYCShEXZrLSbJr78p12e7XnLBBaNf20SgWLlVmQUT9gX9KbWan5V0sUbJfmcS8MRNHgYuA==
"@next/swc-darwin-arm64@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-11.1.4.tgz#6a3d80e7cac3e83746adbe98ed1e78ea9baa504f"
integrity sha512-jt8dMtIRWnJjRYLid6NWCxXzXdpr9VFT/vhDp8ioh+TtOR0UKPHMxei6R4GA3RqoyPEfFcSNmkG7OtyqCSxNIw==

"@next/swc-darwin-x64@11.1.3":
version "11.1.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-11.1.3.tgz#8bd515768d02e4c1e0cd80d33f3f29456ee890ee"
integrity sha512-ZSWmkg/PxccHFNUSeBdrfaH8KwSkoeUtewXKvuYYt7Ph0yRsbqSyNIvhUezDua96lApiXXq6EL2d1THfeWomvw==
"@next/swc-darwin-x64@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-11.1.4.tgz#bd548cf3fb3dc5e79a54b330f14e4b7b8a4e1320"
integrity sha512-5i9tOQNO8kawwggHvQUVR3a5KzIGaE2dw1g1kL//z/N840djvGseHrJSFEGdP1c35gM+dSGPpAKHmeBKrwHM8g==

"@next/swc-linux-x64-gnu@11.1.3":
version "11.1.3"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-11.1.3.tgz#40030577e6ee272afb0080b45468bea73208f46d"
integrity sha512-PrTBN0iZudAuj4jSbtXcdBdmfpaDCPIneG4Oms4zcs93KwMgLhivYW082Mvlgx9QVEiRm7+RkFpIVtG/i7JitA==
"@next/swc-linux-x64-gnu@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-11.1.4.tgz#967c92aa0d9d6bc9414bbff9395c85c7755ba9d1"
integrity sha512-QfVuXugxBkCUHN9yD/VZ1xqszcMlBDj6vrbRiQvmWuyNo39ON6HqGn3jDwVrTHc9oKo2a0XInm+0zEnQeDmjSw==

"@next/swc-win32-x64-msvc@11.1.3":
version "11.1.3"
resolved "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-11.1.3.tgz"
integrity sha512-mRwbscVjRoHk+tDY7XbkT5d9FCwujFIQJpGp0XNb1i5OHCSDO8WW/C9cLEWS4LxKRbIZlTLYg1MTXqLQkvva8w==
"@next/swc-win32-x64-msvc@11.1.4":
version "11.1.4"
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-11.1.4.tgz#370c310ba197e799235492bf71084d3acbbb2c6a"
integrity sha512-7MPXYWsCo5qGZXyyJwBLvQkYi0hKARtpjGxjt/mdxn7A7O+jKJgAuxgOo/lnZIiXfbJzxRnSD8k6WkUwN0IVmg==

"@node-rs/helper@1.2.1":
version "1.2.1"
Expand Down Expand Up @@ -11163,17 +11163,17 @@ next-translate@^1.0.7:
resolved "https://registry.npmjs.org/next-translate/-/next-translate-1.2.0.tgz"
integrity sha512-L15JCfvj6BhQfx7i3nsJReUF7wrHe1RlzK7h25QlBT40prGQLZ5nJ6gx9Vd3Ew0V9QMqaVELUeQQ1u51yyDu0Q==

next@^11.1.2:
version "11.1.3"
resolved "https://registry.npmjs.org/next/-/next-11.1.3.tgz"
integrity sha512-ud/gKmnKQ8wtHC+pd1ZiqPRa7DdgulPkAk94MbpsspfNliwZkYs9SIYWhlLSyg+c661LzdUI2nZshvrtggSYWA==
next@^11.0.1:
version "11.1.4"
resolved "https://registry.yarnpkg.com/next/-/next-11.1.4.tgz#2381eeeffae80f58e6d80d8335ab56d2e157064e"
integrity sha512-GWQJrWYkfAKP8vmrzJcCfRSKv955Khyjqd5jipTcVKDGg+SH+NfjDMWFtCwArcQlHPvzisGu1ERLY0+Eoj7G+g==
dependencies:
"@babel/runtime" "7.15.3"
"@hapi/accept" "5.0.2"
"@next/env" "11.1.3"
"@next/polyfill-module" "11.1.3"
"@next/react-dev-overlay" "11.1.3"
"@next/react-refresh-utils" "11.1.3"
"@next/env" "11.1.4"
"@next/polyfill-module" "11.1.4"
"@next/react-dev-overlay" "11.1.4"
"@next/react-refresh-utils" "11.1.4"
"@node-rs/helper" "1.2.1"
assert "2.0.0"
ast-types "0.13.2"
Expand All @@ -11195,7 +11195,7 @@ next@^11.1.2:
image-size "1.0.0"
jest-worker "27.0.0-next.5"
native-url "0.3.4"
node-fetch "2.6.1"
node-fetch "2.6.7"
node-html-parser "1.4.9"
node-libs-browser "^2.2.1"
os-browserify "0.3.0"
Expand All @@ -11219,10 +11219,10 @@ next@^11.1.2:
vm-browserify "1.1.2"
watchpack "2.1.1"
optionalDependencies:
"@next/swc-darwin-arm64" "11.1.3"
"@next/swc-darwin-x64" "11.1.3"
"@next/swc-linux-x64-gnu" "11.1.3"
"@next/swc-win32-x64-msvc" "11.1.3"
"@next/swc-darwin-arm64" "11.1.4"
"@next/swc-darwin-x64" "11.1.4"
"@next/swc-linux-x64-gnu" "11.1.4"
"@next/swc-win32-x64-msvc" "11.1.4"

nice-try@^1.0.4:
version "1.0.5"
Expand Down Expand Up @@ -11270,10 +11270,12 @@ node-fetch@2.6.0:
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==

node-fetch@2.6.1:
version "2.6.1"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
node-fetch@2.6.7:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^1.0.1:
version "1.7.3"
Expand Down

0 comments on commit d074394

Please sign in to comment.