All files / container/decorators Service.ts

59.09% Statements 13/22
55% Branches 11/20
100% Functions 2/2
59.09% Lines 13/22

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80  7x   7x 7x                                                             7x 33x   33x 33x 33x   33x                             33x       33x           33x               33x        
import { ServiceMetadata } from '../types/ServiceMetadata';
import { Container } from '../Container';
import { ServiceOptions } from '../types/ServiceOptions';
import { Token } from '../Token';
import { createUniqueHash } from '../../helpers/create-unique-hash';
 
export interface TypeProvide<T> extends Function {
    new(...args: any[]): T;
}
/**
 * Marks class as a service that can be injected using Container.
 */
export function Service(): Function;
 
export function Service(config: { providedIn?: TypeProvide<any> | 'root' | null, useFactory?: () => any }): Function;
 
/**
 * Marks class as a service that can be injected using Container.
 */
export function Service(name: string): Function;
 
/**
 * Marks class as a service that can be injected using Container.
 */
export function Service(token: Token<any>): Function;
 
 
/**
 * Marks class as a service that can be injected using Container.
 */
export function Service<T, K extends keyof T>(options?: ServiceOptions<T, K>): Function;
 
/**
 * Marks class as a service that can be injected using container.
 */
export function Service<T, K extends keyof T>(optionsOrServiceName?: ServiceOptions<T, K> | Token<any> | string | { useFactory?: () => any; providedIn?: TypeProvide<any> | 'root' | null }): Function {
    return function (target: Function) {
 
        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'] = {
            useFactory: optionsOrServiceName && optionsOrServiceName['useFactory'] || null,
            provideIn: optionsOrServiceName && optionsOrServiceName['provideIn'] || 'root',
            options: optionsOrServiceName || null,
            moduleName: target['originalName'],
            moduleHash: uniqueHashForClass,
            type: 'service',
            raw: `
            ---- @Service '${target.name}' metadata----
            @Service()
            ${target['originalName']}
            `
        };
 
 
        const service: ServiceMetadata<T, K> = {
            type: target
        };
 
        Iif (typeof optionsOrServiceName === 'string' || optionsOrServiceName instanceof Token) {
            service.id = optionsOrServiceName;
            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 Iif (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);
    };
}