Skip to content

Commit

Permalink
Merge pull request #240 from yoonghan/fix-cors
Browse files Browse the repository at this point in the history
Allow override api url
  • Loading branch information
yoonghan authored Aug 9, 2024
2 parents 3af1d7c + 9f402a9 commit 3629d3f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FIREBASE_CLIENT_X509_CERT_URL=""

CORS_ALLOW_ORIGIN="https://localhost:3000"
NEXT_PUBLIC_SITE_URL=""
NEXT_PUBLIC_IS_LOCAL_API_SITE_URL="true"
NEXT_PUBLIC_API_SITE_URL=""

# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ jobs:
id: env-set
run: |
SITE_URL="https://yoonghan.github.io"
echo "Change site: $SITE_URL"
API_SITE_URL="https://www.walcron.com"
echo "Change site: $SITE_URL;$API_SITE_URL"
echo "siteUrl=$SITE_URL" >> $GITHUB_OUTPUT
echo "apiSiteUrl=$API_SITE_URL" >> $GITHUB_OUTPUT
- name: Url replace
shell: bash
Expand All @@ -79,7 +81,7 @@ jobs:
echo "NEXT_PUBLIC_PUSHER_APP_KEY=${{ secrets.NEXT_PUBLIC_PUSHER_APP_KEY }}" >> .env
echo "NEXT_PUBLIC_PUSHER_CLUSTER=${{ secrets.NEXT_PUBLIC_PUSHER_CLUSTER }}" >> .env
echo "NEXT_PUBLIC_SITE_URL=${{ steps.env-set.outputs.siteUrl }}" >> .env
echo "NEXT_PUBLIC_IS_LOCAL_API_SITE_URL=false" >> .env
echo "NEXT_PUBLIC_API_SITE_URL=${{ steps.env-set.outputs.apiSiteUrl }}" >> .env
- name: Build with Next.js
run: |
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ npm run backstop:approve // Approve the new website ok
## Additional site url

1. CORS_ALLOW_ORIGIN - for whitelisting /api url defined in next.config.js to external apps.
2. NEXT_PUBLIC_SITE_URL - configure for static site to call api.
3. NEXT_PUBLIC_IS_LOCAL_API_SITE_URL - Indicate local api is used. Used for variable domain name.
2. NEXT_PUBLIC_SITE_URL - configure for static site to call api. Overrides blank with https://www.walcron.com.
3. NEXT_PUBLIC_API_SITE_URL - Indicate local api is url.

## Run Github workflows

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/mocks/setEnv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const defaultProcessEnv = { ...process.env }

export const setEnv = (environments: { [key: string]: string }) => {
export const setEnv = (environments: { [key: string]: string | undefined }) => {
Object.keys(environments).forEach((key: string) => {
process.env[key] = environments[key]
})
Expand Down
17 changes: 6 additions & 11 deletions src/config/site.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ describe("root-url", () => {
it("should be default with local website", async () => {
setEnv({
NEXT_PUBLIC_SITE_URL: "",
NEXT_PUBLIC_IS_LOCAL_API_SITE_URL: "false",
})
const { site } = await import("./site")
expect(site.url).toBe("https://www.walcron.com")
expect(site.apiUrl).toBe("https://www.walcron.com/api")
expect(site.apiUrl).toBe("/api")
})

it("should getUrl from env", async () => {
Expand All @@ -18,18 +17,14 @@ describe("root-url", () => {
expect(getUrl()).toBe(url)
})

it("should getUrl + getApiUrl from env", async () => {
const url = "https://yoonghan.github.io"
setEnv({
NEXT_PUBLIC_SITE_URL: url,
NEXT_PUBLIC_IS_LOCAL_API_SITE_URL: "false",
})
it("should override local API url from env", async () => {
setEnv({ NEXT_PUBLIC_API_SITE_URL: "https://yoonghan.github.io" })
const { getApiUrl } = await import("./site")
expect(getApiUrl()).toBe(url + "/api")
expect(getApiUrl()).toBe("https://yoonghan.github.io/api")
})

it("should overridde API URL from env", async () => {
setEnv({ NEXT_PUBLIC_IS_LOCAL_API_SITE_URL: "true" })
it("should use blank API url if undefined", async () => {
setEnv({ NEXT_PUBLIC_API_SITE_URL: undefined })
const { getApiUrl } = await import("./site")
expect(getApiUrl()).toBe("/api")
})
Expand Down
3 changes: 1 addition & 2 deletions src/config/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ function getUrl() {
}

function getApiUrl() {
const apiUrl =
process.env.NEXT_PUBLIC_IS_LOCAL_API_SITE_URL === "true" ? "" : getUrl()
const apiUrl = process.env.NEXT_PUBLIC_API_SITE_URL ?? ""
return `${apiUrl}/api`
}

Expand Down

0 comments on commit 3629d3f

Please sign in to comment.