From 067433425b84462629b5dc9d2d940f5c8d058fb0 Mon Sep 17 00:00:00 2001 From: harnish7576 Date: Thu, 7 Aug 2025 02:41:16 -0400 Subject: [PATCH] replaced hardcoded API URL with environment variable --- .env.example | 1 + Makefile | 1 + src/__tests__/api/call.test.js | 3 ++- src/api/call.js | 3 +-- src/constants.js | 2 ++ 5 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 .env.example create mode 100644 src/constants.js diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..045ad4d0c --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +REACT_APP_POLICYENGINE_API=https://api.policyengine.org \ No newline at end of file diff --git a/Makefile b/Makefile index 2639b6777..c7a7b72d8 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ REACT_APP_DEBUG ?= false install: npm ci + cp .env.example .env pip3 install -U black build: diff --git a/src/__tests__/api/call.test.js b/src/__tests__/api/call.test.js index f43a9aa4a..06c548a5f 100644 --- a/src/__tests__/api/call.test.js +++ b/src/__tests__/api/call.test.js @@ -1,6 +1,7 @@ import { renderHook } from "@testing-library/react"; import { useAuthenticatedApiCall } from "../../api/call"; import * as authenticatedFetch from "../../hooks/useAuthenticatedFetch"; +import { POLICYENGINE_API } from "../../constants"; jest.mock("../../hooks/useAuthenticatedFetch"); let mock_authenticated_fetch; @@ -36,7 +37,7 @@ describe("useAuthenticatedApiCall", () => { expect(response).toEqual(DEFAULT_FETCH_RESULT); expect(mock_authenticated_fetch.mock.calls[0]).toEqual([ - "https://api.policyengine.org/test/path", + POLICYENGINE_API + "/test/path", { body: JSON.stringify(SOME_REQUEST_BODY), headers: { diff --git a/src/api/call.js b/src/api/call.js index 51ba01381..cf618fc15 100644 --- a/src/api/call.js +++ b/src/api/call.js @@ -3,8 +3,7 @@ import { buildParameterTree } from "./parameters"; import { buildVariableTree, getTreeLeavesInOrder } from "./variables"; import { wrappedJsonStringify, wrappedResponseJson } from "../data/wrappedJson"; import { useAuthenticatedFetch } from "../hooks/useAuthenticatedFetch"; - -const POLICYENGINE_API = "https://api.policyengine.org"; +import { POLICYENGINE_API } from "../constants"; /** * returns an api call function that can be used to make requests diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 000000000..d22923f9f --- /dev/null +++ b/src/constants.js @@ -0,0 +1,2 @@ +export const POLICYENGINE_API = + process.env.REACT_APP_POLICYENGINE_API || "https://api.policyengine.org";