UNPKG

@furystack/inject

Version:
43 lines 1.36 kB
/** * The default options for the injectable classes */ export const defaultInjectableOptions = { lifetime: 'transient', }; export const InjectableOptionsSymbol = Symbol('InjectableOptions'); /** * Checks if the constructor is decorated with Injectable() with verifying if it has Injectable options * @param ctor The constructor to check * @returns if the constructor has the InjectableOptionsSymbol */ export const hasInjectableOptions = (ctor) => { return (typeof ctor[InjectableOptionsSymbol] === 'object' && typeof ctor[InjectableOptionsSymbol].lifetime === 'string'); }; /** * @throws Error if the class is not an injectable * @param ctor The constructor to get the options from * @returns The InjectableOptions object */ export const getInjectableOptions = (ctor) => { if (!hasInjectableOptions(ctor)) { throw Error(`The class '${ctor.name}' is not an injectable`); } return ctor[InjectableOptionsSymbol]; }; /** * Decorator method for tagging a class as injectable * @param options The options object * @returns void */ export const Injectable = (options) => { return (ctor) => { Object.assign(ctor, { [InjectableOptionsSymbol]: { ...defaultInjectableOptions, ...options, }, }); }; }; //# sourceMappingURL=injectable.js.map