37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
// Builds the regulation-mapping runtime from the environment: the real
|
|
// retrieval service when configured, otherwise a static disabled/misconfigured
|
|
// stub. Kept separate from regulation-mapping.ts to avoid an import cycle with
|
|
// the service module.
|
|
|
|
import {
|
|
disabledRegulationMapping,
|
|
readRegulationMappingConfig,
|
|
RegulationMappingUnavailableError,
|
|
type RegulationMapping,
|
|
type RegulationMappingEnvironment
|
|
} from './regulation-mapping';
|
|
import { RegulationRetrievalService } from './regulation-retrieval-service';
|
|
|
|
class MisconfiguredRegulationMapping implements RegulationMapping {
|
|
constructor(private readonly detailCode: string) {}
|
|
private report() {
|
|
return { capability: 'misconfigured' as const, detailCode: this.detailCode };
|
|
}
|
|
cachedCapability() {
|
|
return this.report();
|
|
}
|
|
async refreshCapability() {
|
|
return this.report();
|
|
}
|
|
async retrieve(): Promise<never> {
|
|
throw new RegulationMappingUnavailableError(this.report());
|
|
}
|
|
}
|
|
|
|
export function createRegulationMapping(env: RegulationMappingEnvironment = process.env as RegulationMappingEnvironment): RegulationMapping {
|
|
const result = readRegulationMappingConfig(env);
|
|
if (result.state === 'disabled') return disabledRegulationMapping();
|
|
if (result.state === 'error') return new MisconfiguredRegulationMapping(result.detailCode);
|
|
return new RegulationRetrievalService(result.config);
|
|
}
|