You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/service/CanonicalInstantiable.ts

26 lines
1007 B

3 years ago
/**
* Error thrown when the item returned from a canonical definition file is not the expected item.
* @extends Error
*/
import {Canonical, CanonicalDefinition} from "./Canonical";
import {Instantiable, isInstantiable} from "@extollo/di";
export class InvalidCanonicalExportError extends Error {
constructor(name: string) {
super(`Unable to import canonical item from "${name}". The default export of this file is invalid.`)
}
}
export class CanonicalInstantiable<T> extends Canonical<Instantiable<T>> {
public async initCanonicalItem(definition: CanonicalDefinition): Promise<Instantiable<T>> {
if ( isInstantiable(definition.imported.default) ) {
return this.app().make(definition.imported.default)
}
if ( isInstantiable(definition.imported[definition.canonicalName]) ) {
return this.app().make(definition.imported[definition.canonicalName])
}
3 years ago
throw new InvalidCanonicalExportError(definition.originalName)
}
}