My current project involves working on an application with multiple products
. To streamline product-specific configuration and eliminate the need for excessive if-else
statements, I am looking to implement product-specific config keys that are consistently maintained across all products. The use of interfaces will ensure that developers do not overlook any key when adding it to a product.
I seek suggestions on the following points:
In anticipation of potential size increases in the object, I aim to dynamically import objects based on the current
product-name
(product1 | product2
, etc.) to enhance overall performance.Suggestions for improving the structure of the
ProductConfigService
class.Explore efficient alternatives or approaches leveraging TypeScript (TS).
Your input is appreciated.
interface PRODUCT_CONFIG {
API_KEY: string,
AGE: number,
TEXT: string
}
const product1: PRODUCT_CONFIG = {
API_KEY: "SOME_VALUE",
AGE: 10,
TEXT: "SOME_VALUE"
}
const product2: PRODUCT_CONFIG = {
API_KEY: "SOME_VALUE"
AGE: 12,
TEXT: "SOME_VALUE"
}
class ProductConfigService {
private product: PRODUCT_CONFIG;
constructor(private accessKey:string) {
this.product = product2; // should be dynamic based on the current selected application. This is hard-coded to product2.
}
getConfig(): string {
//@ts-ignore
return this.product[this.accessKey];
}
}
// consumer.
let product = new ProductConfigService('AGE');
console.log(product.getConfig());