Skip to content

Commit d9d4035

Browse files
committed
refactor: use typescript narrowing
1 parent 724f8fd commit d9d4035

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

governance/xc_admin/packages/xc_admin_common/src/programs/program_registry.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,22 @@ export const getConfig: Record<
5050

5151
/**
5252
* Function to format the configuration for downloading as a JSON file
53+
* Uses type narrowing to determine the correct implementation based on the config shape
5354
*/
54-
export const getDownloadableConfig: Record<
55-
ProgramType,
56-
(config: ProgramConfig) => DownloadableConfig
57-
> = {
58-
[ProgramType.PYTH_CORE]: (config: ProgramConfig) => {
59-
if ("mappingAccounts" in config) {
60-
return pythCore.getDownloadableConfig(config as RawConfig);
61-
}
62-
throw new Error("Invalid config type for Pyth Core");
63-
},
64-
[ProgramType.PYTH_LAZER]: (config: ProgramConfig) => {
65-
if ("feeds" in config && config.programType === ProgramType.PYTH_LAZER) {
66-
return pythLazer.getDownloadableConfig(config as LazerConfig);
67-
}
68-
throw new Error("Invalid config type for Pyth Lazer");
69-
},
55+
export const getDownloadableConfig = (
56+
config: ProgramConfig,
57+
): DownloadableConfig => {
58+
if ("mappingAccounts" in config) {
59+
return pythCore.getDownloadableConfig(config);
60+
} else if (
61+
"feeds" in config &&
62+
config.programType === ProgramType.PYTH_LAZER
63+
) {
64+
return pythLazer.getDownloadableConfig(config);
65+
}
66+
throw new Error(
67+
"Invalid config type - could not determine program type from config structure",
68+
);
7069
};
7170

7271
/**

0 commit comments

Comments
 (0)