UNPKG

@tsed/config

Version:
43 lines (42 loc) 1.37 kB
import { nameOf } from "@tsed/core"; import { inject, injectable, ProviderScope } from "@tsed/di"; import { camelCase } from "change-case"; export function getConfigSources(configSources) { return configSources .map((configSource, index) => { if (!("use" in configSource)) { return { name: camelCase(nameOf(configSource).replace("ConfigSource", "")), use: configSource, options: {}, priority: index, enabled: true }; } return { enabled: true, ...configSource, priority: configSource.priority || 0, name: configSource.name || camelCase(nameOf(configSource.use).replace("ConfigSource", "")) }; }) .sort((a, b) => a.priority - b.priority) .filter(({ enabled }) => enabled) .reduce((map, options, index) => { let { use, name, options: useOpts } = options; injectable(use).scope(ProviderScope.INSTANCE); const instance = inject(use, { useOpts }); instance.options = useOpts; if (map.has(name)) { name = `${name}_${index}`; } map.set(name, { ...options, instance, data: {} }); return map; }, new Map()); }