/** * Error thrown when the item returned from a canonical definition file is not the expected item. * @extends Error */ import {Canonical, CanonicalDefinition} from './Canonical' import {isInstantiable} from '../di' /** * Error thrown when the export of a canonical file is determined to be invalid. */ export class InvalidCanonicalExportError extends Error { constructor(name: string) { super(`Unable to import canonical item from "${name}". The default export of this file is invalid.`) } } /** * Variant of the Canonical unit whose files export classes which are instantiated using the global container. */ export class CanonicalInstantiable extends Canonical { public async initCanonicalItem(definition: CanonicalDefinition): Promise { if ( isInstantiable(definition.imported.default) ) { return this.app().make(definition.imported.default) } if ( isInstantiable(definition.imported[definition.canonicalName.split(':').reverse()[0]]) ) { return this.app().make(definition.imported[definition.canonicalName.split(':').reverse()[0]]) } throw new InvalidCanonicalExportError(definition.originalName) } }