Skip to content

Commit 6ffefb6

Browse files
committed
Add support for overriding Remote Settings environment via URL query parameter.
1 parent 50e0657 commit 6ffefb6

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/app.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import "./github-corner";
1010

1111
const GITHUB_URL = "https://github.com/Trikolon/url-classifier-exceptions-ui";
1212

13+
// Query parameter which can be used to override the RS environment.
14+
const QUERY_PARAM_RS_ENV = "rs_env";
15+
16+
// The available Remote Settings endpoints.
1317
const RS_ENDPOINTS = {
1418
prod: "https://firefox.settings.services.mozilla.com",
1519
stage: "https://firefox.settings.services.allizom.org",
@@ -18,6 +22,21 @@ const RS_ENDPOINTS = {
1822

1923
type RSEndpointKey = keyof typeof RS_ENDPOINTS;
2024

25+
/**
26+
* Get the RS environment from URL parameters, falling back to the defaults if
27+
* not specified.
28+
* @returns The RS environment key
29+
*/
30+
function getRsEnv(): RSEndpointKey {
31+
const params = new URLSearchParams(window.location.search);
32+
const env = params.get(QUERY_PARAM_RS_ENV);
33+
if (env && Object.keys(RS_ENDPOINTS).includes(env)) {
34+
return env as RSEndpointKey;
35+
}
36+
// Fall back to build env configuration or if env is not set, the default of "prod".
37+
return (import.meta.env.VITE_RS_ENVIRONMENT as RSEndpointKey) || "prod";
38+
}
39+
2140
/**
2241
* Get the URL for the records endpoint for a given Remote Settings environment.
2342
* @param rsOrigin The origin of the Remote Settings environment.
@@ -48,9 +67,10 @@ async function fetchRecords(rsOrigin: string): Promise<ExceptionListEntry[]> {
4867
@customElement("app-root")
4968
export class App extends LitElement {
5069
// The Remote Settings environment to use. The default is configured via env
51-
// at build time. The user can change this via a dropdown.
70+
// at build time. The user can change this via a dropdown. The user can also
71+
// override the environment via a query parameter.
5272
@state()
53-
rsEnv: RSEndpointKey = (import.meta.env.VITE_RS_ENVIRONMENT as RSEndpointKey) || "prod";
73+
rsEnv: RSEndpointKey = getRsEnv();
5474

5575
// Holds all fetched records.
5676
@state()
@@ -210,7 +230,16 @@ export class App extends LitElement {
210230
<select
211231
id="rs-env"
212232
@change=${(e: Event) => {
213-
this.rsEnv = (e.target as HTMLSelectElement).value as RSEndpointKey;
233+
const newEnv = (e.target as HTMLSelectElement).value as RSEndpointKey;
234+
this.rsEnv = newEnv;
235+
236+
// When the env changes reflect the update in the URL.
237+
// Update URL parameter without reloading the page
238+
const url = new URL(window.location.href);
239+
url.searchParams.set(QUERY_PARAM_RS_ENV, newEnv);
240+
window.history.pushState({}, "", url);
241+
242+
// Fetch the records again.
214243
this.init();
215244
}}
216245
>

0 commit comments

Comments
 (0)