Skip to content

Commit e373cd5

Browse files
committed
Use type to validate annotation key
1 parent 013b845 commit e373cd5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './service/router';
2-
export { DevcontainersProcessor } from './processors/DevcontainersProcessor';
2+
export { DevcontainersProcessor, type VsCodeUrlKey } from './processors/DevcontainersProcessor';

plugins/backstage-plugin-devcontainers-backend/src/processors/DevcontainersProcessor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import { parseGitUrl } from '../utils/git';
1414
export const DEFAULT_TAG_NAME = 'devcontainers';
1515
export const PROCESSOR_NAME_PREFIX = 'backstage-plugin-devcontainers-backend';
1616

17+
const vsCodeUrlKey = "vsCodeUrl"
18+
19+
// We export this type instead of the actual constant so we can validate the
20+
// constant on the frontend at compile-time instead of making the backend plugin
21+
// a run-time dependency, so it can continue to run standalone.
22+
export type VsCodeUrlKey = typeof vsCodeUrlKey
23+
1724
type ProcessorOptions = Readonly<{
1825
tagName: string;
1926
logger: Logger;
@@ -138,7 +145,7 @@ export class DevcontainersProcessor implements CatalogProcessor {
138145
...entity.metadata,
139146
annotations: {
140147
...(entity.metadata.annotations ?? {}),
141-
vsCodeUrl: serializeVsCodeUrl(location.target),
148+
[vsCodeUrlKey]: serializeVsCodeUrl(location.target),
142149
},
143150
tags: [...(entity.metadata?.tags ?? []), newTag],
144151
},

plugins/backstage-plugin-devcontainers-react/src/hooks/useDevcontainers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { useDevcontainersConfig } from '../components/DevcontainersProvider';
22
import { useEntity } from '@backstage/plugin-catalog-react';
3+
import type { VsCodeUrlKey } from "@coder/backstage-plugin-devcontainers-backend";
4+
5+
// We avoid importing the actual constant to prevent making the backend plugin a
6+
// run-time dependency, but we can use the type at compile-time to validate the
7+
// string is the same.
8+
const vsCodeUrlKey: VsCodeUrlKey = "vsCodeUrl";
39

410
export type UseDevcontainersResult = Readonly<
511
{
@@ -37,7 +43,7 @@ export function useDevcontainers(): UseDevcontainersResult {
3743
};
3844
}
3945

40-
const vsCodeUrl = entity.metadata.annotations?.vsCodeUrl;
46+
const vsCodeUrl = entity.metadata.annotations?.[vsCodeUrlKey];
4147
if (!vsCodeUrl) {
4248
return {
4349
tagName,

0 commit comments

Comments
 (0)