Skip to content

Commit 2cced91

Browse files
authored
[Issue #6785] Add new X-API-KEY to frontend (#6786)
## Summary Fixes #6785 ## Changes proposed Updated frontend to grab new X-API-KEY param from AWS param store & use it when making calls to the backend. ## Context for reviewers Used same implementation as we are already using for the old API. Left it in because we want there to be a fallback as we roll out API GW. ## Validation steps
1 parent c8e2e1d commit 2cced91

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

frontend/src/constants/environments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
SENDY_LIST_ID,
77
API_URL,
88
API_AUTH_TOKEN,
9+
API_GW_AUTH,
910
NEXT_BUILD,
1011
SESSION_SECRET,
1112
NEXT_PUBLIC_BASE_URL,
@@ -62,6 +63,7 @@ export const environment: { [key: string]: string } = {
6263
API_URL: API_URL || "",
6364
AUTH_LOGIN_URL: AUTH_LOGIN_URL || "",
6465
API_AUTH_TOKEN: API_AUTH_TOKEN || "",
66+
API_GW_AUTH: API_GW_AUTH || "",
6567
GOOGLE_TAG_MANAGER_ID: "GTM-MV57HMHS",
6668
ENVIRONMENT,
6769
NEXT_BUILD: NEXT_BUILD || "false",

frontend/src/services/fetch/fetcherHelpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface HeadersDict {
3030
export function getDefaultHeaders(): HeadersDict {
3131
const headers: HeadersDict = {};
3232

33+
if (environment.API_GW_AUTH) {
34+
headers["X-API-KEY"] = environment.API_GW_AUTH;
35+
}
36+
3337
if (environment.API_AUTH_TOKEN) {
3438
headers["X-AUTH"] = environment.API_AUTH_TOKEN;
3539
}

frontend/tests/services/fetch/FetcherHelpers.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,79 @@ describe("throwError", () => {
8585
});
8686
});
8787
});
88+
89+
describe("getDefaultHeaders", () => {
90+
let mockEnvironment: { [key: string]: string };
91+
92+
beforeEach(() => {
93+
jest.resetModules();
94+
mockEnvironment = {};
95+
96+
// Mock the environment module
97+
jest.doMock("src/constants/environments", () => ({
98+
environment: mockEnvironment,
99+
}));
100+
});
101+
102+
afterEach(() => {
103+
jest.resetAllMocks();
104+
});
105+
106+
it("includes Content-Type header by default", async () => {
107+
const { getDefaultHeaders } = await import(
108+
"src/services/fetch/fetcherHelpers"
109+
);
110+
const headers = getDefaultHeaders();
111+
expect(headers["Content-Type"]).toEqual("application/json");
112+
});
113+
114+
it("includes X-AUTH header when API_AUTH_TOKEN is set", async () => {
115+
mockEnvironment.API_AUTH_TOKEN = "test-auth-token";
116+
mockEnvironment.API_GW_AUTH = "";
117+
118+
const { getDefaultHeaders } = await import(
119+
"src/services/fetch/fetcherHelpers"
120+
);
121+
const headers = getDefaultHeaders();
122+
expect(headers["X-AUTH"]).toEqual("test-auth-token");
123+
expect(headers["Content-Type"]).toEqual("application/json");
124+
});
125+
126+
it("includes X-API-KEY header when API_GW_AUTH is set", async () => {
127+
mockEnvironment.API_AUTH_TOKEN = "";
128+
mockEnvironment.API_GW_AUTH = "test-api-key";
129+
130+
const { getDefaultHeaders } = await import(
131+
"src/services/fetch/fetcherHelpers"
132+
);
133+
const headers = getDefaultHeaders();
134+
expect(headers["X-API-KEY"]).toEqual("test-api-key");
135+
expect(headers["Content-Type"]).toEqual("application/json");
136+
});
137+
138+
it("includes both X-AUTH and X-API-KEY headers when both tokens are set", async () => {
139+
mockEnvironment.API_AUTH_TOKEN = "test-auth-token";
140+
mockEnvironment.API_GW_AUTH = "test-api-key";
141+
142+
const { getDefaultHeaders } = await import(
143+
"src/services/fetch/fetcherHelpers"
144+
);
145+
const headers = getDefaultHeaders();
146+
expect(headers["X-AUTH"]).toEqual("test-auth-token");
147+
expect(headers["X-API-KEY"]).toEqual("test-api-key");
148+
expect(headers["Content-Type"]).toEqual("application/json");
149+
});
150+
151+
it("does not include auth headers when tokens are not set", async () => {
152+
mockEnvironment.API_AUTH_TOKEN = "";
153+
mockEnvironment.API_GW_AUTH = "";
154+
155+
const { getDefaultHeaders } = await import(
156+
"src/services/fetch/fetcherHelpers"
157+
);
158+
const headers = getDefaultHeaders();
159+
expect(headers["X-AUTH"]).toBeUndefined();
160+
expect(headers["X-API-KEY"]).toBeUndefined();
161+
expect(headers["Content-Type"]).toEqual("application/json");
162+
});
163+
});

infra/frontend/app-config/env-config/environment_variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ locals {
119119
manage_method = "manual"
120120
secret_store_name = "/api/${var.environment}/api-jwt-public-key"
121121
},
122-
API_AUTH_X_API_KEY = {
122+
API_GW_AUTH = {
123123
manage_method = "manual"
124124
secret_store_name = "/${var.app_name}/${var.environment}/X-API-KEY"
125125
},

0 commit comments

Comments
 (0)