All files / services/metadata metadata.service.ts

78.26% Statements 18/23
52.94% Branches 9/17
63.64% Functions 7/11
82.35% Lines 14/17

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 691x 1x     1x     2x 2x 2x 3x   3x 3x 3x     2x                   3x                                                     2x                     2x      
import { Service } from '../../container';
import { createUniqueHash } from '../../helpers';
 
@Service()
export class MetadataService {
 
    generateHashData(module, original: Function) {
        const services = module.services || [];
        const imports = module.imports || [];
        const fillMetadata = (injectable) => {
            Iif (injectable && injectable['provide']) {
                return injectable['provide'];
            } else Eif (injectable) {
                this.validateCustomInjectable(injectable, module, original);
                return { moduleName: injectable['metadata']['moduleName'], hash: injectable['metadata']['moduleHash'] };
            }
        };
        return [[...services.map((i) => fillMetadata(i))], [...imports.map((i) => fillMetadata(i))]];
    }
 
    validateCustomInjectableKeys(keys: Array<'useFactory' | 'provide' | 'useValue' | 'useClass' | 'useDynamic' | string>) {
        // keys.forEach(key => {
        //     console.log('TOVA NE E SHEGA', key);
        // });
    }
 
    validateCustomInjectable(injectable, module, original) {
        Iif (!injectable['metadata'] && !injectable['provide']) {
            throw new Error(`
                ---- Wrong service ${JSON.stringify(injectable)} provided inside '${original.name}' ----
                @Module({
                    services: ${JSON.stringify([...module.services.filter(i => !i['metadata']), ...module.services.filter(i => i && i['metadata'] && i['metadata']['moduleName']).map(i => i['metadata']['moduleName'])])}
                })
                ${JSON.stringify(`${original}`, null, 2)}
 
                Hint: System recieved Object but it is not with appropriate format you must provide object with following parameters:
 
                YourObject: ${JSON.stringify(injectable)}
 
                Option 1. [YourClass]
 
                Option 2. [{provide: 'your-value', useClass: YourClass}]
 
                Option 3. [{provide: 'your-value', deps: [YourClass], useFactory: (test: YourClass) => {}}]
 
                Option 4. [{provide: 'your-value', useDynamic: {}}]
 
                Option 5. [{provide: 'your-value', useValue: 'your-value'}]
            `);
 
        }
    }
 
    parseModuleTemplate(moduleName, generatedHashData, targetCurrentSymbol) {
        return `
            ---- @gapi module '${moduleName}' metadata----
            @Module({
                imports: ${JSON.stringify(generatedHashData[1], null, '\t')},
                services: ${JSON.stringify(generatedHashData[0], null, '\t')}
            })
            ${JSON.stringify(targetCurrentSymbol, null, 2)}
        `;
    }
 
    createUniqueHash(string: string) {
        return createUniqueHash(string);
    }
 
}