All files / container/decorators Plugin.ts

17.39% Statements 4/23
0% Branches 0/20
0% Functions 0/2
17.39% Lines 4/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51      4x 4x 4x               4x                                                                          
import { ServiceMetadata } from '../types/ServiceMetadata';
import { ServiceOptions } from '../types/ServiceOptions';
// import { ControllerContainerService } from '../../services/controller-service/controller.service';
import { Token } from '../Token';
import { Container } from '../Container';
import { createUniqueHash } from '../../helpers/create-unique-hash';
import { DecoratorType, Metadata } from '../../decorators/module/module.interfaces';
export interface PluginInterface {
    name?: string;
    version?: string;
    register(server?, options?): void;
    handler?(request, h);
}
export function Plugin<T, K extends keyof T>(optionsOrServiceName?: ServiceOptions<T, K> | Token<any> | string): Function {
    return function (target) {
        const original = target;
        original.prototype._plugin = true;
 
        const uniqueHashForClass = createUniqueHash(`${target}`);
        Object.defineProperty(target, 'originalName', { value: target.name || target.constructor.name, writable: false });
        Object.defineProperty(target, 'name', { value: uniqueHashForClass, writable: true });
 
        target['metadata'] = <Metadata>{
            useFactory: optionsOrServiceName && optionsOrServiceName['useFactory'] || null,
            provideIn: optionsOrServiceName && optionsOrServiceName['provideIn'] || 'root',
            options: optionsOrServiceName || null,
            moduleName: target['originalName'],
            moduleHash: target['name'],
            raw: `${target}`,
            type: <DecoratorType>'plugin',
        };
 
        const service: ServiceMetadata<T, K> = {
            type: original
        };
 
        if (typeof optionsOrServiceName === 'string' || optionsOrServiceName instanceof Token) {
            service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
            service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
            service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;
 
        } else if (optionsOrServiceName) { // ServiceOptions
            service.id = (optionsOrServiceName as ServiceOptions<T, K>).id;
            service.factory = (optionsOrServiceName as ServiceOptions<T, K>).factory;
            service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
            service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
            service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;
        }
        Container.set(service);
    };
}