Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/config/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export type DcdEnvName = 'dev' | 'prod';

export interface DcdEnvironment {
apiUrl: string;
/**
* Older API URLs that still reach this environment. A config saved by an
* earlier `dcd login`, or a `--api-url` in someone's CI, may still name one,
* so it must keep resolving to this environment.
*/
legacyApiUrls?: string[];
frontendUrl: string;
supabase: {
url: string;
Expand All @@ -30,7 +36,8 @@ export const ENVIRONMENTS: Record<DcdEnvName, DcdEnvironment> = {
},
},
dev: {
apiUrl: 'https://api.dev.devicecloud.dev',
apiUrl: 'https://dev-api.devicecloud.dev',
legacyApiUrls: ['https://api.dev.devicecloud.dev'],
frontendUrl: 'https://dev.console.devicecloud.dev',
supabase: {
url: 'https://lbmsowehtjwnqlurpemb.supabase.co',
Expand All @@ -39,18 +46,32 @@ export const ENVIRONMENTS: Record<DcdEnvName, DcdEnvironment> = {
},
};

/** Exact-match lookup of a known environment by API URL (trailing slashes ignored). */
/**
* Exact-match lookup of a known environment by API URL, current or legacy
* (trailing slashes ignored).
*/
export function findEnvByApiUrl(apiUrl: string): DcdEnvironment | undefined {
const normalize = (u: string) => u.replace(/\/+$/, '');
const needle = normalize(apiUrl);
return Object.values(ENVIRONMENTS).find(
(env) => normalize(env.apiUrl) === needle,
return Object.values(ENVIRONMENTS).find((env) =>
[env.apiUrl, ...(env.legacyApiUrls ?? [])].some((u) => normalize(u) === needle),
);
}

/** Map a caller-supplied API URL to one of the known environments. */
/**
* Map a caller-supplied API URL to one of the known environments. This picks
* the Supabase project uploads go to, so a dev URL must never fall through to
* prod: both dev hostnames (`dev-api.` and the older `api.dev.`) and
* localhost are dev.
*/
export function inferEnvFromApiUrl(apiUrl: string): DcdEnvName {
if (apiUrl.includes('api.dev.') || apiUrl.includes('localhost')) return 'dev';
if (
apiUrl.includes('dev-api.') ||
apiUrl.includes('api.dev.') ||
apiUrl.includes('localhost')
) {
return 'dev';
}
return 'prod';
}

Expand Down
42 changes: 42 additions & 0 deletions test/unit/environments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from 'chai';
import {
ENVIRONMENTS,
findEnvByApiUrl,
inferEnvFromApiUrl,
resolveFrontendUrl,
} from '../../src/config/environments.js';

/**
* The dev API answers on two hostnames: `dev-api.devicecloud.dev` (current)
* and `api.dev.devicecloud.dev` (older). Both must resolve to dev. A dev URL
* that fell through to prod would send uploads to the prod Supabase project.
*/
describe('environments', () => {
const DEV_URLS = [
'https://dev-api.devicecloud.dev',
'https://dev-api.devicecloud.dev/',
'https://api.dev.devicecloud.dev',
'https://api.dev.devicecloud.dev/',
];

it('treats both dev hostnames as dev', () => {
for (const url of DEV_URLS) {
expect(inferEnvFromApiUrl(url), url).to.equal('dev');
expect(findEnvByApiUrl(url), url).to.equal(ENVIRONMENTS.dev);
expect(resolveFrontendUrl(url), url).to.equal('https://dev.console.devicecloud.dev');
}
});

it('uses dev-api as the dev API URL', () => {
expect(ENVIRONMENTS.dev.apiUrl).to.equal('https://dev-api.devicecloud.dev');
});

it('keeps prod as prod', () => {
expect(inferEnvFromApiUrl('https://api.devicecloud.dev')).to.equal('prod');
expect(findEnvByApiUrl('https://api.devicecloud.dev')).to.equal(ENVIRONMENTS.prod);
});

it('treats localhost as dev', () => {
expect(inferEnvFromApiUrl('http://localhost:8000')).to.equal('dev');
});
});
Loading